python-terraform-3 Refactor readme and how default value being passed

1. refactor default values
2. add plan method
This commit is contained in:
beelit94 2017-01-04 13:21:30 +08:00
parent 29d391664a
commit 6fc11b313f
3 changed files with 52 additions and 4 deletions

View file

@ -108,9 +108,23 @@ class Terraform(object):
""" """
default = kwargs default = kwargs
default['force'] = force default['force'] = force
option_dict = self._generate_default_options(default) options = self._generate_default_options(default)
args = self._generate_default_args(dir_or_plan) args = self._generate_default_args(dir_or_plan)
return self.cmd('destroy', *args, **option_dict) return self.cmd('destroy', *args, **options)
def plan(self, dir_or_plan=None, detailed_exitcode=IsFlagged, **kwargs):
"""
refert to https://www.terraform.io/docs/commands/plan.html
:param detailed_exitcode: Return a detailed exit code when the command exits.
:param dir_or_plan: relative path to plan/folder
:param kwargs: options
:return: ret_code, stdout, stderr
"""
options = kwargs
options['detailed_exitcode'] = detailed_exitcode
options = self._generate_default_options(options)
args = self._generate_default_args(dir_or_plan)
return self.cmd('plan', *args, **options)
def generate_cmd_string(self, cmd, *args, **kwargs): def generate_cmd_string(self, cmd, *args, **kwargs):
""" """

View file

@ -32,7 +32,8 @@ CMD_CASES = [
] ]
] ]
@pytest.fixture()
@pytest.fixture(scope='function')
def fmt_test_file(request): def fmt_test_file(request):
target = os.path.join(current_path, 'bad_fmt', 'test.backup') target = os.path.join(current_path, 'bad_fmt', 'test.backup')
orgin = os.path.join(current_path, 'bad_fmt', 'test.tf') orgin = os.path.join(current_path, 'bad_fmt', 'test.tf')
@ -131,6 +132,8 @@ class TestTerraform(object):
no_color=IsNotFlagged) no_color=IsNotFlagged)
out = out.replace('\n', '') out = out.replace('\n', '')
assert '\x1b[0m\x1b[1m\x1b[32mApply' in out assert '\x1b[0m\x1b[1m\x1b[32mApply' in out
out = tf.output('test_output')
assert 'test2' in out
def test_get_output(self): def test_get_output(self):
tf = Terraform(working_dir=current_path, variables={'test_var': 'test'}) tf = Terraform(working_dir=current_path, variables={'test_var': 'test'})
@ -143,7 +146,18 @@ class TestTerraform(object):
assert ret == 0 assert ret == 0
assert 'Destroy complete! Resources: 0 destroyed.' in out assert 'Destroy complete! Resources: 0 destroyed.' in out
def test_fmt(self): @pytest.mark.parametrize(
("plan", "variables", "expected_ret"),
[
('vars_require_input', {}, 1)
]
)
def test_plan(self, plan, variables, expected_ret):
tf = Terraform(working_dir=current_path, variables=variables)
ret, out, err = tf.plan(plan)
assert ret == expected_ret
def test_fmt(self, fmt_test_file):
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

View file

@ -0,0 +1,20 @@
variable "ami" {
default = "foo"
type = "string"
}
variable "list" {
default = []
type = "list"
}
variable "map" {
default = {}
type = "map"
}
resource "aws_instance" "bar" {
foo = "${var.ami}"
bar = "${join(",", var.list)}"
baz = "${join(",", keys(var.map))}"
}