Skip to main content

A Python client for the Terraform Cloud API

Project description

terrasnek

CircleCI codecov PyPI version Documentation Status GitHub issues GitHub license

A Python Client for the Terraform Cloud API.


Overview

The goal of this project is to support all endpoints available in the Terraform Cloud API. It's possible that some of these endpoints won't work if you're working against an older version of Terraform Enterprise. This has been tested against TFE v202002-2 (419).

Note: Terraform Enterprise is the self-hosted distribution of Terraform Cloud. It offers enterprises a private instance of the Terraform Cloud application, with no resource limits and with additional enterprise-grade architectural features like audit logging and SAML single sign-on.

Requirements

To make full usage of all the tools and commands here, you should have installed:

  • python3
  • make
  • pylint
  • coverage
  • ag

All Python requirements are outlined in pip-reqs.txt.

Using terrasnek

For more details on using each endpoint, check out the docs or the test directory. I also wrote a blog post showing how this library can be used.

from terrasnek.api import TFC
import os

TFC_TOKEN = os.getenv("TFC_TOKEN", None)
TFC_URL = os.getenv("TFC_URL", None)  # ex: https://app.terraform.io
SSL_VERIFY = os.getenv("SSL_VERIFY", None)  # set to True if you want to use HTTP or insecure HTTPS

if __name__ == "__main__":
    api = TFC(TFC_TOKEN, url=TFC_URL, ssl_verify=SSL_VERIFY)
    api.set_org("YOUR_ORGANIZATION")
Supported Endpoints
Standard Endpoints
Admin Endpoints

Contributing to terrasnek

Before contributing to terrasnek or publishing to PyPi, there are a few must-dos.

  • Each endpoint must have it's own implementation file, it's own test file, and corresponding doc file.
  • The Python code (implementation and test) must be linted.
  • The documentation must be rebuilt with any changes you added.
  • Before merging to master, it must run the full test suite and generate test coverage, and all tests must pass.
  • The test coverage must be uploaded to CodeCov.

The instructions for doing each of these can be found below. This process is not automated for now due to some of the limitations of the free Terraform Cloud offering. In the future, if some of the limitations are lifted, these checks will be automated in CircleCI.

Here is a summary of the commands:

make lint-lib
make lint-tests
make docs
make coverage
make codecov
make pip-publish

Linting the Code

Lint Library Code
make lint-lib
Lint Test Code
make lint-tests

Building Test Data

cd test/testdata/terraform/
tar -zcvf terrasnek_unittest_config_version.tar.gz src/*

Testing

It is recommended that when running the entire suite of tests, you use a sandbox Terraform Enterprise instance. This will allow you to test the Admin Endpoints without any worry of error, and you will not have any run limits.

Due to those limitations, this library does not currently test the full suite of tests in CircleCI. It is recommended that you run the tests locally before submitting pull requests, or at the very least, verify all of the tests that touch code you are contributing.

Running Specific Tests

The test suite takes a long time to execute fully, since there is a lot of async work, and waiting for plans, applies, etc. In the scenario you want to just test a new implementation or change, use the below.

source test/secrets/secrets.sh
python3 -m unittest test/applies_test.py
Running All Tests

Note: When you run all of the tests, you will have to create a user (that matches your TEST_USER in secrets.sh) manually ahead of executing the tests if you don't have an existing user in the system already. Currently not possible to create users with the API, and some of the tests involve adding a user to teams and organizations. Not all endpoints are available in the free Terraform Cloud offering. In order for you to test or use all of the endpoints, you'll need the proper packages for Terraform Cloud or your own private Terraform Enterprise instance.

source test/secrets/secrets.sh
make test
Running the Tests with Coverage Info
source test/secrets/secrets.sh
make coverage
Uploading Coverage Stats to CodeCov.io
export CODECOV_TOKEN="<TOKEN>"
bash <(curl -s https://codecov.io/bash)

Building the Documentation

make docs

Publishing to PyPi

Production
make pip-publish
Test
make pip-test-publish

Note: This repo is not officially maintained by HashiCorp.

Project details


Download files

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

Source Distribution

terrasnek-0.0.4.tar.gz (31.9 kB view details)

Uploaded Source

Built Distribution

terrasnek-0.0.4-py3-none-any.whl (68.7 kB view details)

Uploaded Python 3

File details

Details for the file terrasnek-0.0.4.tar.gz.

File metadata

  • Download URL: terrasnek-0.0.4.tar.gz
  • Upload date:
  • Size: 31.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.3

File hashes

Hashes for terrasnek-0.0.4.tar.gz
Algorithm Hash digest
SHA256 c9f1f4fd59c9ed312457e57d82322d8543b603b369f21fd5d26a5502a99574f3
MD5 bcc11546f2377963bc34329085260dcd
BLAKE2b-256 53e9898ad5317f47f6395ed8a8d6873184952bd8083594cae05713d19de9d006

See more details on using hashes here.

File details

Details for the file terrasnek-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: terrasnek-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 68.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.3

File hashes

Hashes for terrasnek-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c6bb20d8adef15546fd09da61505cabf36e7f98c3fd3924d93bb7048a03dbd99
MD5 b3298375ad208bff2d42251dcffd28e3
BLAKE2b-256 95b600dbd66cbed3f547665a605d944900412d7e5171d240682d50aa2461e528

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page