public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            if (ShouldProcess(this.Name, "Update Cognitive Services Account NetworkRule"))
            {
                if (IpRule == null && VirtualNetworkRule == null && defaultAction == null)
                {
                    throw new System.ArgumentNullException("IpRules, VirtualNetworkRules, DefaultAction", "Request must specify an account NetworkRule property to update.");
                }

                var account = this.CognitiveServicesClient.Accounts.GetProperties(
                    this.ResourceGroupName,
                    this.Name);
                NetworkRuleSet accountACL = account.Properties.NetworkAcls;

                if (accountACL == null)
                {
                    accountACL = new NetworkRuleSet();
                    accountACL.DefaultAction = NetworkRuleAction.Deny;
                }

                PSNetworkRuleSet psNetworkRule = PSNetworkRuleSet.Create(accountACL);

                if (isIpRuleSet)
                {
                    psNetworkRule.IpRules = IpRule;
                }

                if (isNetworkRuleSet)
                {
                    psNetworkRule.VirtualNetworkRules = VirtualNetworkRule;
                }

                if (defaultAction != null)
                {
                    psNetworkRule.DefaultAction = defaultAction.Value;
                }

                var properties = new CognitiveServicesAccountProperties();
                properties.NetworkAcls = psNetworkRule.ToNetworkRuleSet();
                this.CognitiveServicesClient.Accounts.Update(
                    this.ResourceGroupName,
                    this.Name,
                    new CognitiveServicesAccount()
                {
                    Properties = properties
                }
                    );

                account = this.CognitiveServicesClient.Accounts.GetProperties(this.ResourceGroupName, this.Name);

                WriteObject(PSNetworkRuleSet.Create(account.Properties.NetworkAcls));
            }
        }
Exemple #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>();
            }

            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);
                });
            }
        }
Exemple #3
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);
                });
            }
        }
Exemple #4
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            if (ShouldProcess(this.Name, "Remove Cognitive Services Account Networkrules"))
            {
                var account = this.CognitiveServicesClient.Accounts.GetProperties(
                    this.ResourceGroupName,
                    this.Name);
                NetworkRuleSet accountACL = account.Properties.NetworkAcls;

                if (accountACL == null)
                {
                    accountACL = new NetworkRuleSet();
                }

                switch (ParameterSetName)
                {
                case NetWorkRuleStringParameterSet:
                    if (accountACL.VirtualNetworkRules == null)
                    {
                        accountACL.VirtualNetworkRules = new List <VirtualNetworkRule>();
                    }
                    foreach (string s in VirtualNetworkResourceId)
                    {
                        VirtualNetworkRule rule = new VirtualNetworkRule(s);
                        if (!RemoveNetworkRule(accountACL.VirtualNetworkRules, rule))
                        {
                            throw new ArgumentOutOfRangeException("VirtualNetworkResourceId", String.Format("Can't remove VirtualNetworkRule with specific ResourceId since not exist: {0}", rule.Id));
                        }
                    }
                    break;

                case IpRuleStringParameterSet:
                    if (accountACL.IpRules == null)
                    {
                        accountACL.IpRules = new List <IpRule>();
                    }
                    foreach (string s in IpAddressOrRange)
                    {
                        IpRule rule = new IpRule(s);
                        if (!RemoveIpRule(accountACL.IpRules, rule))
                        {
                            throw new ArgumentOutOfRangeException("IPAddressOrRange", String.Format("Can't remove IpRule with specific IPAddressOrRange since not exist: {0}", rule.Value));
                        }
                    }
                    break;

                case NetworkRuleObjectParameterSet:
                    if (accountACL.VirtualNetworkRules == null)
                    {
                        accountACL.VirtualNetworkRules = new List <VirtualNetworkRule>();
                    }
                    foreach (PSVirtualNetworkRule rule in VirtualNetworkRule)
                    {
                        if (!RemoveNetworkRule(accountACL.VirtualNetworkRules, rule.ToVirtualNetworkRule()))
                        {
                            throw new ArgumentOutOfRangeException("VirtualNetworkRule", String.Format("Can't remove VirtualNetworkRule with specific ResourceId since not exist: {0}", rule.Id));
                        }
                    }
                    break;

                case IpRuleObjectParameterSet:
                    if (accountACL.IpRules == null)
                    {
                        accountACL.IpRules = new List <IpRule>();
                    }
                    foreach (PSIpRule rule in IpRule)
                    {
                        if (!RemoveIpRule(accountACL.IpRules, rule.ToIpRule()))
                        {
                            throw new ArgumentOutOfRangeException("IPRule", String.Format("Can't remove IpRule with specific IPAddressOrRange since not exist: {0}", rule.IpAddress));
                        }
                    }
                    break;
                }


                var properties = new CognitiveServicesAccountProperties();
                properties.NetworkAcls = accountACL;
                this.CognitiveServicesClient.Accounts.Update(
                    this.ResourceGroupName,
                    this.Name,
                    new CognitiveServicesAccount()
                {
                    Properties = properties
                }
                    );

                account = this.CognitiveServicesClient.Accounts.GetProperties(this.ResourceGroupName, this.Name);

                switch (ParameterSetName)
                {
                case NetWorkRuleStringParameterSet:
                case NetworkRuleObjectParameterSet:
                    WriteObject(PSNetworkRuleSet.Create(account.Properties.NetworkAcls).VirtualNetworkRules);
                    break;

                case IpRuleStringParameterSet:
                case IpRuleObjectParameterSet:
                    WriteObject(PSNetworkRuleSet.Create(account.Properties.NetworkAcls).IpRules);
                    break;
                }
            }
        }
Exemple #5
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            if (ShouldProcess(this.Name, "Add Cognitive Services Account NetworkRules"))
            {
                var account = this.CognitiveServicesClient.Accounts.GetProperties(
                    this.ResourceGroupName,
                    this.Name);
                NetworkRuleSet accountACL = account.Properties.NetworkAcls;

                if (accountACL == null)
                {
                    accountACL = new NetworkRuleSet();
                    // Deny is the default action value from server side,
                    // Specifically make default action Deny in client side as server side might want this value to be always provided in future.
                    accountACL.DefaultAction = NetworkRuleAction.Deny;
                }

                switch (ParameterSetName)
                {
                case NetWorkRuleStringParameterSet:
                    if (accountACL.VirtualNetworkRules == null)
                    {
                        accountACL.VirtualNetworkRules = new List <VirtualNetworkRule>();
                    }

                    foreach (string s in VirtualNetworkResourceId)
                    {
                        VirtualNetworkRule rule = new VirtualNetworkRule(s, null, true);
                        accountACL.VirtualNetworkRules.Add(rule);
                    }
                    break;

                case IpRuleStringParameterSet:
                    if (accountACL.IpRules == null)
                    {
                        accountACL.IpRules = new List <IpRule>();
                    }

                    foreach (string s in IpAddressOrRange)
                    {
                        IpRule rule = new IpRule(s);
                        accountACL.IpRules.Add(rule);
                    }
                    break;

                case NetworkRuleObjectParameterSet:
                    if (accountACL.VirtualNetworkRules == null)
                    {
                        accountACL.VirtualNetworkRules = new List <VirtualNetworkRule>();
                    }

                    foreach (PSVirtualNetworkRule rule in VirtualNetworkRule)
                    {
                        accountACL.VirtualNetworkRules.Add(rule.ToVirtualNetworkRule());
                    }
                    break;

                case IpRuleObjectParameterSet:
                    if (accountACL.IpRules == null)
                    {
                        accountACL.IpRules = new List <IpRule>();
                    }

                    foreach (PSIpRule rule in IpRule)
                    {
                        accountACL.IpRules.Add(rule.ToIpRule());
                    }
                    break;
                }

                var properties = new CognitiveServicesAccountProperties();
                properties.NetworkAcls = accountACL;
                this.CognitiveServicesClient.Accounts.Update(
                    this.ResourceGroupName,
                    this.Name,
                    new CognitiveServicesAccount()
                {
                    Properties = properties
                }
                    );

                account = this.CognitiveServicesClient.Accounts.GetProperties(this.ResourceGroupName, this.Name);

                switch (ParameterSetName)
                {
                case NetWorkRuleStringParameterSet:
                case NetworkRuleObjectParameterSet:
                    WriteObject(PSNetworkRuleSet.Create(account.Properties.NetworkAcls).VirtualNetworkRules);
                    break;

                case IpRuleStringParameterSet:
                case IpRuleObjectParameterSet:
                    WriteObject(PSNetworkRuleSet.Create(account.Properties.NetworkAcls).IpRules);
                    break;
                }
            }
        }
        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);
                }
            });
        }
Exemple #7
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);
                }
            });
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            if (ShouldProcess(this.Name, "Add Cognitive Services Account NetworkRules"))
            {
                var account = this.CognitiveServicesClient.Accounts.GetProperties(
                    this.ResourceGroupName,
                    this.Name);
                NetworkRuleSet accountACL = account.Properties.NetworkAcls;

                if (accountACL == null)
                {
                    accountACL = new NetworkRuleSet();
                }

                switch (ParameterSetName)
                {
                case NetWorkRuleStringParameterSet:
                    if (accountACL.VirtualNetworkRules == null)
                    {
                        accountACL.VirtualNetworkRules = new List <VirtualNetworkRule>();
                    }

                    foreach (string s in VirtualNetworkResourceId)
                    {
                        VirtualNetworkRule rule = new VirtualNetworkRule(s, null, true);
                        accountACL.VirtualNetworkRules.Add(rule);
                    }
                    break;

                case IpRuleStringParameterSet:
                    if (accountACL.IpRules == null)
                    {
                        accountACL.IpRules = new List <IpRule>();
                    }

                    foreach (string s in IpAddressOrRange)
                    {
                        IpRule rule = new IpRule(s);
                        accountACL.IpRules.Add(rule);
                    }
                    break;

                case NetworkRuleObjectParameterSet:
                    if (accountACL.VirtualNetworkRules == null)
                    {
                        accountACL.VirtualNetworkRules = new List <VirtualNetworkRule>();
                    }

                    foreach (PSVirtualNetworkRule rule in VirtualNetworkRule)
                    {
                        accountACL.VirtualNetworkRules.Add(rule.ToVirtualNetworkRule());
                    }
                    break;

                case IpRuleObjectParameterSet:
                    if (accountACL.IpRules == null)
                    {
                        accountACL.IpRules = new List <IpRule>();
                    }

                    foreach (PSIpRule rule in IpRule)
                    {
                        accountACL.IpRules.Add(rule.ToIpRule());
                    }
                    break;
                }

                var properties = new CognitiveServicesAccountProperties();
                properties.NetworkAcls = accountACL;
                this.CognitiveServicesClient.Accounts.Update(
                    this.ResourceGroupName,
                    this.Name,
                    new CognitiveServicesAccount()
                {
                    Properties = properties
                }
                    );

                account = this.CognitiveServicesClient.Accounts.GetProperties(this.ResourceGroupName, this.Name);

                switch (ParameterSetName)
                {
                case NetWorkRuleStringParameterSet:
                case NetworkRuleObjectParameterSet:
                    WriteObject(PSNetworkRuleSet.Create(account.Properties.NetworkAcls).VirtualNetworkRules);
                    break;

                case IpRuleStringParameterSet:
                case IpRuleObjectParameterSet:
                    WriteObject(PSNetworkRuleSet.Create(account.Properties.NetworkAcls).IpRules);
                    break;
                }
            }
        }