bring credentials-mapping-defaults closer to the domain

This commit is contained in:
Michael Jerger 2023-05-25 18:28:45 +02:00
parent cb17b39433
commit 70a671a06d
8 changed files with 79 additions and 104 deletions

View file

@ -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 .devops_factory import DevopsFactory
from .image import Image from .image import Image
from .c4k import C4k from .c4k import C4k

View file

@ -1,12 +1,13 @@
from typing import List, Optional from typing import List, Optional
from .common import ( from .common import (
Validateable, Validateable,
CredentialMappingDefault,
DnsRecord, DnsRecord,
Devops, Devops,
) )
class C4k(Validateable): class C4k(Validateable, CredentialMappingDefault):
def __init__(self, inp: dict): def __init__(self, inp: dict):
self.module = inp.get("module") self.module = inp.get("module")
self.stage = inp.get("stage") self.stage = inp.get("stage")
@ -17,8 +18,8 @@ class C4k(Validateable):
"https://prometheus-prod-01-eu-west-0.grafana.net/api/prom/push", "https://prometheus-prod-01-eu-west-0.grafana.net/api/prom/push",
) )
self.c4k_auth = inp.get("c4k_auth", {}) self.c4k_auth = inp.get("c4k_auth", {})
self.c4k_grafana_cloud_user = inp.get('c4k_grafana_cloud_user') 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_password = inp.get("c4k_grafana_cloud_password")
self.dns_record: Optional[DnsRecord] = None self.dns_record: Optional[DnsRecord] = None
def update_runtime_config(self, dns_record: DnsRecord): def update_runtime_config(self, dns_record: DnsRecord):
@ -63,3 +64,17 @@ class C4k(Validateable):
auth_path = f"{build_path}/out_c4k_auth.yaml" auth_path = f"{build_path}/out_c4k_auth.yaml"
output_path = f"{build_path}/out_{module}.yaml" output_path = f"{build_path}/out_{module}.yaml"
return f"c4k-{self.c4k_executable_name}-standalone.jar {config_path} {auth_path} > {output_path}" 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",
},
]

View file

@ -57,6 +57,11 @@ class Validateable:
raise ValueError(f"Invalid Validateable: {issues}") raise ValueError(f"Invalid Validateable: {issues}")
class CredentialMappingDefault:
@classmethod
def get_mapping_default(cls) -> List[map]:
return []
class DnsRecord(Validateable): class DnsRecord(Validateable):
def __init__(self, fqdn, ipv4=None, ipv6=None): def __init__(self, fqdn, ipv4=None, ipv6=None):
self.fqdn = fqdn self.fqdn = fqdn

View file

@ -36,3 +36,17 @@ class Image(Validateable):
self.image_build_commons_dir_name, self.image_build_commons_dir_name,
] ]
return "/".join(filter_none(commons_path)) + "/" 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",
},
]

View file

@ -1,8 +1,12 @@
from pathlib import Path from pathlib import Path
from typing import Dict from typing import Dict
from .common import Devops, MixinType, BuildType from .common import Devops, MixinType, BuildType, ProviderType
from .credentials import CredentialMapping, Credentials, GopassType from .credentials import CredentialMapping, Credentials, GopassType
from .devops_factory import DevopsFactory 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 .release import ReleaseType
from ..infrastructure import BuildFileRepository, CredentialsApi, EnvironmentApi, GitApi from ..infrastructure import BuildFileRepository, CredentialsApi, EnvironmentApi, GitApi
@ -35,47 +39,20 @@ class InitService:
def initialize(self, inp: dict) -> Devops: def initialize(self, inp: dict) -> Devops:
build_types = self.devops_factory.__parse_build_types__(inp["build_types"]) build_types = self.devops_factory.__parse_build_types__(inp["build_types"])
mixin_types = self.devops_factory.__parse_mixin_types__(inp["mixin_types"]) mixin_types = self.devops_factory.__parse_mixin_types__(inp["mixin_types"])
provider_types = TerraformDomain.parse_provider_types(inp["tf_provider_types"])
version = None version = None
default_mappings = [] default_mappings = []
if BuildType.C4K in build_types: if BuildType.C4K in build_types:
default_mappings += [ default_mappings += C4k.get_mapping_default()
{
"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",
},
]
if BuildType.IMAGE in build_types: if BuildType.IMAGE in build_types:
default_mappings += [ default_mappings += Image.get_mapping_default()
{ if (
"gopass_path": "meissa/web/docker.com", BuildType.TERRAFORM in build_types
"gopass_field": "login", and ProviderType.DIGITALOCEAN in provider_types
"name": "image_dockerhub_user", ):
}, default_mappings += Digitalocean.get_mapping_default()
{
"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",
},
]
if MixinType.RELEASE in mixin_types: if MixinType.RELEASE in mixin_types:
primary_build_file_id = inp.get( primary_build_file_id = inp.get(

View file

@ -1,10 +1,8 @@
from typing import List from typing import List
from .common import ( from .common import Validateable, CredentialMappingDefault
Validateable,
)
class Digitalocean(Validateable): class Digitalocean(Validateable, CredentialMappingDefault):
def __init__( def __init__(
self, self,
inp: dict, inp: dict,
@ -24,6 +22,23 @@ class Digitalocean(Validateable):
return ["provider_registry.tf", "do_provider.tf", "do_mixin_vars.tf"] return ["provider_registry.tf", "do_provider.tf", "do_mixin_vars.tf"]
def project_vars(self): def project_vars(self):
return {"do_api_key": self.do_api_key, return {
"do_spaces_access_id": self.do_spaces_access_id, "do_api_key": self.do_api_key,
"do_spaces_secret_key": self.do_spaces_secret_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",
},
]

View file

@ -34,7 +34,7 @@ class TerraformDomain(Validateable):
) )
self.tf_use_package_common_files = inp.get("tf_use_package_common_files", True) 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 = {} self.providers = {}
if ProviderType.DIGITALOCEAN in provider_types: if ProviderType.DIGITALOCEAN in provider_types:
self.providers[ProviderType.DIGITALOCEAN] = Digitalocean(inp) self.providers[ProviderType.DIGITALOCEAN] = Digitalocean(inp)
@ -76,8 +76,9 @@ class TerraformDomain(Validateable):
result += self.tf_additional_resources_from_package result += self.tf_additional_resources_from_package
return result return result
def __parse_provider_types__( @classmethod
self, tf_provider_types: List[str] def parse_provider_types(
cls, tf_provider_types: List[str]
) -> List[ProviderType]: ) -> List[ProviderType]:
result = [] result = []
for provider_type in tf_provider_types: for provider_type in tf_provider_types:

View file

@ -8,9 +8,10 @@ from src.main.python.ddadevops.domain import Devops, Release
from .domain.helper import devops_config from .domain.helper import devops_config
from .resource_helper import copy_resource from .resource_helper import copy_resource
def test_release_mixin(tmp_path): def test_release_mixin(tmp_path):
str_tmp_path = str(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") project = Project(str_tmp_path, name="name")
sut = ReleaseMixin( sut = ReleaseMixin(
@ -27,56 +28,3 @@ def test_release_mixin(tmp_path):
sut.initialize_build_dir() sut.initialize_build_dir()
assert sut.build_path() == f"{str_tmp_path}/target/name/release-test" 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", "")