How to Manage Infrastructure Data With the Infrahub Python SDK
Blog post from OpsMill
The Infrahub Python SDK provides a comprehensive interface for programmatically managing data within Infrahub by allowing users to query, create, modify, delete, and upsert various data nodes such as devices, VLANs, and sites. The SDK operates through a client object that can be set up with an API token from the Infrahub demo instance, which resets daily, ensuring a fresh start for each session. Users can create nodes using the create() method, referencing schemas to understand required attributes and relationships, and save them into Infrahub. The SDK supports relationships of different cardinalities, allowing users to link nodes either directly or through IDs and Human-Friendly Identifiers (HFIDs). Updating nodes involves retrieving them, changing their attributes or relationships, and saving the changes, with features to handle both single and multiple relationship cardinalities. Deletion is straightforward, requiring retrieval followed by a delete() call. The SDK also facilitates upserting, which combines updating and inserting nodes, provided the schema includes an HFID for unique identification. The SDK encourages practical use through a sandbox environment and emphasizes the importance of secure API token management and the advantages of strict typing for enhanced development experience.