Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RPD-250] Move _show_terraform_outputs() into provision #188

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/matcha_ml/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
from typing import Optional

from matcha_ml.cli._validation import get_command_validation
from matcha_ml.cli.ui.print_messages import print_status
from matcha_ml.cli.ui.status_message_builders import build_warning_status
from matcha_ml.cli.ui.print_messages import (
print_json,
print_status,
)
from matcha_ml.cli.ui.resource_message_builders import (
dict_to_json,
hide_sensitive_in_output,
)
from matcha_ml.cli.ui.status_message_builders import build_status, build_warning_status
from matcha_ml.config import MatchaConfigService
from matcha_ml.core._validation import is_valid_prefix, is_valid_region
from matcha_ml.errors import MatchaError, MatchaInputError
Expand Down Expand Up @@ -35,6 +42,18 @@ def infer_zenml_version() -> str:
return version


def _show_terraform_outputs(matcha_state: MatchaState) -> None:
"""Print the formatted Terraform outputs.

Args:
matcha_state (MatchaState): Terraform outputs in a MatchaState format.
"""
print_status(build_status("Here are the endpoints for what's been provisioned"))
resources_dict = hide_sensitive_in_output(matcha_state.to_dict())
resources_json = dict_to_json(resources_dict)
print_json(resources_json)


@track(event_name=AnalyticsEvent.GET)
def get(
resource_name: Optional[str],
Expand Down Expand Up @@ -260,8 +279,9 @@ def provision(
)
azure_template.build_template(config, template, destination, verbose)

template_runner.provision()
matcha_state_service = template_runner.provision()

matcha_state_service = MatchaStateService()
if verbose:
_show_terraform_outputs(matcha_state_service._state)

return matcha_state_service.fetch_resources_from_state_file()
11 changes: 7 additions & 4 deletions src/matcha_ml/runners/azure_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,19 @@ def remove_matcha_dir(self) -> None:
if os.path.exists(target):
shutil.rmtree(target)

def provision(self) -> None:
"""Provision resources required for the deployment."""
def provision(self) -> MatchaStateService:
"""Provision resources required for the deployment.

Returns:
(MatchaStateService): a MatchaStateService instance initialized with Terraform output
"""
self._check_terraform_installation()
self._validate_terraform_config()
self._validate_kubeconfig(base_path=".kube/config")
self._initialize_terraform(msg="Matcha")
self._apply_terraform(msg="Matcha")
tf_output = self.tfs.terraform_client.output()
matcha_state_service = MatchaStateService(terraform_output=tf_output)
self._show_terraform_outputs(matcha_state_service._state)
return MatchaStateService(terraform_output=tf_output)

def deprovision(self) -> None:
"""Destroy the provisioned resources."""
Expand Down
3 changes: 2 additions & 1 deletion src/matcha_ml/runners/base_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
TerraformConfig,
TerraformService,
)
from matcha_ml.state.matcha_state import MatchaStateService

SPINNER = "dots"

Expand Down Expand Up @@ -182,7 +183,7 @@ def _destroy_terraform(self, msg: str = "") -> None:
if tf_result.return_code != 0:
raise MatchaTerraformError(tf_error=tf_result.std_err)

def provision(self) -> Optional[Tuple[str, str, str]]:
def provision(self) -> MatchaStateService:
"""Provision resources required for the deployment."""
raise NotImplementedError

Expand Down
1 change: 1 addition & 0 deletions tests/test_cli/test_provision.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def assert_infrastructure(
with open(variables_file_path) as f:
tf_vars = json.load(f)

print(tf_vars, expected_tf_vars)
assert tf_vars == expected_tf_vars

if check_matcha_state_file:
Expand Down
57 changes: 55 additions & 2 deletions tests/test_core/test_core_provision.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
from pathlib import Path
from typing import Dict, Iterator, Union
from unittest import mock
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch

import pytest
from _pytest.capture import SysCapture

from matcha_ml.cli.ui.resource_message_builders import (
dict_to_json,
hide_sensitive_in_output,
)
from matcha_ml.core import provision
from matcha_ml.core._validation import LONGEST_RESOURCE_NAME, MAXIMUM_RESOURCE_NAME_LEN
from matcha_ml.core.core import infer_zenml_version
from matcha_ml.core.core import _show_terraform_outputs, infer_zenml_version
from matcha_ml.errors import MatchaError, MatchaInputError
from matcha_ml.services.global_parameters_service import GlobalParameters
from matcha_ml.state.matcha_state import (
Expand Down Expand Up @@ -341,3 +346,51 @@ def test_stale_remote_state_file_is_removed(matcha_testing_directory: str):
def test_version_inference_latest():
"""Test checking when zenml isn't installed, the latest version is returned."""
assert infer_zenml_version() == "latest"


def test_show_terraform_outputs(state_file_as_object, capsys: SysCapture):
"""Tests the _show_terraform_outputs function makes the relevant function calls and outputs the desired string.

Args:
state_file_as_object (MatchaState): a mocked MatchaState object for use in testing
capsys (SysCapture): fixture to capture stdout and stderr
"""
expected_output = """Here are the endpoints for what's been provisioned"""
expected_cloud = """"cloud": {
"flavor": "azure",
"resource-group-name": "test_resources"
}"""
expected_container_registry = """"container-registry": {
"flavor": "azure",
"registry-name": "azure_registry_name",
"registry-url": "azure_container_registry"
}"""
expected_pipeline = """"pipeline": {
"flavor": "zenml",
"connection-string": "********",
"server-password": "********",
"server-url": "zen_server_url"
}"""
expected_experiment_tracker = """"experiment-tracker": {
"flavor": "mlflow",
"tracking-url": "mlflow_test_url"
}"""

with patch(
"matcha_ml.core.core.dict_to_json", side_effect=dict_to_json
) as mocked_dict_to_json, patch(
"matcha_ml.core.core.hide_sensitive_in_output",
side_effect=hide_sensitive_in_output,
) as mocked_hide_sensitive_output:
_show_terraform_outputs(state_file_as_object)

mocked_hide_sensitive_output.assert_called()
mocked_dict_to_json.assert_called_once()

captured = capsys.readouterr()

assert expected_output in captured.out
assert expected_cloud in captured.out
assert expected_container_registry in captured.out
assert expected_pipeline in captured.out
assert expected_experiment_tracker in captured.out