diff --git a/src/main/python/ddadevops/devops_build.py b/src/main/python/ddadevops/devops_build.py index cc3f860..19f2ab5 100644 --- a/src/main/python/ddadevops/devops_build.py +++ b/src/main/python/ddadevops/devops_build.py @@ -1,7 +1,9 @@ from typing import Optional from subprocess import run, CalledProcessError import deprecation -from .domain import Devops +from .domain import ( + Devops, DevopsFactory +) from .infrastructure import ProjectRepository, FileApi @@ -36,21 +38,12 @@ def get_tag_from_latest_commit(): class DevopsBuild: - def __init__(self, project, config: Optional[dict] = None, devops: Optional[Devops] = None): + def __init__(self, project, input: dict): self.project = project self.file_api = FileApi() + self.devops_factory = DevopsFactory() self.repo = ProjectRepository() - if not devops: - if not config: - raise ValueError("Build parameters could not be set!") - devops = Devops( - stage=config["stage"], - project_root_path=config["project_root_path"], - module=config["module"], - name=project.name, - build_dir_name=config["build_dir_name"], - ) - + devops = self.devops_factory.build_devops(input) self.repo.set_devops(self.project, devops) self.repo.set_build(self.project, self) diff --git a/src/main/python/ddadevops/devops_image_build.py b/src/main/python/ddadevops/devops_image_build.py index a5e6e65..f1a2e30 100644 --- a/src/main/python/ddadevops/devops_image_build.py +++ b/src/main/python/ddadevops/devops_image_build.py @@ -33,24 +33,9 @@ def create_devops_docker_build_config( class DevopsImageBuild(DevopsBuild): - def __init__(self, project, config: Optional[dict] = None, image: Optional[Image] = None): + def __init__(self, project, input: dict): + super().__init__(project, input) self.image_build_service = ImageBuildService() - if not image: - if not config: - raise ValueError("Image parameters could not be set.") - super().__init__(project, config=config) - image = Image( - dockerhub_user=config["dockerhub_user"], - dockerhub_password=config["dockerhub_password"], - devops=self.repo.get_devops(project), - use_package_common_files=config["use_package_common_files"], - build_commons_path=config["build_commons_path"], - docker_build_commons_dir_name=config["docker_build_commons_dir_name"], - docker_publish_tag=config["docker_publish_tag"], - ) - else: - super().__init__(project, devops=image.devops) - self.repo.set_docker(self.project, image) def initialize_build_dir(self): super().initialize_build_dir() diff --git a/src/main/python/ddadevops/domain/__init__.py b/src/main/python/ddadevops/domain/__init__.py index 9601d03..d6b5a11 100644 --- a/src/main/python/ddadevops/domain/__init__.py +++ b/src/main/python/ddadevops/domain/__init__.py @@ -1,4 +1,5 @@ -from .common import Validateable, DnsRecord, Devops, DevopsFactory +from .common import Validateable, DnsRecord, Devops +from .devops_factory import DevopsFactory from .image import Image from .c4k import C4k from .release import Release, ReleaseContext, ReleaseType, Version, EnvironmentKeys diff --git a/src/main/python/ddadevops/domain/common.py b/src/main/python/ddadevops/domain/common.py index d038ec6..98bb33c 100644 --- a/src/main/python/ddadevops/domain/common.py +++ b/src/main/python/ddadevops/domain/common.py @@ -1,7 +1,5 @@ import deprecation from enum import Enum -# TODO: remove logging from domain! -import logging from typing import List def filter_none(list_to_filter): @@ -37,30 +35,28 @@ class DnsRecord(Validateable): class Devops(Validateable): - def __init__( - self, stage: str, project_root_path: str, module: str, name: str | None =None, build_dir_name: str="target" - ): - self.stage = stage - self.name = name - self.project_root_path = project_root_path - logging.warn(f"Set project root in DevOps {self.project_root_path}") - self.module = module - if not name: - self.name = module - self.build_dir_name = build_dir_name - # Deprecated - no longer use generic stack ... - self.stack = {} - - @deprecation.deprecated(deprecated_in="3.2") - # use .name instead - def name(self): - return self.name + def __init__(self, input: dict, specialized_build: Validateable): + self.stage = input.get('stage') + self.project_root_path = input.get('project_root_path') + self.module = input.get('module') + self.name = input.get('name', self.module) + self.build_dir_name = input.get('build_dir_name', 'target') + self.specialized_build=specialized_build def build_path(self): path = [self.project_root_path, self.build_dir_name, self.name, self.module] - logging.warn(f"Set project build_path in Devops {path}") return "/".join(filter_none(path)) + def validate(self) -> List[str]: + result = [] + result += self.__validate_is_not_empty__("stage") + result += self.__validate_is_not_empty__("project_root_path") + result += self.__validate_is_not_empty__("module") + result += self.__validate_is_not_empty__("specialized_build") + if self.specialized_build: + result += self.specialized_build.validate() + return result + def __put__(self, key, value): self.stack[key] = value @@ -73,39 +69,6 @@ class Devops(Validateable): result[key] = self.__get__(key) return result - class BuildType(Enum): IMAGE = 0 C4K = 1 - -class DevopsFactory(): - def __init__(self): - pass - - def build_devops(self, input) -> Devops: - build_type = BuildType[input['build_type']] - specialized_build = None - if build_type == BuildType.IMAGE: - specialized_build = Image( - dockerhub_user=input['dockerhub_user'], - dockerhub_password=input['dockerhub_password'], - docker_publish_tag=input['tag'] - ) - elif build_type == BuildType.C4K: - pass - - devops = Devops( - stage=input['stage'], - project_root_path=input['project_root_path'], - module=input['module'], - name=input['name'], - specialized_build=specialized_build - ) - - # TODO: validate devops - - return devops - - def merge(input, autorization, context) -> dict: - pass - diff --git a/src/main/python/ddadevops/domain/devops_factory.py b/src/main/python/ddadevops/domain/devops_factory.py new file mode 100644 index 0000000..61a3369 --- /dev/null +++ b/src/main/python/ddadevops/domain/devops_factory.py @@ -0,0 +1,32 @@ +import deprecation +from enum import Enum +from typing import List +from .common import ( + Devops, + BuildType +) +from .image import ( + Image, +) + +class DevopsFactory(): + def __init__(self): + pass + + def build_devops(self, input) -> Devops: + build_type = BuildType[input['build_type']] + specialized_build = None + if build_type == BuildType.IMAGE: + specialized_build = Image(input) + elif build_type == BuildType.C4K: + pass + + devops = Devops(input, specialized_build=specialized_build) + + # TODO: validate devops + + return devops + + def merge(input, autorization, context) -> dict: + pass + diff --git a/src/main/python/ddadevops/domain/image.py b/src/main/python/ddadevops/domain/image.py index 49ca555..4a67ee4 100644 --- a/src/main/python/ddadevops/domain/image.py +++ b/src/main/python/ddadevops/domain/image.py @@ -1,4 +1,4 @@ -from typing import Optional +from typing import (Optional, List) from .common import ( filter_none, Validateable, @@ -8,22 +8,21 @@ from .common import ( class Image(Validateable): def __init__( self, - dockerhub_user, - dockerhub_password, - devops: Devops, - build_dir_name="target", - use_package_common_files=True, - build_commons_path=None, - docker_build_commons_dir_name="docker", - docker_publish_tag=None, + input, ): - self.dockerhub_user = dockerhub_user - self.dockerhub_password = dockerhub_password - self.use_package_common_files = use_package_common_files - self.build_commons_path = build_commons_path - self.docker_build_commons_dir_name = docker_build_commons_dir_name - self.docker_publish_tag = docker_publish_tag - self.devops = devops + self.dockerhub_user=input.get('dockerhub_user') + self.dockerhub_password=input.get('dockerhub_password') + self.docker_publish_tag=input.get('docker_publish_tag') + self.build_commons_path = input.get('build_commons_path') + self.docker_publish_tag = input.get('docker_publish_tag') + self.use_package_common_files = input.get('use_package_common_files', True) + self.docker_build_commons_dir_name = input.get('docker_build_commons_dir_name', 'docker') + + def validate(self) -> List[str]: + result = [] + result += self.__validate_is_not_empty__("dockerhub_user") + result += self.__validate_is_not_empty__("dockerhub_password") + return result def docker_build_commons_path(self): list = [self.build_commons_path, self.docker_build_commons_dir_name] diff --git a/src/test/python/domain/test_devops.py b/src/test/python/domain/test_devops.py index 3889454..29f94ad 100644 --- a/src/test/python/domain/test_devops.py +++ b/src/test/python/domain/test_devops.py @@ -1,14 +1,8 @@ import pytest from src.main.python.ddadevops.domain.common import ( Devops, - DevopsFactory, ) -def test_devops_factory(): - with pytest.raises(Exception): - DevopsFactory().build_devops({'build_type': 'NOTEXISTING'}) - - def test_devops_buildpath(): sut = Devops( stage="test", project_root_path="../../..", module="cloud", name="meissa" diff --git a/src/test/python/domain/test_devops_factory.py b/src/test/python/domain/test_devops_factory.py new file mode 100644 index 0000000..df7e15f --- /dev/null +++ b/src/test/python/domain/test_devops_factory.py @@ -0,0 +1,27 @@ +import pytest +from src.main.python.ddadevops.domain.devops_factory import ( + DevopsFactory, +) + +def test_devops_factory(): + with pytest.raises(Exception): + DevopsFactory().build_devops( + {'build_type': 'NOTEXISTING'} + ) + + with pytest.raises(Exception): + DevopsFactory().build_devops( + {'build_type': 'IMAGE'} + ) + + sut = DevopsFactory().build_devops( + {'build_type': 'IMAGE', + 'stage': 'test', + 'project_root_path': "../../..", + 'name': 'mybuild', + 'module': 'test_image', + 'dockerhub_user': 'dockerhub_user', + 'dockerhub_password': 'dockerhub_password', + 'docker_image_tag': 'docker_image_tag',} + ) + assert sut != None diff --git a/src/test/python/domain/test_helper.py b/src/test/python/domain/test_helper.py new file mode 100644 index 0000000..aec7093 --- /dev/null +++ b/src/test/python/domain/test_helper.py @@ -0,0 +1,9 @@ +from src.main.python.ddadevops.domain import ( + DevopsFactory, Devops +) + +def build_devops(overrides: dict) -> Devops: + default = {} + input = default.copy() + input.update(overrides) + return DevopsFactory().build_devops(input)