Skip to content

Commit

Permalink
refactor: Unit tests for environment command into the new format (#708)
Browse files Browse the repository at this point in the history
Co-authored-by: Connor Hindle <Connor.Hindle@digital.trade.gov.uk>
Co-authored-by: Connor Hindle <69192234+DeveloperConnor@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 9, 2025
1 parent fc7afb4 commit f10efe2
Show file tree
Hide file tree
Showing 7 changed files with 773 additions and 538 deletions.
18 changes: 13 additions & 5 deletions dbt_platform_helper/commands/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dbt_platform_helper.constants import PLATFORM_CONFIG_FILE
from dbt_platform_helper.domain.config_validator import ConfigValidator
from dbt_platform_helper.domain.copilot_environment import CopilotEnvironment
from dbt_platform_helper.domain.maintenance_page import MaintenancePageProvider
from dbt_platform_helper.domain.maintenance_page import MaintenancePage
from dbt_platform_helper.domain.terraform_environment import TerraformEnvironment
from dbt_platform_helper.platform_exception import PlatformException
from dbt_platform_helper.providers.config import ConfigProvider
Expand Down Expand Up @@ -37,7 +37,7 @@ def environment():
def offline(app, env, svc, template, vpc):
"""Take load-balanced web services offline with a maintenance page."""
try:
MaintenancePageProvider().activate(app, env, svc, template, vpc)
MaintenancePage().activate(app, env, svc, template, vpc)
except PlatformException as err:
click.secho(str(err), fg="red")
raise click.Abort
Expand All @@ -49,7 +49,7 @@ def offline(app, env, svc, template, vpc):
def online(app, env):
"""Remove a maintenance page from an environment."""
try:
MaintenancePageProvider().deactivate(app, env)
MaintenancePage().deactivate(app, env)
except PlatformException as err:
click.secho(str(err), fg="red")
raise click.Abort
Expand All @@ -71,6 +71,9 @@ def generate(name, vpc_name):
except SchemaError as ex:
click.secho(f"Invalid `{PLATFORM_CONFIG_FILE}` file: {str(ex)}", fg="red")
raise click.Abort
except PlatformException as err:
click.secho(str(err), fg="red")
raise click.Abort


@environment.command(help="Generate terraform manifest for the specified environment.")
Expand All @@ -82,5 +85,10 @@ def generate(name, vpc_name):
help=f"Override the default version of terraform-platform-modules. (Default version is '{DEFAULT_TERRAFORM_PLATFORM_MODULES_VERSION}').",
)
def generate_terraform(name, terraform_platform_modules_version):
config_provider = ConfigProvider(ConfigValidator())
TerraformEnvironment(config_provider).generate(name, terraform_platform_modules_version)

try:
config_provider = ConfigProvider(ConfigValidator())
TerraformEnvironment(config_provider).generate(name, terraform_platform_modules_version)
except PlatformException as err:
click.secho(str(err), fg="red")
raise click.Abort
4 changes: 2 additions & 2 deletions dbt_platform_helper/domain/database_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from dbt_platform_helper.constants import PLATFORM_CONFIG_FILE
from dbt_platform_helper.domain.config_validator import ConfigValidator
from dbt_platform_helper.domain.maintenance_page import MaintenancePageProvider
from dbt_platform_helper.domain.maintenance_page import MaintenancePage
from dbt_platform_helper.providers.aws import AWSException
from dbt_platform_helper.providers.config import ConfigProvider
from dbt_platform_helper.utils.application import Application
Expand All @@ -34,7 +34,7 @@ def __init__(
] = get_connection_string,
maintenance_page_provider: Callable[
[str, str, list[str], str, str], None
] = MaintenancePageProvider(),
] = MaintenancePage(),
input: Callable[[str], str] = click.prompt,
echo: Callable[[str], str] = click.secho,
abort: Callable[[str], None] = abort_with_error,
Expand Down
2 changes: 1 addition & 1 deletion dbt_platform_helper/domain/maintenance_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from dbt_platform_helper.utils.application import load_application


class MaintenancePageProvider:
class MaintenancePage:
def activate(self, app, env, svc, template, vpc):
application = load_application(app)
application_environment = get_app_environment(app, env)
Expand Down
5 changes: 5 additions & 0 deletions dbt_platform_helper/providers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from dbt_platform_helper.constants import PLATFORM_CONFIG_FILE
from dbt_platform_helper.domain.config_validator import ConfigValidator
from dbt_platform_helper.providers.platform_config_schema import PlatformConfigSchema
from dbt_platform_helper.providers.yaml_file import FileNotFoundException
from dbt_platform_helper.providers.yaml_file import FileProvider
from dbt_platform_helper.providers.yaml_file import FileProviderException
from dbt_platform_helper.providers.yaml_file import YamlFileProvider
Expand Down Expand Up @@ -35,6 +36,10 @@ def validate_platform_config(self):
def load_and_validate_platform_config(self, path=PLATFORM_CONFIG_FILE):
try:
self.config = self.file_provider.load(path)
except FileNotFoundException as e:
abort_with_error(
f"{e} Please check it exists and you are in the root directory of your deployment project."
)
except FileProviderException as e:
abort_with_error(f"Error loading configuration from {path}: {e}")

Expand Down
7 changes: 2 additions & 5 deletions dbt_platform_helper/providers/yaml_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class YamlFileProviderException(FileProviderException):
pass


class FileNotFoundException(YamlFileProviderException):
class FileNotFoundException(FileProviderException):
pass


Expand All @@ -43,10 +43,7 @@ def load(path: str) -> dict:
DuplicateKeysException: yaml contains duplicate keys
"""
if not Path(path).exists():
# TODO this error message is domain specific and should not mention deployment directory project here
raise FileNotFoundException(
f"`{path}` is missing. Please check it exists and you are in the root directory of your deployment project."
)
raise FileNotFoundException(f"`{path}` is missing.")
try:
yaml_content = yaml.safe_load(Path(path).read_text())
except ParserError:
Expand Down
Loading

0 comments on commit f10efe2

Please sign in to comment.