- 14 Jul 2023
- 6 Minutes to read
- Print
- DarkLight
Workspace Operations
- Updated on 14 Jul 2023
- 6 Minutes to read
- Print
- DarkLight
You need to have access to the Preset API in order to successfully use the Preset CLI.
Workspace Operations
Most Workspace operations can be executed both against a Preset Workspace and a Superset installation (unless specified). When running against a Preset Workspace, the Workspace can be specified using the --workspaces
flag. For example:
preset-cli --workspaces=$https://workspace_slug.workspace_region.app.preset.io/ superset $command
If not specified, the CLI would prompt you with a list of Workspaces accessible with the credentials provided, to select one.
To run the command against a Superset installation, use superset-cli
(rather than preset-cli
) and directly inform the Superset URL and command:
superset-cli $https://superseturl.com $command
Export ownership (Superset only)
You can use this command to export asset ownership information from a Superset instance, so that it can be imported in Preset. This is extremely useful when migrating from Superset to Preset, to make sure that permissions reflect properly:
superset-cli $https://superseturl.com export-ownership $/path/to/yaml/file
Replace:
$https://suerseturl.com
with the actual link to the Superset application.$/path/to/yaml/file
with the path to the file that should be replaced/created.
The command would generate a YAML with below structure:
chart:
- name: Chart Name
owners:
- example@email.com
...
uuid: Chart UUID
...
dashboard:
- name: Dashboard Name
owners:
- example@email.com
uuid: Dashboard UUID
...
Export resoures
The CLI can be used to export resources from your Workspace (or Superset instance). All available options would download and extract the export ZIP file to the specified location. For all commands it's also possible to include the --overwrite
flag to replace the files in case they already exist in the destination location.
Important note about Jinja templating
Since the CLI supports Jinja templating (and even using Python functions) to dynamically modify exported contents before import, during the export flow Jinja syntax created in Superset is escaped, so that it's not rendered by the CLI during the import. This way, Jinja syntax added by users for content modifications are rendered, and syntax meant to be hanlded by Superset is escaped.
Then, during the import process, the CLI would remove the escaping logic, so that the assets are compatible with Superset.
The UI import flow doesn't perform this task, so if you export an asset that contains Jinja syntax using the CLI, the exported content won't be compatible with an UI import. You can prevent this behavior by including the --disable-jinja-escaping
flag. Keep in mind that using this flag would prevent the escaping logic implemented in the CLI, so if you try to import an asset
Export all assets
Use this option to download all assets (dashboards, charts, datasets and DB connections):
preset-cli --workspaces=$https://workspace_slug.workspace_region.app.preset.io/ superset export $/path/to/directory/
Replace:
$https://workspace_slug.workspace_region.app.preset.io/
with the actual link to the Preset Workspace.$/path/to/directory/
with the destination directory the files should be downloaded to.
Export specific assets (and assets they depend on)
You can download specific resources, instead:
preset-cli --workspaces=$https://workspace_slug.workspace_region.app.preset.io/ superset export $/path/to/directory/ --$asset-ids=$asset-id
Replace:
$https://workspace_slug.workspace_region.app.preset.io/
with the actual link to the Preset Workspace.$/path/to/directory/
with the destination directory the files should be downloaded to.--$asset-ids=
with the actual asset type. Available options:--database-ids=
--dataset-ids=
--chart-ids=
--dashboard-ids=
$asset-id
with the actual asset ID that should be exported. Multiple values can be comma separated.
This option would download the specified assets, and also assets they depend on (for example, downloading a dashboard would include charts, datasets and DB connections used by it).
Export all assets from a particular type
You can choose to download only a particular asset type, instead (for example, only download all dashboards):
preset-cli --workspaces=$https://workspace_slug.workspace_region.app.preset.io/ superset export $/path/to/directory/ --asset-type=$asset_type
Replace:
$https://workspace_slug.workspace_region.app.preset.io/
with the actual link to the Preset Workspace.$/path/to/directory/
with the destination directory the files should be downloaded to.$asset_type
with the desired asset type to be downloaded. You can include this flag multiple times to download multple types. Available options:database
dataset
chart
dashboard
You can also optionally include the --$asset-ids=$asset-id
flag to this command. Replace:
--$asset-ids=
with the actual asset type. Available options:--database-ids=
--dataset-ids=
--chart-ids=
--dashboard-ids=
$asset-id
with the actual asset ID that should be exported. Multiple values can be comma separated.
Note that this would only export the secpficied types. It wouldn't export assets they might depend on (for example, exporting all charts wouldn't include required datasets and DB connections).
Export roles
Export the Data Access Roles (DAR) definitions from your Preset Workspace (or from a Superset instance):
preset-cli --workspaces=$https://workspace_slug.workspace_region.app.preset.io/ superset export-roles $/path/to/yaml/file
Replace:
$https://workspace_slug.workspace_region.app.preset.io/
with the actual link to the Preset Workspace.$/path/to/yaml/file
with the path to the file that should be replaced/created.
The command would generate a YAML with below structure:
- name: DAR name
permissions:
- Schema access on ...
- Database access on ...
...
users:
- example@email.com
...
...
Export RLS rules
Export the Row Level Security (RLS) definitions from your Preset Workspace (or from a Superset instance):
preset-cli --workspaces=$https://workspace_slug.workspace_region.app.preset.io/ superset $https://superseturl.com export-ownership $/path/to/yaml/file
Replace:
$https://workspace_slug.workspace_region.app.preset.io/
with the actual link to the Preset Workspace.$/path/to/yaml/file
with the path to the file that should be replaced/created.
Import ownership
This command allows you to map ownership exported from a Superset instance, to a Preset Workspace:
preset-cli --workspaces=$https://workspace_slug.workspace_region.app.preset.io/ superset import-ownership $/path/to/yaml/file
Replace:
$https://workspace_slug.workspace_region.app.preset.io/
with the actual link to the Preset Workspace.$/path/to/yaml/file
with the path to the file containing the owernship information.
Note that this command doesn't import the users to your Preset team, so make sure the corresponding Preset accounts were already created and added to the team.
Import resources
The CLI can be used to import resources from exports. This approach is meant to be used with resources that were exported using the CLI, and therefore doesn't accept ZIP files, but instead path to files that contain the assets structures. Alternatively, you can download an asset through the UI, extract the folder and use the extracted folder in the command:
preset-cli --workspaces=$https://workspace_slug.workspace_region.app.preset.io/ superset sync native $/path/to/folder
Replace:
$https://workspace_slug.workspace_region.app.preset.io/
with the actual link to the Preset Workspace.$/path/to/folder
with the path to the folder containing the assets.
Additional flags are available for the sync native
command:
--overwrite
flag can be used in case the import should overwrite existing assets in the destination Workspace. If the asset(s) already exist and the flag isn't added, the command fails.--option
command can be used to include custom values for templates.--disallow-edits
flag can be included to prevent synced assets from being modified through the UI. This option is useful in case you want to implement version control, and therefore only changes from the CLI/API are accepted.--load-env
flag can be used to load environment variables so that they can be used in templates.--split
flag can to split the import operation in batches. This is specially useful when performing a large migration.--disable-jinja-templating
flag can be used to indicate that Jinja templating from YAML files shouldn't be loaded by the CLI.
Important note about Jinja templating
Since the CLI supports Jinja templating (and even using Python functions) to dynamically modify exported contents before import, during the import flow Jinja syntax created in Superset is escaped, so that it's not rendered by the CLI during the import. This way, Jinja syntax added by users for content modifications are rendered, and syntax meant to be hanlded by Superset is escaped.
Then, during the import process, the CLI would remove the escaping logic, so that the assets are compatible with Superset.
If you are importing assets that were exported from the UI and contain Jinja templating syntax (meant to be used by Superset), include the --disable-jinja-templating
flag, so that the CLI doesn't render them. If you have exported the assets using the CLI, then the syntax was automatically escaped to avoid any issues.
Import roles (Preset only)
Import Data Access Roles from a YAML file to your Preset Workspaces:
preset-cli --workspaces=$https://workspace_slug.workspace_region.app.preset.io/ superset import-roles $/path/to/yaml/file
$https://workspace_slug.workspace_region.app.preset.io/
with the actual link to the Preset Workspace.$/path/to/yaml/file
with the path to the file containing the roles information.