Skip to content

Commit

Permalink
migrate change to Azure.Core
Browse files Browse the repository at this point in the history
  • Loading branch information
archerzz committed Jan 10, 2022
1 parent 1ea640a commit 70c80cd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
14 changes: 10 additions & 4 deletions sdk/core/Azure.Core/src/ResourceIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ private void Init(ResourceIdentifier parent, ResourceType resourceType, string r
ResourceGroupName = parent.ResourceGroupName;
}

if (resourceType == SubscriptionResourceType)
if (resourceType == SubscriptionResourceType) // allow empty subscription ID
{
if (!Guid.TryParse(resourceName, out _))
throw new ArgumentOutOfRangeException(nameof(resourceName), $"The GUID for subscription is invalid {resourceName}.");
if (!string.IsNullOrEmpty(resourceName))
{
if (!Guid.TryParse(resourceName, out _))
throw new ArgumentOutOfRangeException(nameof(resourceName), $"The GUID for subscription is invalid {resourceName}.");
}
SubscriptionId = resourceName;
}

Expand Down Expand Up @@ -228,7 +231,10 @@ private string ToResourceString()
if (!IsProviderResource)
{
builder.Append($"/{ResourceType.GetLastType()}");
if (!string.IsNullOrWhiteSpace(Name))
if (!string.IsNullOrWhiteSpace(Name) ||
// allow empty subscription ID and resource group name
ResourceType.GetLastType().Equals(SubscriptionsKey, StringComparison.InvariantCultureIgnoreCase) ||
ResourceType.GetLastType().Equals(ResourceGroupsLowerKey, StringComparison.InvariantCultureIgnoreCase))
builder.Append($"/{Name}");
}
else
Expand Down
22 changes: 22 additions & 0 deletions sdk/core/Azure.Core/tests/ResourceIdentifierTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,28 @@ public void ResourceIdRetainsOriginalInput(string resourceId)
Assert.AreEqual(resourceId, id.ToString());
}

[TestCase("/subscriptions//providers/Contoso.Widgets/widgets/myWidget",
Description = "SubscriptionResourceIdentifier")]
[TestCase("/subscriptions//locations/westus2/providers/Contoso.Widgets/widgets/myWidget",
Description = "LocationResourceIdentifier")]
[TestCase("/subscriptions//resourceGroups/myRg/providers/Contoso.Widgets/widgets/myWidget",
Description = "ResourceGroupResourceIdentifier")]
public void EmptySubscriptionId(string resourceId)
{
ResourceIdentifier id = new ResourceIdentifier(resourceId);
Assert.IsEmpty(id.SubscriptionId);
Assert.AreEqual(resourceId, id.ToString());
}

[TestCase("/subscriptions/6b085460-5f21-477e-ba44-1035046e9101/resourceGroups//providers/Contoso.Widgets/widgets/myWidget",
Description = "ResourceGroupResourceIdentifier")]
public void EmptyResourceGroupName(string resourceId)
{
ResourceIdentifier id = new ResourceIdentifier(resourceId);
Assert.IsEmpty(id.ResourceGroupName);
Assert.AreEqual(resourceId, id.ToString());
}

[TestCase(TrackedResourceId, TrackedResourceId, 0)]
[TestCase(TrackedResourceId, ChildResourceId, -1)]
[TestCase(ChildResourceId, TrackedResourceId, 1)]
Expand Down

0 comments on commit 70c80cd

Please sign in to comment.