Skip to content

Commit

Permalink
Rename terraform.py file and terraform_init calls
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelovilla committed Oct 16, 2024
1 parent 6a09d4e commit 80d67b9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,53 +24,53 @@ class OpenTofuException(Exception):

def deploy(
directory,
terraform_init: bool = True,
terraform_import: bool = False,
terraform_apply: bool = True,
terraform_destroy: bool = False,
tofu_init: bool = True,
tofu_import: bool = False,
tofu_apply: bool = True,
tofu_destroy: bool = False,
input_vars: Dict[str, Any] = {},
state_imports: List[Any] = [],
):
"""Execute a given terraform directory.
"""Execute a given directory with OpenTofu infrastructure configuration.
Parameters:
directory: directory in which to run terraform operations on
directory: directory in which to run tofu operations on
terraform_init: whether to run `terraform init` default True
tofu_init: whether to run `tofu init` default True
terraform_import: whether to run `terraform import` default
tofu_import: whether to run `tofu import` default
False for each `state_imports` supplied to function
terraform_apply: whether to run `terraform apply` default True
tofu_apply: whether to run `tofu apply` default True
terraform_destroy: whether to run `terraform destroy` default
tofu_destroy: whether to run `tofu destroy` default
False
input_vars: supply values for "variable" resources within
terraform module
state_imports: (addr, id) pairs for iterate through and attempt
to terraform import
to tofu import
"""
with tempfile.NamedTemporaryFile(
mode="w", encoding="utf-8", suffix=".tfvars.json"
) as f:
json.dump(input_vars, f.file)
f.file.flush()

if terraform_init:
if tofu_init:
init(directory)

if terraform_import:
if tofu_import:
for addr, id in state_imports:
tfimport(
addr, id, directory=directory, var_files=[f.name], exist_ok=True
)

if terraform_apply:
if tofu_apply:
apply(directory, var_files=[f.name])

if terraform_destroy:
if tofu_destroy:
destroy(directory, var_files=[f.name])

return output(directory)
Expand Down Expand Up @@ -184,9 +184,9 @@ def tfimport(addr, id, directory=None, var_files=None, exist_ok=False):
raise e


def show(directory=None, terraform_init: bool = True) -> dict:
def show(directory=None, tofu_init: bool = True) -> dict:

if terraform_init:
if tofu_init:
init(directory)

logger.info(f"tofu show directory={directory}")
Expand Down
32 changes: 16 additions & 16 deletions src/_nebari/stages/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from kubernetes import client, config
from kubernetes.client.rest import ApiException

from _nebari.provider import helm, kubernetes, kustomize, terraform
from _nebari.provider import helm, kubernetes, kustomize, opentofu
from _nebari.stages.tf_objects import NebariTerraformState
from nebari.hookspecs import NebariStage

Expand Down Expand Up @@ -248,7 +248,7 @@ def tf_objects(self) -> List[Dict]:

def render(self) -> Dict[pathlib.Path, str]:
contents = {
(self.stage_prefix / "_nebari.tf.json"): terraform.tf_render_objects(
(self.stage_prefix / "_nebari.tf.json"): opentofu.tf_render_objects(
self.tf_objects()
)
}
Expand Down Expand Up @@ -283,19 +283,19 @@ def deploy(
self,
stage_outputs: Dict[str, Dict[str, Any]],
disable_prompt: bool = False,
terraform_init: bool = True,
tofu_init: bool = True,
):
deploy_config = dict(
directory=str(self.output_directory / self.stage_prefix),
input_vars=self.input_vars(stage_outputs),
terraform_init=terraform_init,
tofu_init=tofu_init,
)
state_imports = self.state_imports()
if state_imports:
deploy_config["terraform_import"] = True
deploy_config["state_imports"] = state_imports

self.set_outputs(stage_outputs, terraform.deploy(**deploy_config))
self.set_outputs(stage_outputs, opentofu.deploy(**deploy_config))
self.post_deploy(stage_outputs, disable_prompt)
yield

Expand All @@ -318,27 +318,27 @@ def destroy(
):
self.set_outputs(
stage_outputs,
terraform.deploy(
opentofu.deploy(
directory=str(self.output_directory / self.stage_prefix),
input_vars=self.input_vars(stage_outputs),
terraform_init=True,
terraform_import=True,
terraform_apply=False,
terraform_destroy=False,
tofu_init=True,
tofu_import=True,
tofu_apply=False,
tofu_destroy=False,
),
)
yield
try:
terraform.deploy(
opentofu.deploy(
directory=str(self.output_directory / self.stage_prefix),
input_vars=self.input_vars(stage_outputs),
terraform_init=True,
terraform_import=True,
terraform_apply=False,
terraform_destroy=True,
tofu_init=True,
tofu_import=True,
tofu_apply=False,
tofu_destroy=True,
)
status["stages/" + self.name] = True
except terraform.OpenTofuException as e:
except opentofu.OpenTofuException as e:
if not ignore_errors:
raise e
status["stages/" + self.name] = False
8 changes: 3 additions & 5 deletions src/_nebari/stages/infrastructure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pydantic import Field, field_validator, model_validator

from _nebari import constants
from _nebari.provider import terraform
from _nebari.provider import opentofu
from _nebari.provider.cloud import (
amazon_web_services,
azure_cloud,
Expand Down Expand Up @@ -735,7 +735,7 @@ def state_imports(self) -> List[Tuple[str, str]]:
def tf_objects(self) -> List[Dict]:
if self.config.provider == schema.ProviderEnum.gcp:
return [
terraform.Provider(
opentofu.Provider(
"google",
project=self.config.google_cloud_platform.project,
region=self.config.google_cloud_platform.region,
Expand All @@ -752,9 +752,7 @@ def tf_objects(self) -> List[Dict]:
]
elif self.config.provider == schema.ProviderEnum.aws:
return [
terraform.Provider(
"aws", region=self.config.amazon_web_services.region
),
opentofu.Provider("aws", region=self.config.amazon_web_services.region),
NebariTerraformState(self.name, self.config),
]
else:
Expand Down
14 changes: 6 additions & 8 deletions src/_nebari/stages/terraform_state/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pydantic import field_validator

from _nebari import utils
from _nebari.provider import terraform
from _nebari.provider import opentofu
from _nebari.provider.cloud import azure_cloud
from _nebari.stages.base import NebariTerraformStage
from _nebari.stages.tf_objects import NebariConfig
Expand Down Expand Up @@ -175,17 +175,15 @@ def tf_objects(self) -> List[Dict]:
resources = [NebariConfig(self.config)]
if self.config.provider == schema.ProviderEnum.gcp:
return resources + [
terraform.Provider(
opentofu.Provider(
"google",
project=self.config.google_cloud_platform.project,
region=self.config.google_cloud_platform.region,
),
]
elif self.config.provider == schema.ProviderEnum.aws:
return resources + [
terraform.Provider(
"aws", region=self.config.amazon_web_services.region
),
opentofu.Provider("aws", region=self.config.amazon_web_services.region),
]
else:
return resources
Expand Down Expand Up @@ -236,9 +234,9 @@ def deploy(
):
self.check_immutable_fields()

# No need to run terraform init here as it's being called when running the
# No need to run tofu init here as it's being called when running the
# terraform show command, inside check_immutable_fields
with super().deploy(stage_outputs, disable_prompt, terraform_init=False):
with super().deploy(stage_outputs, disable_prompt, tofu_init=False):
env_mapping = {}
# DigitalOcean terraform remote state using Spaces Bucket
# assumes aws credentials thus we set them to match spaces credentials
Expand Down Expand Up @@ -286,7 +284,7 @@ def check_immutable_fields(self):

def get_nebari_config_state(self):
directory = str(self.output_directory / self.stage_prefix)
tf_state = terraform.show(directory)
tf_state = opentofu.show(directory)
nebari_config_state = None

# get nebari config from state
Expand Down
2 changes: 1 addition & 1 deletion src/_nebari/stages/tf_objects.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from _nebari.provider.terraform import Data, Provider, Resource, TerraformBackend
from _nebari.provider.opentofu import Data, Provider, Resource, TerraformBackend
from _nebari.utils import (
AZURE_TF_STATE_RESOURCE_GROUP_SUFFIX,
construct_azure_resource_group_name,
Expand Down

0 comments on commit 80d67b9

Please sign in to comment.