Skip to content

Commit

Permalink
terraform: add functionality to init action (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
ITProKyle authored Jul 13, 2021
1 parent f315eba commit 8aca76d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 35 deletions.
2 changes: 2 additions & 0 deletions runway/_cli/commands/_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def deploy(ctx: click.Context, debug: bool, tags: Tuple[str, ...], **_: Any) ->
"""Deploy infrastructure as code.
\b
Process
-------
1. Determines the deploy environment.
- "-e, --deploy-environment" option
- "DEPLOY_ENVIRONMENT" environment variable
Expand Down
2 changes: 2 additions & 0 deletions runway/_cli/commands/_destroy.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def destroy(ctx: click.Context, debug: bool, tags: Tuple[str, ...], **_: Any) ->
"""Destroy infrastructure as code.
\b
Process
-------
1. Determines the deploy environment.
- "-e, --deploy-environment" option
- "DEPLOY_ENVIRONMENT" environment variable
Expand Down
11 changes: 11 additions & 0 deletions runway/_cli/commands/_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def init(ctx: click.Context, debug: bool, tags: Tuple[str, ...], **_: Any) -> No
"""Run initialization/bootstrap steps.
\b
Process
-------
1. Determines the deploy environment.
- "-e, --deploy-environment" option
- "DEPLOY_ENVIRONMENT" environment variable
Expand All @@ -40,6 +42,15 @@ def init(ctx: click.Context, debug: bool, tags: Tuple[str, ...], **_: Any) -> No
3. Initializes/bootstraps selected deployments/modules in the order defined.
(e.g. "cdk bootstrap", "terraform init")
\b
Steps By Module Type
--------------------
- AWS CDK: Runs "cdk bootstrap".
- CFNgin: Creates the "cfngin_bucket" if needed.
- Terraform: Runs "terraform init", changes the workspace if needed, runs
"terraform init" again if the workspace was changed, and finally
downloads/updates Terraform modules.
""" # noqa: D301
try:
Runway(ctx.obj.runway_config, ctx.obj.get_runway_context()).init(
Expand Down
2 changes: 2 additions & 0 deletions runway/_cli/commands/_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def plan(ctx: click.Context, debug: bool, tags: Tuple[str, ...], **_: Any) -> No
"""Determine what infrastructure changes will occur during the next deploy.
\b
Process
-------
1. Determines the deploy environment.
- "-e, --deploy-environment" option
- "DEPLOY_ENVIRONMENT" environment variable
Expand Down
41 changes: 21 additions & 20 deletions runway/module/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,14 @@ def cleanup_dot_terraform(self) -> None:
self.logger.debug("removing: %s", child)
send2trash(str(child)) # does not support Path objects

def deploy(self) -> None:
"""Run Terraform apply."""
self.run("apply")

def destroy(self) -> None:
"""Run Terraform destroy."""
self.run("destroy")

def gen_command(
self,
command: Union[List[str], str, Tuple[str, ...]],
Expand Down Expand Up @@ -337,6 +345,14 @@ def handle_parameters(self) -> None:
self.ctx.env.vars, self.parameters
)

def init(self) -> None:
"""Run init."""
self.run("init")

def plan(self) -> None:
"""Run Terraform plan."""
self.run("plan")

def terraform_apply(self) -> None:
"""Execute ``terraform apply`` command.
Expand Down Expand Up @@ -543,31 +559,16 @@ def run(self, action: TerraformActionTypeDef) -> None:
self.terraform_workspace_new(self.required_workspace)
self.logger.verbose("re-running init after workspace change...")
self.terraform_init()
self.logger.info("init (complete)")
self.terraform_get()
self.logger.info("%s (in progress)", action)
self["terraform_" + action]()
self.logger.info("%s (complete)", action)
self.logger.info("init (complete)")
if action != "init":
self.logger.info("%s (in progress)", action)
self["terraform_" + action]()
self.logger.info("%s (complete)", action)
finally:
if self.auto_tfvars.exists():
self.auto_tfvars.unlink()

def deploy(self) -> None:
"""Run Terraform apply."""
self.run("apply")

def destroy(self) -> None:
"""Run Terraform destroy."""
self.run("destroy")

def init(self) -> None:
"""Run init."""
LOGGER.warning("init not currently supported for %s", self.__class__.__name__)

def plan(self) -> None:
"""Run Terraform plan."""
self.run("plan")


class TerraformOptions(ModuleOptions):
"""Module options for Terraform.
Expand Down
16 changes: 1 addition & 15 deletions tests/unit/module/test_terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def test_env_file(
else:
assert not obj.env_file

@pytest.mark.parametrize("action", ["deploy", "destroy", "plan"])
@pytest.mark.parametrize("action", ["deploy", "destroy", "init", "plan"])
def test_execute(
self,
action: str,
Expand Down Expand Up @@ -476,20 +476,6 @@ def test_handle_parameters(
mock_update_envvars.assert_called_once_with(runway_context.env.vars, {})
assert obj.ctx.env.vars == {"result": "success"}

def test_init(
self,
caplog: LogCaptureFixture,
runway_context: MockRunwayContext,
tmp_path: Path,
) -> None:
"""Test init."""
caplog.set_level(logging.WARNING, logger=MODULE)
obj = Terraform(runway_context, module_root=tmp_path)
assert not obj.init()
assert (
f"init not currently supported for {Terraform.__name__}" in caplog.messages
)

@pytest.mark.parametrize(
"env, param, expected",
[
Expand Down

0 comments on commit 8aca76d

Please sign in to comment.