Skip to content

Commit

Permalink
Feature/rcs/environment template context (#125)
Browse files Browse the repository at this point in the history
* add template context environment structure

* add template context environment resource_group parameter
  • Loading branch information
blueskyjunkie authored Jan 31, 2022
1 parent 0cf155e commit a298aa1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 13 deletions.
4 changes: 3 additions & 1 deletion foodx_devops_tools/deploy_me/application_steps/_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ async def _do_step_deployment(
else:
log.info(f"deployment enabled, {step_context}")

template_parameters = deployment_data.construct_template_parameters()
template_parameters = deployment_data.construct_template_parameters(
resource_group
)
log.debug(f"template parameters, {step_context}, {template_parameters}")

template_files = deployment_data.construct_deployment_paths(
Expand Down
45 changes: 42 additions & 3 deletions foodx_devops_tools/pipeline_config/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ class DeployDataView:
release_state: str
root_fqdn: str
static_secrets: dict
subscription_id: str
template_context: dict
tenant_id: str
url_endpoints: typing.List[str]

frame_folder: typing.Optional[pathlib.Path] = None
Expand All @@ -231,9 +233,11 @@ def __init__(
azure_credentials: AzureCredentials,
deployment_tuple: str,
location_primary: str,
root_fqdn: str,
release_state: str,
root_fqdn: str,
static_secrets: dict,
subscription_id: str,
tenant_id: str,
url_endpoints: typing.List[str],
user_defined_template_context: dict,
location_secondary: typing.Optional[str] = None,
Expand All @@ -245,6 +249,8 @@ def __init__(
self.release_state = release_state
self.root_fqdn = root_fqdn
self.static_secrets = static_secrets
self.subscription_id = subscription_id
self.tenant_id = tenant_id
self.template_context = user_defined_template_context
self.url_endpoints = url_endpoints
self.__location_secondary = location_secondary
Expand Down Expand Up @@ -370,9 +376,27 @@ def construct_app_urls(self: W) -> TemplateParameters:

return result

def construct_template_parameters(self: W) -> TemplateParameters:
"""Construct set of parameters for jinja2 templates."""
def construct_template_parameters(
self: W, resource_group_name: typing.Optional[str] = None
) -> TemplateParameters:
"""
Construct set of parameters for jinja2 templates.
Args:
resource_group_name: Name of current resource group being
deployed to.
Returns:
Dict of parameters to be applied to jinja2 templating.
"""
engine_data = {
"environment": {
"azure": {
"subscription_id": self.data.subscription_id,
"tenant_id": self.data.tenant_id,
},
"resource_group": resource_group_name,
},
"locations": {
"primary": self.data.location_primary,
"secondary": self.data.location_secondary,
Expand Down Expand Up @@ -605,6 +629,19 @@ def deploy_data(self: V) -> typing.List[DeployDataView]:
"secrets, {0}".format(self.subscription_name)
)

subscription_id = (
self.deployment_view.release_view.configuration.subscriptions[
self.subscription_name
].azure_id
)
tenant_name = (
self.deployment_view.release_view.configuration.subscriptions[
self.subscription_name
].tenant
)
tenant_id = self.deployment_view.release_view.configuration.tenants[
tenant_name
].azure_id
this_data: typing.Dict[str, typing.Any] = {
"azure_credentials": this_credentials,
"deployment_tuple": str(self.deployment_view.deployment_tuple),
Expand All @@ -615,6 +652,8 @@ def deploy_data(self: V) -> typing.List[DeployDataView]:
].root_fqdn,
"release_state": self.deployment_view.release_view.deployment_context.release_state, # noqa: E501
"static_secrets": static_secrets,
"subscription_id": subscription_id,
"tenant_id": tenant_id,
"url_endpoints": self.deployment_view.release_view.configuration.deployments.url_endpoints, # noqa: E501
"user_defined_template_context": self.deployment_view.release_view.configuration.context, # noqa: E501
}
Expand Down
18 changes: 10 additions & 8 deletions tests/ci/unit_tests/deploy_me/deployment/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,22 @@ def mock_base_context():
mock_context.frame_name = "f1"

data_view = DeployDataView(
AzureCredentials(
azure_credentials=AzureCredentials(
userid="abc",
secret="verysecret",
subscription="sys1_c1_r1a",
name="n",
tenant="123abc",
),
"a-b-c",
"uswest2",
"r1",
"some.where",
dict(),
["a", "p"],
dict(),
deployment_tuple="a-b-c",
location_primary="uswest2",
release_state="r1",
root_fqdn="some.where",
static_secrets=dict(),
subscription_id="s-12345",
tenant_id="t-abc",
url_endpoints=["a", "p"],
user_defined_template_context=dict(),
)
data_view.frame_folder = pathlib.Path("frame/folder")

Expand Down
21 changes: 20 additions & 1 deletion tests/ci/unit_tests/pipeline_config/views/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,17 @@ def test_construct_template_parameters(self, mock_flattened_deployment):
"v2": "vv2",
}

result = under_test.construct_template_parameters()
result = under_test.construct_template_parameters("this_name")

assert result == {
"context": {
"environment": {
"azure": {
"subscription_id": "abc123",
"tenant_id": "123abc",
},
"resource_group": "this_name",
},
"v1": {"k1": 3.14},
"v2": "vv2",
"locations": {
Expand Down Expand Up @@ -141,6 +148,18 @@ def test_construct_template_parameters(self, mock_flattened_deployment):
}
}

def test_default_resource_group(self, mock_flattened_deployment):
under_test = mock_flattened_deployment[0]
under_test.context.frame_name = "f1"
under_test.data.template_context = {
"v1": {"k1": 3.14},
"v2": "vv2",
}

result = under_test.construct_template_parameters()

assert result["context"]["environment"]["resource_group"] is None


class TestSubscriptionView:
def test_clean(self, mock_pipeline_config):
Expand Down

0 comments on commit a298aa1

Please sign in to comment.