handle the case of non local state

This commit is contained in:
Michael Jerger 2023-05-26 14:47:22 +02:00
parent 8f61e286ae
commit f399d1e637
6 changed files with 19 additions and 9 deletions

View file

@ -50,11 +50,15 @@ class TerraformService:
# TODO: internal? # TODO: internal?
def copy_local_state(self, devops: Devops): def copy_local_state(self, devops: Devops):
self.file_api.cp("terraform.tfstate", devops.build_path(), check=False) terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
if terraform_domain.is_local_state():
self.file_api.cp("terraform.tfstate", devops.build_path(), check=False)
# TODO: internal? # TODO: internal?
def rescue_local_state(self, devops: Devops): def rescue_local_state(self, devops: Devops):
self.file_api.cp(f"{devops.build_path()}/terraform.tfstate", ".", check=False) 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)
def initialize_build_dir(self, devops: Devops): def initialize_build_dir(self, devops: Devops):
terraform = devops.specialized_builds[BuildType.TERRAFORM] terraform = devops.specialized_builds[BuildType.TERRAFORM]

View file

@ -20,12 +20,6 @@ class DigitaloceanBackendPropertiesMixin():
ret.update({"region": self.region}) ret.update({"region": self.region})
return ret return ret
def copy_local_state(self):
pass
def rescue_local_state(self):
pass
def init_client(self): def init_client(self):
terraform = Terraform( terraform = Terraform(
working_dir=self.build_path(), working_dir=self.build_path(),

View file

@ -63,6 +63,9 @@ class Digitalocean(Validateable, CredentialMappingDefault):
"region": self.do_region, "region": self.do_region,
} }
return result return result
def is_local_state(self):
return not self.do_as_backend
@classmethod @classmethod
def get_mapping_default(cls) -> List[Dict[str, str]]: def get_mapping_default(cls) -> List[Dict[str, str]]:

View file

@ -20,6 +20,9 @@ class Hetzner(Validateable, CredentialMappingDefault):
return { return {
"hetzner_api_key": self.hetzner_api_key "hetzner_api_key": self.hetzner_api_key
} }
def is_local_state(self):
return True
@classmethod @classmethod
def get_mapping_default(cls) -> List[Dict[str, str]]: def get_mapping_default(cls) -> List[Dict[str, str]]:

View file

@ -77,6 +77,12 @@ class TerraformDomain(Validateable):
result = result.union(self.tf_additional_resources_from_package) result = result.union(self.tf_additional_resources_from_package)
return result return result
def is_local_state(self):
result = True
for provider in self.providers.values():
result = result and provider.is_local_state()
return result
@classmethod @classmethod
def parse_provider_types( def parse_provider_types(
cls, tf_provider_types: List[str] cls, tf_provider_types: List[str]

View file

@ -153,4 +153,4 @@ def test_should_calculate_local_state_handling():
"tf_provider_types": ["DIGITALOCEAN"], "tf_provider_types": ["DIGITALOCEAN"],
"do_as_backend": True, "do_as_backend": True,
})) }))
assert sut.is_local_state() assert not sut.is_local_state()