private AWSPSCredentials PromptUserForCredentials()
        {
            var storedCredentials = SettingsStore.GetProfileInfo(ProfileLocation);

            if (storedCredentials.Any())
            {
                // If there are stored credentials, ask user which ones to use, or enter new ones
                var choices = new Collection <ChoiceDescription>();
                choices.Add(new ChoiceDescription("<Create new credentials set>"));
                foreach (var profileInfo in storedCredentials)
                {
                    // help message cannot be null
                    var helpMessage = profileInfo.ProfileLocation == null ? profileInfo.StoreTypeName : profileInfo.ProfileLocation;
                    choices.Add(new ChoiceDescription(profileInfo.ProfileName, helpMessage));
                }

                var choice = Host.UI.PromptForChoice("Saved credentials found", CredentialsPrompt, choices, 0);
                if (choice != 0)
                {
                    var            chosenCredentials = choices[choice];
                    AWSCredentials awsCredentials;
                    if (SettingsStore.TryGetAWSCredentials(chosenCredentials.Label, ProfileLocation, out awsCredentials))
                    {
                        return(new AWSPSCredentials(awsCredentials, chosenCredentials.Label, CredentialsSource.Profile));
                    }
                }
            }

            var descriptions = new Collection <FieldDescription>();

            descriptions.Add(new FieldDescription(AccessKeyFieldName));
            descriptions.Add(new FieldDescription(SecretKeyFieldName));
            var response = Host.UI.Prompt("Credentials", "Please enter your AWS Access and Secret Keys", descriptions);

            var accessKey = response[AccessKeyFieldName].BaseObject as string;
            var secretKey = response[SecretKeyFieldName].BaseObject as string;

            if (string.IsNullOrEmpty(accessKey))
            {
                throw new ArgumentOutOfRangeException(AccessKeyFieldName + " is not set");
            }
            if (string.IsNullOrEmpty(secretKey))
            {
                throw new ArgumentOutOfRangeException(SecretKeyFieldName + " is not set");
            }

            var credentials = new BasicAWSCredentials(accessKey, secretKey);

            // this set will be saved as our default name
            return(new AWSPSCredentials(credentials, SettingsStore.PSDefaultSettingName, CredentialsSource.Profile));
        }
        private void GetFromDefaultProfile(out AWSPSCredentials awsPSCredentialsFromDefaultProfile, out RegionEndpoint regionFromDefaultProfile)
        {
            CredentialProfile profile;

            if (SettingsStore.TryGetProfile(SettingsStore.PSDefaultSettingName, ProfileLocation, out profile))
            {
                AWSCredentials defaultAWSCredentials;
                if (SettingsStore.TryGetAWSCredentials(SettingsStore.PSDefaultSettingName, ProfileLocation, out defaultAWSCredentials))
                {
                    awsPSCredentialsFromDefaultProfile = new AWSPSCredentials(defaultAWSCredentials, SettingsStore.PSDefaultSettingName, CredentialsSource.Profile);
                }
                else
                {
                    awsPSCredentialsFromDefaultProfile = null;
                }

                regionFromDefaultProfile = profile.Region;
            }
            else
            {
                awsPSCredentialsFromDefaultProfile = null;
                regionFromDefaultProfile           = null;
            }
        }