Implement Hetzner provider

This commit is contained in:
bom 2023-05-26 08:29:32 +02:00
parent 3d5506ac93
commit b26cf9ff0a
8 changed files with 82 additions and 2 deletions

View file

@ -4,6 +4,7 @@ from .image import Image
from .c4k import C4k from .c4k import C4k
from .terraform import TerraformDomain from .terraform import TerraformDomain
from .provider_digitalocean import Digitalocean from .provider_digitalocean import Digitalocean
from .provider_hetzner import Hetzner
from .provs_k3s import K3s from .provs_k3s import K3s
from .release import Release from .release import Release
from .credentials import Credentials, CredentialMapping, GopassType from .credentials import Credentials, CredentialMapping, GopassType

View file

@ -15,6 +15,7 @@ class BuildType(Enum):
class ProviderType(Enum): class ProviderType(Enum):
DIGITALOCEAN = 0 DIGITALOCEAN = 0
HETZNER = 1
class MixinType(Enum): class MixinType(Enum):

View file

@ -5,6 +5,7 @@ from .credentials import CredentialMapping, Credentials, GopassType
from .devops_factory import DevopsFactory from .devops_factory import DevopsFactory
from .terraform import TerraformDomain from .terraform import TerraformDomain
from .provider_digitalocean import Digitalocean from .provider_digitalocean import Digitalocean
from .provider_hetzner import Hetzner
from .c4k import C4k from .c4k import C4k
from .image import Image from .image import Image
from .release import ReleaseType from .release import ReleaseType
@ -53,6 +54,11 @@ class InitService:
and ProviderType.DIGITALOCEAN in provider_types and ProviderType.DIGITALOCEAN in provider_types
): ):
default_mappings += Digitalocean.get_mapping_default() default_mappings += Digitalocean.get_mapping_default()
if (
BuildType.TERRAFORM in build_types
and ProviderType.HETZNER in provider_types
):
default_mappings += Hetzner.get_mapping_default()
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

@ -0,0 +1,26 @@
from typing import List, Dict
from .common import Validateable, CredentialMappingDefault
class Hetzner(Validateable, CredentialMappingDefault):
def __init__(
self,
inp: dict,
):
self.hetzner_api_key = inp.get("hetzner_api_key")
def validate(self) -> List[str]:
result = []
result += self.__validate_is_not_empty__("hetzner_api_key")
return result
def resources_from_package(self) -> List[str]:
return ["provider_registry.tf", "hetzner_provider.tf", "hetzner_mixin_vars.tf"]
def project_vars(self):
return {
"hetzner_api_key": self.hetzner_api_key
}
@classmethod
def get_mapping_default(cls) -> List[Dict[str, str]]:
return []

View file

@ -6,6 +6,7 @@ from .common import (
filter_none, filter_none,
) )
from .provider_digitalocean import Digitalocean from .provider_digitalocean import Digitalocean
from .provider_hetzner import Hetzner
class TerraformDomain(Validateable): class TerraformDomain(Validateable):
@ -36,6 +37,8 @@ class TerraformDomain(Validateable):
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)
if ProviderType.HETZNER in provider_types:
self.providers[ProviderType.HETZNER] = Hetzner(inp)
def validate(self) -> List[str]: def validate(self) -> List[str]:
result = [] result = []

View file

@ -24,7 +24,7 @@ def devops_config(overrides: dict) -> dict:
"k3s_letsencrypt_endpoint": "k3s_letsencrypt_endpoint", "k3s_letsencrypt_endpoint": "k3s_letsencrypt_endpoint",
"k3s_enable_echo": "false", "k3s_enable_echo": "false",
"k3s_app_filename_to_provision": "k3s_app.yaml", "k3s_app_filename_to_provision": "k3s_app.yaml",
"tf_provider_types": ["DIGITALOCEAN"], "tf_provider_types": ["DIGITALOCEAN", "HETZNER"],
"tf_additional_vars": [], "tf_additional_vars": [],
"tf_output_json_name": "the_out.json", "tf_output_json_name": "the_out.json",
"tf_use_workspace": None, "tf_use_workspace": None,
@ -37,6 +37,7 @@ def devops_config(overrides: dict) -> dict:
"do_api_key": "api_key", "do_api_key": "api_key",
"do_spaces_access_id": "spaces_id", "do_spaces_access_id": "spaces_id",
"do_spaces_secret_key": "spaces_secret", "do_spaces_secret_key": "spaces_secret",
"hetzner_api_key": "hetzner_api_key",
"release_type": "NONE", "release_type": "NONE",
"release_main_branch": "main", "release_main_branch": "main",
"release_current_branch": "my_feature", "release_current_branch": "my_feature",

View file

@ -0,0 +1,19 @@
from pybuilder.core import Project
from pathlib import Path
from src.main.python.ddadevops.domain import (
BuildType,
Hetzner,
)
from .helper import devops_config
def test_hetzner_creation():
sut = Hetzner(
devops_config(
{
"hetzner_api_key": "api_key",
}
)
)
assert sut is not None
assert sut.is_valid()

View file

@ -15,6 +15,7 @@ def test_creation():
assert BuildType.TERRAFORM in devops.specialized_builds assert BuildType.TERRAFORM in devops.specialized_builds
assert sut assert sut
assert sut.providers[ProviderType.DIGITALOCEAN] assert sut.providers[ProviderType.DIGITALOCEAN]
assert sut.providers[ProviderType.HETZNER]
def test_should_calculate_output_json_name(): def test_should_calculate_output_json_name():
@ -71,6 +72,7 @@ def test_should_calculate_project_vars():
"do_api_key": "api_key", "do_api_key": "api_key",
"do_spaces_access_id": "spaces_id", "do_spaces_access_id": "spaces_id",
"do_spaces_secret_key": "spaces_secret", "do_spaces_secret_key": "spaces_secret",
"hetzner_api_key": "hetzner_api_key"
} == sut.project_vars() } == sut.project_vars()
@ -83,7 +85,11 @@ def test_should_calculate_resources_from_package():
sut = TerraformDomain(config) sut = TerraformDomain(config)
assert ["versions.tf", "terraform_build_vars.tf"] == sut.resources_from_package() assert ["versions.tf", "terraform_build_vars.tf"] == sut.resources_from_package()
config = devops_config({}) config = devops_config(
{
"tf_provider_types": ["DIGITALOCEAN"]
}
)
sut = TerraformDomain(config) sut = TerraformDomain(config)
assert [ assert [
"versions.tf", "versions.tf",
@ -93,6 +99,20 @@ def test_should_calculate_resources_from_package():
"do_mixin_vars.tf", "do_mixin_vars.tf",
] == sut.resources_from_package() ] == sut.resources_from_package()
config = devops_config(
{
"tf_provider_types": ["HETZNER"]
}
)
sut = TerraformDomain(config)
assert [
"versions.tf",
"terraform_build_vars.tf",
"provider_registry.tf",
"hetzner_provider.tf",
"hetzner_mixin_vars.tf",
] == sut.resources_from_package()
config = devops_config({"tf_additional_resources_from_package": ["my.file"]}) config = devops_config({"tf_additional_resources_from_package": ["my.file"]})
sut = TerraformDomain(config) sut = TerraformDomain(config)
assert [ assert [
@ -101,5 +121,8 @@ def test_should_calculate_resources_from_package():
"provider_registry.tf", "provider_registry.tf",
"do_provider.tf", "do_provider.tf",
"do_mixin_vars.tf", "do_mixin_vars.tf",
"provider_registry.tf",
"hetzner_provider.tf",
"hetzner_mixin_vars.tf",
"my.file", "my.file",
] == sut.resources_from_package() ] == sut.resources_from_package()