From 500702efee18600137b1bc80cce241df86cf4adf Mon Sep 17 00:00:00 2001 From: akinross Date: Tue, 8 Oct 2024 13:13:22 +0200 Subject: [PATCH] [minor_change] Allow site configuration for tenant policy template types in ndo_template module --- plugins/module_utils/constants.py | 2 +- plugins/modules/ndo_template.py | 10 +++++----- .../targets/ndo_template/tasks/main.yml | 18 +++++++----------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/plugins/module_utils/constants.py b/plugins/module_utils/constants.py index b34591f1..839c328a 100644 --- a/plugins/module_utils/constants.py +++ b/plugins/module_utils/constants.py @@ -83,7 +83,7 @@ "template_type": "tenantPolicy", # templateType in payload "template_type_container": "tenantPolicyTemplate", # templateType container in payload "tenant": True, # tenant required - "site_amount": 0, # site amount, 0 = no sites, 1 = 1 site, 2 = multiple sites + "site_amount": 2, # 1 = 1 site, 2 = multiple sites "template_container": True, # configuration is set in template container in payload }, "l3out": { diff --git a/plugins/modules/ndo_template.py b/plugins/modules/ndo_template.py index 69924e78..08e066e3 100644 --- a/plugins/modules/ndo_template.py +++ b/plugins/modules/ndo_template.py @@ -83,6 +83,8 @@ template: ansible_tenant_policy template_type: tenant tenant: ansible_test_tenant + sites: + - name: ansible_test_site state: present - name: Create a new l3out policy template @@ -182,9 +184,6 @@ def main(): if tenant_id and not TEMPLATE_TYPES[template_type]["tenant"]: mso.fail_json(msg="Tenant cannot be attached to template of type {0}.".format(template_type)) - if site_ids and TEMPLATE_TYPES[template_type]["site_amount"] == 0: - mso.fail_json(msg="Site cannot be attached to template of type {0}.".format(template_type)) - if not tenant_id and TEMPLATE_TYPES[template_type]["tenant"]: mso.fail_json(msg="Tenant must be provided for template of type {0}.".format(template_type)) @@ -233,7 +232,7 @@ def main(): # Set template specific payload in functions to increase readability due to the amount of different templates with small differences if template_type == "tenant": - set_tenant_template_payload(payload, template_type, tenant_id) + set_tenant_template_payload(payload, template_type, tenant_id, site_ids) elif template_type == "l3out": set_l3out_template_payload(payload, template_type, tenant_id, site_ids[0]) elif template_type == "fabric_policy": @@ -263,8 +262,9 @@ def main(): mso.exit_json() -def set_tenant_template_payload(payload, template_type, tenant_id): +def set_tenant_template_payload(payload, template_type, tenant_id, site_ids): payload[TEMPLATE_TYPES[template_type]["template_type_container"]].update({"template": {"tenantId": tenant_id}}) + payload[TEMPLATE_TYPES[template_type]["template_type_container"]].update({"sites": [{"siteId": site_id} for site_id in site_ids]}) def set_l3out_template_payload(payload, template_type, tenant_id, site_id): diff --git a/tests/integration/targets/ndo_template/tasks/main.yml b/tests/integration/targets/ndo_template/tasks/main.yml index b2f1b493..283dbc4d 100644 --- a/tests/integration/targets/ndo_template/tasks/main.yml +++ b/tests/integration/targets/ndo_template/tasks/main.yml @@ -231,6 +231,9 @@ template: ansible_tenant_policy_changed template_id: '{{ nm_create_new_tenant_policy_template_again.current.templateId }}' state: present + sites: + - name: '{{ mso_site | default("ansible_test") }}' + - name: ansible_test_2 check_mode: true register: cm_update_tenant_policy_template @@ -306,6 +309,7 @@ - cm_update_tenant_policy_template.current.templateType == 'tenantPolicy' - cm_update_tenant_policy_template.current.templateId is defined - cm_update_tenant_policy_template.current.tenantPolicyTemplate.template.tenantId is defined + - cm_update_tenant_policy_template.current.tenantPolicyTemplate.sites | length == 2 - nm_update_tenant_policy_template is changed - nm_update_tenant_policy_template.previous.displayName == 'ansible_tenant_policy' - nm_update_tenant_policy_template.previous.templateType == 'tenantPolicy' @@ -314,16 +318,19 @@ - nm_update_tenant_policy_template.current.displayName == 'ansible_tenant_policy_changed' - nm_update_tenant_policy_template.current.templateType == 'tenantPolicy' - nm_update_tenant_policy_template.current.templateId is defined + - nm_update_tenant_policy_template.current.tenantPolicyTemplate.sites | length == 2 - nm_update_tenant_policy_template.current.tenantPolicyTemplate.template.tenantId is defined - nm_update_tenant_policy_template_again is not changed - nm_update_tenant_policy_template_again.previous.displayName == 'ansible_tenant_policy_changed' - nm_update_tenant_policy_template_again.previous.templateType == 'tenantPolicy' - nm_update_tenant_policy_template_again.previous.templateId is defined - nm_update_tenant_policy_template_again.previous.tenantPolicyTemplate.template.tenantId is defined + - nm_update_tenant_policy_template_again.previous.tenantPolicyTemplate.sites | length == 2 - nm_update_tenant_policy_template_again.current.displayName == 'ansible_tenant_policy_changed' - nm_update_tenant_policy_template_again.current.templateType == 'tenantPolicy' - nm_update_tenant_policy_template_again.current.templateId is defined - nm_update_tenant_policy_template_again.current.tenantPolicyTemplate.template.tenantId is defined + - nm_update_tenant_policy_template_again.current.tenantPolicyTemplate.sites | length == 2 - nm_update_new_fabric_policy_template_0_2_sites is changed - nm_update_new_fabric_policy_template_0_2_sites.previous.displayName == 'ansible_fabric_policy_template' - nm_update_new_fabric_policy_template_0_2_sites.previous.fabricPolicyTemplate.sites is not defined @@ -414,15 +421,6 @@ state: present register: err_unknown_template_id ignore_errors: true - - - name: Error on not allowed input site - cisco.mso.ndo_template: - <<: *update_tenant_policy_template - sites: - - name: '{{ mso_site | default("ansible_test") }}' - state: present - register: err_not_allowed_site_id - ignore_errors: true - name: Error on not allowed template_type change cisco.mso.ndo_template: @@ -501,8 +499,6 @@ that: - err_unknown_template_id is failed - err_unknown_template_id.msg.startswith("Provided template id 'unknown_id' does not exist. Existing templates:") - - err_not_allowed_site_id is failed - - err_not_allowed_site_id.msg == "Site cannot be attached to template of type tenant." - err_not_allowed_template_type_change is failed - err_not_allowed_template_type_change.msg == "Template type cannot be changed." - err_not_allowed_site_change is failed