infrahouse_toolkit.terraform package

Subpackages

Submodules

infrahouse_toolkit.terraform.exceptions module

infrahouse_toolkit.terraform exceptions.

exception infrahouse_toolkit.terraform.exceptions.IHParseError[source]

Bases: IHTFException

Error happening when parsing fails.

exception infrahouse_toolkit.terraform.exceptions.IHTFException[source]

Bases: IHException

Terraform related exceptions.

infrahouse_toolkit.terraform.githubpr module

Module for GitHubPR.

class infrahouse_toolkit.terraform.githubpr.GitHubPR(repo_name: str, pull_request: int, github_token: str = None)[source]

Bases: object

GitHubPR represents a pull request on GitHub. The pull request is identified by a repository name and a pull request number. To access GitHub the class needs a GitHub token. All these are input arguments of the class.

Parameters:
  • repo_name – Full repository name. For example, infrahouse/infrahouse-toolkit.

  • pull_request (int) – Pull request number.

  • github_token (str) – GitHub personal access tokens. They are created in https://github.com/settings/tokens

Typo repo_name:

str

property comments

An interator with comments in this PR.

delete_my_comments()[source]

Delete all comments in the pull request.

edit_comment(comment: IssueComment, new_text: str, private_gist: bool = True)[source]

Modify existing comment. If the new comment is too big, publish it as a gist.

find_comment_by_backend(backend: TFBackend) IssueComment | None[source]

Find a comment that describes state of a given backend. It will return None if nothing is found.

Parameters:

backend – Terraform Backend configuration.

Returns:

a comment object or None.

Return type:

IssueComment, None

property github

GitHub client.

property github_token

GitHub token as passed by the class argument or from the GITHUB_TOKEN environment variable. If the GITHUB_TOKEN environment variable is not defined the property will return None.

publish_comment(comment: str, private_gist: bool = None)[source]

Add the given text as a comment in the pull request.

property pull_request

Pull request object of the repository name passed in the class argument.

property repo

Repository object of the repository name passed in the class argument.

infrahouse_toolkit.terraform.status module

Module for TFStatus, Terraform plan run status class.

class infrahouse_toolkit.terraform.status.RunOutput(stdout, stderr)

Bases: tuple

stderr

Alias for field number 1

stdout

Alias for field number 0

class infrahouse_toolkit.terraform.status.RunResult(add, change, destroy)

Bases: tuple

add

Alias for field number 0

change

Alias for field number 1

destroy

Alias for field number 2

class infrahouse_toolkit.terraform.status.TFStatus(backend: TFBackend, success: bool, run_result: RunResult, run_output: RunOutput, affected_resources: RunResult = None)[source]

Bases: object

TFStatus represents a result of a terraform plan run. It includes outputs (both stdout and stderr) and a summary of changes - how many resources are going to be created/changed/destroyed.

Credit for emojis https://emojicombos.com/

property comment

Serialize the status as a comment text eligible to be posted on GitHub.

property metadata

Produces a base64 encoded string with a dictionary that can be used to create the same instance of the class.

property summary_counts

Credit for tabulate: https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data

Returns:

Formatted table.

property summary_resources

Produces a string with a table that lists all added/modified/deleted resources.

infrahouse_toolkit.terraform.status.decolor(text: str) str[source]

Remove ANSI escape sequences that color console output.

infrahouse_toolkit.terraform.status.strip_lines(src: str, pattern: str) str[source]

Remove lines starting with a string pattern.

Parameters:
  • src (str) – Input text

  • pattern (str) – A string. When a line in the input text starts with this string - skip it.

Returns:

Stripped text.

Return type:

str

Module contents

Module for helper functions to deal with Terraform.

infrahouse_toolkit.terraform.execute(cmd, stdout=-1, stderr=-1, cwd=None, env=None)[source]

Execute a command and return a tuple with return code, STDOUT and STDERR.

Parameters:
  • cmd (list) – Command.

  • stdout (int, None) – Where to send stdout. Default PIPE.

  • stderr (int, None) – Where to send stdout. Default PIPE.

  • cwd (str) – Working directory.

  • env (dict) – Dictionary with environment for the process.

Returns:

Tuple (return code, STDOUT, STDERR)

Return type:

tuple

infrahouse_toolkit.terraform.parse_comment(comment_text: str) TFStatus[source]

Given a text comment try to instantiate a Terraform status out of it. Not any comment can yield a status, obviously. A comment with the status will have a metadata in its content:

<details><summary><i>metadata</i></summary>

If parser failed because the comment doesn’t have the status then the function will raise an exception.

Parameters:

comment_text – Comment text.

Returns:

Terraform status.

Return type:

TFStatus

Raises:

IHParseError – When the comment text doesn’t contain a status.

infrahouse_toolkit.terraform.parse_plan(output) -> (<class 'infrahouse_toolkit.terraform.status.RunResult'>, <class 'infrahouse_toolkit.terraform.status.RunResult'>)[source]

Parse a string given by output and return a tuple with execution plan.

Credit for a regexp removing colors from text: https://stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python

Parameters:

output (str) – Output of terraform plan command.

Returns:

Two tuples. One tuple with number of changes (add, change, destroy). Another - with list of to be added/changed/destroyed resources.

Return type:

RunResult, RunResult

infrahouse_toolkit.terraform.terraform_apply(path, destroy_after=True, json_output=False, var_file='terraform.tfvars', enable_trace=False)[source]

Run terraform init and apply, then return a generator. If destroy_after is True, run terraform destroy afterward.

Parameters:
  • path (str) – Path to directory with terraform module.

  • destroy_after (bool) – Run terraform destroy after context it returned back.

  • json_output (bool) – Yield terraform output result as a dict (available in the context)

  • var_file (str) – Path to a file with terraform variables.

  • enable_trace (bool) – If True, it will run terraform with TF_LOG=JSON and save the terraform trace in tf-apply-trace.txt and tf-destroy-trace.txt. Useful if you want to find out what API calls terraform makes and for other debugging.

Returns:

If json_output is true then yield the result from terraform_output otherwise nothing. Use it in the with block.

Raises:

CalledProcessError – if either of terraform commands (except terraform destroy) exits with non-zero.

infrahouse_toolkit.terraform.terraform_output(path)[source]

Run terraform output and return the json results as a dict.

Parameters:

path (str) – Path to directory with terraform module.

Returns:

dict from terraform output

Return type:

dict