Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions docs/docs/objects/manage-from-cli.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
---
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 object 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 object get InfraDevice spine01
```

The identifier you pass to `update` and `delete` is any of these three values. To find it for an object in a list, use its name or HFID directly, or switch to `--output json` or `--output yaml` to see the object's UUID (`id`) and HFID alongside the other fields.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Users cannot follow this documented list workflow to obtain the UUID or HFID needed for a later update or delete: JSON/CSV/YAML list output omits both identifiers, while only detail JSON includes id and the formatters do not emit an HFID field. Please either document querying each object by name/HFID for detail output or update the CLI formatter contract so list output includes the identifiers described here; the affected implementation is opsmill/infrahub-sdk-python#900/infrahub_sdk/ctl/formatters/base.py:80-103.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At docs/docs/objects/manage-from-cli.mdx, line 48:

<comment>Users cannot follow this documented list workflow to obtain the UUID or HFID needed for a later `update` or `delete`: JSON/CSV/YAML list output omits both identifiers, while only detail JSON includes `id` and the formatters do not emit an HFID field. Please either document querying each object by name/HFID for detail output or update the CLI formatter contract so list output includes the identifiers described here; the affected implementation is opsmill/infrahub-sdk-python#900/infrahub_sdk/ctl/formatters/base.py:80-103.</comment>

<file context>
@@ -45,6 +45,8 @@ Provide an identifier — a UUID, a name, or an HFID — to display a single obj
 infrahubctl get InfraDevice spine01

+The identifier you pass to update and delete is any of these three values. To find it for an object in a list, use its name or HFID directly, or switch to --output json or --output yaml to see the object's UUID (id) and HFID alongside the other fields.
+
Filter the results by attribute value with attribute__value=<value>:
</file context>


</details>

```suggestion
The identifier you pass to `update` and `delete` is any of these three values. To find it for an object in a list, use its name or HFID directly, or query the object by name or HFID to display its UUID (`id`) in the detailed output.


Filter the results by attribute value with `attribute__value=<value>`:

```bash
infrahubctl object get InfraDevice --filter name__value=spine01
```

Page through large result sets with `--limit` and `--offset`:

```bash
infrahubctl object get InfraDevice --limit 10 --offset 20
```

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 object get InfraDevice --output json
```

## Create objects

Set field values inline with repeatable `--set key=value` flags:

```bash
infrahubctl object create InfraDevice --set name=spine01 --set status=active
```

Relationship values resolve by name. For example, `--set location=DC1` looks up the `DC1` node and links it:

```bash
infrahubctl object create InfraDevice --set name=spine01 --set location=DC1
```

Alternatively, supply the object as a JSON or YAML file with `--file` (`-f`). The `--set` and `--file` modes are mutually exclusive:

```bash
infrahubctl object create InfraDevice --file devices.yml
```

## 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 object update InfraDevice spine01 --set status=active
```

An update only changes the fields you provide. Attributes and relationships you leave out keep their current values — omitting a field does not clear it.

With `--file`, the file defines which objects to update and what to change, so the kind and identifier on the command line are ignored:

```bash
infrahubctl object update InfraDevice spine01 --file updates.yml
```

## Delete objects

Delete an object by kind and identifier. A confirmation prompt is shown first:

```bash
infrahubctl object delete InfraDevice spine01
```

Pass `--yes` (`-y`) to skip the prompt in scripts:

```bash
infrahubctl object delete InfraDevice spine01 --yes
```

## 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 object get InfraDevice --output yaml > devices.yml
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 object get InfraDevice --branch develop
infrahubctl object create InfraDevice --set name=spine02 --branch develop
```

## Reference

- [`infrahubctl object get`]($(base_url)infrahubctl/infrahubctl-object#infrahubctl-object-get)
- [`infrahubctl object create`]($(base_url)infrahubctl/infrahubctl-object#infrahubctl-object-create)
- [`infrahubctl object update`]($(base_url)infrahubctl/infrahubctl-object#infrahubctl-object-update)
- [`infrahubctl object delete`]($(base_url)infrahubctl/infrahubctl-object#infrahubctl-object-delete)
- [`infrahubctl schema`]($(base_url)infrahubctl/infrahubctl-schema)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- [`infrahubctl schema`]($(base_url)infrahubctl/infrahubctl-schema)
- [`infrahubctl object schema`]($(base_url)infrahubctl/infrahubctl-schema)

1 change: 1 addition & 0 deletions docs/docs/objects/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Objects, their attribute values, and their relationships all carry metadata. At

## What you can do with objects

- **[Manage objects from the command line](./manage-from-cli.mdx)** — Query, create, update, and delete individual objects with `infrahubctl`
- **[Convert object kind](./convert-object-kind.mdx)** — Change the schema kind of an existing object while preserving its data
- **[Metadata & lineage](./metadata.mdx)** — Inspect where each attribute value came from and track data origin
- **[Load data in bulk using YAML file](./load-from-yaml.mdx)** — Bulk-create or update objects by loading structured YAML files
11 changes: 11 additions & 0 deletions docs/docs/schema/create-and-load.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`schema show` prints the attributes and relationships of a kind, which is useful when querying or creating objects with [`infrahubctl`](../objects/manage-from-cli).
`infrahubctl schema show` prints the attributes and relationships of a kind, which is useful when querying or creating objects with [`infrahubctl`](../objects/manage-from-cli).


## Troubleshooting

### Input should be a valid string (string_type)
Expand Down
1 change: 1 addition & 0 deletions docs/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ const sidebars: SidebarsConfig = {
label: 'Objects',
link: { type: 'doc', id: 'objects/overview' }, // hub
items: [
{ type: 'doc', id: 'objects/manage-from-cli', label: 'Manage objects from the command line' },
{ type: 'doc', id: 'objects/convert-object-kind', label: 'Convert object kind' },
{ type: 'doc', id: 'objects/metadata', label: 'Metadata & lineage' },
{ type: 'doc', id: 'objects/load-from-yaml', label: 'Load data in bulk using YAML file' },
Expand Down
Loading