feat: export the schema through the Infrahub export endpoint [IFC-3669]
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,NamespaceExportandSchemaExport(changelog/+schema-export-client-rules.removed.md). - New return type:
InfrahubSchema.exportandInfrahubSchemaSync.exportreturn aSchemaExportDocumentinstead of aSchemaExport(changelog/+schema-export-document.changed.md). - Needs a newer server: against an Infrahub server without the endpoint,
exportandinfrahubctl schema exportraiseVersionNotSupportedErrorwith the message that Infrahub 1.13.0 or later is required. Before, they exported on the client. - Different files: with the default
BRIEFmode,infrahubctl schema exportwrites files that differ from earlier versions.changelog/+schema-export-endpoint.changed.mdlists each difference.
What changed
Behavior:
-
exportcallsGET /api/schema/exportwithbranch,namespacesand a newmodeargument (SchemaExportMode.BRIEFby default, orFULL), and returns the server's document as aSchemaExportDocument. This model is a subclass ofInfrahubSchemaWritewith a requiredinfrahub_version. -
infrahubctl schema exporthas two new options:--mode BRIEF|FULL;--single-file, which writes all namespaces toschema.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_namespaceaccepts the dict body or theSchemaExportDocumentthatexportreturns, 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 behindinfrahubctl schema format, before it writes the file:- the file starts with
---and theyaml-language-servercomment; - 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.
- the file starts with
-
infrahubctl schema formatwritesinfrahub_versiondirectly afterversion, and no longer splits a value longer than 4096 characters. -
The
--directoryhelp 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 newSchemaNamespaceNotFoundError, 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. - 401 or 403:
-
InfrahubSchemaWriteaccepts aninfrahub_versionfield, sovalidate_schemaandinfrahubctl schema loadaccept an exported file. -
KEEP_IN_EXPORT_FIELDSin the generatedcontract.pylists the fields aBRIEFexport keeps. Infrahub'suv run invoke backend.generatewrites 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.pyand theexportcommand ininfrahub_sdk/ctl/schema.py, including_render_schema_fileand_ExportDumper.FILE_ORDERand the line width ininfrahub_sdk/ctl/schema_format.py.infrahub_sdk/schema/generated/contract.pyandwrite.pyare 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 exportneeds Infrahub 1.13.0 or later. - Deployment notes: merge before the opsmill/infrahub pull request. That pull request then moves its
python_sdkpointer 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.mdxis regenerated, and the user guide isdocs/docs/schema/export.mdxin opsmill/infrahub - Internal .md docs updated (internal knowledge and AI code tools knowledge)
š¤ Generated with Claude Code