Skip to content

Metadata exchange without a session#

The metadata exchange without a session lets a script or continuous integration export the metadata of a connection as a package, analyze it in another and import it, without cookies or passwords. It serves to automate the move from development to testing and to production, and to keep the metadata in a code repository.

Before you start#

  • The credential is created by whoever has System administration, in the name of a user who has Metadata administration (see Service credentials). Its scope is Metadata exchange and it goes alone.
  • What is done is the same as Export and import in the metadata editor, with the same rules and the same report. It is worth knowing that process in the application first (see the metadata chapter).
  • On each request the holder's privileges are read again: if they lose Metadata administration, the credential stops working (403 NOT_ALLOWED).

It also works in an installation where users only sign in with single sign-on, where there are no passwords to put in a script.

Create the credential#

  1. Go to Administration › Credentials and click New credential….
  2. In Scope, choose Metadata exchange.
  3. Choose the Holder: a user with Metadata administration. The others appear without being selectable.
  4. Type the Client name (“Metadata continuous integration”) and the expiry: 90 days by default, at most 365.
  5. Click Create credential and store the secret in the continuous integration's secret store.

Tip

Create a dedicated user, with Metadata administration and nothing else, as the holder of the script's credential. This way the audit distinguishes what the script does from what people do, and you can remove the privilege without affecting anyone.

Routes#

All go under /api/metadata-exchange/v1, with Authorization: Bearer ect_…. The session cookie does not work.

Route What it does
GET /connections The connections the holder can use: [{ id, name, engine, hasMetadata }]. If the credential is limited to some, only those.
GET /connections/{c}/export?rowFilters=&diagram=&groupings= The package, a .zip. The X-ECenter-Export-Summary header summarizes what it carries.
POST /connections/{c}/imports[?preview=true][&confirmed=true] Uploads the package and launches the analysis (preview=true) or the import: 202 { job, status } with Location.
GET /connections/{c}/imports/{job}[?wait=30s][&format=csv] The job's report when it finishes, or 202 if it is still running after waiting wait (10 s if not stated; at most 30 s). With format=csv, the report in CSV.

Export options:

Parameter Default What it does
rowFilters false Also carries the universes.
diagram true Carries the layout of the relations diagram.
groupings true Carries the groupings with their values.

The package carries nothing of the installation (neither the connection's URL, nor its user, nor logins), and two exports of the same metadata give the same file: it can be versioned in Git and compared.

The walkthrough#

1. Export#

export ECENTER_URL=https://desarrollo.ecenter.example.org
export ECENTER_METADATA_TOKEN=ect_…
curl -s "$ECENTER_URL/api/metadata-exchange/v1/connections/$ORIGEN/export" \
  -H "Authorization: Bearer $ECENTER_METADATA_TOKEN" -o metadata.zip

2. Analyze in the target#

The package is uploaded as multipart/form-data with two parts: package (the zip) and, optionally, options (JSON).

curl -s -X POST "$ECENTER_URL/api/metadata-exchange/v1/connections/$DESTINO/imports?preview=true" \
  -H "Authorization: Bearer $ECENTER_METADATA_TOKEN" \
  -F "package=@metadata.zip;type=application/zip" \
  -F 'options={"missing":"KEEP"};type=application/json'
{ "job": "4b1d…", "status": "QUEUED" }
Option What it does
schemas Pairs the logical name of a schema in the package with a schema in the target: { "COMERCIAL": "PROD_COMERCIAL" }.
privileges Pairs by hand the key of a privilege in the package with a privilege in the target.
createPrivileges true creates, without assigning them to any role, the package's privileges that are missing.
missing KEEP (default) keeps what is in the target and not in the package; REMOVE deletes it.
diagram IF_EMPTY (only if the target has no layout), REPLACE or KEEP.
basis The basis of an earlier analysis (below).

3. Read the report#

curl -s "$ECENTER_URL/api/metadata-exchange/v1/connections/$DESTINO/imports/4b1d…?wait=30s" \
  -H "Authorization: Bearer $ECENTER_METADATA_TOKEN"

While the job is running, it responds 202; repeat the request. When it finishes, 200 with the report:

{
  "outcome": "ANALYZED",
  "basis": "sha256:9f2c…",
  "tallies": [
    { "kind": "FACT_TABLE", "inPackage": 8, "created": 1, "changed": 2, "unchanged": 5,
      "onlyInTarget": 0, "removed": 0, "pairedByName": 0 } ],
  "issues": [ { "severity": "WARNING", "code": "NOT_IN_CATALOG", "kind": "FACT_TABLE",
                "element": "Devoluciones",
                "message": "La tabla PROD_COMERCIAL.DEVOLUCIONES no existe en la base de datos" } ],
  "impact": { },
  "requiresConfirmation": true
}
  • outcome: ANALYZED, IMPORTED, REJECTED (there are errors), METADATA_IN_USE, METADATA_IMPACT (the change affects documents that use it) or METADATA_CHANGED (the target changed since the analysis).
  • issues[].severity: ERROR prevents importing; WARNING has to be confirmed; INFO is for information. The message texts come from the server in Spanish; programs should rely on code.
  • requiresConfirmation: there are warnings or an impact that a person should review.
  • The report never brings universe conditions or SQL fragments.

4. Import#

With the analysis's basis, so that the import does not go in if the target has changed in the meantime:

curl -s -X POST "$ECENTER_URL/api/metadata-exchange/v1/connections/$DESTINO/imports" \
  -H "Authorization: Bearer $ECENTER_METADATA_TOKEN" \
  -F "package=@metadata.zip;type=application/zip" \
  -F 'options={"missing":"KEEP","basis":"sha256:9f2c…"};type=application/json'

The package travels in each request: the server does not keep it between the analysis and the import. If the analysis asked for confirmation, the import also needs confirmed=true; before adding it in a script, consider whether you really want to accept what has changed without reviewing it.

The sample script#

The repository includes tools/demo-data/metadata_promote.py, in Python and without dependencies, which does the whole walkthrough as continuous integration would and only imports if there is nothing to confirm:

export ECENTER_URL=https://ecenter.example.org
export ECENTER_METADATA_TOKEN=ect_…
python3 tools/demo-data/metadata_promote.py --source "Desarrollo" --target "Producción" --analyze-only
Option What it does
--source, --target The source and target connections, by name or identifier.
--analyze-only Only analyzes; never imports.
--package, --save Promotes a saved package instead of exporting the source; saves the exported one to a file.
--row-filters, --no-diagram, --no-groupings The export options.
--remove-missing Deletes from the target what is not in the package.
--create-privileges Creates the missing privileges without assigning them.

Its exit codes serve to decide in continuous integration:

Code Meaning
0 Promoted (or, with --analyze-only, analyzed with nothing to confirm).
2 There is something to confirm: have a person decide in the application's import wizard. Nothing has been applied.
3 Errors or something that prevents it. Nothing has been applied.
4 The target has changed since the analysis: run it again.
1 The API or the network has failed.

Errors#

HTTP code When
400 INVALID_PACKAGE It is not a zip, the manifest is missing or a file cannot be read.
400 UNSUPPORTED_FORMAT The package is from a newer version of ECenter.
400 INVALID_REQUEST The options are not valid.
403 NOT_ALLOWED The holder no longer has Metadata administration, or the credential does not include the connection.
404 NOT_FOUND The connection or the job do not exist, or the job does not belong to this credential.
409 IMPORT_IN_PROGRESS There is another import or restore in progress in that connection.
409 JOB_CANCELED The job was canceled (for example, when revoking the credential). It left nothing.
410 RESULT_EXPIRED The report is no longer kept.
413 PACKAGE_TOO_LARGE The package exceeds the caps.
500 JOB_FAILED The job has failed.

Security and audit#

  • The job runs with the holder's identity and is only seen by that same credential: from a session of the same user, 404.
  • Revoking the credential, or disabling, locking or deleting the holder, cuts off on the next request and cancels their analyses and imports in progress. A canceled job leaves nothing: an analysis is always undone, and an import is undone entirely.
  • Metadata backups and restoring them do not go through this surface: they are done in the application.
  • The audit records each export and import with the API channel and the credential's reference; analyses are not recorded.

See also#