From 4ebdc607dc014408e2f2495cc5edb113d4a553e7 Mon Sep 17 00:00:00 2001 From: Freddy Tan Date: Thu, 31 Dec 2015 15:15:51 +0800 Subject: [PATCH] update --- .gitignore | 1 + terraform/__init__.py | 8 +++++--- test/__init__.py | 0 test/test_terraform.py | 20 ++++++++++++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 test/__init__.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7e99e36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc \ No newline at end of file diff --git a/terraform/__init__.py b/terraform/__init__.py index 6eeb54e..7e534d2 100644 --- a/terraform/__init__.py +++ b/terraform/__init__.py @@ -9,8 +9,9 @@ log = logging.getLogger(__name__) class Terraform: def __init__(self, targets=None, state='terraform.tfstate', variables=None): self.targets = [] if targets is None else targets - self.state = state self.variables = dict() if variables is None else variables + + self.state = state self.state_data = None self.parallelism = 50 @@ -33,11 +34,13 @@ class Terraform: def _run_cmd(self, cmd): log.debug('command: ' + cmd) + p = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) out, err = p.communicate() ret_code = p.returncode log.debug('output: ' + out) + if ret_code == 0: log.debug('error: ' + err) self.read_state() @@ -50,8 +53,7 @@ class Terraform: parameters = [] parameters += self._generate_targets(targets) parameters += self._generate_var_string(variables) - # hard code 19 for splunk aws limit on us-west-2 region, - # todo move parallelism? + parameters = \ ['terraform', 'destroy', '-force', '-state=%s' % self.state] + \ parameters diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test_terraform.py b/test/test_terraform.py index e69de29..00c4e02 100644 --- a/test/test_terraform.py +++ b/test/test_terraform.py @@ -0,0 +1,20 @@ +from terraform import Terraform + + +class TestTerraform: + def test_apply(self): + tf = Terraform() + + tf.apply() + + def test_refresh(self): + tf = Terraform() + + tf.refresh() + + def test_destroy(self): + tf = Terraform() + + tf.destroy() + + \ No newline at end of file