handle backends in init_client

This commit is contained in:
Michael Jerger 2023-05-26 14:53:59 +02:00
parent f399d1e637
commit 4a2fa628f1
5 changed files with 46 additions and 36 deletions

View file

@ -2,7 +2,7 @@ from pathlib import Path
from dda_python_terraform import Terraform, IsFlagged
from packaging import version
from ..domain import Devops, BuildType
from ..domain import Devops, TerraformDomain, BuildType
from ..infrastructure import FileApi, ResourceApi, TerraformApi
@ -58,7 +58,9 @@ class TerraformService:
def rescue_local_state(self, devops: Devops):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
if terraform_domain.is_local_state():
self.file_api.cp(f"{devops.build_path()}/terraform.tfstate", ".", check=False)
self.file_api.cp(
f"{devops.build_path()}/terraform.tfstate", ".", check=False
)
def initialize_build_dir(self, devops: Devops):
terraform = devops.specialized_builds[BuildType.TERRAFORM]
@ -74,12 +76,17 @@ class TerraformService:
# TODO: internal?
def init_client(self, devops: Devops):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
terraform_domain: TerraformDomain = devops.specialized_builds[
BuildType.TERRAFORM
]
terraform = Terraform(
working_dir=devops.build_path(),
terraform_semantic_version=terraform_domain.tf_terraform_semantic_version,
)
terraform.init()
if terraform_domain.is_local_state():
terraform.init()
else:
terraform.init(backend_config=terraform_domain.backend_config())
self.__print_terraform_command__(terraform, devops)
if terraform_domain.tf_use_workspace:
try:

View file

@ -19,19 +19,3 @@ class DigitaloceanBackendPropertiesMixin():
ret.update({"key": self.key})
ret.update({"region": self.region})
return ret
def init_client(self):
terraform = Terraform(
working_dir=self.build_path(),
terraform_semantic_version=self.terraform_semantic_version,
)
terraform.init(backend_config=self.backend_config)
self.print_terraform_command(terraform)
if self.use_workspace:
try:
terraform.workspace("select", self.stage)
self.print_terraform_command(terraform)
except:
terraform.workspace("new", self.stage)
self.print_terraform_command(terraform)
return terraform

View file

@ -37,7 +37,9 @@ class Digitalocean(Validateable, CredentialMappingDefault):
def resources_from_package(self) -> Set[str]:
result = {"provider_registry.tf", "do_provider.tf", "do_mixin_vars.tf"}
if self.do_as_backend:
result.update({"do_backend_properties_vars.tf", "do_backend_with_properties.tf"})
result.update(
{"do_backend_properties_vars.tf", "do_backend_with_properties.tf"}
)
return result
def project_vars(self):

View file

@ -83,10 +83,14 @@ class TerraformDomain(Validateable):
result = result and provider.is_local_state()
return result
def backend_config(self) -> map:
result = {}
for provider in self.providers.values():
result.update(provider.backend_config())
return result
@classmethod
def parse_provider_types(
cls, tf_provider_types: List[str]
) -> List[ProviderType]:
def parse_provider_types(cls, tf_provider_types: List[str]) -> List[ProviderType]:
result = []
for provider_type in tf_provider_types:
result.append(ProviderType[provider_type])

View file

@ -100,10 +100,14 @@ def test_should_calculate_resources_from_package():
"do_mixin_vars.tf",
} == sut.resources_from_package()
sut = TerraformDomain(devops_config({
"tf_provider_types": ["DIGITALOCEAN"],
"do_as_backend": True,
}))
sut = TerraformDomain(
devops_config(
{
"tf_provider_types": ["DIGITALOCEAN"],
"do_as_backend": True,
}
)
)
assert {
"versions.tf",
"terraform_build_vars.tf",
@ -143,14 +147,23 @@ def test_should_calculate_resources_from_package():
"my.file",
} == sut.resources_from_package()
def test_should_calculate_local_state_handling():
sut = TerraformDomain(devops_config({
"tf_provider_types": [],
}))
sut = TerraformDomain(
devops_config(
{
"tf_provider_types": [],
}
)
)
assert sut.is_local_state()
sut = TerraformDomain(devops_config({
"tf_provider_types": ["DIGITALOCEAN"],
"do_as_backend": True,
}))
sut = TerraformDomain(
devops_config(
{
"tf_provider_types": ["DIGITALOCEAN"],
"do_as_backend": True,
}
)
)
assert not sut.is_local_state()