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

Merged
OG
1d ago
+1194-444

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 accepts the dict body or the SchemaExportDocument that export returns, and returns one dict per namespace.

  • The CLI writes a value without a line break on one line, however long it is. Earlier versions split a value longer than about 80 characters, such as a Jinja2 template, over several lines.

  • The CLI passes each file through format_schema_text, the function behind infrahubctl schema format, before it writes the file:

    • the file starts with --- and the yaml-language-server comment;
    • the keys of each node, generic, attribute, relationship and dropdown choice are in the order of schema format;
    • a list that holds no mapping is written on one line, such as uniqueness_constraints: [[device, name__value]].

    The CLI renders every file before it writes any. When formatting a file would change a value, it writes no file, prints the error and exits with code 1.

  • infrahubctl schema format writes infrahub_version directly after version, and no longer splits a value longer than 4096 characters.

  • The --directory help text shows the default name pattern, ./infrahub-schema-export-<YYYYMMDD-HHMMSS>, and says that the command creates the directory and overwrites files with the same name.

  • 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, including _render_schema_file and _ExportDumper.
  • FILE_ORDER and the line width in infrahub_sdk/ctl/schema_format.py.
  • infrahub_sdk/schema/generated/contract.py and write.py are generated from Infrahub.
  • The tests mock the endpoint's responses with httpx_mock. There is no integration test against a running server yet.

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