refactor to domain-object creation by dict

merge-requests/12/head
Michael Jerger 1 year ago
parent 481d20a14c
commit 79de907396

@ -1,7 +1,9 @@
from typing import Optional from typing import Optional
from subprocess import run, CalledProcessError from subprocess import run, CalledProcessError
import deprecation import deprecation
from .domain import Devops from .domain import (
Devops, DevopsFactory
)
from .infrastructure import ProjectRepository, FileApi from .infrastructure import ProjectRepository, FileApi
@ -36,21 +38,12 @@ def get_tag_from_latest_commit():
class DevopsBuild: class DevopsBuild:
def __init__(self, project, config: Optional[dict] = None, devops: Optional[Devops] = None): def __init__(self, project, input: dict):
self.project = project self.project = project
self.file_api = FileApi() self.file_api = FileApi()
self.devops_factory = DevopsFactory()
self.repo = ProjectRepository() self.repo = ProjectRepository()
if not devops: devops = self.devops_factory.build_devops(input)
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"],
)
self.repo.set_devops(self.project, devops) self.repo.set_devops(self.project, devops)
self.repo.set_build(self.project, self) self.repo.set_build(self.project, self)

@ -33,24 +33,9 @@ def create_devops_docker_build_config(
class DevopsImageBuild(DevopsBuild): 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() 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): def initialize_build_dir(self):
super().initialize_build_dir() super().initialize_build_dir()

@ -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 .image import Image
from .c4k import C4k from .c4k import C4k
from .release import Release, ReleaseContext, ReleaseType, Version, EnvironmentKeys from .release import Release, ReleaseContext, ReleaseType, Version, EnvironmentKeys

@ -1,7 +1,5 @@
import deprecation import deprecation
from enum import Enum from enum import Enum
# TODO: remove logging from domain!
import logging
from typing import List from typing import List
def filter_none(list_to_filter): def filter_none(list_to_filter):
@ -37,30 +35,28 @@ class DnsRecord(Validateable):
class Devops(Validateable): class Devops(Validateable):
def __init__( def __init__(self, input: dict, specialized_build: Validateable):
self, stage: str, project_root_path: str, module: str, name: str | None =None, build_dir_name: str="target" self.stage = input.get('stage')
): self.project_root_path = input.get('project_root_path')
self.stage = stage self.module = input.get('module')
self.name = name self.name = input.get('name', self.module)
self.project_root_path = project_root_path self.build_dir_name = input.get('build_dir_name', 'target')
logging.warn(f"Set project root in DevOps {self.project_root_path}") self.specialized_build=specialized_build
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 build_path(self): def build_path(self):
path = [self.project_root_path, self.build_dir_name, self.name, self.module] 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)) 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): def __put__(self, key, value):
self.stack[key] = value self.stack[key] = value
@ -73,39 +69,6 @@ class Devops(Validateable):
result[key] = self.__get__(key) result[key] = self.__get__(key)
return result return result
class BuildType(Enum): class BuildType(Enum):
IMAGE = 0 IMAGE = 0
C4K = 1 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

@ -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

@ -1,4 +1,4 @@
from typing import Optional from typing import (Optional, List)
from .common import ( from .common import (
filter_none, filter_none,
Validateable, Validateable,
@ -8,22 +8,21 @@ from .common import (
class Image(Validateable): class Image(Validateable):
def __init__( def __init__(
self, self,
dockerhub_user, input,
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,
): ):
self.dockerhub_user = dockerhub_user self.dockerhub_user=input.get('dockerhub_user')
self.dockerhub_password = dockerhub_password self.dockerhub_password=input.get('dockerhub_password')
self.use_package_common_files = use_package_common_files self.docker_publish_tag=input.get('docker_publish_tag')
self.build_commons_path = build_commons_path self.build_commons_path = input.get('build_commons_path')
self.docker_build_commons_dir_name = docker_build_commons_dir_name self.docker_publish_tag = input.get('docker_publish_tag')
self.docker_publish_tag = docker_publish_tag self.use_package_common_files = input.get('use_package_common_files', True)
self.devops = devops 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): def docker_build_commons_path(self):
list = [self.build_commons_path, self.docker_build_commons_dir_name] list = [self.build_commons_path, self.docker_build_commons_dir_name]

@ -1,14 +1,8 @@
import pytest import pytest
from src.main.python.ddadevops.domain.common import ( from src.main.python.ddadevops.domain.common import (
Devops, Devops,
DevopsFactory,
) )
def test_devops_factory():
with pytest.raises(Exception):
DevopsFactory().build_devops({'build_type': 'NOTEXISTING'})
def test_devops_buildpath(): def test_devops_buildpath():
sut = Devops( sut = Devops(
stage="test", project_root_path="../../..", module="cloud", name="meissa" stage="test", project_root_path="../../..", module="cloud", name="meissa"

@ -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

@ -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)
Loading…
Cancel
Save