예제 #1
0
        public override void ExecuteCmdlet()
        {
            AzureSMProfile       AzureSMProfile;
            AzureProfileSettings settings;

            if (ParameterSetName == PropertyBagParameterSet)
            {
                AzureSMProfile = new AzureSMProfile();
                var actualParameterSet = ParseHashTableParameters(Properties, out settings);
                InitializeAzureProfile(AzureSMProfile, actualParameterSet, settings);
            }
            else if (ParameterSetName == FileParameterSet)
            {
                if (string.IsNullOrEmpty(Path) || !File.Exists(Path))
                {
                    throw new ArgumentException(Resources.InvalidNewProfilePath);
                }

                AzureSMProfile = new AzureSMProfile(Path);
            }
            else
            {
                AzureSMProfile = new AzureSMProfile();
                settings       = AzureProfileSettings.Create(this);
                InitializeAzureProfile(AzureSMProfile, ParameterSetName, settings);
            }

            WriteObject(AzureSMProfile);
        }
예제 #2
0
        private string ParseHashTableParameters(Hashtable propertyBag, out AzureProfileSettings settings)
        {
            settings = new AzureProfileSettings();
            string parametSetName = null;

            if (!propertyBag.ContainsKey(SubscriptionIdKey))
            {
                throw new ArgumentException(Resources.MissingSubscriptionInProfileProperties);
            }

            settings.SubscriptionId = (string)propertyBag[SubscriptionIdKey];
            if (propertyBag.ContainsKey(StorageAccountKey))
            {
                settings.StorageAccount = (string)propertyBag[StorageAccountKey];
            }

            if (propertyBag.ContainsKey(EnvironmentKey))
            {
                var environmentValue = (string)propertyBag[EnvironmentKey];
                if (AzureEnvironment.PublicEnvironments.ContainsKey(environmentValue))
                {
                    settings.Environment = AzureEnvironment.PublicEnvironments[environmentValue];
                }
            }

            if (propertyBag.ContainsKey(CertificateKey))
            {
                if (!propertyBag[CertificateKey].GetType().IsAssignableFrom(typeof(X509Certificate2)))
                {
                    throw new ArgumentException(Resources.MissingCertificateInProfileProperties);
                }

                settings.Certificate = (X509Certificate2)propertyBag[CertificateKey];
                parametSetName       = CertificateParameterSet;
            }
            else if (propertyBag.ContainsKey(UsernameKey))
            {
                settings.Credential = CreatePsCredential((string)propertyBag[UsernameKey], propertyBag);
                if (propertyBag.ContainsKey(TenantKey))
                {
                    settings.Tenant = (string)propertyBag[TenantKey];
                }
                parametSetName = CredentialsParameterSet;
            }
            else if (propertyBag.ContainsKey(SPNKey) && propertyBag.ContainsKey(TenantKey))
            {
                settings.Credential = CreatePsCredential((string)propertyBag[SPNKey], propertyBag);
                if (propertyBag.ContainsKey(TenantKey))
                {
                    settings.Tenant = (string)propertyBag[TenantKey];
                }
                parametSetName = ServicePrincipalParameterSet;
            }
            else if (propertyBag.ContainsKey(AccountIdKey) && propertyBag.ContainsKey(TokenKey))
            {
                settings.AccountId   = (string)propertyBag[AccountIdKey];
                settings.AccessToken = (string)propertyBag[TokenKey];
                parametSetName       = AccessTokenParameterSet;
            }
            else
            {
                throw new ArgumentException(Resources.InvalidProfileProperties);
            }

            return(parametSetName);
        }
예제 #3
0
        private void InitializeAzureProfile(AzureSMProfile profile, string parameterSet, AzureProfileSettings settings)
        {
            var savedCache = AzureSession.TokenCache;

            AzureSession.TokenCache = TokenCache.DefaultShared;
            try
            {
                var profileClient = new ProfileClient(profile);
                if (settings.Environment == null)
                {
                    settings.Environment = AzureEnvironment.PublicEnvironments["AzureCloud"];
                }
                switch (parameterSet)
                {
                case CertificateParameterSet:
                    profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId),
                                                    settings.Certificate,
                                                    settings.StorageAccount);
                    break;

                case CredentialsParameterSet:
                    var userAccount = new AzureAccount
                    {
                        Id   = settings.Credential.UserName,
                        Type = AzureAccount.AccountType.User
                    };
                    profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId),
                                                    userAccount,
                                                    settings.Credential.Password, settings.StorageAccount);
                    break;

                case AccessTokenParameterSet:
                    profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId),
                                                    settings.AccessToken,
                                                    settings.AccountId, settings.StorageAccount);
                    break;

                case ServicePrincipalParameterSet:
                    var servicePrincipalAccount = new AzureAccount
                    {
                        Id   = settings.Credential.UserName,
                        Type = AzureAccount.AccountType.ServicePrincipal,
                    };
                    servicePrincipalAccount.SetOrAppendProperty(AzureAccount.Property.Tenants, settings.Tenant);
                    profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId),
                                                    servicePrincipalAccount,
                                                    settings.Credential.Password, settings.StorageAccount);
                    break;

                case EmptyParameterSet:
                    if (!profile.Environments.ContainsKey(settings.Environment.Name))
                    {
                        profile.Environments.Add(settings.Environment.Name, settings.Environment);
                    }
                    break;
                }
            }
            finally
            {
                AzureSession.TokenCache = savedCache;
            }
        }
예제 #4
0
        private void InitializeAzureProfile(AzureProfile profile, string parameterSet, AzureProfileSettings settings)
        {
            var profileClient = new ProfileClient(profile);

            if (settings.Environment == null)
            {
                settings.Environment = AzureEnvironment.PublicEnvironments["AzureCloud"];
            }
            switch (parameterSet)
            {
            case CertificateParameterSet:
                profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId), settings.Certificate,
                                                settings.StorageAccount);
                break;

            case CredentialsParameterSet:
                var userAccount = new AzureAccount
                {
                    Id   = settings.Credential.UserName,
                    Type = AzureAccount.AccountType.User
                };
                profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId), userAccount,
                                                settings.Credential.Password, settings.StorageAccount);
                break;

            case AccessTokenParameterSet:
                profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId), settings.AccessToken,
                                                settings.AccountId, settings.StorageAccount);
                break;

            case ServicePrincipalParameterSet:
                var servicePrincipalAccount = new AzureAccount
                {
                    Id   = settings.Credential.UserName,
                    Type = AzureAccount.AccountType.ServicePrincipal
                };
                profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId), servicePrincipalAccount,
                                                settings.Credential.Password, settings.StorageAccount);
                break;
            }
        }