handle backends in project_vars

This commit is contained in:
Michael Jerger 2023-05-26 15:14:12 +02:00
parent 4a2fa628f1
commit 39d732a04e
4 changed files with 58 additions and 17 deletions

View file

@ -10,12 +10,3 @@ def add_digitalocean_backend_properties_mixin_config(
class DigitaloceanBackendPropertiesMixin(): class DigitaloceanBackendPropertiesMixin():
pass pass
def project_vars(self):
ret = super().project_vars()
ret.update({"account_name": self.account_name})
ret.update({"endpoint": self.endpoint})
ret.update({"bucket": self.bucket})
ret.update({"key": self.key})
ret.update({"region": self.region})
return ret

View file

@ -13,7 +13,7 @@ class Digitalocean(Validateable, CredentialMappingDefault):
self.do_spaces_access_id = inp.get("do_spaces_access_id") self.do_spaces_access_id = inp.get("do_spaces_access_id")
self.do_spaces_secret_key = inp.get("do_spaces_secret_key") self.do_spaces_secret_key = inp.get("do_spaces_secret_key")
self.do_as_backend = inp.get("do_as_backend", False) self.do_as_backend = inp.get("do_as_backend", False)
self.do_account_name = inp.get("do_account_name") self.do_account_name = inp.get("do_account_name", self.stage)
self.do_endpoint = inp.get("do_endpoint") self.do_endpoint = inp.get("do_endpoint")
self.do_bucket = inp.get("do_bucket") self.do_bucket = inp.get("do_bucket")
self.do_bucket_key = inp.get("do_bucket_key") self.do_bucket_key = inp.get("do_bucket_key")
@ -29,6 +29,7 @@ class Digitalocean(Validateable, CredentialMappingDefault):
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") result += self.__validate_is_not_none__("do_as_backend")
if self.do_as_backend: if self.do_as_backend:
result += self.__validate_is_not_empty__("do_account_name")
result += self.__validate_is_not_empty__("do_endpoint") result += self.__validate_is_not_empty__("do_endpoint")
result += self.__validate_is_not_empty__("do_bucket") result += self.__validate_is_not_empty__("do_bucket")
result += self.__validate_is_not_empty__("do_region") result += self.__validate_is_not_empty__("do_region")
@ -43,25 +44,32 @@ class Digitalocean(Validateable, CredentialMappingDefault):
return result return result
def project_vars(self): def project_vars(self):
return { result = {
"do_api_key": self.do_api_key, "do_api_key": self.do_api_key,
"do_spaces_access_id": self.do_spaces_access_id, "do_spaces_access_id": self.do_spaces_access_id,
"do_spaces_secret_key": self.do_spaces_secret_key, "do_spaces_secret_key": self.do_spaces_secret_key,
} }
if self.do_as_backend:
result.update(
{
"account_name": self.do_account_name,
"endpoint": self.do_endpoint,
"bucket": self.do_bucket,
"key": self.__bucket_key__(),
"region": self.do_region,
}
)
return result
def backend_config(self) -> map: def backend_config(self) -> map:
result = {} result = {}
if self.do_as_backend: 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 = { result = {
"access_key": self.do_spaces_access_id, "access_key": self.do_spaces_access_id,
"secret_key": self.do_spaces_secret_key, "secret_key": self.do_spaces_secret_key,
"endpoint": self.do_endpoint, "endpoint": self.do_endpoint,
"bucket": self.do_bucket, "bucket": self.do_bucket,
"key": bucket_key, "key": self.__bucket_key__(),
"region": self.do_region, "region": self.do_region,
} }
return result return result
@ -69,6 +77,15 @@ class Digitalocean(Validateable, CredentialMappingDefault):
def is_local_state(self): def is_local_state(self):
return not self.do_as_backend return not self.do_as_backend
def __bucket_key__(self):
result = ""
if self.do_as_backend:
if self.do_account_name and self.do_bucket_key:
result = f"{self.do_account_name}/{self.do_bucket_key}"
else:
result = f"{self.stage}/{self.module}"
return result
@classmethod @classmethod
def get_mapping_default(cls) -> List[Dict[str, str]]: def get_mapping_default(cls) -> List[Dict[str, str]]:
return [ return [

View file

@ -49,3 +49,36 @@ def test_should_calculate_backend_config():
"key": "test/module", "key": "test/module",
"region": "region", "region": "region",
} == sut.backend_config() } == sut.backend_config()
def test_should_calculate_project_vars():
sut = Digitalocean(
devops_config(
{
"do_as_backend": False,
}
)
)
assert {
"do_api_key": "api_key",
"do_spaces_access_id": "spaces_id",
"do_spaces_secret_key": "spaces_secret",
} == sut.project_vars()
sut = Digitalocean(
devops_config(
{
"do_as_backend": True,
}
)
)
assert {
"do_api_key": "api_key",
"do_spaces_access_id": "spaces_id",
"do_spaces_secret_key": "spaces_secret",
"account_name": "test",
"endpoint": "endpoint",
"bucket": "bucket",
"key": "test/module",
"region": "region",
} == sut.project_vars()

View file

@ -64,7 +64,7 @@ def test_should_calculate_project_vars():
sut = TerraformDomain(config) sut = TerraformDomain(config)
assert {"module": "module", "stage": "test"} == sut.project_vars() assert {"module": "module", "stage": "test"} == sut.project_vars()
config = devops_config({}) config = devops_config({"do_as_backend": False,})
sut = TerraformDomain(config) sut = TerraformDomain(config)
assert { assert {
"module": "module", "module": "module",