private void LogTelemetryData(string region, RegionSource regionSource, RequestContext requestContext)
        {
            requestContext.ApiEvent.RegionDiscovered = region;

            if (requestContext.ApiEvent.RegionSource == 0)
            {
                requestContext.ApiEvent.RegionSource = (int)regionSource;
            }
        }
Пример #2
0
        private void LogTelemetryData(string region, RegionSource regionSource, RequestContext requestContext)
        {
            requestContext.ApiEvent.RegionDiscovered = region;

            if (requestContext.ApiEvent.RegionSource == (int)RegionSource.None)
            {
                requestContext.ApiEvent.RegionSource = (int)regionSource;
            }

            requestContext.ApiEvent.UserProvidedRegion = requestContext.ServiceBundle.Config.AuthorityInfo.RegionToUse;
        }
Пример #3
0
        private static bool TryLoad(
            string name,
            string profileLocation,
            ref RegionEndpoint region,
            ref RegionSource source)
        {
            if (SettingsStore.TryGetProfile(name, profileLocation, out var profile) && profile.Region != null)
            {
                region = profile.Region;
                source = RegionSource.Saved;
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Translates enum into a friendlier 'from xxx' display string
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        internal static string FormatRegionSourceForDisplay(RegionSource source)
        {
            switch (source)
            {
            case RegionSource.RegionObject:
                return("supplied region object");

            case RegionSource.Saved:
                return("stored region");

            case RegionSource.Session:
                return("shell variable $" + SessionKeys.AWSRegionVariableName);

            case RegionSource.String:
                return("region parameter");
            }

            // fallback
            return(Enum.GetName(typeof(RegionSource), source));
        }
Пример #5
0
        public static bool TryGetRegion(
            this IAWSRegionArguments self,
            bool useInstanceMetadata,
            out RegionEndpoint region,
            out RegionSource source,
            SessionState sessionState)
        {
            if (self == null)
            {
                throw new ArgumentNullException("self");
            }

            region = null;
            source = RegionSource.Unknown;

            // user gave a command-level region parameter override?
            if (self.Region != null)
            {
                var regionSysName = string.Empty;
                if (self.Region is PSObject)
                {
                    var paramObject = self.Region as PSObject;
                    if (paramObject.BaseObject is AWSRegion)
                    {
                        regionSysName = (paramObject.BaseObject as AWSRegion).Region.SystemName;
                    }
                    else
                    {
                        regionSysName = paramObject.BaseObject as string;
                    }
                }
                else if (self.Region is string)
                {
                    regionSysName = self.Region as string;
                }

                if (string.IsNullOrEmpty(regionSysName))
                {
                    throw new ArgumentException(
                              "Unsupported parameter type; Region must be a string containing the system name for a region, or an AWSRegion instance");
                }

                try
                {
                    region = RegionEndpoint.GetBySystemName(regionSysName);
                    source = RegionSource.String;
                }
                catch (Exception)
                {
                    // be nice and informative :-)
                    var sb      = new StringBuilder("Unsupported Region value. Supported values: ");
                    var regions = RegionEndpoint.EnumerableAllRegions;
                    for (var i = 0; i < regions.Count(); i++)
                    {
                        if (i > 0)
                        {
                            sb.Append(",");
                        }
                        sb.Append(regions.ElementAt(i).SystemName);
                    }

                    throw new ArgumentOutOfRangeException(sb.ToString());
                }
            }

            // user pushed default shell variable? (this allows override of machine-wide environment setting)
            if (region == null && sessionState != null)
            {
                var variableValue = sessionState.PSVariable.GetValue(SessionKeys.AWSRegionVariableName);
                if (variableValue is string)
                {
                    region = RegionEndpoint.GetBySystemName(variableValue as string);
                    source = RegionSource.Session;
                }
            }

            // region set in profile store (including legacy key name)?
            if (region == null)
            {
                if (!TryLoad(SettingsStore.PSDefaultSettingName, self.ProfileLocation, ref region, ref source))
                {
                    TryLoad(SettingsStore.PSLegacyDefaultSettingName, self.ProfileLocation, ref region, ref source);
                }
            }

            // region set in environment variables?
            if (region == null)
            {
                try
                {
                    var environmentRegion = new EnvironmentVariableAWSRegion();
                    region = environmentRegion.Region;
                    source = RegionSource.Environment;
                }
                catch
                {
                }
            }

            // last chance, attempt load from EC2 instance metadata if allowed
            if (region == null && useInstanceMetadata)
            {
                try
                {
                    region = EC2InstanceMetadata.Region;
                    if (region != null)
                    {
                        source = RegionSource.InstanceMetadata;
                    }
                }
                catch
                {
                }
            }

            return(region != null && source != RegionSource.Unknown);
        }
 /// <summary>
 /// Emit a diagnostic message indicating where the credentials we are about to use
 /// originated from.
 /// </summary>
 /// <param name="regionSource"></param>
 /// <param name="regionValue"></param>
 protected void WriteRegionSourceDiagnostic(RegionSource regionSource, string regionValue = null)
 {
     WriteRegionSourceDiagnostic(FormatRegionSourceForDisplay(regionSource), regionValue);
 }
Пример #7
0
 public RegionInfo(string region, RegionSource regionSource)
 {
     Region       = region;
     RegionSource = regionSource;
 }