Skip to content

Commit

Permalink
refactor resourceIdentifier (Azure#25625)
Browse files Browse the repository at this point in the history
* finalize changes for RI and Loc objects

* revert location changes to isolate resourceidentifier

* more cleanup on resource identifier
add meaningful error messages

* update api

* seal the resource identifier class

* update api after seal

* coupld minor comment clean up and added sort test for CompareTo

* update snippets

* update cdn tests / samples

* temp manual fixes to get alpha version release

* update compute tests temp

* temp add rootresourceid for back compat

* update api after adding back rootresourceid

* update version to latest

* undo temp changes

* update core to use proj ref and use new property

* update tests to use explicit ctor for RI

* update deviceupdate

* update eventhubs tests

* update servicebus

* switch to proj ref in servicebus test project
  • Loading branch information
m-nash authored Dec 9, 2021
1 parent c135867 commit 3ca12a1
Show file tree
Hide file tree
Showing 76 changed files with 352 additions and 448 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected void CleanupResourceGroups()
{
try
{
_cleanupClient.GetManagementGroup(mgmtGroupId).Delete(waitForCompletion: _waitForCleanup);
_cleanupClient.GetManagementGroup(new ResourceIdentifier(mgmtGroupId)).Delete(waitForCompletion: _waitForCleanup);
}
catch (RequestFailedException e) when (e.Status == 404 || e.Status == 403)
{
Expand Down Expand Up @@ -213,7 +213,7 @@ public void OneTimeCleanupResourceGroups()
});
Parallel.ForEach(OneTimeManagementGroupCleanupPolicy.ManagementGroupsCreated, mgmtGroupId =>
{
_cleanupClient.GetManagementGroup(mgmtGroupId).Delete(waitForCompletion: _waitForCleanup);
_cleanupClient.GetManagementGroup(new ResourceIdentifier(mgmtGroupId)).Delete(waitForCompletion: _waitForCleanup);
});
}

Expand Down
2 changes: 1 addition & 1 deletion eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
All should have PrivateAssets="All" set so they don't become package dependencies
-->
<ItemGroup>
<PackageReference Update="Microsoft.Azure.AutoRest.CSharp" Version="3.0.0-beta.20211209.1" PrivateAssets="All" />
<PackageReference Update="Microsoft.Azure.AutoRest.CSharp" Version="3.0.0-beta.20211208.5" PrivateAssets="All" />
<PackageReference Update="Azure.ClientSdk.Analyzers" Version="0.1.1-dev.20210903.4" PrivateAssets="All" />
<PackageReference Update="coverlet.collector" Version="1.3.0" PrivateAssets="All" />
<PackageReference Update="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.1" PrivateAssets="All" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ string originGroupName = "myOriginGroup";
var input3 = new CdnOriginGroupData();
input3.Origins.Add(new WritableSubResource
{
Id = $"{endpoint.Id}/origins/myOrigin"
Id = new ResourceIdentifier($"{endpoint.Id}/origins/myOrigin")
});
CdnOriginGroupCreateOperation lro3 = await endpoint.GetCdnOriginGroups().CreateOrUpdateAsync(originGroupName, input3);
CdnOriginGroup originGroup = lro3.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ protected async Task<CdnEndpoint> CreateCdnEndpointWithOriginGroup(Profile profi
DeepCreatedOriginGroup deepCreatedOriginGroup = ResourceDataHelper.CreateDeepCreatedOriginGroup();
deepCreatedOriginGroup.Origins.Add(new WritableSubResource
{
Id = $"{profile.Id}/endpoints/{endpointName}/origins/{deepCreatedOrigin.Name}"
Id = new ResourceIdentifier($"{profile.Id}/endpoints/{endpointName}/origins/{deepCreatedOrigin.Name}")
});
input.Origins.Add(deepCreatedOrigin);
input.OriginGroups.Add(deepCreatedOriginGroup);
input.DefaultOriginGroup = new EndpointPropertiesUpdateParametersDefaultOriginGroup
{
Id = $"{profile.Id}/endpoints/{endpointName}/originGroups/{deepCreatedOriginGroup.Name}"
Id = new ResourceIdentifier($"{profile.Id}/endpoints/{endpointName}/originGroups/{deepCreatedOriginGroup.Name}")
};
var lro = await profile.GetCdnEndpoints().CreateOrUpdateAsync(endpointName, input);
return lro.Value;
Expand Down Expand Up @@ -109,7 +109,7 @@ protected async Task<CdnOriginGroup> CreateCdnOriginGroup(CdnEndpoint endpoint,
CdnOriginGroupData input = ResourceDataHelper.CreateOriginGroupData();
input.Origins.Add(new WritableSubResource
{
Id = $"{endpoint.Id}/origins/{originName}"
Id = new ResourceIdentifier($"{endpoint.Id}/origins/{originName}")
});
var lro = await endpoint.GetCdnOriginGroups().CreateOrUpdateAsync(originGroupName, input);
return lro.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static class ResourceDataHelper
},
AzureDnsZone = new WritableSubResource
{
Id = "/subscriptions/f3d94233-a9aa-4241-ac82-2dfb63ce637a/resourceGroups/cdntest/providers/Microsoft.Network/dnszones/azuretest.net"
Id = new ResourceIdentifier("/subscriptions/f3d94233-a9aa-4241-ac82-2dfb63ce637a/resourceGroups/cdntest/providers/Microsoft.Network/dnszones/azuretest.net")
}
};

Expand Down Expand Up @@ -127,7 +127,7 @@ public static class ResourceDataHelper
{
WafPolicy = new WritableSubResource
{
Id = "/subscriptions/f3d94233-a9aa-4241-ac82-2dfb63ce637a/resourceGroups/CdnTest/providers/Microsoft.Network/frontdoorWebApplicationFirewallPolicies/testAFDWaf"
Id = new ResourceIdentifier("/subscriptions/f3d94233-a9aa-4241-ac82-2dfb63ce637a/resourceGroups/CdnTest/providers/Microsoft.Network/frontdoorWebApplicationFirewallPolicies/testAFDWaf")
}
}
};
Expand All @@ -136,7 +136,7 @@ public static class ResourceDataHelper
{
Parameters = new CustomerCertificateParameters(new WritableSubResource
{
Id = "/subscriptions/87082bb7-c39f-42d2-83b6-4980444c7397/resourceGroups/CdnTest/providers/Microsoft.KeyVault/vaults/testKV4AFD/certificates/testCert"
Id = new ResourceIdentifier("/subscriptions/87082bb7-c39f-42d2-83b6-4980444c7397/resourceGroups/CdnTest/providers/Microsoft.KeyVault/vaults/testKV4AFD/certificates/testCert")
})
{
UseLatestVersion = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task CreateOriginGroups()
var input3 = new CdnOriginGroupData();
input3.Origins.Add(new WritableSubResource
{
Id = $"{endpoint.Id}/origins/myOrigin"
Id = new ResourceIdentifier($"{endpoint.Id}/origins/myOrigin")
});
CdnOriginGroupCreateOperation lro3 = await endpoint.GetCdnOriginGroups().CreateOrUpdateAsync(originGroupName, input3);
CdnOriginGroup originGroup = lro3.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task Update()
{
WafPolicy = new WritableSubResource
{
Id = "/subscriptions/f3d94233-a9aa-4241-ac82-2dfb63ce637a/resourceGroups/CdnTest/providers/Microsoft.Network/frontdoorWebApplicationFirewallPolicies/testAFDWaf"
Id = new ResourceIdentifier("/subscriptions/f3d94233-a9aa-4241-ac82-2dfb63ce637a/resourceGroups/CdnTest/providers/Microsoft.Network/frontdoorWebApplicationFirewallPolicies/testAFDWaf")
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected async Task<GenericResource> CreateVirtualNetwork()
{
var vnetName = Recording.GenerateAssetName("testVNet-");
var subnetName = Recording.GenerateAssetName("testSubnet-");
ResourceIdentifier vnetId = $"{_resourceGroup.Id}/providers/Microsoft.Network/virtualNetworks/{vnetName}";
ResourceIdentifier vnetId = new ResourceIdentifier($"{_resourceGroup.Id}/providers/Microsoft.Network/virtualNetworks/{vnetName}");
var addressSpaces = new Dictionary<string, object>()
{
{ "addressPrefixes", new List<string>() { "10.0.0.0/16" } }
Expand Down Expand Up @@ -64,15 +64,15 @@ protected ResourceIdentifier GetSubnetId(GenericResource vnet)
var properties = vnet.Data.Properties as IDictionary<string, object>;
var subnets = properties["subnets"] as IEnumerable<object>;
var subnet = subnets.First() as IDictionary<string, object>;
return subnet["id"] as string;
return new ResourceIdentifier(subnet["id"] as string);
}

// WEIRD: second level resources cannot use GenericResourceCollection to create.
// Exception thrown: System.InvalidOperationException : An invalid resource id was given /subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c/resourceGroups/testRG-4544/providers/Microsoft.Network/virtualNetworks/testVNet-9796/subnets/testSubnet-1786
private async Task<GenericResource> CreateSubnet(ResourceIdentifier vnetId)
{
var subnetName = Recording.GenerateAssetName("testSubnet-");
ResourceIdentifier subnetId = $"{vnetId}/subnets/{subnetName}";
ResourceIdentifier subnetId = new ResourceIdentifier($"{vnetId}/subnets/{subnetName}");
var input = new GenericResourceData(DefaultLocation)
{
Properties = new Dictionary<string, object>()
Expand All @@ -87,7 +87,7 @@ private async Task<GenericResource> CreateSubnet(ResourceIdentifier vnetId)
private async Task<GenericResource> CreateNetworkInterface(ResourceIdentifier subnetId)
{
var nicName = Recording.GenerateAssetName("testNic-");
ResourceIdentifier nicId = $"{_resourceGroup.Id}/providers/Microsoft.Network/networkInterfaces/{nicName}";
ResourceIdentifier nicId = new ResourceIdentifier($"{_resourceGroup.Id}/providers/Microsoft.Network/networkInterfaces/{nicName}");
var input = new GenericResourceData(DefaultLocation)
{
Properties = new Dictionary<string, object>()
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core/tests/Azure.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(AzureCoreTestFramework)" />
<PackageReference Include="Azure.ResourceManager" />
<ProjectReference Include="..\..\..\resourcemanager\Azure.ResourceManager\src\Azure.ResourceManager.csproj" />
<ProjectReference Include="..\src\Azure.Core.csproj" />
<ProjectReference Include="..\..\Microsoft.Azure.Core.NewtonsoftJson\src\Microsoft.Azure.Core.NewtonsoftJson.csproj" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" />
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core/tests/TestClients/TestResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class TestResource : ArmResource
{
private DiagnosticScopeFactory _diagnostic = new DiagnosticScopeFactory("Azure.Clients", "Microsoft.Azure.Core.Cool.Tests", true);

protected override ResourceType ValidResourceType => ResourceIdentifier.RootResourceIdentifier.ResourceType;
protected override ResourceType ValidResourceType => ResourceIdentifier.Root.ResourceType;

public virtual TestResource GetAnotherOperations()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ internal static DeviceUpdateAccountData DeserializeDeviceUpdateAccountData(JsonE
}
if (property.NameEquals("id"))
{
id = property.Value.GetString();
id = new ResourceIdentifier(property.Value.GetString());
continue;
}
if (property.NameEquals("name"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@ public async Task SetGetNetworkRuleSets()

//set network rule set
string subscriptionId = DefaultSubscription.Id.ToString();
string subnetId1 = subscriptionId + "/resourcegroups/" + _resourceGroup.Id.Name + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/default1";
string subnetId2 = subscriptionId + "/resourcegroups/" + _resourceGroup.Id.Name + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/default2";
string subnetId3 = subscriptionId + "/resourcegroups/" + _resourceGroup.Id.Name + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/default3";
ResourceIdentifier subnetId1 = new ResourceIdentifier(subscriptionId + "/resourcegroups/" + _resourceGroup.Id.Name + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/default1");
ResourceIdentifier subnetId2 = new ResourceIdentifier(subscriptionId + "/resourcegroups/" + _resourceGroup.Id.Name + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/default2");
ResourceIdentifier subnetId3 = new ResourceIdentifier(subscriptionId + "/resourcegroups/" + _resourceGroup.Id.Name + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/default3");
NetworkRuleSetData parameter = new NetworkRuleSetData()
{
DefaultAction = DefaultAction.Deny,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -687,16 +687,16 @@ public async Task<VirtualNetwork> CreateVirtualNetwork(string vnetName, string s
return getVnetResponse;
}

public static string GetChildLbResourceId(string subscriptionId, string resourceGroupName, string lbname, string childResourceType, string childResourceName)
public static ResourceIdentifier GetChildLbResourceId(string subscriptionId, string resourceGroupName, string lbname, string childResourceType, string childResourceName)
{
return
string.Format(
new ResourceIdentifier(string.Format(
"/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/loadBalancers/{2}/{3}/{4}",
subscriptionId,
resourceGroupName,
lbname,
childResourceType,
childResourceName);
childResourceName));
}

protected ApplicationGatewayCollection GetApplicationGatewayCollection(string resourceGroupName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ public async Task ClearChallengeCacheforRecord()
_subscription = await ArmClient.GetDefaultSubscriptionAsync();
}

private static string GetChildAppGwResourceId(string subscriptionId,
private static ResourceIdentifier GetChildAppGwResourceId(string subscriptionId,
string resourceGroupName,
string appGwname,
string childResourceType,
string childResourceName)
{
return string.Format(
return new ResourceIdentifier(string.Format(
"/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/applicationGateways/{2}/{3}/{4}",
subscriptionId,
resourceGroupName,
appGwname,
childResourceType,
childResourceName);
childResourceName));
}

private List<ApplicationGatewaySslCertificate> CreateSslCertificate(string sslCertName, string password)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Threading.Tasks;
using Azure.Core.TestFramework;
using Azure.ResourceManager.Core;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Resources.Models;
using Azure.ResourceManager.Network.Models;
Expand Down Expand Up @@ -67,7 +68,7 @@ public async Task GlobalSetUp()
{
Name = "fwpip",
PublicIPAddress = new WritableSubResource() { Id = _publicIPAddressIdentifier },
Subnet = new WritableSubResource() { Id = _networkIdentifier.ToString() + "/subnets/AzureFirewallSubnet" },
Subnet = new WritableSubResource() { Id = _networkIdentifier.AppendChildResource("subnets", "AzureFirewallSubnet") },
});
var firewallLro = await rg.GetAzureFirewalls().CreateOrUpdateAsync(SessionRecording.GenerateAssetName("firewall-"), firewallData);
_firewall = firewallLro.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using Azure.ResourceManager.Core;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Resources.Models;
using Azure.ResourceManager.Network.Models;
Expand Down Expand Up @@ -99,7 +100,7 @@ public async Task<AzureFirewall> CreateFirewallAsync()
{
Name = "fwpip",
PublicIPAddress = new WritableSubResource() { Id = _publicIPAddressIdentifier },
Subnet = new WritableSubResource() { Id = _networkIdentifier.ToString() + "/subnets/AzureFirewallSubnet" },
Subnet = new WritableSubResource() { Id = _networkIdentifier.AppendChildResource("subnets", "AzureFirewallSubnet") },
});
var firewallLro = await (await _resourceGroup.GetAzureFirewalls().CreateOrUpdateAsync(_firewallName, firewallData)).WaitForCompletionAsync();
return firewallLro.Value;
Expand Down
8 changes: 2 additions & 6 deletions sdk/resourcemanager/Azure.ResourceManager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,9 @@ However, keep in mind that some of those properties could be null. You can usual
### Example: ResourceIdentifier TryGet methods
```C# Snippet:Readme_CastToBaseResourceIdentifier
string resourceId = "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/workshop2021-rg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet";
// Assume we don't know what type of resource id we have we can cast to the base type
ResourceIdentifier id = new ResourceIdentifier(resourceId);
string property;
if (id.TryGetSubscriptionId(out property))
Console.WriteLine($"Subscription: {property}");
if (id.TryGetResourceGroupName(out property))
Console.WriteLine($"ResourceGroup: {property}");
Console.WriteLine($"Subscription: {id.SubscriptionId}");
Console.WriteLine($"ResourceGroup: {id.ResourceGroupName}");
// Parent is only null when we reach the top of the chain which is a Tenant
Console.WriteLine($"Vnet: {id.Parent.Name}");
// Name will never be null
Expand Down
Loading

0 comments on commit 3ca12a1

Please sign in to comment.