Package: llamazure.rid
llamazure.rid : Resource IDs you can useThellamazure.ridpackage provides a usable resource ID parser.Benefits:quickly get the actual resource targetted and not the basemost resource (for example, easily get a lock and not the resource it's on)differentiate subscriptions, resource groups, resources, and child resourcespreserve resource tree while keeping resource group and subscription information availableautomatic case normalisation (seriously what's up with that)This library includes several implementations of resource ID parsing.ridproduces object-based resources;mpproduces materialised-path based resources. Which to use? Thempmakes it easy to traverse the bodies of Azure resources to reference other Azure resources. For example, if you have a Microsoft.Network/virtualNetworks and you want to show all of its ipConfigurations: You might get the virtualNetwork, list all ipConfigurations, and then join on the resource ID. In contrast,ridis harder to traverse azure resource data but easier to traverse the hierarchy. For example, it is very easy to get information about the target of a lock from the lock itself. It's also convenient to placeridinto a tree structure, such asllamazure.rid.tresource; whilempuse the materialised-path format for representing tree structures in relational systems.rid : object-based resourcesUsageJust callparseto turn resource IDs into objects. That's it. The resource you want is the result, all the other information is chained in.
You can also ask for the chain directly using theparse_chainmethod. This returns a list of all the parents of the resource, starting at the subscription. Having the chain is useful if you intend to use the hierarchy of resources, like pushing resources into aTresourcefor the tree structure.You'll know if a resource is a child resource if it has a non-None parent resource. It is a root resource if parent is None.Examplesfromllamazure.rid.ridimportparse,Resource,ResourceGroup,Subscriptionp=parse("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example/providers/Microsoft.Example/example_type/example_resource")assertp==Resource(provider='microsoft.example',res_type='example_type',name='example_resource',rg=ResourceGroup(name='example',sub=Subscription(uuid='00000000-0000-0000-0000-000000000000')),sub=Subscription(uuid='00000000-0000-0000-0000-000000000000'),parent=None)mp : materialised-path-based resourcesUsageJust callparseto turn resource IDs into tuples of the path and the object. That's it.
You can also get the parsed chain directly by using theparse_chainmethod. Having the chain might be less useful formpthan forrid. Materialised paths are often used to avoid having to join on parents, so having the parents may not be as helpful.ExamplesParse the resource ID:fromllamazure.rid.mpimportparse,Resourcepath,resource=parse("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example/providers/Microsoft.Example/example_type/example_resource")assertresource==Resource(path=path,provider='microsoft.example',res_type='example_type',name='example_resource',rg="/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/example",sub="/subscriptions/00000000-0000-0000-0000-000000000000")Parse many resource IDs and convert them into a lookup table:fromllamazure.rid.mpimportparseresource_ids=[f"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example/providers/Microsoft.Example/example_type/example_resource{i}"foriinrange(10)]resources=dict(parse(rid)forridinresource_ids)Design notesDenormalised RG and SubscriptionDenormalising the data by injecting the rg and subscription in every resource in the chain increases the usability. You can get this information directly, without having to walk up the tree. Also, not every resource has a resource group, so the data model already has to be looser to accomodate that fact. Denormalising also helps with the implementation somewhat, since we can just push forward with None in the resource group.Classes for modellingI used classes to model the type of object you get back. Sometimes this is annoying, like when you can get a Resource or a SubResource. But that's the reality of Azure.