Skip to content

Commit 8a3451c

Browse files
authored
Merge pull request #101 from fkrafi/feature/support_list_workspace
Added support for getting list of workspaces
2 parents f22dfae + 996f27b commit 8a3451c

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

python_terraform/terraform.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,25 @@ def show_workspace(self, **kwargs) -> CommandOutput:
456456
"""
457457
return self.cmd("workspace", "show", **kwargs)
458458

459+
def list_workspace(self) -> List[str]:
460+
"""List of workspaces
461+
462+
:return: workspaces
463+
:example:
464+
>>> tf = Terraform()
465+
>>> tf.list_workspace()
466+
['default', 'test']
467+
"""
468+
return list(
469+
filter(
470+
lambda workspace: len(workspace) > 0,
471+
map(
472+
lambda workspace: workspace.strip('*').strip(),
473+
(self.cmd("workspace", "list")[1] or '').split()
474+
)
475+
)
476+
)
477+
459478
def __exit__(self, exc_type, exc_value, traceback) -> None:
460479
self.temp_var_files.clean_up()
461480

test/test_terraform.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,9 @@ def test_delete_workspace_with_args(self, workspace_setup_teardown, caplog):
447447
f"Command: terraform workspace delete -force test {current_path}"
448448
in caplog.messages
449449
)
450+
451+
def test_list_workspace(self):
452+
tf = Terraform(working_dir=current_path)
453+
workspaces = tf.list_workspace()
454+
assert len(workspaces) > 0
455+
assert 'default' in workspaces

0 commit comments

Comments
 (0)