Skip to content

Commit 3f31175

Browse files
committed
remove custom resource id parser
1 parent 54facbe commit 3f31175

File tree

3 files changed

+21
-36
lines changed

3 files changed

+21
-36
lines changed

src/Compute/Compute/Extension/AEM/AEMHelper.cs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Azure.Commands.Common.Authentication.Models;
1717
using Microsoft.Azure.Commands.Compute.StorageServices;
1818
using Microsoft.Azure.Management.Compute.Models;
19+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
1920
using Microsoft.Azure.Management.Storage.Version2017_10_01;
2021
using Microsoft.Azure.Management.Storage.Version2017_10_01.Models;
2122
using Microsoft.WindowsAzure.Commands.Sync.Download;
@@ -88,29 +89,6 @@ internal StorageAccount GetStorageAccountFromCache(string accountName)
8889

8990
return account;
9091
}
91-
internal string GetResourceGroupFromId(string id)
92-
{
93-
var matcher = new Regex("/([^/]+)/([^/]+)/([^/]+)/([^/]+)");
94-
var result = matcher.Match(id);
95-
if (!result.Success || result.Groups == null || result.Groups.Count < 5)
96-
{
97-
throw new InvalidOperationException(string.Format("Cannot find resource group name and storage account name from resource identity {0}", id));
98-
}
99-
100-
return result.Groups[4].Value;
101-
}
102-
103-
internal string GetResourceNameFromId(string id)
104-
{
105-
var matcher = new Regex("/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)");
106-
var result = matcher.Match(id);
107-
if (!result.Success || result.Groups == null || result.Groups.Count < 9)
108-
{
109-
throw new InvalidOperationException(string.Format("Cannot find resource group name and storage account name from resource identity {0}", id));
110-
}
111-
112-
return result.Groups[8].Value;
113-
}
11492

11593
internal bool IsPremiumStorageAccount(string accountName)
11694
{
@@ -138,7 +116,8 @@ internal bool IsPremiumStorageAccount(string accountName)
138116
try
139117
{
140118
var account = this.GetStorageAccountFromCache(accountName);
141-
var resGroupName = this.GetResourceGroupFromId(account.Id);
119+
120+
var resGroupName = new ResourceIdentifier(account.Id).ResourceGroupName;
142121
StorageCredentialsFactory storageCredentialsFactory = new StorageCredentialsFactory(resGroupName,
143122
this._StorageClient, this._Subscription);
144123
StorageCredentials sc = storageCredentialsFactory.Create(blobUri);
@@ -368,7 +347,7 @@ internal string GetAzureStorageKeyFromCache(string accountName)
368347
}
369348

370349
var account = this.GetStorageAccountFromCache(accountName);
371-
var resourceGroup = this.GetResourceGroupFromId(account.Id);
350+
var resourceGroup = new ResourceIdentifier(account.Id).ResourceGroupName;
372351
var keys = this._StorageClient.StorageAccounts.ListKeys(resourceGroup, account.Name);
373352

374353
_StorageKeyCache.Add(account.Name, keys.GetKey1());

src/Compute/Compute/Extension/AEM/SetAzureRmVMAEMExtension.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
2323
using Microsoft.Azure.Management.Compute;
2424
using Microsoft.Azure.Management.Compute.Models;
25+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
2526
using Microsoft.Azure.Management.Storage.Version2017_10_01;
2627
using Microsoft.WindowsAzure.Storage;
2728
using Microsoft.WindowsAzure.Storage.Auth;
@@ -206,14 +207,15 @@ public override void ExecuteCmdlet()
206207
}
207208
else
208209
{
209-
var osDiskMD = ComputeClient.ComputeManagementClient.Disks.Get(this._Helper.GetResourceGroupFromId(osdisk.ManagedDisk.Id),
210-
this._Helper.GetResourceNameFromId(osdisk.ManagedDisk.Id));
210+
var resId = new ResourceIdentifier(osdisk.ManagedDisk.Id);
211+
212+
var osDiskMD = ComputeClient.ComputeManagementClient.Disks.Get(resId.ResourceGroupName, resId.ResourceName);
211213
if (osDiskMD.Sku.Name == StorageAccountTypes.PremiumLRS)
212214
{
213215
WriteVerbose("OS Disk is a Premium Managed Disk - adding SLAs for OS disk");
214216
var sla = this._Helper.GetDiskSLA(osDiskMD.DiskSizeGB, null);
215217
var caching = osdisk.Caching;
216-
sapmonPublicConfig.Add(new KeyValuePair() { Key = "osdisk.name", Value = this._Helper.GetResourceNameFromId(osdisk.ManagedDisk.Id) });
218+
sapmonPublicConfig.Add(new KeyValuePair() { Key = "osdisk.name", Value = resId.ResourceName });
217219
sapmonPublicConfig.Add(new KeyValuePair() { Key = "osdisk.caching", Value = caching });
218220
sapmonPublicConfig.Add(new KeyValuePair() { Key = "osdisk.type", Value = AEMExtensionConstants.DISK_TYPE_PREMIUM_MD });
219221
sapmonPublicConfig.Add(new KeyValuePair() { Key = "osdisk.sla.throughput", Value = sla.TP });
@@ -231,16 +233,17 @@ public override void ExecuteCmdlet()
231233
{
232234
if (disk.ManagedDisk != null)
233235
{
234-
var diskMD = ComputeClient.ComputeManagementClient.Disks.Get(this._Helper.GetResourceGroupFromId(disk.ManagedDisk.Id),
235-
this._Helper.GetResourceNameFromId(disk.ManagedDisk.Id));
236+
var resId = new ResourceIdentifier(disk.ManagedDisk.Id);
237+
238+
var diskMD = ComputeClient.ComputeManagementClient.Disks.Get(resId.ResourceGroupName, resId.ResourceName);
236239

237240
if (diskMD.Sku.Name == StorageAccountTypes.PremiumLRS)
238241
{
239242
this._Helper.WriteVerbose("Data Disk {0} is a Premium Managed Disk - adding SLAs for disk", diskNumber.ToString());
240243
var sla = this._Helper.GetDiskSLA(diskMD.DiskSizeGB, null);
241244
var cachingMD = disk.Caching;
242245
sapmonPublicConfig.Add(new KeyValuePair() { Key = "disk.lun." + diskNumber, Value = disk.Lun });
243-
sapmonPublicConfig.Add(new KeyValuePair() { Key = "disk.name." + diskNumber, Value = this._Helper.GetResourceNameFromId(disk.ManagedDisk.Id) });
246+
sapmonPublicConfig.Add(new KeyValuePair() { Key = "disk.name." + diskNumber, Value = resId.ResourceName });
244247
sapmonPublicConfig.Add(new KeyValuePair() { Key = "disk.caching." + diskNumber, Value = cachingMD });
245248
sapmonPublicConfig.Add(new KeyValuePair() { Key = "disk.type." + diskNumber, Value = AEMExtensionConstants.DISK_TYPE_PREMIUM_MD });
246249
sapmonPublicConfig.Add(new KeyValuePair() { Key = "disk.sla.throughput." + diskNumber, Value = sla.TP });
@@ -253,7 +256,7 @@ public override void ExecuteCmdlet()
253256
var sla = this._Helper.GetDiskSLA(diskMD.DiskSizeGB, null);
254257
var cachingMD = disk.Caching;
255258
sapmonPublicConfig.Add(new KeyValuePair() { Key = "disk.lun." + diskNumber, Value = disk.Lun });
256-
sapmonPublicConfig.Add(new KeyValuePair() { Key = "disk.name." + diskNumber, Value = this._Helper.GetResourceNameFromId(disk.ManagedDisk.Id) });
259+
sapmonPublicConfig.Add(new KeyValuePair() { Key = "disk.name." + diskNumber, Value = resId.ResourceName });
257260
sapmonPublicConfig.Add(new KeyValuePair() { Key = "disk.caching." + diskNumber, Value = cachingMD });
258261
sapmonPublicConfig.Add(new KeyValuePair() { Key = "disk.type." + diskNumber, Value = AEMExtensionConstants.DISK_TYPE_ULTRA_MD });
259262
sapmonPublicConfig.Add(new KeyValuePair() { Key = "disk.sla.throughput." + diskNumber, Value = diskMD.DiskMBpsReadWrite });

src/Compute/Compute/Extension/AEM/TestAzureRmVMAEMExtension.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
2222
using Microsoft.Azure.Management.Compute;
2323
using Microsoft.Azure.Management.Compute.Models;
24+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
2425
using Microsoft.Azure.Management.Storage.Version2017_10_01;
2526
using Microsoft.Azure.Management.Storage.Version2017_10_01.Models;
2627
using Newtonsoft.Json;
@@ -360,8 +361,9 @@ public override void ExecuteCmdlet()
360361
}
361362
else
362363
{
363-
var osDiskMD = ComputeClient.ComputeManagementClient.Disks.Get(this._Helper.GetResourceGroupFromId(osdisk.ManagedDisk.Id),
364-
this._Helper.GetResourceNameFromId(osdisk.ManagedDisk.Id));
364+
var resId = new ResourceIdentifier(osdisk.ManagedDisk.Id);
365+
366+
var osDiskMD = ComputeClient.ComputeManagementClient.Disks.Get(resId.ResourceGroupName, resId.ResourceName);
365367
if (osDiskMD.Sku.Name == StorageAccountTypes.PremiumLRS)
366368
{
367369
var sla = this._Helper.GetDiskSLA(osDiskMD.DiskSizeGB, null);
@@ -387,8 +389,9 @@ public override void ExecuteCmdlet()
387389
{
388390
if (disk.ManagedDisk != null)
389391
{
390-
var diskMD = ComputeClient.ComputeManagementClient.Disks.Get(this._Helper.GetResourceGroupFromId(disk.ManagedDisk.Id),
391-
this._Helper.GetResourceNameFromId(disk.ManagedDisk.Id));
392+
var resId = new ResourceIdentifier(disk.ManagedDisk.Id);
393+
394+
var diskMD = ComputeClient.ComputeManagementClient.Disks.Get(resId.ResourceGroupName, resId.ResourceName);
392395

393396
if (diskMD.Sku.Name == StorageAccountTypes.PremiumLRS)
394397
{

0 commit comments

Comments
 (0)