GetSettingsValue() 공개 정적인 메소드

Returns the setting with the given name from the currently active gcloud configuration.
public static GetSettingsValue ( string settingName ) : string
settingName string
리턴 string
        /// <summary>
        /// Given a settingName (e.g. Project) and a settingValue (e.g. "gcloud-testing" or $project),
        /// this function will try to first resolve the settingValue to a string. If it fails to do so,
        /// then the function will look into the Cloud SDK Settings to get the default value.
        /// </summary>
        protected string GetCloudSdkSettingValue(string settingName, string settingValue)
        {
            if (settingValue != null)
            {
                // If the cmdlet is not executing and the user is only using tab completion, the string parameterValue
                // will have double quotes at the start and end so we have to trim that.
                settingValue = settingValue.Trim('"');

                // If the parameterValue is a variable, then we have to extract out the variable name.
                if (settingValue.StartsWith("$"))
                {
                    // Try to resolve the variable parameterValue, if unsuccessful, set it to an empty string.
                    settingValue = ResolveVariable(settingValue, string.Empty).ToString();
                }
            }

            // If we cannot resolve the variable or the user has not entered parameter yet, parameterValue is null here.
            // So we will get the value from Cloud SDK Settings.
            if (string.IsNullOrWhiteSpace(settingValue))
            {
                settingValue = CloudSdkSettings.GetSettingsValue(settingName);
            }

            return(settingValue);
        }
예제 #2
0
        /// <summary>
        /// Gives the property a default value from the gcould config. This sets the property regardless of its
        /// current value.
        /// </summary>
        /// <param name="property">The property to set.</param>
        /// <param name="instance">The instance that contains the property to set.</param>
        public void SetObjectConfigDefault(PropertyInfo property, object instance)
        {
            string settingsValue = CloudSdkSettings.GetSettingsValue(Property);

            if (string.IsNullOrEmpty(settingsValue))
            {
                throw new PSInvalidOperationException(
                          $"Parameter {property.Name} was not set and does not have a default value.");
            }

            property.SetValue(instance, settingsValue);
        }
예제 #3
0
        /// <summary>
        /// Returns the access token of the current active config.
        /// </summary>
        public static async Task <ActiveUserToken> GetAccessToken(CancellationToken cancellationToken)
        {
            // We get the issued time before the command so we won't be too late
            // when it comes to token expiry.
            DateTime issuedTime = DateTime.Now;

            string userCredentialJson = await GetGCloudCommandOutput("auth print-access-token");

            cancellationToken.ThrowIfCancellationRequested();
            string currentUser = CloudSdkSettings.GetSettingsValue("account");

            return(new ActiveUserToken(userCredentialJson, currentUser));
        }
예제 #4
0
        /// <summary>
        /// Gives the field a default value from the gcould config.
        /// </summary>
        /// <param name="field">
        /// The field info.
        /// </param>
        /// <param name="instance">
        /// The instance the field is a member of.
        /// </param>
        public void SetConfigDefault(FieldInfo field, object instance)
        {
            if (field.GetValue(instance) == null)
            {
                string settingsValue = CloudSdkSettings.GetSettingsValue(Property);
                if (string.IsNullOrEmpty(settingsValue))
                {
                    throw new PSInvalidOperationException(
                              $"Parameter {field.Name} was not set and does not have a default value.");
                }

                field.SetValue(instance, settingsValue);
            }
        }
예제 #5
0
        /// <summary>
        /// Gives the field a default value from the gcould config.
        /// </summary>
        /// <param name="field">
        /// The field info.
        /// </param>
        /// <param name="instance">
        /// The instance the field is a member of.
        /// </param>
        public void SetConfigDefault(FieldInfo field, GCloudCmdlet instance)
        {
            bool isBoundParameter = instance.MyInvocation.BoundParameters.ContainsKey(field.Name);

            if (!isBoundParameter)
            {
                string settingsValue = CloudSdkSettings.GetSettingsValue(Property);
                if (string.IsNullOrEmpty(settingsValue))
                {
                    throw new PSInvalidOperationException(
                              $"Parameter {field.Name} was not set and does not have a default value.");
                }

                field.SetValue(instance, settingsValue);
            }
        }
        public virtual async Task <string> GetAccessTokenForRequestAsync(string authUri = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (s_token == null)
            {
                await RefreshTokenAsync(cancellationToken).ConfigureAwait(false);

                return(s_token.AccessToken);
            }

            string currentCloudSdkUser = CloudSdkSettings.GetSettingsValue("account");

            if (s_token.IsExpired || s_token.User != currentCloudSdkUser)
            {
                if (!await RefreshTokenAsync(cancellationToken).ConfigureAwait(false))
                {
                    throw new InvalidOperationException(
                              "The access token has expired or the user has changed, but we can't refresh it.");
                }
            }

            return(s_token.AccessToken);
        }