From 70a671a06d5dc32fc39c710615d1beeca5e15deb Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Thu, 25 May 2023 18:28:45 +0200 Subject: [PATCH] bring credentials-mapping-defaults closer to the domain --- src/main/python/ddadevops/domain/__init__.py | 2 +- src/main/python/ddadevops/domain/c4k.py | 21 ++++++- src/main/python/ddadevops/domain/common.py | 5 ++ src/main/python/ddadevops/domain/image.py | 14 +++++ .../python/ddadevops/domain/init_service.py | 49 +++++----------- .../ddadevops/domain/provider_digitalocean.py | 29 +++++++--- src/main/python/ddadevops/domain/terraform.py | 7 ++- src/test/python/test_release_mixin.py | 56 +------------------ 8 files changed, 79 insertions(+), 104 deletions(-) diff --git a/src/main/python/ddadevops/domain/__init__.py b/src/main/python/ddadevops/domain/__init__.py index d6258b4..3972995 100644 --- a/src/main/python/ddadevops/domain/__init__.py +++ b/src/main/python/ddadevops/domain/__init__.py @@ -1,4 +1,4 @@ -from .common import Validateable, DnsRecord, Devops, BuildType, MixinType, ReleaseType, ProviderType +from .common import Validateable, CredentialMappingDefault, DnsRecord, Devops, BuildType, MixinType, ReleaseType, ProviderType from .devops_factory import DevopsFactory from .image import Image from .c4k import C4k diff --git a/src/main/python/ddadevops/domain/c4k.py b/src/main/python/ddadevops/domain/c4k.py index e5cb060..635d770 100644 --- a/src/main/python/ddadevops/domain/c4k.py +++ b/src/main/python/ddadevops/domain/c4k.py @@ -1,12 +1,13 @@ from typing import List, Optional from .common import ( Validateable, + CredentialMappingDefault, DnsRecord, Devops, ) -class C4k(Validateable): +class C4k(Validateable, CredentialMappingDefault): def __init__(self, inp: dict): self.module = inp.get("module") self.stage = inp.get("stage") @@ -17,8 +18,8 @@ class C4k(Validateable): "https://prometheus-prod-01-eu-west-0.grafana.net/api/prom/push", ) self.c4k_auth = inp.get("c4k_auth", {}) - self.c4k_grafana_cloud_user = inp.get('c4k_grafana_cloud_user') - self.c4k_grafana_cloud_password = inp.get('c4k_grafana_cloud_password') + self.c4k_grafana_cloud_user = inp.get("c4k_grafana_cloud_user") + self.c4k_grafana_cloud_password = inp.get("c4k_grafana_cloud_password") self.dns_record: Optional[DnsRecord] = None def update_runtime_config(self, dns_record: DnsRecord): @@ -63,3 +64,17 @@ class C4k(Validateable): auth_path = f"{build_path}/out_c4k_auth.yaml" output_path = f"{build_path}/out_{module}.yaml" return f"c4k-{self.c4k_executable_name}-standalone.jar {config_path} {auth_path} > {output_path}" + + @classmethod + def get_mapping_default(cls) -> List[map]: + return [ + { + "gopass_path": "server/meissa/grafana-cloud", + "gopass_field": "grafana-cloud-user", + "name": "c4k_grafana_cloud_user", + }, + { + "gopass_path": "server/meissa/grafana-cloud", + "name": "c4k_grafana_cloud_password", + }, + ] diff --git a/src/main/python/ddadevops/domain/common.py b/src/main/python/ddadevops/domain/common.py index 3495b2b..6eba93f 100644 --- a/src/main/python/ddadevops/domain/common.py +++ b/src/main/python/ddadevops/domain/common.py @@ -57,6 +57,11 @@ class Validateable: raise ValueError(f"Invalid Validateable: {issues}") +class CredentialMappingDefault: + @classmethod + def get_mapping_default(cls) -> List[map]: + return [] + class DnsRecord(Validateable): def __init__(self, fqdn, ipv4=None, ipv6=None): self.fqdn = fqdn diff --git a/src/main/python/ddadevops/domain/image.py b/src/main/python/ddadevops/domain/image.py index a3cd888..5a1a5f6 100644 --- a/src/main/python/ddadevops/domain/image.py +++ b/src/main/python/ddadevops/domain/image.py @@ -36,3 +36,17 @@ class Image(Validateable): self.image_build_commons_dir_name, ] return "/".join(filter_none(commons_path)) + "/" + + @classmethod + def get_mapping_default(cls) -> List[map]: + return [ + { + "gopass_path": "meissa/web/docker.com", + "gopass_field": "login", + "name": "image_dockerhub_user", + }, + { + "gopass_path": "meissa/web/docker.com", + "name": "image_dockerhub_password", + }, + ] diff --git a/src/main/python/ddadevops/domain/init_service.py b/src/main/python/ddadevops/domain/init_service.py index ca0b9ec..d44046b 100644 --- a/src/main/python/ddadevops/domain/init_service.py +++ b/src/main/python/ddadevops/domain/init_service.py @@ -1,8 +1,12 @@ from pathlib import Path from typing import Dict -from .common import Devops, MixinType, BuildType +from .common import Devops, MixinType, BuildType, ProviderType from .credentials import CredentialMapping, Credentials, GopassType from .devops_factory import DevopsFactory +from .terraform import TerraformDomain +from .provider_digitalocean import Digitalocean +from .c4k import C4k +from .image import Image from .release import ReleaseType from ..infrastructure import BuildFileRepository, CredentialsApi, EnvironmentApi, GitApi @@ -35,47 +39,20 @@ class InitService: def initialize(self, inp: dict) -> Devops: build_types = self.devops_factory.__parse_build_types__(inp["build_types"]) mixin_types = self.devops_factory.__parse_mixin_types__(inp["mixin_types"]) + provider_types = TerraformDomain.parse_provider_types(inp["tf_provider_types"]) version = None default_mappings = [] if BuildType.C4K in build_types: - default_mappings += [ - { - "gopass_path": "server/meissa/grafana-cloud", - "gopass_field": "grafana-cloud-user", - "name": "c4k_grafana_cloud_user", - }, - { - "gopass_path": "server/meissa/grafana-cloud", - "name": "c4k_grafana_cloud_password", - }, - ] + default_mappings += C4k.get_mapping_default() if BuildType.IMAGE in build_types: - default_mappings += [ - { - "gopass_path": "meissa/web/docker.com", - "gopass_field": "login", - "name": "image_dockerhub_user", - }, - { - "gopass_path": "meissa/web/docker.com", - "name": "image_dockerhub_password", - }, - ] - if False: - default_mappings += [ - { - "gopass_path": "server/devops/digitalocean/s3", - "gopass_field": "id", - "name": "do_spaces_access_id", - }, - { - "gopass_path": "server/devops/digitalocean/s3", - "gopass_field": "secret", - "name": "do_spaces_secret_key", - }, - ] + default_mappings += Image.get_mapping_default() + if ( + BuildType.TERRAFORM in build_types + and ProviderType.DIGITALOCEAN in provider_types + ): + default_mappings += Digitalocean.get_mapping_default() if MixinType.RELEASE in mixin_types: primary_build_file_id = inp.get( diff --git a/src/main/python/ddadevops/domain/provider_digitalocean.py b/src/main/python/ddadevops/domain/provider_digitalocean.py index a27057d..3325567 100644 --- a/src/main/python/ddadevops/domain/provider_digitalocean.py +++ b/src/main/python/ddadevops/domain/provider_digitalocean.py @@ -1,10 +1,8 @@ from typing import List -from .common import ( - Validateable, -) +from .common import Validateable, CredentialMappingDefault -class Digitalocean(Validateable): +class Digitalocean(Validateable, CredentialMappingDefault): def __init__( self, inp: dict, @@ -24,6 +22,23 @@ class Digitalocean(Validateable): return ["provider_registry.tf", "do_provider.tf", "do_mixin_vars.tf"] def project_vars(self): - return {"do_api_key": self.do_api_key, - "do_spaces_access_id": self.do_spaces_access_id, - "do_spaces_secret_key": self.do_spaces_secret_key,} + return { + "do_api_key": self.do_api_key, + "do_spaces_access_id": self.do_spaces_access_id, + "do_spaces_secret_key": self.do_spaces_secret_key, + } + + @classmethod + def get_mapping_default(cls) -> List[map]: + return [ + { + "gopass_path": "server/devops/digitalocean/s3", + "gopass_field": "id", + "name": "do_spaces_access_id", + }, + { + "gopass_path": "server/devops/digitalocean/s3", + "gopass_field": "secret", + "name": "do_spaces_secret_key", + }, + ] diff --git a/src/main/python/ddadevops/domain/terraform.py b/src/main/python/ddadevops/domain/terraform.py index 5a46073..4f5ba89 100644 --- a/src/main/python/ddadevops/domain/terraform.py +++ b/src/main/python/ddadevops/domain/terraform.py @@ -34,7 +34,7 @@ class TerraformDomain(Validateable): ) self.tf_use_package_common_files = inp.get("tf_use_package_common_files", True) - provider_types = self.__parse_provider_types__(self.tf_provider_types) + provider_types = TerraformDomain.parse_provider_types(self.tf_provider_types) self.providers = {} if ProviderType.DIGITALOCEAN in provider_types: self.providers[ProviderType.DIGITALOCEAN] = Digitalocean(inp) @@ -76,8 +76,9 @@ class TerraformDomain(Validateable): result += self.tf_additional_resources_from_package return result - def __parse_provider_types__( - self, tf_provider_types: List[str] + @classmethod + def parse_provider_types( + cls, tf_provider_types: List[str] ) -> List[ProviderType]: result = [] for provider_type in tf_provider_types: diff --git a/src/test/python/test_release_mixin.py b/src/test/python/test_release_mixin.py index a266630..291eb73 100644 --- a/src/test/python/test_release_mixin.py +++ b/src/test/python/test_release_mixin.py @@ -8,9 +8,10 @@ from src.main.python.ddadevops.domain import Devops, Release from .domain.helper import devops_config from .resource_helper import copy_resource + def test_release_mixin(tmp_path): str_tmp_path = str(tmp_path) - copy_resource(Path('package.json'), tmp_path) + copy_resource(Path("package.json"), tmp_path) project = Project(str_tmp_path, name="name") sut = ReleaseMixin( @@ -27,56 +28,3 @@ def test_release_mixin(tmp_path): sut.initialize_build_dir() assert sut.build_path() == f"{str_tmp_path}/target/name/release-test" - - -# def test_release_mixin_git(tmp_path: Path, monkeypatch: pt.MonkeyPatch): -# # init -# th = ResourceHelper() -# th.copy_files(th.TEST_FILE_PATH, tmp_path) -# th.TEST_FILE_PATH = tmp_path / th.TEST_FILE_NAME - -# change_test_dir(tmp_path, monkeypatch) -# project = Project(tmp_path) - -# git_api = GitApi() -# git_api.init() -# git_api.set_user_config("ex.ample@mail.com", "Ex Ample") -# git_api.add_file(th.TEST_FILE_NAME) -# git_api.commit("MAJOR release") - -# build = initialize_with_object(project, th.TEST_FILE_PATH) -# build.prepare_release() -# release_version = build.release_repo.version_repository.get_version() - -# # test -# assert "124.0.1-SNAPSHOT" in release_version.get_version_string() - - -# def test_release_mixin_environment(tmp_path: Path, monkeypatch: pt.MonkeyPatch): - -# # init -# th = Helper() -# th.copy_files(th.TEST_FILE_PATH, tmp_path) -# th.TEST_FILE_PATH = tmp_path / th.TEST_FILE_NAME - -# change_test_dir(tmp_path, monkeypatch) -# project = Project(tmp_path) - -# git_api = GitApi() -# git_api.init() -# git_api.set_user_config("ex.ample@mail.com", "Ex Ample") -# git_api.add_file(th.TEST_FILE_NAME) -# git_api.commit("Commit Message") - -# environment_api = EnvironmentApi() -# environment_api.set("DDADEVOPS_RELEASE_TYPE", "MAJOR") - -# build = initialize_with_object(project, th.TEST_FILE_PATH) -# build.prepare_release() -# release_version = build.release_repo.version_repository.get_version() - -# # test -# assert "124.0.1-SNAPSHOT" in release_version.get_version_string() - -# # tear down -# environment_api.set("DDADEVOPS_RELEASE_TYPE", "")