diff --git a/README.md b/README.md index 68a6e7a..5a7a0f7 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,25 @@ python-terraform is a python module provide a wrapper of `terraform` command lin pip install python-terraform ## Usage -####For any terraform command +#### For any terraform command from python_terraform import Terraform t = Terraform() return_code, stdout, stderr = t.(*arguments, **options) + +**Note**: method name same as reserved keyword like `import` could be called directly by using the following + + from python_terraform import Terraform + t = Terraform() + return_code, stdout, stderr = t._method(*arguments, **options) + +or just call cmd method directly + + from python_terraform import Terraform + t = Terraform() + return_code, stdout, stderr = t.cmd(, *arguments, **options) -####For any argument +#### For any argument simply pass the string to arguments of the method, for example, terraform apply target_dir @@ -24,7 +36,7 @@ simply pass the string to arguments of the method, for example, terraform import aws_instance.foo i-abcd1234 --> .import('aws_instance.foo', 'i-abcd1234') -####For any options +#### For any options * dash to underscore diff --git a/test/test_terraform.py b/test/test_terraform.py index eacb781..961c155 100644 --- a/test/test_terraform.py +++ b/test/test_terraform.py @@ -6,6 +6,8 @@ import re import shutil logging.basicConfig(level=logging.DEBUG) +ch = logging.StreamHandler(sys.stdout) +logging.getLogger().addHandler(ch) current_path = os.path.dirname(os.path.realpath(__file__)) STRING_CASES = [ @@ -23,11 +25,18 @@ STRING_CASES = [ ] CMD_CASES = [ - ['method', 'expected_output'], + ['method', 'expected_output', 'expected_ret_code'], [ [ 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), + 'Import complete!', + 1 ] ] ] @@ -73,11 +82,11 @@ class TestTerraform(object): assert s in result @pytest.mark.parametrize(*CMD_CASES) - def test_cmd(self, method, expected_output): + def test_cmd(self, method, expected_output, expected_ret_code): tf = Terraform(working_dir=current_path) ret, out, err = method(tf) assert expected_output in out - assert ret == 0 + assert expected_ret_code == ret @pytest.mark.parametrize( ("folder", "variables", "var_files", "expected_output", "options"), @@ -161,3 +170,8 @@ class TestTerraform(object): tf = Terraform(working_dir=current_path, variables={'test_var': 'test'}) ret, out, err = tf.fmt(diff=True) assert ret == 0 + + def test_import(self): + tf = Terraform(working_dir=current_path) + tf.cmd('import', 'aws_instance.foo', 'i-abc123') + assert False