跳转到主要内容

HashiCorp Terraform Cloud的客户端

项目描述

Terraform Cloud客户端

HashiCorp的Terraform Cloud的API客户端

安装

$ pip install terraform-cloud-client

此客户端仅在python 3.7上进行了测试

作为库的使用

设置

>>> import tfc
>>> client = tfc.TerraformClient("my_api_token", "my_organization", "my_workspace")

获取Terraform变量

>>> variables = client.get_variables()
>>> variables
{
    'my_username': <TerraformVariable id='var-someid1', name='my_username', value='john_doe'>,
    'my_password': <TerraformVariable id='var-someid2', name='my_password', sensitive>,
}
>>> username_variable = variables['my_username']
>>> username_variable.id
'var-someid1'
>>> username_variable.name
'my_username'
>>> username_variable.value
'john_doe'

更改变量的值

>>> client.update_variable(variable_id=username_variable.id, new_value='jane_doe')

触发运行

>>> run = client.create_run('My message')
>>> run.id
'run-someid'
>>> run.url # Go here in a web browser to view the run
'https://app.terraform.io/app/my_organization/workspaces/my_workspace/runs/run-someid'

作为命令行工具的使用

通过pip安装此工具也会安装一个tfc命令行工具

$ tfc --help
usage: tfc [-h] [--message MESSAGE]
           organization workspace [name=value [name=value ...]]

Trigger a Terraform Cloud run

positional arguments:
  organization          The name of your organization in Terraform Cloud
  workspace             The name of your workspace in Terraform Cloud
  name=value            Set terraform variable NAME to VALUE before creating
                        the run

optional arguments:
  -h, --help            show this help message and exit
  --message MESSAGE, -m MESSAGE
                        The message to be associated with this run

Put your API token in the TERRAFORM_CLOUD_TOKEN environment variable

例如

$ tfc my_organization my_workspace foo=baz --message="Reticulating splines"
Updated 'foo' from 'bar' to 'baz'
Created run run-g6SmSsLVKg71yeNw - check status at: https://app.terraform.io/app/my_organization/workspaces/my_workspace/runs/run-g6SmSsLVKg71yeNw

消息参数可以包含在运行时展开的变量。这些遵循与Python的str.format(...)相同的格式化规则。当前定义的变量有

  • git_repository
  • git_branch
  • git_commit_subject
  • git_commit_author

例如

$ tfc my_organization my_workspace foo=baz --message="{git_commit_subject} (author: {git_commit_author}, branch: {git_branch}, repo: {git_repository})"

可能创建一条包含消息的运行

My commit message (author: Joe Bloggs, branch: my-branch, repo: terraform-cloud-client)

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关安装软件包的更多信息。

源分布

terraform-cloud-client-0.0.3.tar.gz (5.0 kB 查看散列)

上传时间:

构建分布

terraform_cloud_client-0.0.3-py3-none-any.whl (7.2 kB 查看散列)

上传于 Python 3

由以下支持