2015-12-31 06:48:26 +00:00
## Introduction
python-terraform is a python module provide a wrapper of `terraform` command line tool.
`terraform` is a tool made by Hashicorp, please refer to https://terraform.io/
2016-11-18 09:39:25 +00:00
### Status
[![Build Status ](https://travis-ci.org/beelit94/python-terraform.svg?branch=develop )](https://travis-ci.org/beelit94/python-terraform)
2015-12-31 06:57:23 +00:00
## Installation
2016-11-19 17:11:21 +00:00
pip install python-terraform
2017-01-03 15:09:23 +00:00
2016-11-19 10:24:33 +00:00
## Usage
2017-05-09 07:39:09 +00:00
#### For any terraform command
2016-11-19 10:24:33 +00:00
2016-12-20 10:53:01 +00:00
from python_terraform import Terraform
t = Terraform()
return_code, stdout, stderr = t.< cmd_name > (*arguments, **options)
2017-05-09 07:39:09 +00:00
2017-05-09 07:48:28 +00:00
**Note**: method name same as reserved keyword like `import` could be called directly
by adding `_cmd` after command name
2017-05-09 07:39:09 +00:00
from python_terraform import Terraform
t = Terraform()
2017-05-09 07:48:28 +00:00
return_code, stdout, stderr = t.< cmd_name > _cmd(*arguments, **options)
2017-05-09 07:39:09 +00:00
or just call cmd method directly
from python_terraform import Terraform
t = Terraform()
return_code, stdout, stderr = t.cmd(< cmd_name > , *arguments, * *options)
2016-12-20 10:53:01 +00:00
2017-05-09 07:39:09 +00:00
#### For any argument
2017-01-03 15:53:31 +00:00
simply pass the string to arguments of the method, for example,
2017-01-03 15:46:38 +00:00
2017-01-03 15:53:31 +00:00
terraform apply target_dir
--> < instance > .apply('target_dir')
terraform import aws_instance.foo i-abcd1234
--> < instance > .import('aws_instance.foo', 'i-abcd1234')
2017-01-03 15:46:38 +00:00
2017-05-09 07:39:09 +00:00
#### For any options
2016-12-20 10:53:01 +00:00
2017-01-03 15:46:38 +00:00
* dash to underscore
remove first dash, and then use underscore to replace dash symbol as option name
2016-12-20 10:53:01 +00:00
ex. -no-color --> no_color
2017-01-03 15:46:38 +00:00
* for a simple flag option
use ```IsFlagged/None``` as value for raising/not raising flag, for example,
2017-01-03 15:09:23 +00:00
2017-01-03 15:46:38 +00:00
terraform taint -allow-missing
--> < instance > .taint(allow_ missing=IsFlagged)
terraform taint
--> < instance > .taint(allow_ missing=None) or < instance > .taint()
terraform apply -no-color
--> < instance > .apply(no_color=IsFlagged)
* for a boolean value option
assign True or False, for example,
terraform apply -refresh=true --> < instance > .apply(refresh=True)
* if a flag could be used multiple times, assign a list to it's value
terraform apply -target=aws_instance.foo[1] -target=aws_instance.foo[2]
--->
< 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
2016-11-19 10:24:33 +00:00
## Examples
2016-12-20 10:53:01 +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
2016-11-19 10:24:33 +00:00
In shell:
cd /home/test
terraform apply -var='a=b' -var='c=d' -refresh=false -no-color
In python-terraform:
from python_terraform import Terraform
2016-12-20 10:53:01 +00:00
tf = Terraform(working_dir='/home/test')
2016-11-24 07:09:16 +00:00
tf.apply(no_color=IsFlagged, refresh=False, var={'a':'b', 'c':'d'})
2016-12-20 10:53:01 +00:00
or
from python_terraform import Terraform
tf = Terraform()
tf.apply('/home/test', no_color=IsFlagged, refresh=False, var={'a':'b', 'c':'d'})
2017-01-03 17:02:11 +00:00
or
from python_terraform import Terraform
tf = Terraform(working_dir='/home/test', variables={'a':'b', 'c':'d'})
tf.apply(no_color=IsFlagged, refresh=False)
2016-12-20 10:53:01 +00:00
#### 2. fmt command, diff=true
2016-11-19 10:24:33 +00:00
In shell:
cd /home/test
2016-12-20 10:53:01 +00:00
terraform fmt -diff=true
2016-11-19 10:24:33 +00:00
In python-terraform:
from python_terraform import Terraform
tf = terraform(working_dir='/home/test')
2016-12-20 10:53:01 +00:00
tf.fmt(diff=True)
2017-03-31 18:32:47 +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)
2017-01-03 17:02:11 +00:00
## default values
for apply/plan/destroy command, assign with following default value to make
caller easier in python
2017-01-04 06:34:00 +00:00
2017-01-03 17:02:11 +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
2017-01-03 15:09:23 +00:00
## Implementation
IMHO, how terraform design boolean options is confusing.
Take `input=True` and `-no-color` option of `apply` command for example,
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.
Therefore I end-up with using `IsFlagged` or `IsNotFlagged` as value of option
2017-01-03 17:02:11 +00:00
like `-no-color` and `True/False` value reserved for option like `refresh=true`
2017-01-03 15:09:23 +00:00
2016-11-19 10:24:33 +00:00
2017-03-31 18:32:47 +00:00