Merge branch 'release/0.9.0'
This commit is contained in:
commit
d23f85a035
7 changed files with 72 additions and 30 deletions
|
@ -1,5 +1,5 @@
|
|||
[bumpversion]
|
||||
current_version = 0.8.6
|
||||
current_version = 0.9.0
|
||||
commit = True
|
||||
tag = False
|
||||
|
||||
|
|
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -6,3 +6,5 @@
|
|||
.cache
|
||||
/.pypirc
|
||||
/.tox/
|
||||
.dropbox
|
||||
Icon
|
|
@ -5,7 +5,7 @@ python:
|
|||
- '3.6'
|
||||
before_install: sudo apt-get install unzip
|
||||
before_script:
|
||||
- export TFVER=0.9.5
|
||||
- export TFVER=0.10.0
|
||||
- export TFURL=https://releases.hashicorp.com/terraform/
|
||||
- TFURL+=$TFVER
|
||||
- TFURL+="/terraform_"
|
||||
|
|
6
CHANGELOG.md
Normal file
6
CHANGELOG.md
Normal file
|
@ -0,0 +1,6 @@
|
|||
# Changelog
|
||||
## [0.9.0]
|
||||
### Fixed
|
||||
1. [#12] Output function doesn't accept parameter 'module'
|
||||
1. [#16] Handle empty space/special characters when passing string to command line options
|
||||
1. Tested with terraform 0.10.0
|
|
@ -35,7 +35,7 @@ class Terraform(object):
|
|||
var_file=None,
|
||||
terraform_bin_path=None,
|
||||
is_env_vars_included=True):
|
||||
"""
|
||||
"""
|
||||
:param working_dir: the folder of the working folder, if not given,
|
||||
will be current working folder
|
||||
:param targets: list of target
|
||||
|
@ -49,7 +49,7 @@ class Terraform(object):
|
|||
could be string or list, list stands for multiple -var-file option
|
||||
: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
|
||||
: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
|
||||
|
@ -192,8 +192,7 @@ class Terraform(object):
|
|||
cmds += ['-{k}={v}'.format(k=k, v=v)]
|
||||
|
||||
cmds += args
|
||||
cmd = ' '.join(cmds)
|
||||
return cmd
|
||||
return cmds
|
||||
|
||||
def cmd(self, cmd, *args, **kwargs):
|
||||
"""
|
||||
|
@ -224,8 +223,8 @@ class Terraform(object):
|
|||
stderr = sys.stderr
|
||||
stdout = sys.stdout
|
||||
|
||||
cmd_string = self.generate_cmd_string(cmd, *args, **kwargs)
|
||||
log.debug('command: {c}'.format(c=cmd_string))
|
||||
cmds = self.generate_cmd_string(cmd, *args, **kwargs)
|
||||
log.debug('command: {c}'.format(c=' '.join(cmds)))
|
||||
|
||||
working_folder = self.working_dir if self.working_dir else None
|
||||
|
||||
|
@ -233,7 +232,7 @@ class Terraform(object):
|
|||
if self.is_env_vars_included:
|
||||
environ_vars = os.environ.copy()
|
||||
|
||||
p = subprocess.Popen(cmd_string, stdout=stdout, stderr=stderr, shell=True,
|
||||
p = subprocess.Popen(cmds, stdout=stdout, stderr=stderr,
|
||||
cwd=working_folder, env=environ_vars)
|
||||
out, err = p.communicate()
|
||||
ret_code = p.returncode
|
||||
|
@ -250,13 +249,15 @@ class Terraform(object):
|
|||
else:
|
||||
return ret_code, None, None
|
||||
|
||||
def output(self, name):
|
||||
def output(self, name, *args, **kwargs):
|
||||
"""
|
||||
https://www.terraform.io/docs/commands/output.html
|
||||
:param name: name of output
|
||||
:return: output value
|
||||
"""
|
||||
ret, out, err = self.cmd('output', name, json=IsFlagged)
|
||||
|
||||
ret, out, err = self.cmd(
|
||||
'output', name, json=IsFlagged, *args, **kwargs)
|
||||
|
||||
log.debug('output raw string: {0}'.format(out))
|
||||
if ret != 0:
|
||||
|
|
2
setup.py
2
setup.py
|
@ -20,7 +20,7 @@ except IOError:
|
|||
|
||||
setup(
|
||||
name=module_name,
|
||||
version='0.8.6',
|
||||
version='0.9.0',
|
||||
url='https://github.com/beelit94/python-terraform',
|
||||
license='MIT',
|
||||
author='Freddy Tan',
|
||||
|
|
|
@ -8,13 +8,13 @@ import os
|
|||
import logging
|
||||
import re
|
||||
import shutil
|
||||
import fnmatch
|
||||
|
||||
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__))
|
||||
|
||||
FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS = "test 'test.out!"
|
||||
STRING_CASES = [
|
||||
[
|
||||
lambda x: x.generate_cmd_string('apply', 'the_folder',
|
||||
|
@ -30,20 +30,30 @@ STRING_CASES = [
|
|||
]
|
||||
|
||||
CMD_CASES = [
|
||||
['method', 'expected_output', 'expected_ret_code', 'expected_logs'],
|
||||
['method', 'expected_output', 'expected_ret_code', 'expected_logs', 'folder'],
|
||||
[
|
||||
[
|
||||
lambda x: x.cmd('plan', 'var_to_output', no_color=IsFlagged, var={'test_var': 'test'}) ,
|
||||
"doesn't need to do anything",
|
||||
0,
|
||||
''
|
||||
'',
|
||||
'var_to_output'
|
||||
],
|
||||
# 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'
|
||||
'command: terraform import -no-color aws_instance.foo i-abcd1234',
|
||||
''
|
||||
],
|
||||
# test with space and special character in file path
|
||||
[
|
||||
lambda x: x.cmd('plan', 'var_to_output', out=FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS),
|
||||
'',
|
||||
0,
|
||||
'',
|
||||
'var_to_output'
|
||||
]
|
||||
]
|
||||
]
|
||||
|
@ -71,9 +81,10 @@ def string_logger(request):
|
|||
|
||||
def td():
|
||||
root_logger.removeHandler(handler)
|
||||
log_stream.close()
|
||||
|
||||
request.addfinalizer(td)
|
||||
return log_stream
|
||||
return lambda: str(log_stream.getvalue())
|
||||
|
||||
|
||||
class TestTerraform(object):
|
||||
|
@ -83,12 +94,17 @@ class TestTerraform(object):
|
|||
"""
|
||||
|
||||
def purge(dir, pattern):
|
||||
for f in os.listdir(dir):
|
||||
if re.search(pattern, f):
|
||||
if os.path.isfile(f):
|
||||
os.remove(os.path.join(dir, f))
|
||||
for root, dirnames, filenames in os.walk(dir):
|
||||
for filename in fnmatch.filter(filenames, pattern):
|
||||
f = os.path.join(root, filename)
|
||||
os.remove(f)
|
||||
for dirname in fnmatch.filter(dirnames, pattern):
|
||||
d = os.path.join(root, dirname)
|
||||
shutil.rmtree(d)
|
||||
|
||||
purge('.', '.tfstate')
|
||||
purge('.', '*.tfstate')
|
||||
purge('.', '*.terraform')
|
||||
purge('.', FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS)
|
||||
|
||||
@pytest.mark.parametrize([
|
||||
"method", "expected"
|
||||
|
@ -102,10 +118,11 @@ class TestTerraform(object):
|
|||
assert s in result
|
||||
|
||||
@pytest.mark.parametrize(*CMD_CASES)
|
||||
def test_cmd(self, method, expected_output, expected_ret_code, expected_logs, string_logger):
|
||||
def test_cmd(self, method, expected_output, expected_ret_code, expected_logs, string_logger, folder):
|
||||
tf = Terraform(working_dir=current_path)
|
||||
tf.init(folder)
|
||||
ret, out, err = method(tf)
|
||||
logs = str(string_logger.getvalue())
|
||||
logs = string_logger()
|
||||
logs = logs.replace('\n', '')
|
||||
assert expected_output in out
|
||||
assert expected_ret_code == ret
|
||||
|
@ -123,6 +140,8 @@ class TestTerraform(object):
|
|||
])
|
||||
def test_apply(self, folder, variables, var_files, expected_output, options):
|
||||
tf = Terraform(working_dir=current_path, variables=variables, var_file=var_files)
|
||||
# after 0.10.0 we always need to init
|
||||
tf.init(folder)
|
||||
ret, out, err = tf.apply(folder, **options)
|
||||
assert ret == 0
|
||||
assert expected_output in out.replace('\n', '').replace(' ', '')
|
||||
|
@ -160,6 +179,7 @@ class TestTerraform(object):
|
|||
)
|
||||
def test_override_default(self, folder, variables):
|
||||
tf = Terraform(working_dir=current_path, variables=variables)
|
||||
tf.init(folder)
|
||||
ret, out, err = tf.apply(folder, var={'test_var': 'test2'},
|
||||
no_color=IsNotFlagged)
|
||||
out = out.replace('\n', '')
|
||||
|
@ -167,13 +187,28 @@ class TestTerraform(object):
|
|||
out = tf.output('test_output')
|
||||
assert 'test2' in out
|
||||
|
||||
def test_get_output(self):
|
||||
@pytest.mark.parametrize(
|
||||
("param"),
|
||||
[
|
||||
({}),
|
||||
({'module': 'test2'}),
|
||||
]
|
||||
)
|
||||
def test_output(self, param, string_logger):
|
||||
tf = Terraform(working_dir=current_path, variables={'test_var': 'test'})
|
||||
tf.init('var_to_output')
|
||||
tf.apply('var_to_output')
|
||||
assert tf.output('test_output') == 'test'
|
||||
result = tf.output('test_output', **param)
|
||||
regex = re.compile("terraform output (-module=test2 -json|-json -module=test2) test_output")
|
||||
log_str = string_logger()
|
||||
if param:
|
||||
assert re.search(regex, log_str), log_str
|
||||
else:
|
||||
assert result == 'test'
|
||||
|
||||
def test_destroy(self):
|
||||
tf = Terraform(working_dir=current_path, variables={'test_var': 'test'})
|
||||
tf.init('var_to_output')
|
||||
ret, out, err = tf.destroy('var_to_output')
|
||||
assert ret == 0
|
||||
assert 'Destroy complete! Resources: 0 destroyed.' in out
|
||||
|
@ -197,6 +232,4 @@ class TestTerraform(object):
|
|||
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
|
||||
assert 'command: terraform import -no-color aws_instance.foo i-abc1234' in string_logger()
|
||||
|
|
Loading…
Reference in a new issue