Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: support lastest terraform, v1.1.0 #117

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 47 additions & 6 deletions python_terraform/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,37 @@ def apply(
default = kwargs.copy()
default["input"] = input
default["no_color"] = no_color
default["auto-approve"] = True # a False value will require an input
# default["auto-approve"] = True # a False value will require an input
default["auto-approve"] = IsFlagged # a False value will require an input
option_dict = self._generate_default_options(default)
args = self._generate_default_args(dir_or_plan)
return self.cmd("apply", *args, **option_dict)

def show(
self,
dir_or_plan: Optional[str] = None,
no_color: Type[TerraformFlag] = IsFlagged,
**kwargs,
) -> CommandOutput:
"""Refer to https://terraform.io/docs/commands/apply.html

no-color is flagged by default
:param no_color: disable color of stdout
:param input: disable prompt for a missing variable
:param dir_or_plan: folder relative to working folder
:param skip_plan: force apply without plan (default: false)
:param kwargs: same as kwags in method 'cmd'
:returns return_code, stdout, stderr
"""
kwargs["json"] = IsFlagged
option_dict = {
"no_color": no_color,
"json": IsFlagged,
**kwargs
}
args = self._generate_default_args(dir_or_plan)
return self.cmd("show", *args, **option_dict)

def _generate_default_args(self, dir_or_plan: Optional[str]) -> Sequence[str]:
return [dir_or_plan] if dir_or_plan else []

Expand Down Expand Up @@ -152,7 +178,8 @@ def destroy(
:return: ret_code, stdout, stderr
"""
default = kwargs.copy()
default["force"] = force
default["auto-approve"] = force
# default["auto-approve"] = IsFlagged
options = self._generate_default_options(default)
args = self._generate_default_args(dir_or_plan)
return self.cmd("destroy", *args, **options)
Expand Down Expand Up @@ -346,6 +373,8 @@ def cmd(

out, err = p.communicate()
ret_code = p.returncode
if out and type(out) is not str:
out = out.decode()
logger.info("output: %s", out)

if ret_code == 0:
Expand All @@ -355,8 +384,10 @@ def cmd(

self.temp_var_files.clean_up()
if capture_output is True:
out = out.decode()
err = err.decode()
if out and type(out) is not str:
out = out.decode()
if err and type(err) is not str:
err = err.decode()
else:
out = None
err = None
Expand Down Expand Up @@ -449,12 +480,22 @@ def delete_workspace(self, workspace, *args, **kwargs) -> CommandOutput:
"""
return self.cmd("workspace", "delete", workspace, *args, **kwargs)

def show_workspace(self, **kwargs) -> CommandOutput:
def show_workspace(self, raw_out=True, **kwargs) -> CommandOutput:
"""Show workspace, this command does not need the [DIR] part

:return: workspace
"""
ret, out, err = self.cmd("workspace", "show", **kwargs)
if not raw_out and out:
out = out.strip()
return ret, out, err

def list_workspace(self, **kwargs) -> CommandOutput:
"""Show workspace, this command does not need the [DIR] part

:return: workspace
"""
return self.cmd("workspace", "show", **kwargs)
return self.cmd("workspace", "list", **kwargs)

def __exit__(self, exc_type, exc_value, traceback) -> None:
self.temp_var_files.clean_up()
Expand Down