Infrahub Golang SDK
1.8K
An idiomatic Go client and command-line tool for Infrahub, inspired by the official Python SDK.
This project is an early port. It currently provides the transport foundation, arbitrary GraphQL execution, branch management, schema APIs, and dynamic node mutations. Specialized Python SDK features are tracked in the roadmap below.
go get github.com/Helvethink/infrahub-go-sdk
client, err := infrahub.NewClient(
"https://infrahub.example.com",
infrahub.WithAPIToken(os.Getenv("INFRAHUB_API_TOKEN")),
infrahub.WithDefaultBranch("main"),
)
if err != nil {
log.Fatal(err)
}
branches, err := client.Branches.List(context.Background())
All network operations accept context.Context. A client is safe for concurrent use, and branch selection is request-scoped.
infrahub: client facade and configurationpkg/batch: generic bounded concurrent executionpkg/api: low-level HTTP and GraphQL protocolpkg/automation: Go-native transforms, generators and checkspkg/branch: branch lifecycle and typespkg/diff: branch diff summaries and complete treespkg/schema: schema discovery, validation, and loadingpkg/task: background-task filtering and pollingpkg/traversal: graph paths and reachable nodespkg/config: strict TOML and environment configurationpkg/node: generic operations for schema-defined objectspkg/objectstore: stored objects and text-file retrievalpkg/repository: repository discovery and commit trackingpkg/resourcepool: IP address/prefix allocation and utilizationpkg/tracking: request trackers and group collectioncmd/infrahubctl: executable entry pointinternal/cli: testable, non-public CLI implementationMost applications should import only the root package. Packages under pkg/ are available for deliberate advanced use; implementation details remain under internal/.
See the Python SDK porting map for implemented and planned capabilities.
make check
make race
make build
make check verifies formatting, runs go vet, runs golangci-lint, and executes all unit and facade tests. This check is mandatory after adding a feature.
Build the command with make build, or install it directly:
go install github.com/Helvethink/infrahub-go-sdk/cmd/infrahubctl@latest
Configuration uses flags or environment variables:
export INFRAHUB_ADDRESS=https://infrahub.example.com
export INFRAHUB_API_TOKEN=...
infrahubctl branch list
infrahubctl branch create --description "SDK work" sdk-work
infrahubctl object validate objects/
infrahubctl object load objects/ --branch sdk-work
infrahubctl task list --state running --limit 10
infrahubctl repository list
infrahubctl schema graphql > schema.graphql
printf 'query { Branch { name } }' | infrahubctl graphql
Run infrahubctl help for the complete command list.
Structured zap logs are written to stderr and never mixed with JSON results on stdout. Set --log-level info or INFRAHUB_LOG_LEVEL=info to log command lifecycle events; the default level is error.
TOML configuration is also supported from the platform user configuration directory, INFRAHUB_CONFIG, INFRAHUBCTL_CONFIG, or -config. See the configuration guide for the file format and precedence rules.
Infrahub generates a GraphQL schema for each data schema and branch. Use Execute for arbitrary queries:
var result struct {
Tags []struct {
ID string `json:"id"`
} `json:"BuiltinTag"`
}
err := client.Execute(ctx, infrahub.GraphQLRequest{
Query: `query Tags { BuiltinTag { id } }`,
OperationName: "Tags",
Branch: "main",
}, &result)
If a GraphQL response contains both data and errors, data is decoded and the returned error can be inspected with errors.As as *infrahub.GraphQLError.
tag, err := client.Nodes.Create(ctx, "BuiltinTag", map[string]any{
"name": map[string]any{"value": "staging"},
"description": map[string]any{"value": "Staging resources"},
}, "main")
Dynamic filters and nested selections are available through client.Nodes.Query. See the dynamic query guide.
repositories, err := client.Repositories.List(ctx, infrahub.RepositoryListOptions{
Branches: []string{"main", "staging"},
})
Repository discovery aggregates commits and internal status across branches. Commit updates are also supported. See the repository guide.
group, err := tracking.NewGroup(tracking.GroupOptions{Identifier: "inventory-import"})
ctx = group.Context(tracking.WithTracker(ctx, "inventory-import"))
_, err = client.Nodes.List(ctx, "BuiltinTag", 0, 100, "main")
result, err := group.Save(ctx, client)
Tracking is request-scoped and safe for concurrent workflows. See the tracking and group-context guide.
uploaded, err := client.ObjectStore.Upload(ctx, "generated configuration")
content, err := client.ObjectStore.Get(ctx, uploaded.Identifier)
file, err := client.ObjectStore.GetFileByID(ctx, nodeID)
Stored objects and text-file endpoints preserve base paths, escape identifiers, and honor response-size limits. See the object-store guide.
tasks, err := client.Tasks.All(ctx, infrahub.TaskListOptions{
Filter: infrahub.TaskFilter{States: []infrahub.TaskState{
infrahub.TaskStateRunning,
}},
})
task, err := client.Tasks.Wait(ctx, taskID, time.Second)
Task filters, logs, related nodes, pagination and cancellation-aware polling are supported. See the tasks guide.
results, err := batch.Map(ctx, nodeIDs, func(ctx context.Context, id string) (*infrahub.Node, error) {
return client.Nodes.GetByID(ctx, "BuiltinDevice", id, "main")
}, batch.Options{Concurrency: 5})
Batch results retain input indexes and support fail-fast or per-result error collection. See the batches guide.
prefixLength := 32
address, err := client.ResourcePools.AllocateAddress(ctx, infrahub.ResourcePoolAddressOptions{
PoolID: poolID,
Identifier: "loopback-edge-01",
PrefixLength: &prefixLength,
AddressKind: "IpamIPAddress",
Branch: "main",
})
Prefix allocation, allocation history and utilization are also supported. See the resource-pool guide.
tree, err := client.Diffs.Tree(ctx, infrahub.DiffOptions{
Branch: "feature/inventory",
})
Node summaries, complete metadata, time ranges, attributes, relationships and peer changes are supported. See the diff guide.
paths, err := client.Traversal.Paths(ctx, infrahub.TraversalPathsOptions{
SourceID: sourceID,
DestinationID: destinationID,
Branch: "main",
})
Path existence, filters, point-in-time traversal and reachable-node discovery are supported on Infrahub 1.10+. See the graph traversal guide.
result, err := client.Automation.RunCheck(ctx, infrahub.AutomationRunOptions{
Query: infrahub.AutomationQueryOptions{Name: "check_input", Branch: "main"},
}, func(ctx context.Context, data map[string]any, report *infrahub.AutomationReporter) error {
report.Error("management address is missing", nodeID, "DcimDevice")
return nil
})
Transforms, idempotent generators and structured checks are implemented as compiled Go extension points. See the automation guide.
Planned ports include schema-aware custom-field query construction, graph traversal, diffs, IP resource allocation, object/file storage, tasks, batches, and tracking. Python-only runtime features such as Jinja transforms and pytest plugins will not be copied into the core Go library.
Content type
Image
Digest
sha256:6daea445e…
Size
3.9 MB
Last updated
17 days ago
docker pull helvethink/infrahub-go-sdk