Skip to main content

A developer friendly Terraform Cloud API client

Project description

Terraform Cloud API Python Client

Try to offer a good python object interface to Terraform Cloud API.

PyPI version Code Style: Black

Quick start

from tfc_client import TFCClient
from tfc_client.enums import (
    RunStatus,
    NotificationTrigger,
    NotificationsDestinationType,
)
from tfc_client.models import VCSRepoModel

# Instanciate the client
client = TFCClient(token="WXDFR3ZSDFGYTdftredfgtre")

# Retreive any object type by ID from the client
my_org = client.get("organization", id="myorg")
my_ws = client.get("workspace", id="ws-gvcdr54dfd")
my_run = client.get("run", id="run-wvfgkdlz")
my_var = client.get("var", id="var-vcerjvjk")

# If you need to retreive a workspace by name, you need to retreive it from an organization object:
my_ws = my_org.workspace(name="my_workspace")

# Create a ssh key in the org
my_sshkey = my_org.create(
    "ssh-key",
    name="my-ssh-key,
    value="-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEAm6+JVgl...",
)

# To retreive all workspaces:
for ws in my_org.workspaces:
    print(ws.name)

# To retreive a subset of workspaces:
for ws in my_org.workspaces_search(search="my_"):
    print(ws.name)

# If you need to retreive all workspaces with associated current-run info efficiently (in one api call):
for ws in my_org.workspaces_search(include="current-run"):
    print(f"{ws.name} -> {ws.current_run.status}")

# To create a workspace linked with a github repository

# To create the binding between TFC and GitHub (to create a oauth_token_id):
# See: https://www.terraform.io/docs/cloud/vcs/github.html

# First: Create the repository object:
vcs_repo = VCSRepoModel(
    identifier="github/repo",
    oauth_token_id="ot-fgtredfgtr",
    branch="master",
    default_branch=True,
)

# Finally: Send the workspace object to TFC API:
my_ws = my_org.create(
    "workspace",
    name="my_workspace_test",
    terraform_version="0.11.10",
    working_directory="",
    vcs_repo=vcs_repo,
    source_name="TFC Python Client",
    source_url="https://pypi.org/project/tfc-client/"
)

# Assign a ssh-key to the workspace:
my_ws.assign("ssh-key", my_sshkey)

# Unassign it with:
# my_ws.unassign("ssh-key")

# Create a Notification configuration
my_notification = new_ws.create(
    "notification-configuration",
    enabled=True,
    name="MyNotif",
    url="https://httpstat.us/200",
    destination_type=NotificationsDestinationType.generic,
    token="TestToken",
    triggers=[NotificationTrigger.created],
)

# Launch a run on a workspace:
my_run = my_ws.create("run", message="Run run run")

# Wait for the run plan execution
if my_run.wait_plan(timeout=200, progress_callback=lambda run, duration: print(f"{run.id} status is {run.status}")):
    print(f"{my_run.id} reached the target status ({my_run.status})")
    # Display log of the plan (with ANSI color)
    print(my_run.plan.log_colored)

else:
    print(f"{my_run.id} is pending. Don't wait...")

if RunStatus(my_run.status) == RunStatus.planned:
    # Launch the Apply
    my_run.do_apply(comment="Apply !")
    # Wait for the run apply execution
    if my_run.wait_apply(timeout=200, progress_callback=lambda run, duration: print(f"{run.id} status is {run.status}")):
        print(f"{my_run.id} reached the target status ({my_run.status})")
        # Display log of the apply (with ANSI color)
        print(my_run.apply.log_colored)
    else:
        print(f"{my_run.id} is pending. Don't wait...")

# To retreive all runs of a workspace:
for run in my_ws.runs:
    print(f"{run.id}: {run.status}")

# Delete the workspace
my_org.delete(my_ws)

Current coverage of the TFC API

Currently the following endpoints are supported:

  • <input type="checkbox" disabled="" /> Account
  • <input type="checkbox" checked="" disabled="" /> Applies
  • <input type="checkbox" disabled="" /> Configuration Versions
  • <input type="checkbox" disabled="" /> Cost Estimates
  • <input type="checkbox" checked="" disabled="" /> Notification Configurations
    • <input type="checkbox" checked="" disabled="" /> List
    • <input type="checkbox" checked="" disabled="" /> Show
    • <input type="checkbox" checked="" disabled="" /> Create
    • <input type="checkbox" checked="" disabled="" /> Update
    • <input type="checkbox" checked="" disabled="" /> Delete
    • <input type="checkbox" checked="" disabled="" /> Verify
  • <input type="checkbox" disabled="" /> OAuth Clients
  • <input type="checkbox" disabled="" /> OAuth Tokens
  • <input type="checkbox" checked="" disabled="" /> Organizations
    • <input type="checkbox" checked="" disabled="" /> List
    • <input type="checkbox" checked="" disabled="" /> Show
    • <input type="checkbox" checked="" disabled="" /> Create
    • <input type="checkbox" checked="" disabled="" /> Update
    • <input type="checkbox" checked="" disabled="" /> Destroy
  • <input type="checkbox" disabled="" /> Organization Tokens
  • <input type="checkbox" disabled="" /> Plan Exports
  • <input type="checkbox" checked="" disabled="" /> Plans
  • <input type="checkbox" disabled="" /> Policies
  • <input type="checkbox" disabled="" /> Policy Checks
  • <input type="checkbox" disabled="" /> Policy Sets
  • <input type="checkbox" disabled="" /> Registry Modules
  • <input type="checkbox" checked="" disabled="" /> Runs
    • <input type="checkbox" checked="" disabled="" /> Create
    • <input type="checkbox" checked="" disabled="" /> Apply
    • <input type="checkbox" checked="" disabled="" /> List runs in a workspace
    • <input type="checkbox" checked="" disabled="" /> Get details
    • <input type="checkbox" checked="" disabled="" /> Discard
    • <input type="checkbox" checked="" disabled="" /> Cancel
    • <input type="checkbox" checked="" disabled="" /> Force cancel
    • <input type="checkbox" checked="" disabled="" /> Force execute
  • <input type="checkbox" checked="" disabled="" /> SSH Keys
    • <input type="checkbox" checked="" disabled="" /> Create
    • <input type="checkbox" checked="" disabled="" /> Get
    • <input type="checkbox" checked="" disabled="" /> List
    • <input type="checkbox" checked="" disabled="" /> Update
    • <input type="checkbox" checked="" disabled="" /> Delete
  • <input type="checkbox" disabled="" /> State Versions
  • <input type="checkbox" disabled="" /> State Version Outputs
  • <input type="checkbox" disabled="" /> Team Access
  • <input type="checkbox" disabled="" /> Team Memberships
  • <input type="checkbox" disabled="" /> Team Tokens
  • <input type="checkbox" disabled="" /> Teams
  • <input type="checkbox" disabled="" /> User Tokens
  • <input type="checkbox" checked="" disabled="" /> Users
  • <input type="checkbox" checked="" disabled="" /> Variables
    • <input type="checkbox" checked="" disabled="" /> Create
    • <input type="checkbox" checked="" disabled="" /> List
    • <input type="checkbox" checked="" disabled="" /> Update
    • <input type="checkbox" checked="" disabled="" /> Delete
  • <input type="checkbox" checked="" disabled="" /> Workspaces
    • <input type="checkbox" checked="" disabled="" /> List
    • <input type="checkbox" checked="" disabled="" /> Show
    • <input type="checkbox" checked="" disabled="" /> Create
    • <input type="checkbox" checked="" disabled="" /> Update
    • <input type="checkbox" checked="" disabled="" /> Delete
    • <input type="checkbox" checked="" disabled="" /> Lock
    • <input type="checkbox" checked="" disabled="" /> Unlock
    • <input type="checkbox" checked="" disabled="" /> Force Unlock
    • <input type="checkbox" checked="" disabled="" /> Assigh SSH key
    • <input type="checkbox" checked="" disabled="" /> Unassign SSH key

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for tfc-client, version 0.7.3
Filename, size File type Python version Upload date Hashes
Filename, size tfc_client-0.7.3-py3-none-any.whl (20.3 kB) File type Wheel Python version py3 Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page