Skip to content

Commit

Permalink
Merge pull request #145 from baloise/feat/return-preview-info
Browse files Browse the repository at this point in the history
feat(create-preview): return preview info as tmp file
  • Loading branch information
christiansiegel authored Jan 28, 2021
2 parents 0c5dad9 + cce81a3 commit c15cbdc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
11 changes: 11 additions & 0 deletions docs/commands/create-preview.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ previewConfig:
variable: ROUTE_HOST # this is the resolved host.template from above
```
## Returned Information
After running this command you'll find a YAML file at `/tmp/gitopscli-preview-info.yaml`. It contains generated information about your preview environment:

```yaml
previewId: PREVIEW_ID
previewIdHash: 685912d3
routeHost: app.xy-685912d3.example.tld
namespace: my-app-685912d3-preview
```

## Example

```bash
Expand Down
15 changes: 14 additions & 1 deletion gitopscli/commands/create_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dataclasses import dataclass
from typing import Any, Callable, Dict
from gitopscli.git_api import GitApiConfig, GitRepo, GitRepoApi, GitRepoApiFactory
from gitopscli.io_api.yaml_util import update_yaml_file, YAMLException
from gitopscli.io_api.yaml_util import update_yaml_file, YAMLException, yaml_file_dump
from gitopscli.gitops_config import GitOpsConfig
from gitopscli.gitops_exception import GitOpsException
from .common import load_gitops_config
Expand Down Expand Up @@ -41,6 +41,7 @@ def register_callbacks(

def execute(self,) -> None:
gitops_config = self.__get_gitops_config()
self.__create_preview_info_file(gitops_config)
route_host = gitops_config.get_route_host(self.__args.preview_id)

team_config_git_repo_api = self.__create_team_config_git_repo_api(gitops_config)
Expand Down Expand Up @@ -121,6 +122,18 @@ def __replace_values(self, git_repo: GitRepo, gitops_config: GitOpsConfig) -> bo
logging.info("Keep property '%s' value: %s", replacement.path, replacement_value)
return any_value_replaced

def __create_preview_info_file(self, gitops_config: GitOpsConfig) -> None:
preview_id = self.__args.preview_id
yaml_file_dump(
{
"previewId": preview_id,
"previewIdHash": gitops_config.create_hashed_preview_id(preview_id),
"routeHost": gitops_config.get_route_host(preview_id),
"namespace": gitops_config.get_preview_namespace(preview_id),
},
"/tmp/gitopscli-preview-info.yaml",
)

@staticmethod
def __update_yaml_file(git_repo: GitRepo, file_path: str, key: str, value: Any) -> bool:
full_file_path = git_repo.get_full_file_path(file_path)
Expand Down
6 changes: 3 additions & 3 deletions gitopscli/gitops_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ def __post_init__(self) -> None:
assert isinstance(replacement, self.Replacement), f"replacement[{index}] of wrong type!"

def get_route_host(self, preview_id: str) -> str:
hashed_preview_id = self.__create_hashed_preview_id(preview_id)
hashed_preview_id = self.create_hashed_preview_id(preview_id)
return self.route_host_template.replace("{SHA256_8CHAR_BRANCH_HASH}", hashed_preview_id)

def get_preview_namespace(self, preview_id: str) -> str:
hashed_preview_id = self.__create_hashed_preview_id(preview_id)
hashed_preview_id = self.create_hashed_preview_id(preview_id)
return f"{self.application_name}-{hashed_preview_id}-preview"

@staticmethod
def __create_hashed_preview_id(preview_id: str) -> str:
def create_hashed_preview_id(preview_id: str) -> str:
return hashlib.sha256(preview_id.encode("utf-8")).hexdigest()[:8]

@staticmethod
Expand Down
28 changes: 27 additions & 1 deletion tests/commands/test_create_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import shutil
import logging
from unittest.mock import call, Mock
from gitopscli.io_api.yaml_util import update_yaml_file, YAMLException
from gitopscli.io_api.yaml_util import update_yaml_file, YAMLException, yaml_file_dump
from gitopscli.git_api import GitRepo, GitRepoApi, GitRepoApiFactory, GitProvider
from gitopscli.gitops_config import GitOpsConfig
from gitopscli.gitops_exception import GitOpsException
Expand All @@ -25,6 +25,13 @@
git_hash=DUMMY_GIT_HASH,
)

INFO_YAML = {
"previewId": "PREVIEW_ID",
"previewIdHash": "685912d3",
"routeHost": "app.xy-685912d3.example.tld",
"namespace": "my-app-685912d3-preview",
}


class CreatePreviewCommandTest(MockMixin, unittest.TestCase):
def setUp(self):
Expand All @@ -42,6 +49,9 @@ def setUp(self):
self.update_yaml_file_mock = self.monkey_patch(update_yaml_file)
self.update_yaml_file_mock.return_value = True

self.yaml_file_dump_mock = self.monkey_patch(yaml_file_dump)
self.yaml_file_dump_mock.return_value = None

self.load_gitops_config_mock = self.monkey_patch(load_gitops_config)
self.load_gitops_config_mock.return_value = GitOpsConfig(
team_config_org="TEAM_CONFIG_ORG",
Expand Down Expand Up @@ -90,6 +100,15 @@ def test_create_new_preview(self):

assert self.mock_manager.method_calls == [
call.load_gitops_config(ARGS, "ORGA", "REPO",),
call.yaml_file_dump(
{
"previewId": "PREVIEW_ID",
"previewIdHash": "685912d3",
"routeHost": "app.xy-685912d3.example.tld",
"namespace": "my-app-685912d3-preview",
},
"/tmp/gitopscli-preview-info.yaml",
),
call.GitRepoApiFactory.create(ARGS, "TEAM_CONFIG_ORG", "TEAM_CONFIG_REPO",),
call.GitRepo(self.git_repo_api_mock),
call.GitRepo.clone(),
Expand Down Expand Up @@ -147,6 +166,7 @@ def test_update_existing_preview(self):

assert self.mock_manager.method_calls == [
call.load_gitops_config(ARGS, "ORGA", "REPO",),
call.yaml_file_dump(INFO_YAML, "/tmp/gitopscli-preview-info.yaml",),
call.GitRepoApiFactory.create(ARGS, "TEAM_CONFIG_ORG", "TEAM_CONFIG_REPO",),
call.GitRepo(self.git_repo_api_mock),
call.GitRepo.clone(),
Expand Down Expand Up @@ -196,6 +216,7 @@ def test_preview_already_up_to_date(self):

assert self.mock_manager.method_calls == [
call.load_gitops_config(ARGS, "ORGA", "REPO",),
call.yaml_file_dump(INFO_YAML, "/tmp/gitopscli-preview-info.yaml",),
call.GitRepoApiFactory.create(ARGS, "TEAM_CONFIG_ORG", "TEAM_CONFIG_REPO",),
call.GitRepo(self.git_repo_api_mock),
call.GitRepo.clone(),
Expand Down Expand Up @@ -231,6 +252,7 @@ def test_create_preview_for_unknown_template(self):

assert self.mock_manager.method_calls == [
call.load_gitops_config(ARGS, "ORGA", "REPO",),
call.yaml_file_dump(INFO_YAML, "/tmp/gitopscli-preview-info.yaml",),
call.GitRepoApiFactory.create(ARGS, "TEAM_CONFIG_ORG", "TEAM_CONFIG_REPO",),
call.GitRepo(self.git_repo_api_mock),
call.GitRepo.clone(),
Expand All @@ -252,6 +274,7 @@ def test_create_preview_values_yaml_not_found(self):

assert self.mock_manager.method_calls == [
call.load_gitops_config(ARGS, "ORGA", "REPO",),
call.yaml_file_dump(INFO_YAML, "/tmp/gitopscli-preview-info.yaml",),
call.GitRepoApiFactory.create(ARGS, "TEAM_CONFIG_ORG", "TEAM_CONFIG_REPO",),
call.GitRepo(self.git_repo_api_mock),
call.GitRepo.clone(),
Expand All @@ -277,6 +300,7 @@ def test_create_preview_values_yaml_parse_error(self):

assert self.mock_manager.method_calls == [
call.load_gitops_config(ARGS, "ORGA", "REPO",),
call.yaml_file_dump(INFO_YAML, "/tmp/gitopscli-preview-info.yaml",),
call.GitRepoApiFactory.create(ARGS, "TEAM_CONFIG_ORG", "TEAM_CONFIG_REPO",),
call.GitRepo(self.git_repo_api_mock),
call.GitRepo.clone(),
Expand All @@ -302,6 +326,7 @@ def test_create_preview_with_invalid_replacement_path(self):

assert self.mock_manager.method_calls == [
call.load_gitops_config(ARGS, "ORGA", "REPO",),
call.yaml_file_dump(INFO_YAML, "/tmp/gitopscli-preview-info.yaml",),
call.GitRepoApiFactory.create(ARGS, "TEAM_CONFIG_ORG", "TEAM_CONFIG_REPO",),
call.GitRepo(self.git_repo_api_mock),
call.GitRepo.clone(),
Expand Down Expand Up @@ -332,6 +357,7 @@ def test_create_new_preview_invalid_chart_template(self):

assert self.mock_manager.method_calls == [
call.load_gitops_config(ARGS, "ORGA", "REPO",),
call.yaml_file_dump(INFO_YAML, "/tmp/gitopscli-preview-info.yaml",),
call.GitRepoApiFactory.create(ARGS, "TEAM_CONFIG_ORG", "TEAM_CONFIG_REPO",),
call.GitRepo(self.git_repo_api_mock),
call.GitRepo.clone(),
Expand Down

0 comments on commit c15cbdc

Please sign in to comment.