diff --git a/python_terraform/__init__.py b/python_terraform/__init__.py index 8de4c0b..6ed9362 100644 --- a/python_terraform/__init__.py +++ b/python_terraform/__init__.py @@ -108,9 +108,23 @@ class Terraform(object): """ default = kwargs default['force'] = force - option_dict = self._generate_default_options(default) + options = self._generate_default_options(default) 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): """ diff --git a/test/test_terraform.py b/test/test_terraform.py index f55323c..eacb781 100644 --- a/test/test_terraform.py +++ b/test/test_terraform.py @@ -32,7 +32,8 @@ CMD_CASES = [ ] ] -@pytest.fixture() + +@pytest.fixture(scope='function') def fmt_test_file(request): target = os.path.join(current_path, 'bad_fmt', 'test.backup') orgin = os.path.join(current_path, 'bad_fmt', 'test.tf') @@ -131,6 +132,8 @@ class TestTerraform(object): no_color=IsNotFlagged) out = out.replace('\n', '') assert '\x1b[0m\x1b[1m\x1b[32mApply' in out + out = tf.output('test_output') + assert 'test2' in out def test_get_output(self): tf = Terraform(working_dir=current_path, variables={'test_var': 'test'}) @@ -143,7 +146,18 @@ class TestTerraform(object): assert ret == 0 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'}) ret, out, err = tf.fmt(diff=True) assert ret == 0 diff --git a/test/vars_require_input/main.tf b/test/vars_require_input/main.tf new file mode 100644 index 0000000..43e8e65 --- /dev/null +++ b/test/vars_require_input/main.tf @@ -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))}" +} \ No newline at end of file