Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
147 changes: 147 additions & 0 deletions docs/docs/objects/manage-from-cli.mdx
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

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 get InfraDevice
infrahubctl object get InfraDevice

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.

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.

Image

```

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

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 get InfraDevice spine01
infrahubctl object get InfraDevice spine01

```

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

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

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 get InfraDevice --filter name__value=spine01
infrahubctl object get InfraDevice --filter name__value=spine01

```

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

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

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 get InfraDevice --limit 10 --offset 20
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 get InfraDevice --output json

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

```

## Create objects

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

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

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 create InfraDevice --set name=spine01 --set status=active
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 create InfraDevice --set name=spine01 --set location=DC1

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 create InfraDevice --set name=spine01 --set location=DC1
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 create InfraDevice --file devices.yml

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 create InfraDevice --file devices.yml
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 update InfraDevice spine01 --set status=active

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

```

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

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.

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
infrahubctl update InfraDevice spine01 --file updates.yml
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 delete InfraDevice spine01

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 delete InfraDevice spine01
infrahubctl object delete InfraDevice spine01

```

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

```bash
infrahubctl delete InfraDevice spine01 --yes

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 delete InfraDevice spine01 --yes
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 get InfraDevice --output yaml > devices.yml

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 get InfraDevice --output yaml > devices.yml
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 get InfraDevice --branch develop

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 get InfraDevice --branch develop
infrahubctl object get InfraDevice --branch develop

infrahubctl create InfraDevice --set name=spine02 --branch develop

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 create InfraDevice --set name=spine02 --branch develop
infrahubctl object create InfraDevice --set name=spine02 --branch develop

```

## Reference

- [`infrahubctl get`]($(base_url)infrahubctl/infrahubctl-get)

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 get`]($(base_url)infrahubctl/infrahubctl-get)
- [`infrahubctl object get`]($(base_url)infrahubctl/infrahubctl-get)

- [`infrahubctl create`]($(base_url)infrahubctl/infrahubctl-create)

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 create`]($(base_url)infrahubctl/infrahubctl-create)
- [`infrahubctl object create`]($(base_url)infrahubctl/infrahubctl-create)

- [`infrahubctl update`]($(base_url)infrahubctl/infrahubctl-update)

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 update`]($(base_url)infrahubctl/infrahubctl-update)
- [`infrahubctl object update`]($(base_url)infrahubctl/infrahubctl-update)

- [`infrahubctl delete`]($(base_url)infrahubctl/infrahubctl-delete)

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 delete`]($(base_url)infrahubctl/infrahubctl-delete)
- [`infrahubctl object delete`]($(base_url)infrahubctl/infrahubctl-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 @@ -147,6 +147,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