diff --git a/foodx_devops_tools/deploy_me/application_steps/_deploy.py b/foodx_devops_tools/deploy_me/application_steps/_deploy.py index aed2857..d9ed0aa 100644 --- a/foodx_devops_tools/deploy_me/application_steps/_deploy.py +++ b/foodx_devops_tools/deploy_me/application_steps/_deploy.py @@ -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( diff --git a/foodx_devops_tools/pipeline_config/views.py b/foodx_devops_tools/pipeline_config/views.py index f095028..8a6290c 100644 --- a/foodx_devops_tools/pipeline_config/views.py +++ b/foodx_devops_tools/pipeline_config/views.py @@ -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 @@ -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, @@ -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 @@ -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, @@ -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), @@ -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 } diff --git a/tests/ci/unit_tests/deploy_me/deployment/conftest.py b/tests/ci/unit_tests/deploy_me/deployment/conftest.py index ade46cb..a7678f1 100644 --- a/tests/ci/unit_tests/deploy_me/deployment/conftest.py +++ b/tests/ci/unit_tests/deploy_me/deployment/conftest.py @@ -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") diff --git a/tests/ci/unit_tests/pipeline_config/views/test_views.py b/tests/ci/unit_tests/pipeline_config/views/test_views.py index 6489909..664db65 100644 --- a/tests/ci/unit_tests/pipeline_config/views/test_views.py +++ b/tests/ci/unit_tests/pipeline_config/views/test_views.py @@ -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": { @@ -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):