Export a schema
A schema export is a schema file that Infrahub builds from the schema an instance runs. It holds the nodes and generics you defined, in the same format as the files you load with infrahubctl schema load, and it loads into an empty instance with no edits.
Export the schema to copy it from one instance to another, for example from a test instance to a new production instance, or to recover schema files that you no longer have outside Infrahub.
Export the schema of a branch​
- infrahubctl
- REST API
infrahubctl schema export writes the export of the default branch to a new directory, one file per namespace:
infrahubctl schema export --directory schemas/
| Option | Default | Effect |
|---|---|---|
--directory | ./infrahub-schema-export-<YYYYMMDD-HHMMSS> | Directory the files are written to. The command creates it if it does not exist, and overwrites a file with the same name |
--branch | The default branch | Branch whose schema is exported |
--namespaces | Every namespace you defined | Namespace to export. Repeat the option to export several namespaces |
--mode | BRIEF | BRIEF or FULL, see Choose between BRIEF and FULL |
--single-file | Off | Write one file, schema.yml, that holds every exported namespace |
Without --single-file, each file is named after its namespace in lower case, such as network.yml for the Network namespace. When the branch has no nodes or generics you defined, the command writes no file and prints No user-defined schema found to export.
The command needs an Infrahub server on release 1.13.0 or later. Against an earlier release, it writes no file and prints:
Error: The schema export requires Infrahub 1.13.0 or later.
GET /api/schema/export returns the export as one JSON document:
curl -H "X-INFRAHUB-KEY: $INFRAHUB_API_TOKEN" \
"http://localhost:8000/api/schema/export?branch=main&mode=BRIEF&namespaces=Network&namespaces=Location"
| Parameter | Default | Effect |
|---|---|---|
branch | The default branch | Branch whose schema is exported |
namespaces | Every namespace you defined | Namespace to export. Repeat the parameter to export several namespaces |
mode | BRIEF | BRIEF or FULL, see Choose between BRIEF and FULL |
Any account that can read the schema with GET /api/schema can call the export. A load of the document needs the permission to manage the schema on the target instance.
A release earlier than 1.13.0 does not have the endpoint. It answers GET /api/schema/export with HTTP 422 and the message Unable to find the schema 'export' in the registry.
An export holds the schema file format version, the Infrahub version of the instance that produced it, then the generics and the nodes. infrahubctl schema export writes each file in the layout that infrahubctl schema format gives a schema file: the file starts with --- and the yaml-language-server comment that links the schema file format, the keys of each node, generic, attribute, relationship and dropdown choice are in the order that infrahubctl schema format uses, and a list that holds no mapping is written on one line. When a file cannot be formatted without changing a value, the command writes no file and exits with code 1. The example below is the BRIEF export of a Network namespace with one generic and two nodes:
---
# yaml-language-server: $schema=https://schema.infrahub.app/infrahub/schema/latest.json
version: '1.0'
infrahub_version: <version of the source instance>
generics:
- name: Interface
namespace: Network
human_friendly_id: [device__name__value, name__value]
display_label: name__value
uniqueness_constraints: [[device, name__value]]
attributes:
- name: name
kind: Text
- name: description
kind: Text
optional: true
relationships:
- name: device
peer: NetworkDevice
kind: Parent
cardinality: one
optional: false
identifier: networkdevice__networkinterface
nodes:
- name: Device
namespace: Network
icon: mdi:server
attributes:
- name: name
kind: Text
unique: true
relationships:
- name: interfaces
peer: NetworkInterface
kind: Component
identifier: networkdevice__networkinterface
on_delete: cascade
- name: PhysicalInterface
namespace: Network
inherit_from: [NetworkInterface]
human_friendly_id: [device__name__value, name__value]
display_label: name__value
uniqueness_constraints: [[device, name__value]]
attributes:
- name: speed
kind: Number
label: Speed (Mbps)
Load the export with the same command as any other schema file. Load every file of the directory in one command, because a node in one namespace can refer to a node or a generic in another:
infrahubctl schema load schemas/
The schema load accepts the infrahub_version field and ignores it.
Export selected namespaces​
With the namespaces filter, the export holds only the nodes and generics of the namespaces you choose. Each node and generic is written with the same values as in an export without the filter. A relationship to a peer in another namespace stays in the export, so the target instance must already have that peer before you load the file.
When the filter names a namespace that does not exist on the branch, or a reserved namespace such as Core, the export fails. The API returns HTTP 404 with one error per such namespace, and each error names its namespace in extensions.namespace. infrahubctl schema export prints each message and exits with code 1:
Error: The namespace 'Locaton' does not exist on the branch 'main'
Error: The namespace 'Core' is reserved for Infrahub and cannot be exported
Choose between BRIEF and FULL​
The schema an instance stores holds two kinds of values: the values you wrote in your schema files, and the values Infrahub derived when it processed them, such as a label built from the attribute name or an order_weight. The stored schema does not record which is which. The two modes handle the derived values differently.
BRIEF, the default, leaves out every value that Infrahub derives again when the export is loaded. To find these values, Infrahub loads the export into a copy of the schema of an empty instance, and writes back each value that would otherwise differ from the stored value. The result is close to the files you wrote, and the target gets the same values as the source. In the example above, the Device node has no label, no order_weight and no human_friendly_id, because Infrahub derives them from the name of the node, the order of the attributes and the unique: true attribute.
BRIEF also leaves out the deprecated default_filter of each node and generic, although the target then does not have it. Infrahub sets and changes default_filter while it processes a schema, for example when a node takes it from a generic, so the stored value is not always one you wrote. A node that takes default_filter from a generic Infrahub defines, such as IpamIPPrefix from BuiltinIPPrefix, takes it again on the target.
FULL writes every value of the nodes, generics, attributes and relationships you defined, including the derived ones, such as label, branch, order_weight and a human_friendly_id taken from a unique attribute, and including default_filter.
Choose the mode by the instance you load the export into:
- An empty instance: use
BRIEF. Both modes give the same schema on the target, except fordefault_filter, and theBRIEFfile is shorter to review. UseFULLwhen the target needsdefault_filter. - An instance that already has an earlier version of the same nodes: use
FULL. A value thatBRIEFleaves out does not replace a different value on the target. For example, when you set alabelback to the derived value on the source, aBRIEFexport leaves it out and the target keeps its own label.
What an export leaves out​
Both modes leave out:
- Nodes and generics in the reserved namespaces, such as
CoreandBuiltin. See Namespaces for the list. - Nodes and generics that Infrahub defines in a namespace that is not reserved. Today this is
IpamNamespace, in theIpamnamespace. Nodes and generics you defined inIpamare exported. - Profile and template schemas, which Infrahub generates for your nodes.
- Relationships that Infrahub adds to your nodes and generics:
member_of_groups,subscriber_of_groups,profiles,object_template,parentandchildren, unless one of them has other values (see below). - IDs, which match nothing on another instance.
- The
stateof nodes, generics, attributes, relationships and dropdown choices. An element that the schema stores is alwayspresent. - An attribute or a relationship that a node takes from its generic. It is written only on the generic.
- Extensions to
IpamNamespace, and extensions to a node in a reserved namespace that an earlier Infrahub release stored. An attribute or a relationship you added with an extension to one of your own nodes is written on that node, not in anextensionssection.
Both modes write a relationship that Infrahub adds when it has other values than the ones Infrahub gives it. This is the case when you wrote the relationship yourself with other values, for example parent with optional: true or a label of your own. It is also the case when an earlier Infrahub release stored the relationship with values that Infrahub no longer gives it, so an instance created with an older release can export parent or children although you never wrote them. To find these relationships, Infrahub loads the export into a copy of the schema of an empty instance and compares each one with the relationship that Infrahub adds there.
The export also writes deprecated settings in their current form:
- On a
TextorTextAreaattribute,regex,min_lengthandmax_lengthare written underparameters. On aListattribute,regexis written underparameters, andmin_lengthandmax_lengthstay at the top level of the attribute. - On a
NumberPoolattribute,start_rangeandend_rangeare written as one entry ofrangeswith the same bounds. A bound that was not set stays unset.
A load of the export gives the same validation settings and pool ranges on the target.
Known limitations​
-
Extensions to reserved nodes that an earlier release stored are not exported. Infrahub 1.12.4 and later reject a schema file that extends a node in a reserved namespace, such as
BuiltinTag, but an instance can still hold such an extension from an earlier release. The export leaves it out with no warning. See remove extensions of built-in kinds. -
IpamNamespaceis not exported. A change or an extension you made toIpamNamespaceis left out with no warning. -
A value you wrote that equals the derived value is left out in
BRIEF. The stored schema does not record which values you wrote, so the export cannot tell them apart from derived values. For example, when you wrotelabel: Nameon the attributename, aBRIEFexport leaves the label out, and the schema files you keep from the export no longer show it. Infrahub derives the same label on the target. -
A derived value can be written in
BRIEFnext to the value it comes from. When leaving out a derived value and its source together gives a different result on load, the export writes both. The export can then be longer than the files you wrote, and it loads to the same schema. -
The first load of an export into the instance it came from updates the schema when a
NumberPoolattribute was written withstart_rangeandend_range. The export writes these settings asranges, so the load stores them in that form, and the schema load reports a change instead of "already up to date". The pool keeps the same ranges and the numbers it allocated. A second load of the same export makes no change. -
Each API worker of the Infrahub server runs at most two exports at the same time. A further export request waits until one of them ends.
-
Properties that a node takes from its generic are written on the node. These are the node-level properties, such as
human_friendly_id,display_label,uniqueness_constraints,iconandorder_by. On the target, as on the source, Infrahub does not apply a later change of the property on the generic to that node. -
The load gives a deprecation warning for each node that takes
display_labelsfrom a generic, and inFULLfor each node that takesdefault_filterfrom a generic. The export writes these properties on each of those nodes, so the load gives the warning for each node as well as for the generic. InFULLthis includes each node that takesdefault_filterfrom a generic Infrahub defines, such as a node that inherits fromBuiltinIPPrefixorBuiltinIPAddress, so the load warns for that node although you never wrote the property. -
A
BRIEFexport does not keepdefault_filter. On the target, a node or generic that had its owndefault_filter, or took it from a generic you defined, has none. Infrahub usesdefault_filterin these cases:- An SDK
getby a value that is not an ID searches the node by itsdefault_filter. - A peer written as a plain string in an object file is looked up by the
default_filterof a peer generic. - Infrahub loads the objects of a repository only for a node that has a
human_friendly_idor adefault_filter.
Set a
human_friendly_idon these nodes, or export inFULL. - An SDK
Load an export into an instance that has some of the same nodes​
A load of an export adds to the schema of the target and changes the nodes and generics it holds. It removes nothing, in both modes:
- Nodes, generics, attributes and relationships that the target has and the source does not stay on the target. To remove one, load a schema file that sets
state: absenton it, see Schema migration. - A value the source does not set, such as a
descriptionyou removed on the source, is not written in the export, so the target keeps its own value.
The target matches nodes and generics by kind, and attributes and relationships by name. A rename on the source therefore reaches the target as a new element. For example, when you renamed the attribute name to hostname on the source and then added a new name attribute, the load keeps the values of the existing name attribute on the target and creates hostname with no values. The load reports no error for this.
Errors when you load an export​
An export can still fail to load on the target in the cases below. infrahubctl schema load prints Unable to load the schema:, then the error that the target returns.
The target runs an earlier release​
A release that does not have the schema export rejects the infrahub_version field, and also any schema setting it does not know. infrahubctl schema load then prints only the first line, with no detail:
Unable to load the schema:
The response of POST /api/schema/load names the field in loc. The response below is shortened:
{
"detail": [
{
"type": "value_error",
"loc": ["body", "schemas", 0, "infrahub_version"],
"msg": "Unknown field, it is not part of the schema"
}
]
}
Load the export into an instance on the same release as the source, or on a later one.
infrahubctl is older than the source​
The export files hold every schema setting the source returns, including settings added in a release later than your infrahubctl. Before it sends the files, infrahubctl schema load checks them and rejects a setting it does not know with Unknown field, it is not part of the schema. Load the files with an infrahubctl version that matches the release of the target.
The export refers to a node or a generic the target does not have​
This happens most often with a namespace filter, when a relationship or an inherit_from refers to a kind in a namespace you did not export:
Unable to load the schema:
Unable to find the schema 'LocationSite' in the registry
Unable to load the schema:
NetworkPort Unable to find the generic NetworkInterface
Load the missing namespaces first, or export them together.
The same kind is a node on one instance and a generic on the other​
Unable to load the schema:
TestThing already exist in the schema as a Generic. Either rename it or delete the existing one.
When the target holds the kind as a node and the export holds it as a generic, the error ends with as a Node instead.
A setting that cannot be changed differs​
Some settings cannot be changed after the first load, such as branch on an attribute, see Schema migration. When the export holds a different branch value for an attribute, the load fails and the target keeps its value:
Unable to load the schema:
'not_supported': TestCar color None
The error names the kind and the attribute or relationship, not the setting.
The data on the target does not meet a constraint in the export​
A new mandatory attribute with no default value fails when the target already has objects of that kind:
Unable to load the schema:
Node-level 'attribute' constraint violation on schema 'TestRack'. Node (TestRack(ID: 18eba688-a3d1-bf01-392e-271debe94a79)) is not compliant.
An attribute that becomes unique fails when several objects on the target have the same value. The error lists each object and each constraint it breaks:
Unable to load the schema:
Node-level 'uniqueness_constraints' constraint violation on schema 'TestCable'. Node (TestCable(ID: 18eba689-4b5b-f6cc-392a-271d315e862b)) is not compliant. The error relates to field label.value='same'.,
Attribute-level 'unique' constraint violation on schema 'TestCable'. Node (TestCable(ID: 18eba689-4b5b-f6cc-392a-271d315e862b)) is not compliant. The error relates to field label='same'.,
Update or remove the objects that break the constraint, or set a default_value on the new attribute, then load the export again.
Your account cannot manage the schema on the target​
Unable to load the schema:
You are not allowed to manage the schema
Ask an administrator of the target instance for a role with the permission to manage the schema. See Permissions.
Related concepts​
- Create and load schema: the schema file format and the
infrahubctl schema loadcommand - Generics & inheritance: the properties a node takes from its generic
- Schema migration: how a load changes the schema and data of an instance, and the settings that cannot be changed
- Namespaces: the reserved namespaces that an export leaves out