diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 07dac08..1c1f0f9 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.10.0 +current_version = 0.10.1 commit = True tag = False diff --git a/CHANGELOG.md b/CHANGELOG.md index e4a7b5e..f7fa6ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,14 +3,17 @@ 1. [#10] log handler error on Linux environment 1. [#11] Fix reading state file for remote state and support backend config for init command + ## [0.9.0] -### Fixed 1. [#12] Output function doesn't accept parameter 'module' 1. [#16] Handle empty space/special characters when passing string to command line options 1. Tested with terraform 0.10.0 ## [0.10.0] -### Fixed 1. [#27] No interaction for apply function 1. [#18] Return access to the subprocess so output can be handled as desired -1. [#24] Full support for output(); support for raise_on_error \ No newline at end of file +1. [#24] Full support for output(); support for raise_on_error + +## [0.10.1] +1. [#48] adding extension for temp file to adopt the change in terraform 0.12.0 +1. [#49] add workspace support \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..e69de29 diff --git a/python_terraform/__init__.py b/python_terraform/__init__.py index 4e0ff7d..709103b 100644 --- a/python_terraform/__init__.py +++ b/python_terraform/__init__.py @@ -49,7 +49,8 @@ class Terraform(object): parallelism=None, var_file=None, terraform_bin_path=None, - is_env_vars_included=True): + is_env_vars_included=True, + ): """ :param working_dir: the folder of the working folder, if not given, will be current working folder @@ -387,6 +388,41 @@ class Terraform(object): self.tfstate = Tfstate.load_file(file_path) + def set_workspace(self, workspace): + """ + set workspace + :param workspace: the desired workspace. + :return: status + """ + + return self.cmd('workspace' ,'select', workspace) + + def create_workspace(self, workspace): + """ + create workspace + :param workspace: the desired workspace. + :return: status + """ + + return self.cmd('workspace', 'new', workspace) + + def delete_workspace(self, workspace): + """ + delete workspace + :param workspace: the desired workspace. + :return: status + """ + + return self.cmd('workspace', 'delete', workspace) + + def show_workspace(self): + """ + show workspace + :return: workspace + """ + + return self.cmd('workspace', 'show') + def __exit__(self, exc_type, exc_value, traceback): self.temp_var_files.clean_up() @@ -396,7 +432,7 @@ class VariableFiles(object): self.files = [] def create(self, variables): - with tempfile.NamedTemporaryFile('w+t', delete=False) as temp: + with tempfile.NamedTemporaryFile('w+t', suffix='.tfvars.json', delete=False) as temp: log.debug('{0} is created'.format(temp.name)) self.files.append(temp) log.debug( diff --git a/setup.py b/setup.py index 81f7609..1d8c5d1 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ except IOError: setup( name=module_name, - version='0.10.0', + version='0.10.1', url='https://github.com/beelit94/python-terraform', license='MIT', author='Freddy Tan', diff --git a/test/test_terraform.py b/test/test_terraform.py index d787ae6..3b76e32 100644 --- a/test/test_terraform.py +++ b/test/test_terraform.py @@ -320,3 +320,42 @@ class TestTerraform(object): tf = Terraform(working_dir=current_path) tf.import_cmd('aws_instance.foo', 'i-abc1234', no_color=IsFlagged) assert 'command: terraform import -no-color aws_instance.foo i-abc1234' in string_logger() + + def test_create_workspace(self): + tf = Terraform(working_dir=current_path) + tf.init() + ret, out, err = tf.create_workspace('test') + tf.set_workspace('default') + tf.delete_workspace('test') + assert ret == 0 + assert err == '' + + def test_set_workspace(self): + tf = Terraform(working_dir=current_path) + tf.init() + tf.create_workspace('test') + tf.set_workspace('test') + tf.set_workspace('default') + ret, out, err = tf.delete_workspace('test') + assert ret == 0 + assert err == '' + + def test_show_workspace(self): + tf = Terraform(working_dir=current_path) + tf.init() + tf.create_workspace('test') + ret, out, err = tf.show_workspace() + tf.set_workspace('default') + tf.delete_workspace('test') + assert ret == 0 + assert err == '' + + def test_delete_workspace(self): + tf = Terraform(working_dir=current_path) + tf.init() + tf.create_workspace('test') + tf.set_workspace('default') + ret, out, err = tf.delete_workspace('test') + tf.show_workspace() + assert ret == 0 + assert err == ''