Skip to content

Commit 70eca57

Browse files
authored
fix error when parsing id with subscriptions of other resource types (Azure#25078)
* fix error when parsing subscriptions of other resource types; add a test case using id from ApiManagement and ServiceBus * use switch rather than if * using subscription's parent resource type to judge * correct typo Azure#24861
1 parent b3681b2 commit 70eca57

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

sdk/resourcemanager/Azure.ResourceManager/src/ResourceIdentifier.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ private void Init(ResourceIdentifier parent, ResourceType resourceType, string n
105105
private static ResourceType ChooseResourceType(string resourceTypeName, ResourceIdentifier parent) => resourceTypeName.ToLowerInvariant() switch
106106
{
107107
ResourceGroupsLowerKey => ResourceGroup.ResourceType,
108-
SubscriptionsKey => Subscription.ResourceType,
108+
//subscriptions' type is Microsoft.Resources/subscriptions only when its parent is Tenant
109+
SubscriptionsKey when parent.ResourceType==Tenant.ResourceType => Subscription.ResourceType,
109110
_ => new ResourceType(parent.ResourceType, resourceTypeName)
110111
};
111112

@@ -127,7 +128,7 @@ public static ResourceIdentifier Create(string resourceId)
127128
throw new ArgumentOutOfRangeException(nameof(resourceId), "Invalid resource id.");
128129

129130
var firstToLower = parts[0].ToLowerInvariant();
130-
if (firstToLower != SubscriptionsKey && firstToLower != ProvidersKey)
131+
if (firstToLower != SubscriptionsKey && firstToLower != ProvidersKey)
131132
throw new ArgumentOutOfRangeException(nameof(resourceId), "Invalid resource id.");
132133

133134
return AppendNext(RootResourceIdentifier, parts);
@@ -384,7 +385,7 @@ public override int GetHashCode()
384385
/// <returns></returns>
385386
public static bool operator ==(ResourceIdentifier id1, ResourceIdentifier id2)
386387
{
387-
return ResourceIdentifier.Equals(id1,id2);
388+
return ResourceIdentifier.Equals(id1, id2);
388389
}
389390

390391
/// <summary>
@@ -395,7 +396,7 @@ public override int GetHashCode()
395396
/// <returns></returns>
396397
public static bool operator !=(ResourceIdentifier id1, ResourceIdentifier id2)
397398
{
398-
return !ResourceIdentifier.Equals(id1,id2);
399+
return !ResourceIdentifier.Equals(id1, id2);
399400
}
400401

401402
/// <summary>

sdk/resourcemanager/Azure.ResourceManager/tests/Unit/ResourceIdentifierTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ public void CanParseExtensionResourceIds(string baseId, string extensionNamespac
111111
Assert.AreEqual(targetResourceId, subject.Parent);
112112
}
113113

114+
[TestCase("/subscriptions/17fecd63-33d8-4e43-ac6f-0aafa111b38d/resourceGroups/myRg/providers/Microsoft.ApiManagement/service/myservicename/subscriptions/mysubscription", "Microsoft.ApiManagement/service/subscriptions",
115+
Description = "From ApiManagement")]
116+
[TestCase("/subscriptions/17fecd63-33d8-4e43-ac6f-0aafa111b38d/resourceGroups/myRg/providers/Microsoft.ServiceBus/namespaces/mynamespace/topics/mytopic/subscriptions/mysubscription", "Microsoft.ServiceBus/namespaces/topics/subscriptions",
117+
Description = "From ServiceBus")]
118+
public void CanParseResourceIdsWithSubscriptionsOfOtherResourceTypes(string resourceId, string expectedResourcetype)
119+
{
120+
ResourceIdentifier subject = resourceId;
121+
Assert.AreEqual(expectedResourcetype, subject.ResourceType.ToString());
122+
}
123+
114124
[TestCase("UnformattedString", Description = "Too Few Elements")]
115125
[TestCase("/subs/sub1/rgs/rg1/", Description = "No known parts")]
116126
[TestCase("/subscriptions/sub1/rgs/rg1/", Description = "Subscription not a Guid")]

0 commit comments

Comments
 (0)