Skip to content

Migrate network rule related cmdlets to Track2 SDK #16

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

Merged
merged 2 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion src/Storage/Storage.Management/Models/PSNetworkRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Management.Storage.Models;
using System;
using System.Collections.Generic;
using Microsoft.WindowsAzure.Commands.Common.Attributes;
Expand Down
4 changes: 0 additions & 4 deletions src/Storage/Storage.Management/Storage.Management.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
<Compile Remove="File\GetAzStorageFileServiceProperty.cs" />
<Compile Remove="File\UpdateAzAzStorageFileServiceProperty.cs" />
<Compile Remove="Models\PSFileServiceProperties.cs" />
<Compile Remove="StorageAccount\AddAzureStorageAccountNetworkRule.cs" />
<Compile Remove="StorageAccount\GetAzureStorageAccountNetworkRuleSet.cs" />
<Compile Remove="StorageAccount\InvokeAzureStorageAccountHierarchicalNamespaceUpgrade.cs" />
<Compile Remove="StorageAccount\RemoveAzureStorageAccountNetworkRule.cs" />
<Compile Remove="StorageAccount\UpdateAzureStorageAccountNetworkRuleSet.cs" />
<Compile Remove="StorageAccount\InvokeAzureStorageAccountFailover.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System.Collections;
using System.Management.Automation;
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Management.Storage.Models;
using StorageModels = Microsoft.Azure.Management.Storage.Models;
using Microsoft.Azure.Commands.Management.Storage.Models;
using System.Collections.Generic;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using Track2 = Azure.ResourceManager.Storage;
using Track2Models = Azure.ResourceManager.Storage.Models;

namespace Microsoft.Azure.Commands.Management.Storage
{
Expand Down Expand Up @@ -132,26 +131,23 @@ public override void ExecuteCmdlet()

if (ShouldProcess(this.Name, "Add Storage Account NetworkRules"))
{
var storageAccount = this.StorageClient.StorageAccounts.GetProperties(
this.ResourceGroupName,
this.Name);
NetworkRuleSet storageACL = storageAccount.NetworkRuleSet;
Track2.StorageAccountResource storageAccount = this.StorageClientTrack2
.GetStorageAccount(this.ResourceGroupName, this.Name).Get();
Track2Models.NetworkRuleSet storageACL = storageAccount.Data.NetworkRuleSet;

if (storageACL == null)
{
storageACL = new NetworkRuleSet();
storageACL = new Track2Models.NetworkRuleSet(Track2Models.DefaultAction.Allow);
}
bool ruleChanged = false;

switch (ParameterSetName)
{
case NetWorkRuleStringParameterSet:
if (storageACL.VirtualNetworkRules == null)
storageACL.VirtualNetworkRules = new List<VirtualNetworkRule>();
foreach (string s in VirtualNetworkResourceId)
{
bool ruleExist = false;
foreach (VirtualNetworkRule originRule in storageACL.VirtualNetworkRules)
foreach (Track2Models.VirtualNetworkRule originRule in storageACL.VirtualNetworkRules)
{
if (originRule.VirtualNetworkResourceId.Equals(s, System.StringComparison.InvariantCultureIgnoreCase))
{
Expand All @@ -162,19 +158,17 @@ public override void ExecuteCmdlet()
}
if (!ruleExist)
{
VirtualNetworkRule rule = new VirtualNetworkRule(s);
Track2Models.VirtualNetworkRule rule = new Track2Models.VirtualNetworkRule(s);
storageACL.VirtualNetworkRules.Add(rule);
ruleChanged = true;
}
}
break;
case IpRuleStringParameterSet:
if (storageACL.IpRules == null)
storageACL.IpRules = new List<IPRule>();
foreach (string s in IPAddressOrRange)
{
bool ruleExist = false;
foreach (IPRule originRule in storageACL.IpRules)
foreach (Track2Models.IPRule originRule in storageACL.IPRules)
{
if (originRule.IPAddressOrRange.Equals(s, System.StringComparison.InvariantCultureIgnoreCase))
{
Expand All @@ -185,19 +179,15 @@ public override void ExecuteCmdlet()
}
if (!ruleExist)
{
IPRule rule = new IPRule(s);
storageACL.IpRules.Add(rule);
Track2Models.IPRule rule = new Track2Models.IPRule(s);
storageACL.IPRules.Add(rule);
ruleChanged = true;
}
}
break;
case ResourceAccessRuleStringParameterSet:
if (storageACL.ResourceAccessRules == null)
{
storageACL.ResourceAccessRules = new List<ResourceAccessRule>();
}
bool ResourceAccessruleExist = false;
foreach (ResourceAccessRule originRule in storageACL.ResourceAccessRules)
foreach (Track2Models.ResourceAccessRule originRule in storageACL.ResourceAccessRules)
{
if (originRule.TenantId.Equals(this.TenantId, System.StringComparison.InvariantCultureIgnoreCase)
&& originRule.ResourceId.Equals(this.ResourceId, System.StringComparison.InvariantCultureIgnoreCase))
Expand All @@ -209,18 +199,19 @@ public override void ExecuteCmdlet()
}
if (!ResourceAccessruleExist)
{
ResourceAccessRule rule = new ResourceAccessRule(this.TenantId, this.ResourceId);
Track2Models.ResourceAccessRule rule = new Track2Models.ResourceAccessRule{
TenantId = this.TenantId,
ResourceId = this.ResourceId,
};
storageACL.ResourceAccessRules.Add(rule);
ruleChanged = true;
}
break;
case NetworkRuleObjectParameterSet:
if (storageACL.VirtualNetworkRules == null)
storageACL.VirtualNetworkRules = new List<VirtualNetworkRule>();
foreach (PSVirtualNetworkRule rule in VirtualNetworkRule)
{
bool ruleExist = false;
foreach (VirtualNetworkRule originRule in storageACL.VirtualNetworkRules)
foreach (Track2Models.VirtualNetworkRule originRule in storageACL.VirtualNetworkRules)
{
if (originRule.VirtualNetworkResourceId.Equals(rule.VirtualNetworkResourceId, System.StringComparison.InvariantCultureIgnoreCase))
{
Expand All @@ -237,14 +228,10 @@ public override void ExecuteCmdlet()
}
break;
case ResourceAccessRuleObjectParameterSet:
if (storageACL.ResourceAccessRules == null)
{
storageACL.ResourceAccessRules = new List<ResourceAccessRule>();
}
foreach (PSResourceAccessRule rule in ResourceAccessRule)
{
bool ruleExist = false;
foreach (ResourceAccessRule originRule in storageACL.ResourceAccessRules)
foreach (Track2Models.ResourceAccessRule originRule in storageACL.ResourceAccessRules)
{
if (originRule.TenantId.Equals(rule.TenantId, System.StringComparison.InvariantCultureIgnoreCase)
&& originRule.ResourceId.Equals(rule.ResourceId, System.StringComparison.InvariantCultureIgnoreCase))
Expand All @@ -263,12 +250,10 @@ public override void ExecuteCmdlet()
}
break;
case IpRuleObjectParameterSet:
if (storageACL.IpRules == null)
storageACL.IpRules = new List<IPRule>();
foreach (PSIpRule rule in IPRule)
{
bool ruleExist = false;
foreach (IPRule originRule in storageACL.IpRules)
foreach (Track2Models.IPRule originRule in storageACL.IPRules)
{
if (originRule.IPAddressOrRange.Equals(rule.IPAddressOrRange, System.StringComparison.InvariantCultureIgnoreCase))
{
Expand All @@ -279,8 +264,7 @@ public override void ExecuteCmdlet()
}
if (!ruleExist)
{

storageACL.IpRules.Add(PSNetworkRuleSet.ParseStorageNetworkRuleIPRule(rule));
storageACL.IPRules.Add(PSNetworkRuleSet.ParseStorageNetworkRuleIPRule(rule));
ruleChanged = true;
}
}
Expand All @@ -289,30 +273,31 @@ public override void ExecuteCmdlet()

if (ruleChanged)
{
StorageAccountUpdateParameters updateParameters = new StorageAccountUpdateParameters();
updateParameters.NetworkRuleSet = storageACL;
Track2Models.StorageAccountPatch patch = new Track2Models.StorageAccountPatch();
patch.NetworkRuleSet = storageACL;

var updatedAccountResponse = this.StorageClient.StorageAccounts.Update(
this.ResourceGroupName,
this.Name,
updateParameters);
Track2.StorageAccountResource updatedStorageAccount = this.StorageClientTrack2
.GetStorageAccount(this.ResourceGroupName, this.Name)
.Update(patch);

storageAccount = this.StorageClient.StorageAccounts.GetProperties(this.ResourceGroupName, this.Name);
storageAccount = this.StorageClientTrack2
.GetStorageAccount(this.ResourceGroupName, this.Name)
.Get();
}

switch (ParameterSetName)
{
case NetWorkRuleStringParameterSet:
case NetworkRuleObjectParameterSet:
WriteObject(PSNetworkRuleSet.ParsePSNetworkRule(storageAccount.NetworkRuleSet).VirtualNetworkRules);
WriteObject(PSNetworkRuleSet.ParsePSNetworkRule(storageAccount.Data.NetworkRuleSet).VirtualNetworkRules);
break;
case IpRuleStringParameterSet:
case IpRuleObjectParameterSet:
WriteObject(PSNetworkRuleSet.ParsePSNetworkRule(storageAccount.NetworkRuleSet).IpRules);
WriteObject(PSNetworkRuleSet.ParsePSNetworkRule(storageAccount.Data.NetworkRuleSet).IpRules);
break;
case ResourceAccessRuleStringParameterSet:
case ResourceAccessRuleObjectParameterSet:
WriteObject(PSNetworkRuleSet.ParsePSNetworkRule(storageAccount.NetworkRuleSet).ResourceAccessRules);
WriteObject(PSNetworkRuleSet.ParsePSNetworkRule(storageAccount.Data.NetworkRuleSet).ResourceAccessRules);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Management.Storage.Models;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Management.Storage.Models;
using StorageModels = Microsoft.Azure.Management.Storage.Models;
using Microsoft.Azure.Commands.Management.Storage.Models;
using System.Collections.Generic;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Track2 = Azure.ResourceManager.Storage;
using Track2Models = Azure.ResourceManager.Storage.Models;

namespace Microsoft.Azure.Commands.Management.Storage
{
Expand Down Expand Up @@ -49,12 +49,12 @@ public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();

var storageAccount = this.StorageClient.StorageAccounts.GetProperties(
this.ResourceGroupName,
this.Name);
if (storageAccount.NetworkRuleSet != null)
Track2.StorageAccountResource account = this.StorageClientTrack2.GetStorageAccount(this.ResourceGroupName, this.Name)
.Get();

if (account.Data.NetworkRuleSet != null)
{
WriteObject(PSNetworkRuleSet.ParsePSNetworkRule(storageAccount.NetworkRuleSet));
WriteObject(PSNetworkRuleSet.ParsePSNetworkRule(account.Data.NetworkRuleSet));
}
}
}
Expand Down
Loading