Merge branch 'release/0.8.5'

This commit is contained in:
beelit94 2017-05-12 01:30:17 +08:00
commit a57a001d70
8 changed files with 94 additions and 29 deletions

View file

@ -1,5 +1,5 @@
[bumpversion] [bumpversion]
current_version = 0.8.4 current_version = 0.8.5
commit = True commit = True
tag = False tag = False

View file

@ -2,9 +2,10 @@ language: python
python: python:
- '2.7' - '2.7'
- '3.5' - '3.5'
- '3.6'
before_install: sudo apt-get install unzip before_install: sudo apt-get install unzip
before_script: before_script:
- export TFVER=0.8.1 - export TFVER=0.9.5
- export TFURL=https://releases.hashicorp.com/terraform/ - export TFURL=https://releases.hashicorp.com/terraform/
- TFURL+=$TFVER - TFURL+=$TFVER
- TFURL+="/terraform_" - TFURL+="/terraform_"

View file

@ -10,13 +10,27 @@ python-terraform is a python module provide a wrapper of `terraform` command lin
pip install python-terraform pip install python-terraform
## Usage ## Usage
####For any terraform command #### For any terraform command
from python_terraform import Terraform from python_terraform import Terraform
t = Terraform() t = Terraform()
return_code, stdout, stderr = t.<cmd_name>(*arguments, **options) return_code, stdout, stderr = t.<cmd_name>(*arguments, **options)
**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
from python_terraform import Terraform
t = Terraform()
return_code, stdout, stderr = t.import_cmd(*arguments, **options)
or just call cmd method directly
from python_terraform import Terraform
t = Terraform()
return_code, stdout, stderr = t.cmd(<cmd_name>, *arguments, **options)
####For any argument #### For any argument
simply pass the string to arguments of the method, for example, simply pass the string to arguments of the method, for example,
terraform apply target_dir terraform apply target_dir
@ -24,7 +38,7 @@ simply pass the string to arguments of the method, for example,
terraform import aws_instance.foo i-abcd1234 terraform import aws_instance.foo i-abcd1234
--> <instance>.import('aws_instance.foo', 'i-abcd1234') --> <instance>.import('aws_instance.foo', 'i-abcd1234')
####For any options #### For any options
* dash to underscore * dash to underscore
@ -60,6 +74,15 @@ simply pass the string to arguments of the method, for example,
--> tf.apply(var={'a':'b', 'c':'d'}) --> tf.apply(var={'a':'b', 'c':'d'})
* if an option with None as value, it won't be used * if an option with None as value, it won't be used
#### 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 ## Examples
### Have a test.tf file under folder "/home/test" ### Have a test.tf file under folder "/home/test"
#### 1. apply with variables a=b, c=d, refresh=false, no color in the output #### 1. apply with variables a=b, c=d, refresh=false, no color in the output
@ -98,15 +121,6 @@ In python-terraform:
tf = terraform(working_dir='/home/test') tf = terraform(working_dir='/home/test')
tf.fmt(diff=True) tf.fmt(diff=True)
# 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)
## default values ## default values
for apply/plan/destroy command, assign with following default value to make for apply/plan/destroy command, assign with following default value to make

View file

@ -33,8 +33,9 @@ class Terraform(object):
variables=None, variables=None,
parallelism=None, parallelism=None,
var_file=None, var_file=None,
terraform_bin_path=None): terraform_bin_path=None,
""" is_env_vars_included=True):
"""
:param working_dir: the folder of the working folder, if not given, :param working_dir: the folder of the working folder, if not given,
will be current working folder will be current working folder
:param targets: list of target :param targets: list of target
@ -47,7 +48,10 @@ class Terraform(object):
:param var_file: passed as value of -var-file option, :param var_file: passed as value of -var-file option,
could be string or list, list stands for multiple -var-file option could be string or list, list stands for multiple -var-file option
:param terraform_bin_path: binary path of terraform :param terraform_bin_path: binary path of terraform
:type is_env_vars_included: bool
:param is_env_vars_included: included env variables when calling terraform cmd
""" """
self.is_env_vars_included = is_env_vars_included
self.working_dir = working_dir self.working_dir = working_dir
self.state = state self.state = state
self.targets = [] if targets is None else targets self.targets = [] if targets is None else targets
@ -64,8 +68,11 @@ class Terraform(object):
def __getattr__(self, item): def __getattr__(self, item):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
cmd_name = str(item)
if cmd_name.endswith('_cmd'):
cmd_name = cmd_name[:-4]
logging.debug('called with %r and %r' % (args, kwargs)) logging.debug('called with %r and %r' % (args, kwargs))
return self.cmd(item, *args, **kwargs) return self.cmd(cmd_name, *args, **kwargs)
return wrapper return wrapper
@ -222,9 +229,12 @@ class Terraform(object):
working_folder = self.working_dir if self.working_dir else None working_folder = self.working_dir if self.working_dir else None
p = subprocess.Popen(cmd_string, stdout=stdout, environ_vars = {}
stderr=stderr, shell=True, if self.is_env_vars_included:
cwd=working_folder) environ_vars = os.environ.copy()
p = subprocess.Popen(cmd_string, stdout=stdout, stderr=stderr, shell=True,
cwd=working_folder, env=environ_vars)
out, err = p.communicate() out, err = p.communicate()
ret_code = p.returncode ret_code = p.returncode
log.debug('output: {o}'.format(o=out)) log.debug('output: {o}'.format(o=out))

View file

@ -20,7 +20,7 @@ except IOError:
setup( setup(
name=module_name, name=module_name,
version='0.8.4', version='0.8.5',
url='https://github.com/beelit94/python-terraform', url='https://github.com/beelit94/python-terraform',
license='MIT', license='MIT',
author='Freddy Tan', author='Freddy Tan',

View file

@ -1,3 +1,7 @@
try:
from cStringIO import StringIO # Python 2
except ImportError:
from io import StringIO
from python_terraform import * from python_terraform import *
import pytest import pytest
import os import os
@ -6,6 +10,9 @@ import re
import shutil import shutil
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
root_logger = logging.getLogger()
# ch = logging.StreamHandler(sys.stdout)
# root_logger.addHandler(ch)
current_path = os.path.dirname(os.path.realpath(__file__)) current_path = os.path.dirname(os.path.realpath(__file__))
STRING_CASES = [ STRING_CASES = [
@ -23,11 +30,20 @@ STRING_CASES = [
] ]
CMD_CASES = [ CMD_CASES = [
['method', 'expected_output'], ['method', 'expected_output', 'expected_ret_code', 'expected_logs'],
[ [
[ [
lambda x: x.cmd('plan', 'var_to_output', no_color=IsFlagged, var={'test_var': 'test'}) , lambda x: x.cmd('plan', 'var_to_output', no_color=IsFlagged, var={'test_var': 'test'}) ,
"doesn't need to do anything" "doesn't need to do anything",
0,
''
],
# try import aws instance
[
lambda x: x.cmd('import', 'aws_instance.foo', 'i-abcd1234', no_color=IsFlagged),
'',
1,
'command: terraform import -no-color aws_instance.foo i-abcd1234'
] ]
] ]
] ]
@ -47,6 +63,19 @@ def fmt_test_file(request):
return return
@pytest.fixture()
def string_logger(request):
log_stream = StringIO()
handler = logging.StreamHandler(log_stream)
root_logger.addHandler(handler)
def td():
root_logger.removeHandler(handler)
request.addfinalizer(td)
return log_stream
class TestTerraform(object): class TestTerraform(object):
def teardown_method(self, method): def teardown_method(self, method):
""" teardown any state that was previously setup with a setup_method """ teardown any state that was previously setup with a setup_method
@ -73,11 +102,14 @@ class TestTerraform(object):
assert s in result assert s in result
@pytest.mark.parametrize(*CMD_CASES) @pytest.mark.parametrize(*CMD_CASES)
def test_cmd(self, method, expected_output): def test_cmd(self, method, expected_output, expected_ret_code, expected_logs, string_logger):
tf = Terraform(working_dir=current_path) tf = Terraform(working_dir=current_path)
ret, out, err = method(tf) ret, out, err = method(tf)
logs = str(string_logger.getvalue())
logs = logs.replace('\n', '')
assert expected_output in out assert expected_output in out
assert ret == 0 assert expected_ret_code == ret
assert expected_logs in logs
@pytest.mark.parametrize( @pytest.mark.parametrize(
("folder", "variables", "var_files", "expected_output", "options"), ("folder", "variables", "var_files", "expected_output", "options"),
@ -161,3 +193,10 @@ class TestTerraform(object):
tf = Terraform(working_dir=current_path, variables={'test_var': 'test'}) tf = Terraform(working_dir=current_path, variables={'test_var': 'test'})
ret, out, err = tf.fmt(diff=True) ret, out, err = tf.fmt(diff=True)
assert ret == 0 assert ret == 0
def test_import(self, string_logger):
tf = Terraform(working_dir=current_path)
tf.import_cmd('aws_instance.foo', 'i-abc1234', no_color=IsFlagged)
logs = string_logger.getvalue()
print(logs)
assert 'command: terraform import -no-color aws_instance.foo i-abc1234' in logs

View file

@ -10,11 +10,11 @@ variable "list" {
variable "map" { variable "map" {
default = {} default = {}
type = "map" type = "map"
} }
resource "aws_instance" "bar" { resource "aws_instance" "bar" {
foo = "${var.ami}" foo = "${var.ami}"
bar = "${join(",", var.list)}" bar = "${join(",", var.list)}"
baz = "${join(",", keys(var.map))}" baz = "${join(",", keys(var.map))}"
} }

View file

@ -1,6 +1,6 @@
# content of: tox.ini , put in same dir as setup.py # content of: tox.ini , put in same dir as setup.py
[tox] [tox]
envlist = py27, py35 envlist = py27, py35, py36
[testenv] [testenv]
deps=pytest deps=pytest
commands=py.test test commands=py.test test
@ -8,4 +8,5 @@ commands=py.test test
[travis] [travis]
python = python =
2.7: py27 2.7: py27
3.5: py35 3.5: py35
3.6: py36