Skip to content

feat: export the schema through the Infrahub export endpoint [IFC-3669] - #1554

Draft
ogenstad wants to merge 11 commits into
infrahub-developfrom
po-ifc-3669-schema-export-cli
Draft

ogenstad wants to merge 11 commits into
infrahub-developfrom
po-ifc-3669-schema-export-cli

Conversation

@ogenstad

@ogenstad ogenstad commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Why

infrahubctl schema export wrote values Infrahub generated and elements the schema owner did not define, so the file did not load into another instance without editing. The export rules were written by hand in the SDK and fell behind the schema model (spec).

Infrahub 1.13.0 adds GET /api/schema/export, which builds the export on the server (opsmill/infrahub#11772). This pull request makes the SDK and infrahubctl schema export use that endpoint and removes the client-side export rules.

Jira: IFC-3669, part of IFC-2964.

Breaking changes to the public API

The SDK AGENTS.md lists changes to public API signatures under "Ask first". The feature owner accepted these on 2026-11-27, and the changelog fragments list them:

  • Removed from infrahub_sdk.schema, with no deprecation period: schema_to_export_dict, RESTRICTED_NAMESPACES, NamespaceExport and SchemaExport (changelog/+schema-export-client-rules.removed.md).
  • New return type: InfrahubSchema.export and InfrahubSchemaSync.export return a SchemaExportDocument instead of a SchemaExport (changelog/+schema-export-document.changed.md).
  • Needs a newer server: against an Infrahub server without the endpoint, export and infrahubctl schema export raise VersionNotSupportedError with the message that Infrahub 1.13.0 or later is required. Before, they exported on the client.
  • Different files: with the default BRIEF mode, infrahubctl schema export writes files that differ from earlier versions. changelog/+schema-export-endpoint.changed.md lists each difference.

What changed

Behavior:

  • export calls GET /api/schema/export with branch, namespaces and a new mode argument (SchemaExportMode.BRIEF by default, or FULL), and returns the server's document as a SchemaExportDocument. This model is a subclass of InfrahubSchemaWrite with a required infrahub_version.

  • infrahubctl schema export has two new options:

    • --mode BRIEF|FULL;
    • --single-file, which writes all namespaces to schema.yml.

    Without --single-file, it writes one file per namespace, as before.

  • The CLI writes the JSON body the server returned, after the SDK has checked it, instead of the SDK models. The files therefore keep a field that a later Infrahub release adds and this SDK version does not know. split_by_namespace takes and returns dicts for this reason.

  • Errors of the export are raised as SDK exceptions:

    • 401 or 403: AuthenticationError;
    • 400: BranchNotFoundError;
    • the 422 an older server returns: VersionNotSupportedError;
    • a 404 whose errors each name a namespace in extensions.namespace: the new SchemaNamespaceNotFoundError, with one message per namespace;
    • any other 404: URLNotFoundError;
    • a document this SDK version cannot read: InvalidResponseError;
    • any other error: ApiError.

    The CLI prints one Error: line per namespace and exits with code 1.

  • InfrahubSchemaWrite accepts an infrahub_version field, so validate_schema and infrahubctl schema load accept an exported file.

  • KEEP_IN_EXPORT_FIELDS in the generated contract.py lists the fields a BRIEF export keeps. Infrahub's uv run invoke backend.generate writes it.

What stayed the same: infrahubctl schema load and the other schema commands. export in Python still returns the model, which drops fields this SDK version does not know; only the CLI keeps them.

How to review

  • infrahub_sdk/schema/__init__.py: _parse_export_response, which maps the responses to exceptions, and _export_payload, which the CLI uses.
  • infrahub_sdk/schema/export.py and the export command in infrahub_sdk/ctl/schema.py.
  • infrahub_sdk/schema/generated/contract.py and write.py are generated from Infrahub.

How to test

uv run pytest tests/unit/sdk/test_schema_export.py tests/unit/ctl/schema/test_schema.py tests/unit/sdk/test_schema.py
uv run pytest tests/unit/
  • The tests mock the endpoint's responses with httpx_mock. There is no integration test against a running server yet.
  • Last local run of the whole unit suite: 3188 passed, 2 skipped, 1 xfailed.

Impact & rollout

  • Backward compatibility: breaking, see above. infrahubctl schema export needs Infrahub 1.13.0 or later.
  • Deployment notes: merge before the opsmill/infrahub pull request. That pull request then moves its python_sdk pointer to this branch's merge commit.

Checklist

  • Tests added/updated
  • Changelog entry added (uv run towncrier create ...)
  • External docs updated: docs/docs/infrahubctl/infrahubctl-schema.mdx is regenerated, and the user guide is docs/docs/schema/export.mdx in opsmill/infrahub
  • Internal .md docs updated (internal knowledge and AI code tools knowledge)

🤖 Generated with Claude Code

ogenstad and others added 11 commits November 26, 2026 14:35
…port keeps [IFC-3669]

Regenerated with uv run invoke backend.generate: InfrahubSchemaWrite gains the
optional infrahub_version field, and contract.py gains KEEP_IN_EXPORT_FIELDS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…C-3669]

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s [IFC-3669]

Regenerated from the Infrahub schema definitions, which now mark choices,
read_only and unique on attributes, and cardinality, identifier and optional
on relationships.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…FC-3669]

infrahubctl schema export and InfrahubSchema.export now call
GET /api/schema/export and return its document as a SchemaExportDocument.
The CLI gains --mode and --single-file, splits the document by namespace
with split_by_namespace, and writes only the keys the server returned.
A server without the endpoint gives an error that names the first release
with it; any other error response is reported as the server returned it.

The client-side export rules and their models are removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nt [IFC-3669]

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… export [IFC-3669]

An Infrahub server without the export endpoint answers with
"Unable to find the schema 'export' in the registry", so the SDK now
matches that message and names the first release with the endpoint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…export comment [IFC-3669]

State which attribute kinds write regex, min_length and max_length under
parameters, and regenerate the KEEP_IN_EXPORT_FIELDS comment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…IFC-3669]

The server answers a namespace that does not exist on the branch, or a
restricted one, with a 404 whose errors name the namespace in
extensions.namespace. The SDK raises SchemaNamespaceNotFoundError for
that response, and URLNotFoundError for a 404 that names no namespace,
such as Infrahub's answer to an unknown URL. infrahubctl schema export
prints one error line per namespace and exits with code 1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s [IFC-3669]

- An unknown branch raises BranchNotFoundError again, as the export did
  before it used the server endpoint.
- A document with a value this SDK version cannot read raises
  InvalidResponseError instead of a pydantic ValidationError, and
  infrahubctl prints it, and a JsonDecodeError, without a traceback.
- SchemaNamespaceNotFoundError and VersionNotSupportedError survive
  pickle and copy.
- A namespace error without a text message is named in a fallback
  message, so the CLI never prints an empty error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… schema export [IFC-3669]

- A 401 or 403 from the export endpoint raises AuthenticationError, as
  the GraphQL paths do, and infrahubctl prints it as an authentication
  failure.
- infrahubctl escapes the directory, file and namespace names it prints,
  so a name with square brackets no longer fails a finished export.
- The command help mentions the --single-file layout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t files [IFC-3669]

infrahubctl schema export wrote its files from the SDK write models, which
drop the fields they do not declare. With an SDK older than the server, a
field that a later Infrahub release adds was left out of the files with no
message, and a load of the files lost it.

The CLI now checks the response as before, then writes the JSON body the
server returned. split_by_namespace takes and returns dicts and copies
every top-level field to each namespace document. InfrahubSchema.export
still returns a SchemaExportDocument.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the type/documentation Improvements or additions to documentation label Nov 27, 2026
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying infrahub-sdk-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: efeebb7
Status: ✅  Deploy successful!
Preview URL: https://8df3296b.infrahub-sdk-python.pages.dev
Branch Preview URL: https://po-ifc-3669-schema-export-cl.infrahub-sdk-python.pages.dev

View logs

@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.31250% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
infrahub_sdk/schema/__init__.py 91.30% 6 Missing ⚠️
@@                 Coverage Diff                  @@
##           infrahub-develop    #1554      +/-   ##
====================================================
+ Coverage             87.92%   87.99%   +0.07%     
====================================================
  Files                   157      157              
  Lines                 15853    15716     -137     
  Branches               2162     2112      -50     
====================================================
- Hits                  13938    13829     -109     
+ Misses                 1336     1324      -12     
+ Partials                579      563      -16     
Flag Coverage Δ
integration-tests 42.85% <3.90%> (+0.10%) ⬆️
python-3.10 63.17% <71.09%> (+0.50%) ⬆️
python-3.11 63.17% <71.09%> (+0.50%) ⬆️
python-3.12 63.17% <71.09%> (+0.51%) ⬆️
python-3.13 63.17% <71.09%> (+0.50%) ⬆️
python-3.14 63.15% <71.09%> (+0.48%) ⬆️
python-3.15 63.17% <71.09%> (+0.50%) ⬆️
python-filler-3.12 23.04% <24.21%> (-0.54%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
infrahub_sdk/ctl/schema.py 79.24% <100.00%> (+6.72%) ⬆️
infrahub_sdk/exceptions/__init__.py 100.00% <ø> (ø)
infrahub_sdk/exceptions/base.py 95.68% <100.00%> (+0.15%) ⬆️
infrahub_sdk/schema/export.py 100.00% <100.00%> (+17.64%) ⬆️
infrahub_sdk/schema/generated/contract.py 100.00% <100.00%> (ø)
infrahub_sdk/schema/generated/write.py 99.40% <100.00%> (-0.17%) ⬇️
infrahub_sdk/schema/__init__.py 73.77% <91.30%> (-0.26%) ⬇️

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant