Skip to content

Duplicate handling of ips and vnet rules for cognitive services. #26001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -27,7 +27,7 @@ namespace Microsoft.Azure.Commands.Management.CognitiveServices
[OutputType(typeof(PSIpRule), ParameterSetName = new string[] { IpRuleStringParameterSet, IpRuleObjectParameterSet })]
public class AddAzureCognitiveServicesAccountNetworkRuleCommand : CognitiveServicesAccountBaseCmdlet
{
/// <summary>
/// <summary>
/// NetWorkRule in String parameter set name
/// </summary>
private const string NetWorkRuleStringParameterSet = "NetWorkRuleString";
Expand Down Expand Up @@ -104,8 +104,6 @@ public override void ExecuteCmdlet()
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;
}

Expand All @@ -116,45 +114,93 @@ public override void ExecuteCmdlet()
{
accountACL.VirtualNetworkRules = new List<VirtualNetworkRule>();
}

foreach (string s in VirtualNetworkResourceId)
{
VirtualNetworkRule rule = new VirtualNetworkRule(s, null, true);
accountACL.VirtualNetworkRules.Add(rule);
bool ruleExist = false;
foreach (VirtualNetworkRule originRule in accountACL.VirtualNetworkRules)
{
if (originRule.Id.Equals(s, System.StringComparison.InvariantCultureIgnoreCase))
{
ruleExist = true;
WriteDebug($"Skip add VirtualNetworkRule as it already exists: {s}");
break;
}
}
if (!ruleExist)
{
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);
bool ruleExist = false;
foreach (IpRule originRule in accountACL.IPRules)
{
if (originRule.Value.Equals(s, System.StringComparison.InvariantCultureIgnoreCase))
{
ruleExist = true;
WriteDebug($"Skip add IpRule as it already exists: {s}");
break;
}
}
if (!ruleExist)
{
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());
bool ruleExist = false;
foreach (VirtualNetworkRule originRule in accountACL.VirtualNetworkRules)
{
if (originRule.Id.Equals(rule.Id, System.StringComparison.InvariantCultureIgnoreCase))
{
ruleExist = true;
WriteDebug($"Skip add VirtualNetworkRule as it already exists: {rule.Id}");
break;
}
}
if (!ruleExist)
{
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());
bool ruleExist = false;
foreach (IpRule originRule in accountACL.IPRules)
{
if (originRule.Value.Equals(rule.Value, System.StringComparison.InvariantCultureIgnoreCase))
{
ruleExist = true;
WriteDebug($"Skip add IpRule as it already exists: {rule.Value}");
break;
}
}
if (!ruleExist)
{
accountACL.IPRules.Add(rule.ToIpRule());
}
}
break;
}
Expand All @@ -167,8 +213,7 @@ public override void ExecuteCmdlet()
new Account()
{
Properties = properties
}
);
});

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

Expand Down
Loading