dda-python-terraform/README.md

143 lines
4.9 KiB
Markdown
Raw Normal View History

2015-12-31 06:48:26 +00:00
## Introduction
2020-10-19 22:30:02 +00:00
python-terraform is a python module provide a wrapper of `terraform` command line tool.
2015-12-31 06:48:26 +00:00
`terraform` is a tool made by Hashicorp, please refer to https://terraform.io/
2020-10-20 22:15:14 +00:00
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
2016-11-18 09:39:25 +00:00
### Status
2020-10-20 22:15:14 +00:00
[![Build Status](https://travis-ci.org/aubustou/python-terraform.svg?branch=develop)](https://travis-ci.org/aubustou/python-terraform)
2015-12-31 06:57:23 +00:00
## Installation
2016-11-19 17:11:21 +00:00
pip install python-terraform
2020-10-19 22:30:02 +00:00
## Usage
#### For any terraform command
2017-05-11 17:58:09 +00:00
from python_terraform import *
t = Terraform()
return_code, stdout, stderr = t.<cmd_name>(*arguments, **options)
2017-05-09 08:09:28 +00:00
**Note**: method name same as reserved keyword like `import` won't be accepted by python interpreter,
to be able to call the method, you could call cmd_name by adding `_cmd` after command name, for example,
`import` here could be called by
2017-05-11 17:58:09 +00:00
from python_terraform import *
t = Terraform()
2017-05-09 08:09:28 +00:00
return_code, stdout, stderr = t.import_cmd(*arguments, **options)
or just call cmd method directly
2017-05-11 17:58:09 +00:00
from python_terraform import *
t = Terraform()
return_code, stdout, stderr = t.cmd(<cmd_name>, *arguments, **options)
2020-10-19 22:30:02 +00:00
#### For any argument
simply pass the string to arguments of the method, for example,
2020-10-19 22:30:02 +00:00
terraform apply target_dir
--> <instance>.apply('target_dir')
2020-10-19 22:30:02 +00:00
terraform import aws_instance.foo i-abcd1234
--> <instance>.import('aws_instance.foo', 'i-abcd1234')
#### For any options
2020-10-19 22:30:02 +00:00
* dash to underscore
remove first dash, and then use underscore to replace dash symbol as option name
2020-10-19 22:30:02 +00:00
ex. -no-color --> no_color
* for a simple flag option
2020-10-19 22:30:02 +00:00
use ```IsFlagged/None``` as value for raising/not raising flag, for example,
terraform taint -allow-missing
--> <instance>.taint(allow_missing=IsFlagged)
2020-10-19 22:30:02 +00:00
terraform taint
--> <instance>.taint(allow_missing=None) or <instance>.taint()
terraform apply -no-color
--> <instance>.apply(no_color=IsFlagged)
2020-10-19 22:30:02 +00:00
* for a boolean value option
2020-10-19 22:30:02 +00:00
assign True or False, for example,
2020-10-19 22:30:02 +00:00
terraform apply -refresh=true --> <instance>.apply(refresh=True)
2020-10-19 22:30:02 +00:00
* if a flag could be used multiple times, assign a list to it's value
2020-10-19 22:30:02 +00:00
terraform apply -target=aws_instance.foo[1] -target=aws_instance.foo[2]
2020-10-19 22:30:02 +00:00
--->
<instance>.apply(target=['aws_instance.foo[1]', 'aws_instance.foo[2]'])
* for the "var" flag, assign dictionary to it
terraform apply -var='a=b' -var='c=d'
--> tf.apply(var={'a':'b', 'c':'d'})
* if an option with None as value, it won't be used
2017-05-11 09:21:31 +00:00
#### Terraform Output
By default, stdout and stderr are captured and returned. This causes the application to appear to hang. To print terraform output in real time, provide the `capture_output` option with any value other than `None`. This will cause the output of terraform to be printed to the terminal in real time. The value of `stdout` and `stderr` below will be `None`.
from python_terraform import Terraform
t = Terraform()
return_code, stdout, stderr = t.<cmd_name>(capture_output=False)
## Examples
2020-10-19 22:30:02 +00:00
### Have a test.tf file under folder "/home/test"
#### 1. apply with variables a=b, c=d, refresh=false, no color in the output
In shell:
cd /home/test
terraform apply -var='a=b' -var='c=d' -refresh=false -no-color
2020-10-19 22:30:02 +00:00
In python-terraform:
2017-05-11 17:58:09 +00:00
from python_terraform import *
tf = Terraform(working_dir='/home/test')
tf.apply(no_color=IsFlagged, refresh=False, var={'a':'b', 'c':'d'})
2020-10-19 22:30:02 +00:00
or
2017-05-11 17:58:09 +00:00
from python_terraform import *
tf = Terraform()
tf.apply('/home/test', no_color=IsFlagged, refresh=False, var={'a':'b', 'c':'d'})
or
2017-05-11 17:58:09 +00:00
from python_terraform import *
tf = Terraform(working_dir='/home/test', variables={'a':'b', 'c':'d'})
tf.apply(no_color=IsFlagged, refresh=False)
2020-10-19 22:30:02 +00:00
#### 2. fmt command, diff=true
In shell:
cd /home/test
2020-10-19 22:30:02 +00:00
terraform fmt -diff=true
In python-terraform:
2020-10-19 22:30:02 +00:00
2017-05-11 17:58:09 +00:00
from python_terraform import *
tf = terraform(working_dir='/home/test')
tf.fmt(diff=True)
2020-10-19 22:30:02 +00:00
## default values
2020-10-19 22:30:02 +00:00
for apply/plan/destroy command, assign with following default value to make
caller easier in python
2017-01-04 06:34:00 +00:00
1. ```input=False```, in this case process won't hang because you missing a variable
1. ```no_color=IsFlagged```, in this case, stdout of result is easier for parsing
## Implementation
2020-10-19 22:30:02 +00:00
IMHO, how terraform design boolean options is confusing.
Take `input=True` and `-no-color` option of `apply` command for example,
2020-10-19 22:30:02 +00:00
they're all boolean value but with different option type.
This make api caller don't have a general rule to follow but to do
a exhaustive method implementation which I don't prefer to.
2020-10-19 22:30:02 +00:00
Therefore I end-up with using `IsFlagged` or `IsNotFlagged` as value of option
like `-no-color` and `True/False` value reserved for option like `refresh=true`