infrahouse_toolkit.terraform package¶
Subpackages¶
- infrahouse_toolkit.terraform.backends package
- infrahouse_toolkit.terraform.tests package
- Subpackages
- infrahouse_toolkit.terraform.tests.github_pr package
- Submodules
- infrahouse_toolkit.terraform.tests.github_pr.test_find_comment_by_backend module
- infrahouse_toolkit.terraform.tests.github_pr.test_init module
- infrahouse_toolkit.terraform.tests.github_pr.test_publish_comment module
- infrahouse_toolkit.terraform.tests.github_pr.test_publish_gist module
- Module contents
- infrahouse_toolkit.terraform.tests.status package
- Submodules
- infrahouse_toolkit.terraform.tests.status.test_comment module
- infrahouse_toolkit.terraform.tests.status.test_eq module
- infrahouse_toolkit.terraform.tests.status.test_metadata module
- infrahouse_toolkit.terraform.tests.status.test_summary_counts module
- infrahouse_toolkit.terraform.tests.status.test_summary_resources module
- Module contents
- infrahouse_toolkit.terraform.tests.github_pr package
- Submodules
- infrahouse_toolkit.terraform.tests.test_parse_comment module
- infrahouse_toolkit.terraform.tests.test_parse_plan module
- infrahouse_toolkit.terraform.tests.test_strip_lines module
- Module contents
- Subpackages
Submodules¶
infrahouse_toolkit.terraform.exceptions module¶
infrahouse_toolkit.terraform exceptions.
- exception infrahouse_toolkit.terraform.exceptions.IHParseError[source]¶
Bases:
IHTFExceptionError happening when parsing fails.
- exception infrahouse_toolkit.terraform.exceptions.IHTFException[source]¶
Bases:
IHExceptionTerraform 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:
objectGitHubPRrepresents 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.
- 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_TOKENenvironment variable. If theGITHUB_TOKENenvironment 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:
objectTFStatusrepresents a result of aterraform planrun. 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.
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:
- 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
- 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
terraformwithTF_LOG=JSONand save the terraform trace intf-apply-trace.txtandtf-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
withblock.- Raises:
CalledProcessError – if either of terraform commands (except
terraform destroy) exits with non-zero.