Skip to content

Commit 8aca76d

Browse files
authored
terraform: add functionality to init action (#772)
1 parent f315eba commit 8aca76d

File tree

6 files changed

+39
-35
lines changed

6 files changed

+39
-35
lines changed

runway/_cli/commands/_deploy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def deploy(ctx: click.Context, debug: bool, tags: Tuple[str, ...], **_: Any) ->
2626
"""Deploy infrastructure as code.
2727
2828
\b
29+
Process
30+
-------
2931
1. Determines the deploy environment.
3032
- "-e, --deploy-environment" option
3133
- "DEPLOY_ENVIRONMENT" environment variable

runway/_cli/commands/_destroy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def destroy(ctx: click.Context, debug: bool, tags: Tuple[str, ...], **_: Any) ->
2626
"""Destroy infrastructure as code.
2727
2828
\b
29+
Process
30+
-------
2931
1. Determines the deploy environment.
3032
- "-e, --deploy-environment" option
3133
- "DEPLOY_ENVIRONMENT" environment variable

runway/_cli/commands/_init.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def init(ctx: click.Context, debug: bool, tags: Tuple[str, ...], **_: Any) -> No
2626
"""Run initialization/bootstrap steps.
2727
2828
\b
29+
Process
30+
-------
2931
1. Determines the deploy environment.
3032
- "-e, --deploy-environment" option
3133
- "DEPLOY_ENVIRONMENT" environment variable
@@ -40,6 +42,15 @@ def init(ctx: click.Context, debug: bool, tags: Tuple[str, ...], **_: Any) -> No
4042
3. Initializes/bootstraps selected deployments/modules in the order defined.
4143
(e.g. "cdk bootstrap", "terraform init")
4244
45+
\b
46+
Steps By Module Type
47+
--------------------
48+
- AWS CDK: Runs "cdk bootstrap".
49+
- CFNgin: Creates the "cfngin_bucket" if needed.
50+
- Terraform: Runs "terraform init", changes the workspace if needed, runs
51+
"terraform init" again if the workspace was changed, and finally
52+
downloads/updates Terraform modules.
53+
4354
""" # noqa: D301
4455
try:
4556
Runway(ctx.obj.runway_config, ctx.obj.get_runway_context()).init(

runway/_cli/commands/_plan.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def plan(ctx: click.Context, debug: bool, tags: Tuple[str, ...], **_: Any) -> No
2626
"""Determine what infrastructure changes will occur during the next deploy.
2727
2828
\b
29+
Process
30+
-------
2931
1. Determines the deploy environment.
3032
- "-e, --deploy-environment" option
3133
- "DEPLOY_ENVIRONMENT" environment variable

runway/module/terraform.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,14 @@ def cleanup_dot_terraform(self) -> None:
239239
self.logger.debug("removing: %s", child)
240240
send2trash(str(child)) # does not support Path objects
241241

242+
def deploy(self) -> None:
243+
"""Run Terraform apply."""
244+
self.run("apply")
245+
246+
def destroy(self) -> None:
247+
"""Run Terraform destroy."""
248+
self.run("destroy")
249+
242250
def gen_command(
243251
self,
244252
command: Union[List[str], str, Tuple[str, ...]],
@@ -337,6 +345,14 @@ def handle_parameters(self) -> None:
337345
self.ctx.env.vars, self.parameters
338346
)
339347

348+
def init(self) -> None:
349+
"""Run init."""
350+
self.run("init")
351+
352+
def plan(self) -> None:
353+
"""Run Terraform plan."""
354+
self.run("plan")
355+
340356
def terraform_apply(self) -> None:
341357
"""Execute ``terraform apply`` command.
342358
@@ -543,31 +559,16 @@ def run(self, action: TerraformActionTypeDef) -> None:
543559
self.terraform_workspace_new(self.required_workspace)
544560
self.logger.verbose("re-running init after workspace change...")
545561
self.terraform_init()
546-
self.logger.info("init (complete)")
547562
self.terraform_get()
548-
self.logger.info("%s (in progress)", action)
549-
self["terraform_" + action]()
550-
self.logger.info("%s (complete)", action)
563+
self.logger.info("init (complete)")
564+
if action != "init":
565+
self.logger.info("%s (in progress)", action)
566+
self["terraform_" + action]()
567+
self.logger.info("%s (complete)", action)
551568
finally:
552569
if self.auto_tfvars.exists():
553570
self.auto_tfvars.unlink()
554571

555-
def deploy(self) -> None:
556-
"""Run Terraform apply."""
557-
self.run("apply")
558-
559-
def destroy(self) -> None:
560-
"""Run Terraform destroy."""
561-
self.run("destroy")
562-
563-
def init(self) -> None:
564-
"""Run init."""
565-
LOGGER.warning("init not currently supported for %s", self.__class__.__name__)
566-
567-
def plan(self) -> None:
568-
"""Run Terraform plan."""
569-
self.run("plan")
570-
571572

572573
class TerraformOptions(ModuleOptions):
573574
"""Module options for Terraform.

tests/unit/module/test_terraform.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def test_env_file(
214214
else:
215215
assert not obj.env_file
216216

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

479-
def test_init(
480-
self,
481-
caplog: LogCaptureFixture,
482-
runway_context: MockRunwayContext,
483-
tmp_path: Path,
484-
) -> None:
485-
"""Test init."""
486-
caplog.set_level(logging.WARNING, logger=MODULE)
487-
obj = Terraform(runway_context, module_root=tmp_path)
488-
assert not obj.init()
489-
assert (
490-
f"init not currently supported for {Terraform.__name__}" in caplog.messages
491-
)
492-
493479
@pytest.mark.parametrize(
494480
"env, param, expected",
495481
[

0 commit comments

Comments
 (0)