Beispiel #1
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            bool hasPropertiesChange = false;
            var  properties          = new CognitiveServicesAccountProperties();

            if (!string.IsNullOrWhiteSpace(CustomSubdomainName))
            {
                hasPropertiesChange            = true;
                properties.CustomSubDomainName = CustomSubdomainName;
            }
            if (NetworkRuleSet != null)
            {
                hasPropertiesChange    = true;
                properties.NetworkAcls = NetworkRuleSet.ToNetworkRuleSet();
            }

            Sku sku = null;

            if (!string.IsNullOrWhiteSpace(this.SkuName))
            {
                sku = new Sku(this.SkuName);
            }

            Dictionary <string, string> tags = null;

            if (this.Tag != null)
            {
                Dictionary <string, string> tagDictionary = TagsConversionHelper.CreateTagDictionary(Tag);
                tags = tagDictionary ?? new Dictionary <string, string>();
            }

            CognitiveServicesAccount updateParameters = new CognitiveServicesAccount()
            {
                Sku        = sku,
                Tags       = tags,
                Properties = properties
            };

            if (!string.IsNullOrEmpty(PublicNetworkAccess))
            {
                hasPropertiesChange = true;
                updateParameters.Properties.PublicNetworkAccess = PublicNetworkAccess;
            }

            if (AssignIdentity.IsPresent)
            {
                hasPropertiesChange       = true;
                updateParameters.Identity = new Identity(Microsoft.Azure.Management.CognitiveServices.Models.IdentityType.SystemAssigned);
            }

            if (this.IsParameterBound(c => c.IdentityType))
            {
                hasPropertiesChange       = true;
                updateParameters.Identity = new Identity(this.IdentityType);
            }

            if (CognitiveServicesEncryption.IsPresent)
            {
                hasPropertiesChange = true;
                updateParameters.Properties.Encryption = new Encryption(null, KeySource.MicrosoftCognitiveServices);
            }

            if (ParameterSetName == KeyVaultEncryptionParameterSet)
            {
                hasPropertiesChange = true;
                updateParameters.Properties.Encryption = new Encryption(
                    new KeyVaultProperties()
                {
                    KeyName     = KeyName,
                    KeyVersion  = KeyVersion,
                    KeyVaultUri = KeyVaultUri
                },
                    KeySource.MicrosoftKeyVault);
            }

            if (StorageAccountId != null && StorageAccountId.Length > 0)
            {
                hasPropertiesChange = true;
                updateParameters.Properties.UserOwnedStorage = new List <UserOwnedStorage>();
                foreach (var storageAccountId in StorageAccountId)
                {
                    updateParameters.Properties.UserOwnedStorage.Add(new UserOwnedStorage(storageAccountId));
                }
            }

            string processMessage = string.Empty;

            if (sku != null && tags != null)
            {
                processMessage = string.Format(CultureInfo.CurrentCulture, Resources.SetAccount_ProcessMessage_UpdateSkuAndTags, this.Name, sku.Name);
            }
            else if (sku != null)
            {
                processMessage = string.Format(CultureInfo.CurrentCulture, Resources.SetAccount_ProcessMessage_UpdateSku, this.Name, sku.Name);
            }
            else if (tags != null)
            {
                processMessage = string.Format(CultureInfo.CurrentCulture, Resources.SetAccount_ProcessMessage_UpdateTags, this.Name);
            }
            else
            {
                if (!hasPropertiesChange)
                {
                    // Not updating anything (this is allowed) - just return the account, no need for approval.
                    var cognitiveServicesAccount = this.CognitiveServicesClient.Accounts.GetProperties(this.ResourceGroupName, this.Name);
                    WriteCognitiveServicesAccount(cognitiveServicesAccount);
                    return;
                }
                else
                {
                    processMessage = string.Format(CultureInfo.CurrentCulture, Resources.SetAccount_ProcessMessage, this.Name);
                }
            }

            if (ShouldProcess(
                    this.Name, processMessage)
                ||
                Force.IsPresent)
            {
                RunCmdLet(() =>
                {
                    var updatedAccount = this.CognitiveServicesClient.Accounts.Update(
                        this.ResourceGroupName,
                        this.Name,
                        updateParameters
                        );

                    WriteCognitiveServicesAccount(updatedAccount);
                });
            }
        }
Beispiel #2
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            bool hasPropertiesChange = false;
            var  properties          = new CognitiveServicesAccountProperties();

            if (!string.IsNullOrWhiteSpace(CustomSubdomainName))
            {
                hasPropertiesChange            = true;
                properties.CustomSubDomainName = CustomSubdomainName;
            }
            if (NetworkRuleSet != null)
            {
                hasPropertiesChange    = true;
                properties.NetworkAcls = NetworkRuleSet.ToNetworkRuleSet();
            }

            Sku sku = null;

            if (!string.IsNullOrWhiteSpace(this.SkuName))
            {
                sku = new Sku(this.SkuName);
            }

            Dictionary <string, string> tags = null;

            if (this.Tag != null)
            {
                Dictionary <string, string> tagDictionary = TagsConversionHelper.CreateTagDictionary(Tag);
                tags = tagDictionary ?? new Dictionary <string, string>();
            }

            string processMessage = string.Empty;

            if (sku != null && tags != null)
            {
                processMessage = string.Format(CultureInfo.CurrentCulture, Resources.SetAccount_ProcessMessage_UpdateSkuAndTags, this.Name, sku.Name);
            }
            else if (sku != null)
            {
                processMessage = string.Format(CultureInfo.CurrentCulture, Resources.SetAccount_ProcessMessage_UpdateSku, this.Name, sku.Name);
            }
            else if (tags != null)
            {
                processMessage = string.Format(CultureInfo.CurrentCulture, Resources.SetAccount_ProcessMessage_UpdateTags, this.Name);
            }
            else
            {
                if (!hasPropertiesChange)
                {
                    // Not updating anything (this is allowed) - just return the account, no need for approval.
                    var cognitiveServicesAccount = this.CognitiveServicesClient.Accounts.GetProperties(this.ResourceGroupName, this.Name);
                    WriteCognitiveServicesAccount(cognitiveServicesAccount);
                    return;
                }
                else
                {
                    processMessage = string.Format(CultureInfo.CurrentCulture, Resources.SetAccount_ProcessMessage, this.Name);
                }
            }

            if (ShouldProcess(
                    this.Name, processMessage)
                ||
                Force.IsPresent)
            {
                RunCmdLet(() =>
                {
                    var updatedAccount = this.CognitiveServicesClient.Accounts.Update(
                        this.ResourceGroupName,
                        this.Name,
                        new CognitiveServicesAccount()
                    {
                        Sku        = sku,
                        Tags       = tags,
                        Properties = properties
                    }
                        );

                    WriteCognitiveServicesAccount(updatedAccount);
                });
            }
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            RunCmdLet(() =>
            {
                var properties = new CognitiveServicesAccountProperties();
                if (!string.IsNullOrWhiteSpace(CustomSubdomainName))
                {
                    properties.CustomSubDomainName = CustomSubdomainName;
                }
                if (NetworkRuleSet != null)
                {
                    properties.NetworkAcls = NetworkRuleSet.ToNetworkRuleSet();
                }

                CognitiveServicesAccount createParameters = new CognitiveServicesAccount()
                {
                    Location   = Location,
                    Kind       = Type, // must have value, mandatory parameter
                    Sku        = new Sku(SkuName, null),
                    Tags       = TagsConversionHelper.CreateTagDictionary(Tag),
                    Properties = properties
                };

                if (ShouldProcess(
                        Name, string.Format(CultureInfo.CurrentCulture, Resources.NewAccount_ProcessMessage, Name, Type, SkuName, Location)))
                {
                    if (Type.StartsWith("Bing.", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (Force.IsPresent)
                        {
                            WriteWarning(Resources.NewAccount_Notice);
                        }
                        else
                        {
                            bool yesToAll = false, noToAll = false;
                            if (!ShouldContinue(Resources.NewAccount_Notice, "Notice", true, ref yesToAll, ref noToAll))
                            {
                                return;
                            }
                        }
                    }
                    try
                    {
                        CognitiveServicesAccount createAccountResponse = CognitiveServicesClient.Accounts.Create(
                            ResourceGroupName,
                            Name,
                            createParameters);
                    }
                    catch (Exception ex)
                    {
                        // Give users a specific message says `Failed to create Cognitive Services account.`
                        // Details should able be found in the exception.
                        throw new Exception("Failed to create Cognitive Services account.", ex);
                    }

                    CognitiveServicesAccount cognitiveServicesAccount = CognitiveServicesClient.Accounts.GetProperties(ResourceGroupName, Name);
                    WriteCognitiveServicesAccount(cognitiveServicesAccount);
                }
            });
        }
Beispiel #4
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            RunCmdLet(() =>
            {
                var properties = new CognitiveServicesAccountProperties();
                if (!string.IsNullOrWhiteSpace(CustomSubdomainName))
                {
                    properties.CustomSubDomainName = CustomSubdomainName;
                }

                if (NetworkRuleSet != null)
                {
                    properties.NetworkAcls = NetworkRuleSet.ToNetworkRuleSet();
                }

                if (ApiProperty != null)
                {
                    properties.ApiProperties = ApiProperty;
                }

                CognitiveServicesAccount createParameters = new CognitiveServicesAccount()
                {
                    Location   = Location,
                    Kind       = Type, // must have value, mandatory parameter
                    Sku        = new Sku(SkuName, null),
                    Tags       = TagsConversionHelper.CreateTagDictionary(Tag),
                    Properties = properties
                };

                if (!string.IsNullOrEmpty(PublicNetworkAccess))
                {
                    createParameters.Properties.PublicNetworkAccess = PublicNetworkAccess;
                }

                if (AssignIdentity.IsPresent)
                {
                    createParameters.Identity = new Identity(IdentityType.SystemAssigned);
                }

                if (CognitiveServicesEncryption.IsPresent)
                {
                    createParameters.Properties.Encryption = new Encryption(null, KeySource.MicrosoftCognitiveServices);
                }

                if (ParameterSetName == KeyVaultEncryptionParameterSet)
                {
                    createParameters.Properties.Encryption = new Encryption(
                        new KeyVaultProperties()
                    {
                        KeyName     = KeyName,
                        KeyVersion  = KeyVersion,
                        KeyVaultUri = KeyVaultUri
                    },
                        KeySource.MicrosoftKeyVault);
                }


                if (StorageAccountId != null && StorageAccountId.Length > 0)
                {
                    createParameters.Properties.UserOwnedStorage = new List <UserOwnedStorage>();
                    foreach (var storageAccountId in StorageAccountId)
                    {
                        createParameters.Properties.UserOwnedStorage.Add(new UserOwnedStorage(storageAccountId));
                    }
                }

                if (ShouldProcess(
                        Name, string.Format(CultureInfo.CurrentCulture, Resources.NewAccount_ProcessMessage, Name, Type, SkuName, Location)))
                {
                    if (Type.StartsWith("Bing.", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (Force.IsPresent)
                        {
                            WriteWarning(Resources.NewAccount_Notice);
                        }
                        else
                        {
                            bool yesToAll = false, noToAll = false;
                            if (!ShouldContinue(Resources.NewAccount_Notice, "Notice", true, ref yesToAll, ref noToAll))
                            {
                                return;
                            }
                        }
                    }

                    if (Type.Equals("Face", StringComparison.InvariantCultureIgnoreCase) || Type.Equals("CognitiveServices", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (Force.IsPresent)
                        {
                            WriteWarning(Resources.NewAccount_LegalTerm_NotPolice);
                        }
                        else
                        {
                            bool yesToAll = false, noToAll = false;
                            if (!ShouldContinue(Resources.NewAccount_LegalTerm_NotPolice, "Notice", true, ref yesToAll, ref noToAll))
                            {
                                return;
                            }
                        }
                    }


                    try
                    {
                        CognitiveServicesAccount createAccountResponse = CognitiveServicesClient.Accounts.Create(
                            ResourceGroupName,
                            Name,
                            createParameters);
                    }
                    catch (ErrorException ex)
                    {
                        // If the Exception is ErrorException, clone the exception with modified message.
                        var newEx      = new ErrorException($"Failed to create Cognitive Services account. {ex.Message}", ex);
                        newEx.Body     = ex.Body;
                        newEx.Request  = ex.Request;
                        newEx.Response = ex.Response;
                        throw newEx;
                    }
                    catch (Exception ex)
                    {
                        // Give users a specific message says `Failed to create Cognitive Services account.`
                        // Details should able be found in the inner exception.
                        throw new Exception("Failed to create Cognitive Services account.", ex);
                    }

                    CognitiveServicesAccount cognitiveServicesAccount = CognitiveServicesClient.Accounts.GetProperties(ResourceGroupName, Name);
                    WriteCognitiveServicesAccount(cognitiveServicesAccount);
                }
            });
        }
Beispiel #5
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            RunCmdLet(() =>
            {
                var properties = new AccountProperties();
                if (!string.IsNullOrWhiteSpace(CustomSubdomainName))
                {
                    properties.CustomSubDomainName = CustomSubdomainName;
                }

                if (NetworkRuleSet != null)
                {
                    properties.NetworkAcls = NetworkRuleSet.ToNetworkRuleSet();
                }

                if (ApiProperty != null)
                {
                    properties.ApiProperties = ApiProperty;
                }

                var createParameters = new Account()
                {
                    Location   = Location,
                    Kind       = Type, // must have value, mandatory parameter
                    Sku        = new Sku(SkuName, null),
                    Tags       = TagsConversionHelper.CreateTagDictionary(Tag),
                    Properties = properties
                };

                if (!string.IsNullOrEmpty(PublicNetworkAccess))
                {
                    createParameters.Properties.PublicNetworkAccess = PublicNetworkAccess;
                }

                if (DisableLocalAuth != null)
                {
                    createParameters.Properties.DisableLocalAuth = DisableLocalAuth;
                }

                if (RestrictOutboundNetworkAccess != null)
                {
                    createParameters.Properties.RestrictOutboundNetworkAccess = RestrictOutboundNetworkAccess;
                }

                if (AllowedFqdnList != null)
                {
                    createParameters.Properties.AllowedFqdnList = AllowedFqdnList;
                }

                if (AssignIdentity.IsPresent || this.UserAssignedIdentityId != null || this.IdentityType != null)
                {
                    ResourceIdentityType resourceIdentityType = ResourceIdentityType.SystemAssigned;
                    if (this.IdentityType == null || !Enum.TryParse(this.IdentityType.ToString(), out resourceIdentityType))
                    {
                        resourceIdentityType = ResourceIdentityType.SystemAssigned;
                    }

                    createParameters.Identity = new Identity(resourceIdentityType);
                    if (this.UserAssignedIdentityId != null)
                    {
                        createParameters.Identity.UserAssignedIdentities = new Dictionary <string, UserAssignedIdentity>();
                        foreach (var userAssignedIdentityId in this.UserAssignedIdentityId)
                        {
                            createParameters.Identity.UserAssignedIdentities.Add(userAssignedIdentityId, new UserAssignedIdentity());
                        }
                    }
                }

                if (CognitiveServicesEncryption.IsPresent)
                {
                    createParameters.Properties.Encryption = new Encryption(null, KeySource.MicrosoftCognitiveServices);
                }

                if (ParameterSetName == KeyVaultEncryptionParameterSet)
                {
                    createParameters.Properties.Encryption = new Encryption(
                        new KeyVaultProperties()
                    {
                        KeyName          = KeyName,
                        KeyVersion       = KeyVersion,
                        KeyVaultUri      = KeyVaultUri,
                        IdentityClientId = KeyVaultIdentityClientId
                    },
                        KeySource.MicrosoftKeyVault);
                }


                if (StorageAccountId != null && StorageAccountId.Length > 0)
                {
                    createParameters.Properties.UserOwnedStorage = new List <UserOwnedStorage>();
                    foreach (var storageAccountId in StorageAccountId)
                    {
                        createParameters.Properties.UserOwnedStorage.Add(new UserOwnedStorage(storageAccountId));
                    }
                }

                if (ShouldProcess(
                        Name, string.Format(CultureInfo.CurrentCulture, Resources.NewAccount_ProcessMessage, Name, Type, SkuName, Location)))
                {
                    if (Type.StartsWith("Bing.", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (Force.IsPresent)
                        {
                            WriteWarning(Resources.NewAccount_Notice);
                        }
                        else
                        {
                            bool yesToAll = false, noToAll = false;
                            if (!ShouldContinue(Resources.NewAccount_Notice, "Notice", true, ref yesToAll, ref noToAll))
                            {
                                return;
                            }
                        }
                    }

                    if (Type.Equals("Face", StringComparison.InvariantCultureIgnoreCase) || Type.Equals("CognitiveServices", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (Force.IsPresent)
                        {
                            WriteWarning(Resources.NewAccount_LegalTerm_NotPolice);
                        }
                        else
                        {
                            bool yesToAll = false, noToAll = false;
                            if (!ShouldContinue(Resources.NewAccount_LegalTerm_NotPolice, "Notice", true, ref yesToAll, ref noToAll))
                            {
                                return;
                            }
                        }
                    }

                    var createAccountResponse = CognitiveServicesClient.Accounts.Create(
                        ResourceGroupName,
                        Name,
                        createParameters);

                    var cognitiveServicesAccount = CognitiveServicesClient.Accounts.Get(ResourceGroupName, Name);

                    WriteCognitiveServicesAccount(cognitiveServicesAccount);
                }
            });
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            var properties = new AccountProperties();

            if (!string.IsNullOrWhiteSpace(CustomSubdomainName))
            {
                properties.CustomSubDomainName = CustomSubdomainName;
            }
            if (NetworkRuleSet != null)
            {
                properties.NetworkAcls = NetworkRuleSet.ToNetworkRuleSet();
            }
            if (ApiProperty != null)
            {
                properties.ApiProperties = ApiProperty;
            }

            Sku sku = null;

            if (!string.IsNullOrWhiteSpace(this.SkuName))
            {
                sku = new Sku(this.SkuName);
            }

            Dictionary <string, string> tags = null;

            if (this.Tag != null)
            {
                Dictionary <string, string> tagDictionary = TagsConversionHelper.CreateTagDictionary(Tag);
                tags = tagDictionary ?? new Dictionary <string, string>();
            }

            Account updateParameters = new Account()
            {
                Sku        = sku,
                Tags       = tags,
                Properties = properties
            };

            if (!string.IsNullOrEmpty(PublicNetworkAccess))
            {
                updateParameters.Properties.PublicNetworkAccess = PublicNetworkAccess;
            }

            if (DisableLocalAuth != null)
            {
                updateParameters.Properties.DisableLocalAuth = DisableLocalAuth;
            }

            if (RestrictOutboundNetworkAccess != null)
            {
                updateParameters.Properties.RestrictOutboundNetworkAccess = RestrictOutboundNetworkAccess;
            }

            if (AllowedFqdnList != null)
            {
                updateParameters.Properties.AllowedFqdnList = AllowedFqdnList;
            }

            if (AssignIdentity.IsPresent || this.UserAssignedIdentityId != null || this.IdentityType != null)
            {
                ResourceIdentityType resourceIdentityType = ResourceIdentityType.SystemAssigned;
                if (this.IdentityType == null || !Enum.TryParse(this.IdentityType.ToString(), out resourceIdentityType))
                {
                    resourceIdentityType = ResourceIdentityType.SystemAssigned;
                }

                if (this.UserAssignedIdentityId != null && resourceIdentityType == ResourceIdentityType.SystemAssigned)
                {
                    resourceIdentityType = ResourceIdentityType.SystemAssignedUserAssigned;
                }

                updateParameters.Identity = new Identity(resourceIdentityType);
                if (this.UserAssignedIdentityId != null)
                {
                    updateParameters.Identity.UserAssignedIdentities = new Dictionary <string, UserAssignedIdentity>();
                    foreach (var userAssignedIdentityId in this.UserAssignedIdentityId)
                    {
                        updateParameters.Identity.UserAssignedIdentities.Add(userAssignedIdentityId, new UserAssignedIdentity());
                    }
                }
            }

            if (CognitiveServicesEncryption.IsPresent)
            {
                updateParameters.Properties.Encryption = new Encryption(null, KeySource.MicrosoftCognitiveServices);
            }

            if (ParameterSetName == KeyVaultEncryptionParameterSet)
            {
                updateParameters.Properties.Encryption = new Encryption(
                    new KeyVaultProperties()
                {
                    KeyName          = KeyName,
                    KeyVersion       = KeyVersion,
                    KeyVaultUri      = KeyVaultUri,
                    IdentityClientId = KeyVaultIdentityClientId
                },
                    KeySource.MicrosoftKeyVault);
            }

            if (StorageAccountId != null && StorageAccountId.Length > 0)
            {
                updateParameters.Properties.UserOwnedStorage = new List <UserOwnedStorage>();
                foreach (var storageAccountId in StorageAccountId)
                {
                    updateParameters.Properties.UserOwnedStorage.Add(new UserOwnedStorage(storageAccountId));
                }
            }

            string processMessage = string.Empty;

            if (sku != null && tags != null)
            {
                processMessage = string.Format(CultureInfo.CurrentCulture, Resources.SetAccount_ProcessMessage_UpdateSkuAndTags, this.Name, sku.Name);
            }
            else if (sku != null)
            {
                processMessage = string.Format(CultureInfo.CurrentCulture, Resources.SetAccount_ProcessMessage_UpdateSku, this.Name, sku.Name);
            }
            else if (tags != null)
            {
                processMessage = string.Format(CultureInfo.CurrentCulture, Resources.SetAccount_ProcessMessage_UpdateTags, this.Name);
            }
            else
            {
                processMessage = string.Format(CultureInfo.CurrentCulture, Resources.SetAccount_ProcessMessage, this.Name);
            }

            if (ShouldProcess(
                    this.Name, processMessage)
                ||
                Force.IsPresent)
            {
                RunCmdLet(() =>
                {
                    var updatedAccount = this.CognitiveServicesClient.Accounts.Update(
                        this.ResourceGroupName,
                        this.Name,
                        updateParameters
                        );

                    WriteCognitiveServicesAccount(updatedAccount);
                });
            }
        }