示例#1
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;
                }
            }
        }
示例#2
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

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

                if (accountACL == null)
                {
                    accountACL = new NetworkRuleSet();
                    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);
                        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 AccountProperties();
                properties.NetworkAcls = accountACL;
                this.CognitiveServicesClient.Accounts.Update(
                    this.ResourceGroupName,
                    this.Name,
                    new Account()
                {
                    Properties = properties
                }
                    );

                account = this.CognitiveServicesClient.Accounts.Get(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();

            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;
                }
            }
        }