-
Notifications
You must be signed in to change notification settings - Fork 55
docs: document infrahubctl CRUD and schema discovery commands #10016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: stable
Are you sure you want to change the base?
Changes from 1 commit
f7a6460
cfdfd76
2d38255
e302af1
3dfb46b
b82b394
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,147 @@ | ||||||
| --- | ||||||
| title: Manage objects from the command line | ||||||
| --- | ||||||
|
|
||||||
| Use `infrahubctl` to query, create, update, and delete objects directly from your terminal. The commands accept any schema kind in your instance and can display results as a table, JSON, CSV, or YAML. | ||||||
|
|
||||||
| These commands operate on individual objects, which suits interactive work and scripting. To load many objects at once from version-controlled files, see [Load data in bulk using YAML file](./load-from-yaml.mdx). | ||||||
|
|
||||||
| ## Prerequisites | ||||||
|
|
||||||
| - A running Infrahub instance | ||||||
| - `infrahubctl` installed and configured against that instance — see the [infrahubctl documentation]($(base_url)infrahubctl/infrahubctl) | ||||||
|
|
||||||
| ## Discover the schema | ||||||
|
|
||||||
| Before you query or create objects, list the schema kinds available in your instance: | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl schema list | ||||||
| ``` | ||||||
|
|
||||||
| Narrow the list with a case-insensitive match on the kind name: | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl schema list --filter Device | ||||||
| ``` | ||||||
|
|
||||||
| Show the attributes and relationships of a single kind. Use this to find the field names you pass to `--set` and `--filter`: | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl schema show InfraDevice | ||||||
| ``` | ||||||
|
|
||||||
| ## Query objects | ||||||
|
|
||||||
| Omit the identifier to list every object of a kind. Empty columns are hidden by default; pass `--all-columns` to show them: | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl get InfraDevice | ||||||
| ``` | ||||||
|
|
||||||
| Provide an identifier — a UUID, a name, or an HFID — to display a single object in detail. For a multi-part HFID, separate the components with `/`: | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl get InfraDevice spine01 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| Filter the results by attribute value with `attribute__value=<value>`: | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl get InfraDevice --filter name__value=spine01 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| Page through large result sets with `--limit` and `--offset`: | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl get InfraDevice --limit 10 --offset 20 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| A list query that matches no objects exits with code `80`, which lets scripts distinguish an empty result from an error. | ||||||
|
|
||||||
| ### Output formats | ||||||
|
|
||||||
| The `--output` (`-o`) option controls the display format. `get` defaults to a table on an interactive terminal and to JSON when the output is piped. | ||||||
|
|
||||||
| | Format | Flag | Use for | | ||||||
| |---|---|---| | ||||||
| | Table | `--output table` | Reading in an interactive terminal | | ||||||
| | JSON | `--output json` | Scripting and piping to other tools | | ||||||
| | CSV | `--output csv` | Importing into a spreadsheet | | ||||||
| | YAML | `--output yaml` | Backing up and reloading with `infrahubctl object load` | | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl get InfraDevice --output json | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| ## Create objects | ||||||
|
|
||||||
| Set field values inline with repeatable `--set key=value` flags: | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl create InfraDevice --set name=spine01 --set status=active | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| Relationship values resolve by name. For example, `--set location=DC1` looks up the `DC1` node and links it: | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl create InfraDevice --set name=spine01 --set location=DC1 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| Alternatively, supply the object as a JSON or YAML file with `--file` (`-f`). The `--set` and `--file` modes are mutually exclusive: | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl create InfraDevice --file devices.yml | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| ## Update objects | ||||||
|
|
||||||
| Identify the object by kind and identifier, then apply the changes with `--set` or `--file`. The command fetches the object, applies the changes, and saves it back: | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl update InfraDevice spine01 --set status=active | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl update InfraDevice spine01 --file updates.yml | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this nullify unset attributes/relationships in the file, or will it skip? May be good to have a note documenting that here. Also may be beneficial to have a "--set-none" flag or something so that you can declare the fields on the object if we aren't pushing the null attributes/relationships.
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| ## Delete objects | ||||||
|
|
||||||
| Delete an object by kind and identifier. A confirmation prompt is shown first: | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl delete InfraDevice spine01 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| Pass `--yes` (`-y`) to skip the prompt in scripts: | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl delete InfraDevice spine01 --yes | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| ## Back up and reload objects | ||||||
|
|
||||||
| YAML output uses HFID references and omits empty values, so it reloads cleanly. Export a set of objects, then load them back with the `object` subcommand: | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl get InfraDevice --output yaml > devices.yml | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| infrahubctl object load devices.yml | ||||||
| ``` | ||||||
|
|
||||||
| ## Work on a branch | ||||||
|
|
||||||
| Every command accepts `--branch` (`-b`) to target a branch other than the default. Changes on a branch stay isolated until the branch is merged: | ||||||
|
|
||||||
| ```bash | ||||||
| infrahubctl get InfraDevice --branch develop | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| infrahubctl create InfraDevice --set name=spine02 --branch develop | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| ## Reference | ||||||
|
|
||||||
| - [`infrahubctl get`]($(base_url)infrahubctl/infrahubctl-get) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| - [`infrahubctl create`]($(base_url)infrahubctl/infrahubctl-create) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| - [`infrahubctl update`]($(base_url)infrahubctl/infrahubctl-update) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| - [`infrahubctl delete`]($(base_url)infrahubctl/infrahubctl-delete) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| - [`infrahubctl schema`]($(base_url)infrahubctl/infrahubctl-schema) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -89,6 +89,17 @@ schemas: | |||||
| </TabItem> | ||||||
| </Tabs> | ||||||
|
|
||||||
| ## Inspect the loaded schema | ||||||
|
|
||||||
| Once a schema is loaded, list the kinds available in your instance and inspect any one of them from the command line: | ||||||
|
|
||||||
| ```shell | ||||||
| infrahubctl schema list [--filter <name>] [--branch <branch_name>] | ||||||
| infrahubctl schema show <kind> [--branch <branch_name>] | ||||||
| ``` | ||||||
|
|
||||||
| `schema show` prints the attributes and relationships of a kind, which is useful when querying or creating objects with [`infrahubctl`](../objects/manage-from-cli). | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| ## Troubleshooting | ||||||
|
|
||||||
| ### Input should be a valid string (string_type) | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is just a documentation PR (and not a PR on the feature, but another thought, the ordering from the get shows non-hfid attributes first and doesn't include UUID. It would be good to start the table (leftmost column) with something that can be used to get a device.