/** * Extract resource type from a resource ID string. * @param id the resource ID string * @return the resource type */ public static String resourceTypeFromResourceId(String id) { return (id != null) ? ResourceId.fromString(id).resourceType() : null; }
/** * Extract resource type from a resource ID string. * @param id the resource ID string * @return the resource type */ public static String resourceTypeFromResourceId(String id) { return (id != null) ? ResourceId.fromString(id).resourceType() : null; }
/** * Extract parent resource path from a resource ID string. * E.g. subscriptions/s/resourcegroups/r/foos/foo/bars/bar will return foos/foo. * * @param id the resource ID string * @return the parent resource ID */ public static String parentRelativePathFromResourceId(String id) { if (id == null) { return null; } ResourceId parent = ResourceId.fromString(id).parent(); if (parent != null) { return parent.resourceType() + "/" + parent.name(); } return ""; }
/** * Extract parent resource path from a resource ID string. * E.g. subscriptions/s/resourcegroups/r/foos/foo/bars/bar will return foos/foo. * * @param id the resource ID string * @return the parent resource ID */ public static String parentRelativePathFromResourceId(String id) { if (id == null) { return null; } ResourceId parent = ResourceId.fromString(id).parent(); if (parent != null) { return parent.resourceType() + "/" + parent.name(); } return ""; }
@Test public void resourceIdForTopLevelResourceWorksFine() { ResourceId resourceId = ResourceId.fromString("/subscriptions/9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef/resourceGroups/resourceGroupName/providers/Microsoft.Network/applicationGateways/something"); Assert.assertEquals(resourceId.name(), "something"); Assert.assertEquals(resourceId.subscriptionId(), "9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef"); Assert.assertEquals(resourceId.resourceGroupName(), "resourceGroupName"); Assert.assertEquals(resourceId.providerNamespace(), "Microsoft.Network"); Assert.assertEquals(resourceId.resourceType(), "applicationGateways"); Assert.assertEquals(resourceId.fullResourceType(), "Microsoft.Network/applicationGateways"); Assert.assertNull(resourceId.parent()); }
@Test public void resourceIdForChildLevelResourceWorksFine() { ResourceId resourceId = ResourceId.fromString("/subscriptions/9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef/resourceGroups/resourceGroupName/providers/Microsoft.Network/applicationGateways/something/someChildType/childName"); Assert.assertEquals(resourceId.name(), "childName"); Assert.assertEquals(resourceId.subscriptionId(), "9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef"); Assert.assertEquals(resourceId.resourceGroupName(), "resourceGroupName"); Assert.assertEquals(resourceId.providerNamespace(), "Microsoft.Network"); Assert.assertEquals(resourceId.resourceType(), "someChildType"); Assert.assertEquals(resourceId.fullResourceType(), "Microsoft.Network/applicationGateways/someChildType"); Assert.assertNotNull(resourceId.parent()); Assert.assertEquals(resourceId.parent().name(), "something"); Assert.assertEquals(resourceId.parent().subscriptionId(), "9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef"); Assert.assertEquals(resourceId.parent().resourceGroupName(), "resourceGroupName"); Assert.assertEquals(resourceId.parent().name(), "something"); Assert.assertEquals(resourceId.parent().providerNamespace(), "Microsoft.Network"); Assert.assertEquals(resourceId.parent().resourceType(), "applicationGateways"); Assert.assertEquals(resourceId.parent().fullResourceType(), "Microsoft.Network/applicationGateways"); }
@Test public void resourceIdForGrandChildLevelResourceWorksFine() { ResourceId resourceId = ResourceId.fromString("/subscriptions/9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef/resourceGroups/resourceGroupName/providers/Microsoft.Network/applicationGateways/something/someChildType/childName/grandChildType/grandChild"); Assert.assertEquals(resourceId.name(), "grandChild"); Assert.assertEquals(resourceId.subscriptionId(), "9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef"); Assert.assertEquals(resourceId.resourceGroupName(), "resourceGroupName"); Assert.assertEquals(resourceId.providerNamespace(), "Microsoft.Network"); Assert.assertEquals(resourceId.resourceType(), "grandChildType"); Assert.assertEquals(resourceId.fullResourceType(), "Microsoft.Network/applicationGateways/someChildType/grandChildType"); Assert.assertNotNull(resourceId.parent()); Assert.assertEquals(resourceId.parent().name(), "childName"); Assert.assertEquals(resourceId.parent().subscriptionId(), "9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef"); Assert.assertEquals(resourceId.parent().resourceGroupName(), "resourceGroupName"); Assert.assertEquals(resourceId.parent().providerNamespace(), "Microsoft.Network"); Assert.assertEquals(resourceId.parent().resourceType(), "someChildType"); Assert.assertEquals(resourceId.parent().fullResourceType(), "Microsoft.Network/applicationGateways/someChildType"); Assert.assertNotNull(resourceId.parent().parent()); Assert.assertEquals(resourceId.parent().parent().name(), "something"); Assert.assertEquals(resourceId.parent().parent().subscriptionId(), "9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef"); Assert.assertEquals(resourceId.parent().parent().resourceGroupName(), "resourceGroupName"); Assert.assertEquals(resourceId.parent().parent().name(), "something"); Assert.assertEquals(resourceId.parent().parent().providerNamespace(), "Microsoft.Network"); Assert.assertEquals(resourceId.parent().parent().resourceType(), "applicationGateways"); Assert.assertEquals(resourceId.parent().parent().fullResourceType(), "Microsoft.Network/applicationGateways"); } }