https://www.pexels.com/photo/man-in-santa-claus-costume-716658/ |
Merry Christmas. We are taking this holiday break to learn about Azure Resource Graph API in Python.
Source code is available here. And you can follow the README.md to setup up the Python development environment and test it out.
Here are few things to learn before diving deeper.
- Know the graph tables
- Learn the Kusto Query Language (KQL)
This is what we did to get things started
import json import os from az_resources.hosting import container from az_resources.protocols.i_resource_group_query import IResourceGroupQuery def main(): svc = container[IResourceGroupQuery] # get all resource groups in the subscription # subscruption_id is in .env file all_resource_groups = await svc.list_all() with open(os.path.join("outputs", "resource_group.json"), "w") as f: json.dump([rg.model_dump() for rg in all_resource_groups], f, indent=2) # list all resources in the first resource group if len(all_resource_groups) > 0: resources = await svc.fetch_resources(all_resource_groups[0].name) with open(os.path.join("outputs", "resources.json"), "w") as f: json.dump([r.model_dump() for r in resources], f, indent=2) # list all changes to the first resource group changes = await svc.fetch_changes_to_resource_group(all_resource_groups[0].name) with open(os.path.join("outputs", "changes.json"), "w") as f: json.dump([r.model_dump() for r in changes], f, indent=2) if __name__ == "__main__": main()
The code is written with dependency injection so that it is easier to test and most importantly the service can be plugged into existing code base that supports DI.
As you can see that it is easy to query for resources in your Azure subscription.
We will continue to build on it to support more filters and features
Comments
Post a Comment