add resource handling for do_backend

This commit is contained in:
Michael Jerger 2023-05-26 14:39:13 +02:00
parent 7476c9a2d3
commit 8f61e286ae
6 changed files with 129 additions and 53 deletions

View file

@ -54,6 +54,12 @@ classDiagram
do_api_key
do_spaces_access_key
do_spaces_secret_key
do_as_backend
do_account_name
do_endpoint
do_bucket
do_bucket_key
do_region
}
class Hetzner {

View file

@ -5,37 +5,11 @@ from .digitalocean_terraform_build import DigitaloceanTerraformBuild
def add_digitalocean_backend_properties_mixin_config(
config, account_name, endpoint, bucket, key, region="eu-central-1"
):
config.update(
{
"DigitaloceanBackendPropertiesMixin": {
"account_name": account_name,
"endpoint": endpoint,
"bucket": bucket,
"key": key,
"region": region,
}
}
)
return config
pass
class DigitaloceanBackendPropertiesMixin(DigitaloceanTerraformBuild):
def __init__(self, project, config):
super().__init__(project, config)
do_mixin_config = config["DigitaloceanBackendPropertiesMixin"]
self.account_name = do_mixin_config["account_name"]
self.endpoint = do_mixin_config["endpoint"]
self.bucket = do_mixin_config["bucket"]
self.key = do_mixin_config["account_name"] + "/" + do_mixin_config["key"]
self.region = do_mixin_config["region"]
self.backend_config = {
"access_key": self.do_spaces_access_id,
"secret_key": self.do_spaces_secret_key,
"endpoint": self.endpoint,
"bucket": self.bucket,
"key": self.key,
"region": self.region,
}
class DigitaloceanBackendPropertiesMixin():
pass
def project_vars(self):
ret = super().project_vars()
@ -46,11 +20,6 @@ class DigitaloceanBackendPropertiesMixin(DigitaloceanTerraformBuild):
ret.update({"region": self.region})
return ret
def copy_build_resources_from_package(self):
super().copy_build_resources_from_package()
self.copy_build_resource_file_from_package("do_backend_properties_vars.tf")
self.copy_build_resource_file_from_package("do_backend_with_properties.tf")
def copy_local_state(self):
pass

View file

@ -7,19 +7,38 @@ class Digitalocean(Validateable, CredentialMappingDefault):
self,
inp: dict,
):
self.stage = inp.get("stage")
self.module = inp.get("module")
self.do_api_key = inp.get("do_api_key")
self.do_spaces_access_id = inp.get("do_spaces_access_id")
self.do_spaces_secret_key = inp.get("do_spaces_secret_key")
self.do_as_backend = inp.get("do_as_backend", False)
self.do_account_name = inp.get("do_account_name")
self.do_endpoint = inp.get("do_endpoint")
self.do_bucket = inp.get("do_bucket")
self.do_bucket_key = inp.get("do_bucket_key")
self.do_region = inp.get("do_region")
def validate(self) -> List[str]:
result = []
result += self.__validate_is_not_empty__("stage")
result += self.__validate_is_not_empty__("module")
result += self.__validate_is_not_empty__("do_api_key")
result += self.__validate_is_not_empty__("do_spaces_access_id")
result += self.__validate_is_not_empty__("do_spaces_secret_key")
result += self.__validate_is_not_empty__("do_spaces_secret_key")
result += self.__validate_is_not_none__("do_as_backend")
if self.do_as_backend:
result += self.__validate_is_not_empty__("do_endpoint")
result += self.__validate_is_not_empty__("do_bucket")
result += self.__validate_is_not_empty__("do_region")
return result
def resources_from_package(self) -> Set[str]:
return {"provider_registry.tf", "do_provider.tf", "do_mixin_vars.tf"}
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"})
return result
def project_vars(self):
return {
@ -28,6 +47,23 @@ class Digitalocean(Validateable, CredentialMappingDefault):
"do_spaces_secret_key": self.do_spaces_secret_key,
}
def backend_config(self) -> map:
result = {}
if self.do_as_backend:
if self.do_account_name and self.do_bucket_key:
bucket_key = f"{self.do_account_name}/{self.do_bucket_key}"
else:
bucket_key = f"{self.stage}/{self.module}"
result = {
"access_key": self.do_spaces_access_id,
"secret_key": self.do_spaces_secret_key,
"endpoint": self.do_endpoint,
"bucket": self.do_bucket,
"key": bucket_key,
"region": self.do_region,
}
return result
@classmethod
def get_mapping_default(cls) -> List[Dict[str, str]]:
return [

View file

@ -37,6 +37,13 @@ def devops_config(overrides: dict) -> dict:
"do_api_key": "api_key",
"do_spaces_access_id": "spaces_id",
"do_spaces_secret_key": "spaces_secret",
"do_api_key": "api_key",
"do_spaces_access_id": "spaces_id",
"do_spaces_secret_key": "spaces_secret",
"do_as_backend": True,
"do_endpoint": "endpoint",
"do_bucket": "bucket",
"do_region": "region",
"hetzner_api_key": "hetzner_api_key",
"release_type": "NONE",
"release_main_branch": "main",

View file

@ -9,13 +9,43 @@ from .helper import devops_config
def test_digitalocean_creation():
sut = Digitalocean(
devops_config(
{
"do_api_key": "api_key",
"do_spaces_access_id": "spaces_id",
"do_spaces_secret_key": "spaces_secret",
}
)
{
"module": "module",
"stage": "test",
"do_api_key": "api_key",
"do_spaces_access_id": "spaces_id",
"do_spaces_secret_key": "spaces_secret",
}
)
assert sut is not None
assert sut.is_valid()
sut = Digitalocean(
{
"module": "module",
"stage": "test",
"do_api_key": "api_key",
"do_spaces_access_id": "spaces_id",
"do_spaces_secret_key": "spaces_secret",
"do_as_backend": True,
"do_account_name": "account_name",
"do_endpoint": "endpoint",
"do_bucket": "bucket",
"do_bucket_key": "bucket_key",
"do_region": "region",
}
)
assert sut is not None
assert sut.is_valid()
def test_should_calculate_backend_config():
sut = Digitalocean(devops_config({}))
assert {
"access_key": "spaces_id",
"secret_key": "spaces_secret",
"endpoint": "endpoint",
"bucket": "bucket",
"key": "test/module",
"region": "region",
} == sut.backend_config()

View file

@ -72,7 +72,7 @@ def test_should_calculate_project_vars():
"do_api_key": "api_key",
"do_spaces_access_id": "spaces_id",
"do_spaces_secret_key": "spaces_secret",
"hetzner_api_key": "hetzner_api_key"
"hetzner_api_key": "hetzner_api_key",
} == sut.project_vars()
@ -87,7 +87,8 @@ def test_should_calculate_resources_from_package():
config = devops_config(
{
"tf_provider_types": ["DIGITALOCEAN"]
"tf_provider_types": ["DIGITALOCEAN"],
"do_as_backend": False,
}
)
sut = TerraformDomain(config)
@ -97,13 +98,23 @@ def test_should_calculate_resources_from_package():
"provider_registry.tf",
"do_provider.tf",
"do_mixin_vars.tf",
} == sut.resources_from_package()
} == sut.resources_from_package()
config = devops_config(
{
"tf_provider_types": ["HETZNER"]
}
)
sut = TerraformDomain(devops_config({
"tf_provider_types": ["DIGITALOCEAN"],
"do_as_backend": True,
}))
assert {
"versions.tf",
"terraform_build_vars.tf",
"provider_registry.tf",
"do_provider.tf",
"do_mixin_vars.tf",
"do_backend_properties_vars.tf",
"do_backend_with_properties.tf",
} == sut.resources_from_package()
config = devops_config({"tf_provider_types": ["HETZNER"]})
sut = TerraformDomain(config)
assert {
"versions.tf",
@ -111,9 +122,14 @@ def test_should_calculate_resources_from_package():
"provider_registry.tf",
"hetzner_provider.tf",
"hetzner_mixin_vars.tf",
} == sut.resources_from_package()
} == sut.resources_from_package()
config = devops_config({"tf_additional_resources_from_package": {"my.file"}})
config = devops_config(
{
"tf_additional_resources_from_package": {"my.file"},
"do_as_backend": False,
}
)
sut = TerraformDomain(config)
assert {
"versions.tf",
@ -125,4 +141,16 @@ def test_should_calculate_resources_from_package():
"hetzner_provider.tf",
"hetzner_mixin_vars.tf",
"my.file",
} == sut.resources_from_package()
} == sut.resources_from_package()
def test_should_calculate_local_state_handling():
sut = TerraformDomain(devops_config({
"tf_provider_types": [],
}))
assert sut.is_local_state()
sut = TerraformDomain(devops_config({
"tf_provider_types": ["DIGITALOCEAN"],
"do_as_backend": True,
}))
assert sut.is_local_state()