add list workspace

This commit is contained in:
jerger 2022-08-05 18:09:17 +02:00
parent f94f3a7c43
commit 9c9d7dc14b
2 changed files with 30 additions and 4 deletions

View file

@ -437,6 +437,26 @@ class Terraform:
global_opts = self._generate_default_general_options(False) global_opts = self._generate_default_general_options(False)
return self.cmd(global_opts, "workspace", "show", **kwargs) return self.cmd(global_opts, "workspace", "show", **kwargs)
def list_workspace(self) -> List[str]:
"""List of workspaces
:return: workspaces
:example:
>>> tf = Terraform()
>>> tf.list_workspace()
['default', 'test']
"""
global_opts = self._generate_default_general_options(False)
return list(
filter(
lambda workspace: len(workspace) > 0,
map(
lambda workspace: workspace.strip('*').strip(),
(self.cmd(global_opts, "workspace", "list")[1] or '').split()
)
)
)
def _generate_default_args(self, dir_or_plan: Optional[str]) -> Sequence[str]: def _generate_default_args(self, dir_or_plan: Optional[str]) -> Sequence[str]:
if (self.terraform_version < 1.0 and dir_or_plan): if (self.terraform_version < 1.0 and dir_or_plan):
return [dir_or_plan] return [dir_or_plan]
@ -467,7 +487,7 @@ class Terraform:
""" """
""" """
result = [] result = []
for option, value in kwargs.items(): for option, value in kwargs.items():
if "_" in option: if "_" in option:
option = option.replace("_", "-") option = option.replace("_", "-")
@ -507,10 +527,10 @@ class Terraform:
result += [f"-{option}={value}"] result += [f"-{option}={value}"]
return result return result
def __exit__(self, exc_type, exc_value, traceback) -> None: def __exit__(self, exc_type, exc_value, traceback) -> None:
self.temp_var_files.clean_up() self.temp_var_files.clean_up()
def __getattr__(self, item: str) -> Callable: def __getattr__(self, item: str) -> Callable:
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
cmd_name = str(item) cmd_name = str(item)
@ -522,7 +542,7 @@ class Terraform:
return wrapper return wrapper
class VariableFiles: class VariableFiles:

View file

@ -529,3 +529,9 @@ class TestTerraform:
in caplog.messages in caplog.messages
) )
""" """
def test_list_workspace(self):
tf = Terraform(working_dir=current_path)
workspaces = tf.list_workspace()
assert len(workspaces) > 0
assert 'default' in workspaces