Pyterprise
This is an object oriented client API for Terraform Enterprise written in Python.
The methods included in this library generally map 1 to 1 in terms of function naming conventions to
terraform enterprise documentation, so please review the available methods
if you are uncertain on this library's usage.
Happy Terraforming!
Installation:
This module can be installed via pip, this library is compatible with python3.7
. Install it with pip for python3.
pip3 install --user pyterprise
Usage:
Note: Some examples are covered here, but more are covered in the
examples directory for more basic use-cases of this api.
First import the module and authenticate using the init method, you can retrieve a token from the terraform enterprise UI.
import pyterprise
tfe_token = 'TOKENHERE'
client = pyterprise.Client()
client.init(token=tfe_token, url='https://example-host.com')
After instantiating your client you need to set an organization to access workspaces and more.
From the client base-level you can also create/destroy/list organizations:
org = client.set_organization(id='my-organization')
This organization object org
has access to workspaces, ssh keys and other methods (Check source code/examples for more):
vcs_options = {
"identifier": "my-github-org/my-repo",
"oauth-token-id": "ot-xxxxxxxxxxx",
"branch": "master",
"default-branch": False
}
org.create_workspace(name='test-delete-me2',
vcs_repo=vcs_options,
auto_apply=False,
queue_all_runs=False,
working_directory='/',
trigger_prefixes=['/'])
org.delete_workspace(name='my-safe-to-delete-test-workspace')
You can access workspace objects through your organization client object,
which gives you access to runs, plans, statefiles, ssh-key additions and variables.
workspace = org.get_workspace('test-workspace')
for workspace in org.list_workspaces():
print(workspace)
print(workspace.name, workspace.id, workspace.created_at)
workspace.run(destroy_flag=False)
workspace.plan_apply(destroy_flag=False)
workspace.update(auto_apply=True)
workspace.create_variable(key='foo', value='bar', sensitive=False, category='env')
for variable in workspace.list_variables():
print(variable)
for run in workspace.list_runs(page=1, page_size=20):
if run.status == 'planned':
run.apply(comment='Running latest plan.')
Please consult module contents, examples
or terraform enterprise api documentation
for more api client usage. This API does not currently cover administrative functions and teams,
help is wanted for expanding functionality. Thank you.
API Coverage/Helper Methods