Skip to content

Commit 010a874

Browse files
committed
Fix issues related with resource id
1 parent 8caac51 commit 010a874

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

src/ResourceManager/ContainerInstance/Commands.ContainerInstance/Commands/ContainerInstanceCmdletBase.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Collections;
1717
using System.Collections.Generic;
1818
using System.Linq;
19+
using System.Text.RegularExpressions;
1920
using AutoMapper;
2021
using Microsoft.Azure.Commands.Common.Authentication;
2122
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
@@ -159,6 +160,28 @@ public string GetResourceGroupLocation(string resourceGroupName)
159160
return resourceGroup?.Location;
160161
}
161162

163+
/// <summary>
164+
/// Parse resource group from resource id.
165+
/// </summary>
166+
/// <param name="resourceId">A resource id.</param>
167+
public string ParseResourceGroupFromResourceId(string resourceId)
168+
{
169+
if (string.IsNullOrEmpty(resourceId))
170+
{
171+
throw new ArgumentNullException("resourceId");
172+
}
173+
174+
Regex r = new Regex(@"(.*?)/resourcegroups/(?<rgname>\S+)/providers/(.*?)", RegexOptions.IgnoreCase);
175+
Match m = r.Match(resourceId);
176+
177+
if (!m.Success)
178+
{
179+
throw new ArgumentException("Invalid format of resourceId");
180+
}
181+
182+
return m.Groups["rgname"].Value;
183+
}
184+
162185
/// <summary>
163186
/// Convert hashtable to dictionary.
164187
/// </summary>

src/ResourceManager/ContainerInstance/Commands.ContainerInstance/Commands/GetAzureContainerGroupCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ protected override void ExecuteCmdletInternal()
7878
}
7979
else if (!string.IsNullOrEmpty(this.ResourceId))
8080
{
81-
var resource = this.ResourceClient.Resources.GetById(this.ResourceId, this.ResourceClient.ApiVersion);
81+
var resource = this.ResourceClient.Resources.GetById(this.ResourceId, this.ContainerClient.ApiVersion);
8282
if (resource != null)
8383
{
8484
var psContainerGroup = PSContainerGroup.FromContainerGroup(
85-
this.ContainerClient.ContainerGroups.Get(resource.ResourceGroupName, resource.Name));
85+
this.ContainerClient.ContainerGroups.Get(this.ParseResourceGroupFromResourceId(this.ResourceId), resource.Name));
8686
this.WriteObject(psContainerGroup);
8787
}
8888
}

src/ResourceManager/ContainerInstance/Commands.ContainerInstance/Commands/GetAzureContainerInstanceLogCommand.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,9 @@ protected override void ExecuteCmdletInternal()
8282
&& string.IsNullOrWhiteSpace(containerGroupName)
8383
&& !string.IsNullOrWhiteSpace(this.ResourceId))
8484
{
85-
var resource = this.ResourceClient.Resources.GetById(this.ResourceId, this.ResourceClient.ApiVersion);
86-
if (resource != null)
87-
{
88-
var containerGroup = this.ContainerClient.ContainerGroups.Get(resource.ResourceGroupName, resource.Name);
89-
resourceGroupName = resource.ResourceGroupName;
90-
containerGroupName = containerGroup?.Name;
91-
}
85+
var resource = this.ResourceClient.Resources.GetById(this.ResourceId, this.ContainerClient.ApiVersion);
86+
resourceGroupName = this.ParseResourceGroupFromResourceId(this.ResourceId);
87+
containerGroupName = resource?.Name;
9288
}
9389

9490
var containerName = this.Name ?? containerGroupName;

src/ResourceManager/ContainerInstance/Commands.ContainerInstance/Commands/RemoveAzureContainerGroupCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ protected override void ExecuteCmdletInternal()
8181
}
8282
else if (!string.IsNullOrEmpty(this.ResourceId))
8383
{
84-
var resource = this.ResourceClient.Resources.GetById(this.ResourceId, this.ResourceClient.ApiVersion);
84+
var resource = this.ResourceClient.Resources.GetById(this.ResourceId, this.ContainerClient.ApiVersion);
8585
if (resource != null)
8686
{
87-
containerGroupDeleted = this.ContainerClient.ContainerGroups.Delete(resource.ResourceGroupName, resource.Name);
87+
containerGroupDeleted = this.ContainerClient.ContainerGroups.Delete(this.ParseResourceGroupFromResourceId(this.ResourceId), resource.Name);
8888
}
8989
}
9090

0 commit comments

Comments
 (0)