-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add CRUD and schema discovery commands to infrahubctl #900
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
Merged
Merged
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
85502b4
feat: add end-user `infrahub` CLI for CRUD operations and schema disc…
petercrocker 8fba90d
fix: resolve CI failures in markdown lint, integration tests, and docs
petercrocker 94e3780
test: increase unit test coverage for end-user CLI
petercrocker e71acb1
fix: resolve remaining CI failures
petercrocker 52c2b05
fix: resolve CI failures in integration tests, markdown lint, and docs
petercrocker a941e57
refactor: move end-user CLI commands under infrahubctl
petercrocker b8d30b4
fix: resolve doc generation and integration test failures
petercrocker 4f465f9
fix: resolve vale spelling errors in generated CLI docs
petercrocker 0c2ecc5
feat: hide empty columns by default in table and CSV output
petercrocker 36332e3
feat: improve empty results UX for infrahubctl get
petercrocker 7b7fb9f
feat: resolve nodes by UUID, default filter, or HFID
petercrocker c9119ea
fix: make YAML output round-trippable with infrahubctl object load
petercrocker ee6b73f
fix: clean error output for missing --set/--file arguments
petercrocker 233ac0b
feat: resolve relationship values by name in create/update commands
petercrocker aca8c89
fix: show name instead of None in create confirmation message
petercrocker 9f2813e
feat: distinguish create vs upsert in confirmation message
petercrocker 0c730be
feat: skip save and show no-op message when update values unchanged
petercrocker db23a7d
docs: regenerate get command docs (--all-columns flag added)
petercrocker 4633713
docs: add usage examples to all command help text
petercrocker 0a3a408
fix: add 'yaml' to vale spelling exceptions
petercrocker 4caff14
docs: clarify exit code 80 applies only to list mode, not detail lookups
petercrocker 1b4b2bd
docs: document HFID support in identifier argument help text
petercrocker ff5c177
fix: compare relationship IDs not display strings for change detection
petercrocker 419b13b
fix: warn that kind/identifier are ignored in update --file mode
petercrocker f1b73c0
fix: narrow exception handling in resolve_relationship_values
petercrocker 93634c6
test: remove redundant @pytest.mark.anyio from resolve_node tests
petercrocker fa3688f
docs: fix contract to show exit code 80 for empty list results
petercrocker 52f77ce
docs: update spec to reference infrahubctl instead of infrahub command
petercrocker 432ba94
docs: update research.md to reflect infrahubctl integration decision
petercrocker 2a8fb43
fix: preserve leading zeros in --set value coercion
petercrocker 03714b7
test: add relationship no-op test for update command
petercrocker bc4bfb6
fix: ruff formatting for update.py console.print line
petercrocker 1d36d8d
rework
3c3f628
linter
8b314ea
sanitize
b74c893
coderabbit
9af2752
cubic
56e5a0c
filter by NodeSchemaAPI type before accessing .kind in schema list
petercrocker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,6 +133,7 @@ validators | |
| Version Control | ||
| Vitest | ||
| VLANs | ||
| yaml | ||
| Yaml | ||
| yamllint | ||
| YouTube | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # `infrahubctl create` | ||
|
|
||
| Create a new object in Infrahub. | ||
|
|
||
| Provide field values with repeatable --set flags or supply a | ||
| JSON/YAML object file via --file. The two modes are mutually exclusive. | ||
|
|
||
| Examples: | ||
| infrahubctl create InfraDevice --set name=spine01 --set status=active | ||
| infrahubctl create InfraDevice --set name=spine01 --set location=DC1 | ||
| infrahubctl create InfraDevice --file devices.yml | ||
|
|
||
| **Usage**: | ||
|
|
||
| ```console | ||
| $ infrahubctl create [OPTIONS] KIND | ||
| ``` | ||
|
|
||
| **Arguments**: | ||
|
|
||
| * `KIND`: Infrahub schema kind to create [required] | ||
|
|
||
| **Options**: | ||
|
|
||
| * `--set TEXT`: Field value in key=value format | ||
| * `-f, --file PATH`: JSON or YAML file with object data | ||
| * `-b, --branch TEXT`: Target branch | ||
| * `--config-file TEXT`: [env var: INFRAHUBCTL_CONFIG; default: infrahubctl.toml] | ||
| * `--install-completion`: Install completion for the current shell. | ||
| * `--show-completion`: Show completion for the current shell, to copy it or customize the installation. | ||
| * `--help`: Show this message and exit. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # `infrahubctl delete` | ||
|
|
||
| Delete an Infrahub object. | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Fetches the object by KIND and IDENTIFIER, then deletes it. | ||
| Unless --yes is provided, a confirmation prompt is shown first. | ||
|
|
||
| Examples: | ||
| infrahubctl delete InfraDevice spine01 | ||
| infrahubctl delete InfraDevice spine01 --yes | ||
|
|
||
| **Usage**: | ||
|
|
||
| ```console | ||
| $ infrahubctl delete [OPTIONS] KIND IDENTIFIER | ||
| ``` | ||
|
|
||
| **Arguments**: | ||
|
|
||
| * `KIND`: Infrahub schema kind [required] | ||
| * `IDENTIFIER`: UUID, name, or HFID (use / for multi-part, for example: Cisco/NX-OS) [required] | ||
|
|
||
| **Options**: | ||
|
|
||
| * `-y, --yes`: Skip confirmation prompt | ||
| * `-b, --branch TEXT`: Target branch | ||
| * `--config-file TEXT`: [env var: INFRAHUBCTL_CONFIG; default: infrahubctl.toml] | ||
| * `--install-completion`: Install completion for the current shell. | ||
| * `--show-completion`: Show completion for the current shell, to copy it or customize the installation. | ||
| * `--help`: Show this message and exit. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # `infrahubctl get` | ||
|
|
||
| Query and display Infrahub objects. | ||
|
|
||
| When IDENTIFIER is omitted the command lists all objects of the given | ||
| KIND. When IDENTIFIER is provided it displays a single object in | ||
| detail view. Empty columns are hidden by default (use --all-columns). | ||
|
|
||
| Examples: | ||
petercrocker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| infrahubctl get InfraDevice | ||
| infrahubctl get InfraDevice spine01 | ||
| infrahubctl get InfraDevice --filter name__value=spine01 | ||
| infrahubctl get InfraDevice --output json | ||
| infrahubctl get InfraDevice --output yaml > backup.yml | ||
|
|
||
| Exit codes: 0 = results found, 1 = error (including not found in detail | ||
| mode), 80 = list query succeeded but returned zero objects. | ||
|
|
||
| **Usage**: | ||
|
|
||
| ```console | ||
| $ infrahubctl get [OPTIONS] KIND [IDENTIFIER] | ||
| ``` | ||
|
|
||
| **Arguments**: | ||
|
|
||
| * `KIND`: Infrahub schema kind to query [required] | ||
| * `[IDENTIFIER]`: UUID, name, or HFID (use / for multi-part, for example: Cisco/NX-OS) | ||
|
|
||
| **Options**: | ||
|
|
||
| * `--filter TEXT`: Filter in attr__value=x format | ||
| * `-o, --output [table|json|csv|yaml]`: Output format | ||
| * `-b, --branch TEXT`: Target branch | ||
| * `--limit INTEGER`: Maximum results | ||
| * `--offset INTEGER`: Skip first N results | ||
| * `--all-columns`: Show all columns including empty ones | ||
| * `--config-file TEXT`: [env var: INFRAHUBCTL_CONFIG; default: infrahubctl.toml] | ||
| * `--install-completion`: Install completion for the current shell. | ||
| * `--show-completion`: Show completion for the current shell, to copy it or customize the installation. | ||
| * `--help`: Show this message and exit. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # `infrahubctl update` | ||
|
|
||
| Update an existing object in Infrahub. | ||
|
|
||
| Fetches the object by KIND and IDENTIFIER, applies the requested | ||
| changes, and saves back to the server. Use --set or --file. | ||
|
|
||
| Examples: | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| infrahubctl update InfraDevice spine01 --set status=active | ||
| infrahubctl update InfraDevice spine01 --set location=DC1 | ||
| infrahubctl update InfraDevice spine01 --file updates.yml | ||
|
|
||
| **Usage**: | ||
|
|
||
| ```console | ||
| $ infrahubctl update [OPTIONS] KIND IDENTIFIER | ||
| ``` | ||
|
|
||
| **Arguments**: | ||
|
|
||
| * `KIND`: Infrahub schema kind [required] | ||
| * `IDENTIFIER`: UUID, name, or HFID (use / for multi-part, for example: Cisco/NX-OS) [required] | ||
|
|
||
| **Options**: | ||
|
|
||
| * `--set TEXT`: Field value in key=value format | ||
| * `-f, --file PATH`: JSON or YAML file with update data | ||
| * `-b, --branch TEXT`: Target branch | ||
| * `--config-file TEXT`: [env var: INFRAHUBCTL_CONFIG; default: infrahubctl.toml] | ||
| * `--install-completion`: Install completion for the current shell. | ||
| * `--show-completion`: Show completion for the current shell, to copy it or customize the installation. | ||
| * `--help`: Show this message and exit. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| """Command modules for the ``infrahub`` end-user CLI.""" | ||
|
|
||
| from __future__ import annotations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| """Command implementation for ``infrahub create``. | ||
|
|
||
| Creates a new object in Infrahub either from inline ``--set`` key=value | ||
| arguments or from a JSON/YAML object file specified via ``--file``. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import contextlib | ||
| from pathlib import Path | ||
|
|
||
| import typer | ||
| from rich.console import Console | ||
|
|
||
| from infrahub_sdk.ctl.client import initialize_client | ||
| from infrahub_sdk.ctl.commands.utils import derive_identifier, prepare_relationship_data, resolve_node | ||
| from infrahub_sdk.ctl.parameters import CONFIG_PARAM | ||
| from infrahub_sdk.ctl.parsers import parse_set_args, validate_set_fields | ||
| from infrahub_sdk.ctl.utils import catch_exception | ||
| from infrahub_sdk.exceptions import NodeNotFoundError | ||
| from infrahub_sdk.spec.object import ObjectFile | ||
|
|
||
| console = Console() | ||
|
|
||
|
|
||
| @catch_exception(console=console) | ||
| async def create_command( | ||
| kind: str = typer.Argument(..., help="Infrahub schema kind to create"), | ||
| set_args: list[str] | None = typer.Option(None, "--set", help="Field value in key=value format"), | ||
| file: Path | None = typer.Option(None, "--file", "-f", help="JSON or YAML file with object data"), | ||
| branch: str | None = typer.Option(None, "--branch", "-b", help="Target branch"), | ||
| _: str = CONFIG_PARAM, | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) -> None: | ||
| """Create a new object in Infrahub. | ||
|
|
||
| Provide field values with repeatable --set flags or supply a | ||
| JSON/YAML object file via --file. The two modes are mutually exclusive. | ||
|
|
||
| \b | ||
| Examples: | ||
| infrahubctl create InfraDevice --set name=spine01 --set status=active | ||
| infrahubctl create InfraDevice --set name=spine01 --set location=DC1 | ||
| infrahubctl create InfraDevice --file devices.yml | ||
| """ | ||
| if set_args and file: | ||
| console.print("[red]Error: --set and --file are mutually exclusive.") | ||
| raise typer.Exit(code=1) | ||
| if not set_args and not file: | ||
| console.print("[red]Error: provide --set key=value or --file <path>.") | ||
| console.print("Example: infrahubctl create MyKind --set name=foo --set status=active") | ||
| raise typer.Exit(code=1) | ||
|
|
||
| client = initialize_client(branch=branch) | ||
|
|
||
| if file: | ||
| files = ObjectFile.load_from_disk(paths=[file]) | ||
|
|
||
| # Validate all files before processing any to avoid partial application | ||
| for obj_file in files: | ||
| if obj_file.spec.kind != kind: | ||
| console.print(f"[red]Error: file kind '{obj_file.spec.kind}' does not match positional kind '{kind}'.") | ||
| raise typer.Exit(code=1) | ||
| await obj_file.validate_format(client=client, branch=branch) | ||
|
|
||
| for obj_file in files: | ||
| await obj_file.process(client=client, branch=branch) | ||
| object_count = len(obj_file.spec.data) | ||
| console.print(f"[green]Created {object_count} objects of kind {obj_file.spec.kind}") | ||
| else: | ||
| data = parse_set_args(set_args) # type: ignore[arg-type] | ||
| schema = await client.schema.get(kind=kind, branch=branch) | ||
| validate_set_fields(data, schema.attribute_names, schema.relationship_names) | ||
| data = prepare_relationship_data(data, schema) | ||
|
|
||
| # Check if node already exists to distinguish create from upsert | ||
| existing = None | ||
| identifier_value = derive_identifier(data, schema) | ||
| if identifier_value is not None: | ||
| with contextlib.suppress(NodeNotFoundError): | ||
| existing = await resolve_node(client, kind, identifier_value, schema=schema, branch=branch) | ||
|
|
||
| node = await client.create(kind=kind, data=data, branch=branch) | ||
| await node.save(allow_upsert=True) | ||
| label = node.display_label or identifier_value or node.id | ||
|
|
||
| if existing: | ||
| console.print(f"[yellow]Updated {kind} '{label}' (id: {node.id}) — already existed") | ||
| else: | ||
| console.print(f"[green]Created {kind} '{label}' (id: {node.id})") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.