Querying Data in Infrahub via the Python SDK
Blog post from OpsMill
Infrahub provides a versatile platform for managing infrastructure data, accessible through a web GUI, GraphQL queries, and a Python SDK, with this guide focusing on utilizing the Python SDK for querying data. Users can interact with the Infrahub sandbox without setting up anything, and the SDK supports both synchronous and asynchronous operations, although the emphasis here is on the synchronous approach. The SDK enables users to establish a client object, authenticate, and perform various operations on data, such as querying, creating, modifying, and deleting resources. Authentication is necessary for modifying data, requiring an API token obtainable through the Infrahub GUI. The guide illustrates querying techniques, like retrieving VLANs using methods such as all(), get(), and filters(), and explains the use of filters to refine searches based on attributes and relationships. It also explores accessing attributes and relationships of nodes, employing fetch() to explicitly fetch related data, and highlights the use of prefetch_relationships to automatically retrieve related nodes for more comprehensive queries.