genvid.toolbox.BastionAPI

class genvid.toolbox.BastionAPI(use_wan=False, **kwargs)

Bases: BaseHttpApiTool, ConsulTool

Client for the Bastion API.

Changed in version 1.20.0: Now, add a use_wan address parameter, default to False, If True, will use the TaggedAddresses “wan” when resolving the bastion-api.

NAME = 'bastion-api'

The name of the logger for this tool. Must be redefined in children.

jobs

Get the JobsAPI

logs

Get the LogsAPI

Get the LinksAPI

get_genvid_repository_name() str

Return the Genvid Repository name

New in version 1.13.0.

get_genvid_repository_config() dict

Return the Genvid Repository configuration.

New in version 1.13.0.

get_base_url(timeout=0.0) str

Get the base URL.

Parameters

timeout – Timeout to wait for the service to be available.

is_bastion_running(timeout=0.0) bool

Check whether the bastion instance is running.

New in version 1.20.0.

get_bastion_version(timeout=0.0) str

Get the version of this bastion instance.

Parameters

timeout – Timeout to wait for the service to be available.

New in version 1.20.0.

Get the list of links.

Parameters
  • category – The link’s category.

  • link_id – The link’s ID.

  • timeout – The timeout in seconds.

Returns

A list of strings.

get_backends(timeout=0.0) dict

Get the list of all backend templates.

Parameters

timeout – The timeout in seconds.

get_backend(backend_id: str, timeout=0.0) dict

Get a backend template.

Parameters
  • backend_id – The backend ID.

  • timeout – The timeout in seconds.

add_backend(backend_id: str, backend_type: str, variables: dict = {}, timeout=0.0)

Add a new backend template.

Parameters
  • backend_id – The backend ID.

  • backend_type – The type of backend.

  • variables – A list of default variables for the backend.

  • timeout – The timeout in seconds.

update_backend(backend_id: str, backend_type: str, variables: dict = {}, timeout=0.0)

Update an existing backend template.

Parameters
  • backend_id – The backend ID.

  • backend_type – The type of backend.

  • variables – A list of default variables for the backend.

  • timeout – The timeout in seconds.

delete_backend(backend_id: str, timeout=0.0)

Remove a backend template.

Parameters
  • backend_id – The backend ID.

  • timeout – The timeout in seconds.

get_clusters(timeout=0.0, *, query_parameters: str = '') List[dict]

Query the BastionAPI for all clusters.

Parameters
  • query_parameters – String, beginning in ? which will be appended at the end of the request URL and interpreted as query parameters.

  • timeout – Maximum waiting time before giving up.

Changed in version 1.21.0: Added the query_parameters keyword-only argument to allow further customization of the query.

get_cluster(cluster_id: str, timeout=0.0) dict

Get a cluster.

Parameters
  • cluster_id – The cluster ID.

  • timeout – The timeout in seconds.

set_cluster(cluster_id: str, backend_id: str = 'default', backend_variables: dict = {}, category: str = '', is_static: bool = False, static_output: dict = {}, timeout=0.0) None

Create a cluster.

Parameters
  • cluster_id – The cluster ID.

  • backend_id – The backend ID.

  • backend_variables – The backend variables.

  • category – A category for the cluster.

  • is_static – If the cluster is static or not.

  • static_output – The static cluster output. It should be Terraform like.

  • timeout – The timeout in seconds.

Changed in version 1.13.0: New arguments for is_static and server_public_ips.

delete_cluster(cluster_id: str, force: bool = False, timeout=0.0) None

Delete a cluster.

Parameters
  • cluster_id – The cluster ID.

  • force – Force the cluster to be deleted.

  • timeout – The timeout in seconds.

update_cluster_static(cluster_id: str, output: dict = {}, timeout=0.0) None

Update a static cluster.

Parameters
  • cluster_id – The cluster ID.

  • output – The static cluster output. It should be Terraform like.

  • timeout – The timeout in seconds.

New in version 1.42.0.

cluster_get_setup_jobs(cluster_id: str, timeout=0.0) List[dict]

Get the list of setup jobs.

Parameters
  • cluster_id – The cluster ID.

  • timeout – The timeout in seconds.

Returns

A list of setup job definition.

cluster_set_setup_jobs(cluster_id: str, jobs: List[dict], timeout=0.0)

Set the list of setup jobs.

Parameters
  • cluster_id – The cluster ID.

  • jobs – A list of setup job definition.

  • timeout – The timeout in seconds.

cluster_do_setup(cluster_id, timeout=0.0)

Execute setup for a cluster. This action is used to start cluster-api.

Parameters
  • cluster_id – The cluster ID.

  • timeout – The timeout in seconds.

Returns

cluster_do_clean(cluster_id, timeout=0.0)

Execute clean for a cluster. This action will stop all Nomad jobs and remove Consul KV.

Parameters
  • cluster_id – The cluster ID.

  • timeout – The timeout in seconds.

Returns

get_cluster_service(cluster_id, service_id, timeout=0.0) List[str]

Get a service address.

Parameters
  • cluster_id – The cluster ID.

  • service_id – The service ID.

  • timeout – The timeout in seconds.

Returns

get_cluster_images(cluster_id, timeout=0.0) dict

Retrieve multiple image definitions.

Parameters
  • cluster_id – The cluster ID.

  • timeout – The timeout in seconds.

Returns

The data associated with the image definitions.

set_cluster_images(cluster_id, data: dict, timeout=0.0)

Set multiple image definitions.

Parameters
  • cluster_id – The cluster ID.

  • data – Data associated with the images.

  • timeout – The timeout in seconds.

delete_cluster_images(cluster_id, timeout=0.0)

Delete multiple image definitions.

Parameters
  • cluster_id – The cluster ID.

  • timeout – The timeout in seconds.

delete_cluster_image(cluster_id, image_id: str, timeout=0.0)

Delete link definitions.

Parameters
  • cluster_id – The cluster ID.

  • image_id – The image ID.

  • timeout – The timeout in seconds.

get_global(timeout=0.0, retry_request: bool = False) dict

Get the global configuration.

Parameters

timeout – The timeout in seconds.

set_global(bastion_id: str, timeout=0.0)

Set the global configuration.

Parameters
  • bastion_id – The bastion ID. This is used to isolate multiple bastions.

  • timeout – The timeout in seconds.

terraform_get_modules(prefix: str = '', timeout=0.0) List[dict]

Return the list of modules.

Parameters
  • prefix – A prefix to filter the modules.

  • timeout – The timeout in seconds.

Returns

A list of modules.

terraform_get_module(moduleId: str, timeout=0.0) dict

Return information about a module.

Parameters
  • moduleId – The module path.

  • timeout – The timeout in seconds.

Returns

The module definition.

terraform_get_repositories(timeout=0.0) Dict[str, dict]

Return the list of Terraform repositories.

Parameters

timeout – The timeout in seconds.

Returns

A map of repositories by name.

terraform_add_repository(name: str, config: dict, timeout=0.0) dict

Add a repository.

Parameters
  • name – The unique name for the repository.

  • config – A dictionary with the repository configuration.

Returns

The repository configuration.

terraform_update_repository(name: str, config: dict = {}, timeout=0.0) dict

Update an existing repository.

Parameters
  • name – The name of the repository.

  • config – A dictionary with the repository configuration. (Optional)

Returns

The repository configuration.

terraform_remove_repository(name: str, timeout=0.0) dict

Remove a repository.

Parameters

name – The name of the repository.

Returns

The repository configuration.

terraform_get_instances(timeout=0.0) List[dict]

Get the Terraform instances.

Parameters

timeout – The timeout in seconds.

terraform_get_instance(instance_id: str, timeout=0.0) dict

Get a specific Terraform instance.

Parameters
  • instance_id – The instance ID.

  • timeout – The timeout in seconds.

terraform_get_global_tfvars(timeout=0.0) dict

Get the global tfvars content.

Parameters

timeout – The timeout in seconds.

terraform_set_global_tfvars(tfvars: dict, timeout=0.0) str

Set the global tfvars content.

Parameters
  • tfvars – The tfvars content.

  • timeout – The timeout in seconds.

terraform_clean_global_tfvars(timeout=0.0) str

Clean the global tfvars content.

Parameters

timeout – The timeout in seconds.

terraform_get_instance_tfvars_default(instance_id: str, timeout=0.0) str

Get the instance tfvars content.

Parameters
  • instance_id – The instance ID.

  • timeout – The timeout in seconds.

terraform_get_instance_tfvars(instance_id: str, timeout=0.0) str

Get the instance tfvars content.

Parameters
  • instance_id – The instance ID.

  • timeout – The timeout in seconds.

terraform_set_instance_tfvars(instance_id: str, tfvars: dict, timeout=0.0)

Set the instance tfvars content.

Parameters
  • instance_id – The instance ID.

  • tfvars – The tfvars content.

  • timeout – The timeout in seconds.

terraform_get_instance_schema(instance_id: str, timeout=0.0) str

Get the instance schema for its variables.

Parameters
  • instance_id – The instance ID.

  • timeout – The timeout in seconds.

terraform_get_instance_content(instance_id: str, content_id: str, timeout=0.0)

Get a .tf.json Terraform file content.

Parameters
  • instance_id – The instance ID.

  • content_id – The Terraform file ID.

  • timeout – The timeout in seconds.

Returns

The content of the specified file in JSON.

terraform_set_instance_content(instance_id: str, content_id: str, content: dict, timeout=0.0)

Set a .tf.json Terraform file content.

Parameters
  • instance_id – The instance ID.

  • content_id – The Terraform file ID.

  • content – The Terraform file content.

  • timeout – The timeout in seconds.

terraform_delete_instance_content(instance_id: str, content_id: str, timeout=0.0)

Delete a .tf.json Terraform file content.

Parameters
  • instance_id – The instance ID.

  • content_id – The Terraform file ID.

  • timeout – The timeout in seconds.

terraform_get_instance_status(instance_id: str, timeout=0.0) dict

Get the status of an instance.

The instance states can be:

  • VOID: The instance doesn’t exist.

  • EMPTY: The instance exists but isn’t initialized.

  • DOWN: The instance is initialized but doesn’t contain any

    resources.

  • UP: The instance manage some resources.

  • BUSY: The instance is currently running a command.

  • ERROR: An error has occured during the retrieval of the status.

  • INVALID: The current status of the instance is unknown.

Parameters
  • instance_id – The instance ID.

  • timeout – The timeout in seconds.

Returns

A dictionary with a single key status.

terraform_wait_on_instance(instance_id: str, *, timeout: int = 60, increment: int = 1)

Wait on a specific instance to finish running a Terraform command.

More specifically, we wait for the command to have a status different from ‘BUSY’.

Parameters
  • instance_id – The ID of the instance to wait on.

  • timeout – The maximum number of time to wait.

  • increment – How much time to wait between each status query.

terraform_do_instance_get(instance_id: str, timeout=0.0) dict

Execute terraform get for the instance.

Parameters
  • instance_id – The instance ID.

  • timeout – The timeout in seconds.

terraform_get_providers(instance_id: str, *, timeout=0.0) Sequence[TerraformProvider]

Get all providers known to the specified instance.

Parameters
  • instance_id – Instance ID of the cluster we operate on.

  • timeout – Timeout in seconds.

Returns

All known providers, as a sequence.

terraform_get_default_providers(*, timeout=0.0) Sequence[TerraformProvider]

Get the global default configuration for all providers.

Parameters

timeout – Timeout in seconds.

Returns

All known providers, as a sequence.

terraform_put_providers(instance_id: str, providers: Iterable[TerraformProvider], *, timeout=0.0) dict

Override the configuration of all providers on a specific instance.

Parameters
  • instance_id – Instance ID of the cluster we operate on.

  • providers – The new provider configurations to use.

  • timeout – Timeout in seconds.

Returns

The response of the Bastion API, should be {}.

terraform_put_default_providers(providers: Iterable[TerraformProvider], *, timeout=0.0) dict

Override the global default configuration of all providers.

Parameters
  • providers – The new provider configurations to use.

  • timeout – Timeout in seconds.

Returns

The response of the Bastion API, should be {}.

terraform_delete_providers(instance_id: str, *, timeout=0.0) dict

Delete the configuration of all providers on a specific instance.

Parameters
  • instance_id – Instance ID of the cluster we operate on.

  • timeout – Timeout in seconds.

Returns

The response of the Bastion API, should be {}.

terraform_delete_default_providers(*, timeout=0.0) dict

Delete the global default configuration of all providers.

Parameters

timeout – Timeout in seconds.

Returns

The response of the Bastion API, should be {}.

terraform_get_provider(instance_id: str, provider_name: str, provider_alias: Optional[str] = None, *, timeout=0.0) TerraformProvider

Get the configuration of a specific provider on a specific cluster.

Parameters
  • instance_id – Instance ID of the cluster we operate on.

  • provider_name – Provider’s name.

  • provider_alias – Optional alias for the provider.

  • timeout – Timeout in seconds.

Returns

The matching provider.

terraform_get_default_provider(provider_name: str, provider_alias: Optional[str] = None, *, timeout=0.0) Sequence[TerraformProvider]

Get the global default configuration for a specific provider.

Parameters
  • provider_name – Provider’s name.

  • provider_alias – Optional alias for the provider.

  • timeout – Timeout in seconds.

Returns

The matching provider.

terraform_put_provider(instance_id: str, provider: TerraformProvider, *, timeout=0.0) dict

Override a provider configuration on a specific instance.

Parameters
  • instance_id – Instance ID of the cluster we operate on.

  • provider – The provider configuration to send to the back-end.

  • timeout – Timeout in seconds.

Returns

The response of the Bastion API, should be {}.

terraform_put_default_provider(provider: TerraformProvider, *, timeout=0.0) dict

Override the global default configuration for a specific provider.

Parameters
  • providers – The new provider configurations to use.

  • timeout – Timeout in seconds.

Returns

The response of the Bastion API, should be {}.

terraform_delete_provider(instance_id: str, provider_name: str, provider_alias: Optional[str] = None, *, timeout=0.0) dict

Delete a specific provider on a specific cluster.

Parameters
  • instance_id – Instance ID of the cluster we operate on.

  • provider_name – Provider’s name.

  • provider_alias – Optional alias for the provider.

  • timeout – Timeout in seconds.

Returns

The response of the Bastion API, should be {}.

terraform_delete_default_provider(provider_name: str, provider_alias: Optional[str] = None, *, timeout=0.0) dict

Delete the global default configuration for a specific provider.

Parameters
  • provider_name – Provider’s name.

  • provider_alias – Optional alias for the provider.

  • timeout – Timeout in seconds.

Returns

The response of the Bastion API, should be {}.

terraform_do_instance_import_module(instance_id: str, module_path: str = 'cluster/default', timeout=0.0, force: bool = False, **kwargs) dict

Execute terraform init for the instance.

Parameters
  • instance_id – The instance ID.

  • module_path – The module to use.

  • timeout – The timeout in seconds.

  • force – Force the import even if the cluster is in an error state.

terraform_do_instance_init(instance_id: str, timeout: float = 0.0, force: bool = False) dict

Execute terraform init for the instance

Parameters
  • instance_id – The instance ID.

  • timeout – The timeout in seconds.

  • force – Force the initialization even if the cluster is in an error state.

terraform_do_instance_refresh(instance_id: str, timeout=0.0) dict

Execute terraform refresh for the instance.

Parameters
  • instance_id – The instance ID.

  • timeout – The timeout in seconds.

terraform_regenerate_instance(instance_id: str, timeout=0.0) dict

Regenerate the Terraform working directory for the instance.

Parameters
  • instance_id – ID of the instance.

  • timeout – Timeout in seconds.

New in version 1.21.

terraform_do_instance_plan(instance_id: str, destroy: bool = False, timeout=0.0) dict

Execute terraform plan for the instance.

Parameters
  • instance_id – The instance ID.

  • destroy – Execute the destroy plan.

  • timeout – The timeout in seconds.

terraform_do_instance_apply(instance_id: str, timeout=0.0) dict

Execute terraform apply for the instance.

Parameters
  • instance_id – The instance ID.

  • timeout – The timeout in seconds.

terraform_do_instance_apply_plan(instance_id: str, plan_file: str, timeout=0.0) dict

Execute terraform apply on the instance for the given plan file. The plan file name is usually the command ID of the plan command.

Parameters
  • instance_id – The instance ID.

  • plan_file – The plan file name.

  • timeout – The timeout in seconds.

terraform_do_instance_destroy(instance_id: str, timeout=0.0) dict

Execute terraform destroy for the instance.

Parameters
  • instance_id – The instance ID.

  • timeout – The timeout in seconds.

terraform_do_instance_output(instance_id: str, timeout=0.0) dict

Return the terraform output command.

Parameters
  • instance_id – The instance ID.

  • timeout – The timeout in seconds.

terraform_get_instance_commands(instance_id: str, current: bool = True, timeout=0.0) List[dict]

Get the commands related to a Terraform instance.

Parameters
  • instance_id – The instance ID.

  • current – If True, only return the currently running commands.

  • timeout – The timeout in seconds.

terraform_get_instance_command(instance_id: str, command_id: str, timeout=0.0) dict

Get the command base on the instance ID and the command ID.

Parameters
  • instance_id – The instance ID.

  • command_id – The command ID.

  • timeout – The timeout in seconds.

terraform_wait_instance_command(instance_id: str, command_id: str, timeout=0.0) dict

Get the command base on the instance ID and the command ID.

Parameters
  • instance_id – The instance ID.

  • command_id – The command ID.

  • timeout – The timeout in seconds.

terraform_get_instance_command_log(instance_id: str, command_id: str, origin: str = 'start', offset: int = 0, size: int = 20000, timeout=0.0) dict

Get an instance’s command log.

Parameters
  • instance_id – The instance ID.

  • command_id – The command ID.

  • origin – If the log is from the start or end.

  • offset – The offset relative to the origin.

  • size – The max size of the log to be returned.

  • timeout – The timeout in seconds.

terraform_print_command_log(instance_id: str, command_id: str, output=None, tail=False, follow=False, lines: int = 10, timeout=0.0)

Print a command’s log on output.

Parameters
  • instance_id – The instance ID.

  • command_id – The command ID.

  • output – If None, default to stdout.

  • tail – If True, print the end of the log.

  • follow – If True, continue to show the log as it runs.

  • lines – An approximate number of lines to show.

  • timeout – The timeout in seconds.

get_bastion_id()

Get the bastion ID.

:raises RuntimeError : If the bastion ID is not set. :return : Bastion ID

render_template(content: Optional[str] = None, templatePath: Optional[str] = None, sources: Optional[str] = None, environment: Optional[Dict[str, str]] = None, timeout=5.0) dict

Execute a consul-template on the server site and return the result.

Parameters
  • content – The content of the template to render. Must be None or empty if templatePath is used.

  • templatePath – The path of the template inside the source. content must be None or empty if it is used and sources must be provided.

  • sources – A url for the sources. Must be provided if templatePath is set.

  • environment – A dictionary of environment variables. Keys and values must be string.

  • timeout – The timeout in seconds.

Returns

Ex: { “data”: “result”, “stderr”: “message”}

New in version 1.33.0.

class bastion_api.BastionAPI

Implementation of genvid.toolbox.BastionAPI