Merge pull request #2 from beelit94/python-terraform-1
python-terraform-1 Make option with bool value/ more sense for the ca…
This commit is contained in:
commit
81bcc77de3
7 changed files with 50 additions and 48 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -5,3 +5,4 @@
|
||||||
.idea
|
.idea
|
||||||
.cache
|
.cache
|
||||||
/.pypirc
|
/.pypirc
|
||||||
|
/.tox/
|
||||||
|
|
|
@ -17,16 +17,12 @@ before_script:
|
||||||
|
|
||||||
install:
|
install:
|
||||||
- "curl https://bootstrap.pypa.io/ez_setup.py -o - | python"
|
- "curl https://bootstrap.pypa.io/ez_setup.py -o - | python"
|
||||||
- "pip install -r test/requirements.txt"
|
- "pip install tox-travis"
|
||||||
- "sudo apt-get -y install pandoc"
|
|
||||||
- "pip install pypandoc"
|
|
||||||
- "pip install ."
|
- "pip install ."
|
||||||
# command to run tests
|
# command to run tests
|
||||||
script:
|
script:
|
||||||
- "export PATH=$PATH:$PWD/tf_bin"
|
- "export PATH=$PATH:$PWD/tf_bin"
|
||||||
- cd test
|
- tox
|
||||||
- py.test
|
|
||||||
- cd ..
|
|
||||||
|
|
||||||
deploy:
|
deploy:
|
||||||
# test pypi
|
# test pypi
|
||||||
|
|
|
@ -29,10 +29,6 @@ For any terraform command
|
||||||
if a value is None, will skip this option
|
if a value is None, will skip this option
|
||||||
:return: ret_code, out, err
|
:return: ret_code, out, err
|
||||||
|
|
||||||
For apply/destroy method, the flag options, like, `-no-color` or `-force`
|
|
||||||
have been implemented as boolean argument. simply use `is_no_color=True/False` for
|
|
||||||
apply/destroy method
|
|
||||||
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
### Have a test.tf file under folder "/home/test"
|
### Have a test.tf file under folder "/home/test"
|
||||||
|
@ -46,7 +42,7 @@ In python-terraform:
|
||||||
|
|
||||||
from python_terraform import Terraform
|
from python_terraform import Terraform
|
||||||
tf = terraform(working_dir='/home/test')
|
tf = terraform(working_dir='/home/test')
|
||||||
tf.apply(is_no_color=True, refresh=False, var={'a':'b', 'c':'d'})
|
tf.apply(no_color=IsFlagged, refresh=False, var={'a':'b', 'c':'d'})
|
||||||
#### taint command, allow-missing and no color
|
#### taint command, allow-missing and no color
|
||||||
In shell:
|
In shell:
|
||||||
|
|
||||||
|
|
|
@ -53,57 +53,40 @@ class Terraform:
|
||||||
# store the tfstate data
|
# store the tfstate data
|
||||||
self.tfstate = dict()
|
self.tfstate = dict()
|
||||||
|
|
||||||
def apply(self,
|
def apply(self, dir_or_plan=None, **kwargs):
|
||||||
dir=None,
|
|
||||||
is_no_color=True,
|
|
||||||
is_input=False,
|
|
||||||
**kwargs):
|
|
||||||
"""
|
"""
|
||||||
refer to https://terraform.io/docs/commands/apply.html
|
refer to https://terraform.io/docs/commands/apply.html
|
||||||
:raise RuntimeError when return code is not zero
|
no-color is flagged by default
|
||||||
:param is_no_color: if True, add flag -no-color
|
:param dir_or_plan: folder relative to working folder
|
||||||
:param is_input: if True, add option -input=true
|
|
||||||
:param dir: folder relative to working folder
|
|
||||||
:param kwargs: same as kwags in method 'cmd'
|
:param kwargs: same as kwags in method 'cmd'
|
||||||
:returns return_code, stdout, stderr
|
:returns return_code, stdout, stderr
|
||||||
"""
|
"""
|
||||||
|
default = dict()
|
||||||
args, option_dict = self._create_cmd_args(is_input,
|
args, option_dict = self._create_cmd_args(dir_or_plan, default, kwargs)
|
||||||
is_no_color,
|
|
||||||
dir,
|
|
||||||
kwargs)
|
|
||||||
|
|
||||||
return self.cmd('apply', *args, **option_dict)
|
return self.cmd('apply', *args, **option_dict)
|
||||||
|
|
||||||
def _create_cmd_args(self, is_input, is_no_color, dir, kwargs):
|
def _create_cmd_args(self, dir_or_plan, default_dict, kwargs):
|
||||||
option_dict = dict()
|
option_dict = default_dict
|
||||||
option_dict['state'] = self.state
|
option_dict['state'] = self.state
|
||||||
option_dict['target'] = self.targets
|
option_dict['target'] = self.targets
|
||||||
option_dict['var'] = self.variables
|
option_dict['var'] = self.variables
|
||||||
option_dict['var_file'] = self.var_file
|
option_dict['var_file'] = self.var_file
|
||||||
option_dict['parallelism'] = self.parallelism
|
option_dict['parallelism'] = self.parallelism
|
||||||
if is_no_color:
|
option_dict['no_color'] = IsFlagged
|
||||||
option_dict['no_color'] = IsFlagged
|
option_dict['input'] = True
|
||||||
option_dict['input'] = is_input
|
|
||||||
option_dict.update(kwargs)
|
option_dict.update(kwargs)
|
||||||
args = [dir] if dir else []
|
args = [dir_or_plan] if dir_or_plan else []
|
||||||
return args, option_dict
|
return args, option_dict
|
||||||
|
|
||||||
def destroy(self, working_dir=None, is_force=True,
|
def destroy(self, dir_or_plan=None, **kwargs):
|
||||||
is_no_color=True, is_input=False, **kwargs):
|
|
||||||
"""
|
"""
|
||||||
refer to https://www.terraform.io/docs/commands/destroy.html
|
refer to https://www.terraform.io/docs/commands/destroy.html
|
||||||
:raise RuntimeError when return code is not zero
|
force/no-color option is flagged by default
|
||||||
:return: ret_code, stdout, stderr
|
:return: ret_code, stdout, stderr
|
||||||
"""
|
"""
|
||||||
|
default = {'force': IsFlagged}
|
||||||
args, option_dict = self._create_cmd_args(is_input,
|
args, option_dict = self._create_cmd_args(dir_or_plan, default, kwargs)
|
||||||
is_no_color,
|
|
||||||
working_dir,
|
|
||||||
kwargs)
|
|
||||||
if is_force:
|
|
||||||
option_dict['force'] = IsFlagged
|
|
||||||
|
|
||||||
return self.cmd('destroy', *args, **option_dict)
|
return self.cmd('destroy', *args, **option_dict)
|
||||||
|
|
||||||
def generate_cmd_string(self, cmd, *args, **kwargs):
|
def generate_cmd_string(self, cmd, *args, **kwargs):
|
||||||
|
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
tox-pyenv
|
||||||
|
pytest
|
||||||
|
tox
|
|
@ -5,7 +5,7 @@ import logging
|
||||||
import re
|
import re
|
||||||
|
|
||||||
logging.basicConfig(level=logging.WARN)
|
logging.basicConfig(level=logging.WARN)
|
||||||
|
current_path = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
||||||
STRING_CASES = [
|
STRING_CASES = [
|
||||||
[
|
[
|
||||||
|
@ -61,28 +61,40 @@ class TestTerraform:
|
||||||
|
|
||||||
@pytest.mark.parametrize(*CMD_CASES)
|
@pytest.mark.parametrize(*CMD_CASES)
|
||||||
def test_cmd(self, method, expected_output):
|
def test_cmd(self, method, expected_output):
|
||||||
tf = Terraform()
|
tf = Terraform(working_dir=current_path)
|
||||||
ret, out, err = method(tf)
|
ret, out, err = method(tf)
|
||||||
assert expected_output in out
|
assert expected_output in out
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
||||||
def test_state_data(self):
|
def test_state_data(self):
|
||||||
tf = Terraform(working_dir='test_tfstate_file', state='tfstate.test')
|
cwd = os.path.join(current_path, 'test_tfstate_file')
|
||||||
|
tf = Terraform(working_dir=cwd, state='tfstate.test')
|
||||||
tf.read_state_file()
|
tf.read_state_file()
|
||||||
assert tf.tfstate.modules[0]['path'] == ['root']
|
assert tf.tfstate.modules[0]['path'] == ['root']
|
||||||
|
|
||||||
def test_apply(self):
|
def test_apply(self):
|
||||||
tf = Terraform(working_dir='apply_tf', variables={'test_var': 'test'})
|
cwd = os.path.join(current_path, 'apply_tf')
|
||||||
|
tf = Terraform(working_dir=cwd, variables={'test_var': 'test'})
|
||||||
ret, out, err = tf.apply(var={'test_var': 'test2'})
|
ret, out, err = tf.apply(var={'test_var': 'test2'})
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
||||||
|
def test_override_no_color(self):
|
||||||
|
cwd = os.path.join(current_path, 'apply_tf')
|
||||||
|
tf = Terraform(working_dir=cwd, variables={'test_var': 'test'})
|
||||||
|
ret, out, err = tf.apply(var={'test_var': 'test2'},
|
||||||
|
no_color=IsNotFlagged)
|
||||||
|
out = out.replace('\n', '')
|
||||||
|
assert '\x1b[0m\x1b[1m\x1b[32mApply' in out
|
||||||
|
|
||||||
def test_get_output(self):
|
def test_get_output(self):
|
||||||
tf = Terraform(working_dir='apply_tf', variables={'test_var': 'test'})
|
cwd = os.path.join(current_path, 'apply_tf')
|
||||||
|
tf = Terraform(working_dir=cwd, variables={'test_var': 'test'})
|
||||||
tf.apply()
|
tf.apply()
|
||||||
assert tf.output('test_output') == 'test'
|
assert tf.output('test_output') == 'test'
|
||||||
|
|
||||||
def test_destroy(self):
|
def test_destroy(self):
|
||||||
tf = Terraform(working_dir='apply_tf', variables={'test_var': 'test'})
|
cwd = os.path.join(current_path, 'apply_tf')
|
||||||
|
tf = Terraform(working_dir=cwd, variables={'test_var': 'test'})
|
||||||
ret, out, err = tf.destroy()
|
ret, out, err = tf.destroy()
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
assert 'Destroy complete! Resources: 0 destroyed.' in out
|
assert 'Destroy complete! Resources: 0 destroyed.' in out
|
||||||
|
|
11
tox.ini
Normal file
11
tox.ini
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
# content of: tox.ini , put in same dir as setup.py
|
||||||
|
[tox]
|
||||||
|
envlist = py27, py35
|
||||||
|
[testenv]
|
||||||
|
deps=pytest
|
||||||
|
commands=py.test test
|
||||||
|
|
||||||
|
[travis]
|
||||||
|
python =
|
||||||
|
2.7: py27
|
||||||
|
3.5: py35
|
Loading…
Reference in a new issue