Skip to content

Fix policy delete for freeform deploy false #403

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

Merged
merged 5 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions playbooks/roles/dcnm_policy/dcnm_hosts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
all:
vars:
ansible_user: "admin"
ansible_password: "password"
ansible_python_interpreter: python
ansible_httpapi_validate_certs: False
ansible_httpapi_use_ssl: True
children:
dcnm:
vars:
ansible_connection: ansible.netcommon.httpapi
ansible_network_os: cisco.dcnm.dcnm
hosts:
nac-ndfc1:
ansible_host: 10.15.0.5
23 changes: 23 additions & 0 deletions playbooks/roles/dcnm_policy/dcnm_tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
# This playbook can be used to execute integration tests for
# the role located in:
#
# tests/integration/targets/dcnm_policy
#
# Modify the vars section with details for your testing setup.
#
- hosts: dcnm
gather_facts: no
connection: ansible.netcommon.httpapi

vars:
# Uncomment 'testcase:' to run a specific test under 'tests/integration/targets/dcnm_policy/tests/dcnm'
# testcase: dcnm_policy_with*
ansible_it_fabric: test_fabric
ansible_switch1: 192.168.55.21
ansible_sw1_sno: 9YEXD0OHA7Z
ansible_switch2: 192.168.55.22
ansible_sw2_sno: 9M2TXMZ7D3N

roles:
- dcnm_policy
21 changes: 16 additions & 5 deletions plugins/modules/dcnm_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ class DcnmPolicy:
"POLICY_BULK_CREATE": "/rest/control/policies/bulk-create",
"POLICY_BULK_UPDATE": "/rest/control/policies/{}/bulk",
"POLICY_MARK_DELETE": "/rest/control/policies/{}/mark-delete",
"POLICY_DELETE": "/rest/control/policies/policyIds?policyIds={}",
"POLICY_DEPLOY": "/rest/control/policies/deploy",
"POLICY_CFG_DEPLOY": "/rest/control/fabrics/{}/config-deploy/",
"POLICY_WITH_POLICY_ID": "/rest/control/policies/{}",
Expand All @@ -427,6 +428,7 @@ class DcnmPolicy:
"POLICY_BULK_CREATE": "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/policies/bulk-create",
"POLICY_BULK_UPDATE": "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/policies/{}/bulk",
"POLICY_MARK_DELETE": "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/policies/{}/mark-delete",
"POLICY_DELETE": "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/policies/policyIds?policyIds={}",
"POLICY_DEPLOY": "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/policies/deploy",
"POLICY_CFG_DEPLOY": "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{}/config-deploy/",
"POLICY_WITH_POLICY_ID": "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/policies/{}",
Expand Down Expand Up @@ -957,13 +959,23 @@ def dcnm_policy_get_diff_deleted(self):

# Filter the list of policies and keep only those that are matching. For delete case, playbook policies
# may either contain template names of policy names. So compare both.

# NDFC has strange behavior when deleting switch_freeform template policies when the
# deploy flag is set to False. NDFC will change the policy name from switch_freeform
# to switch_freeform_config making it impossible to use the playbook name switch_freeform
# to delete the policy on a subsequent run where the flag is set to True.
# The following 3 lines of code will change switch_freeform to switch_freeform_config when
# the deploy flag is set to True.
for pl in plist:
if pl["templateName"] == "switch_freeform_config" and self.deploy is True:
pl["templateName"] = "switch_freeform"

match_pol = [
pl
for pl in plist
for wp in self.want
if (
not pl["deleted"]
and (
(
(wp["policy_id_given"] is False)
and (pl["templateName"] == wp["templateName"])
and (
Expand All @@ -980,7 +992,6 @@ def dcnm_policy_get_diff_deleted(self):
]
# match_pol contains all the policies which exist and are to be deleted
# Build the delete payloads

for pol in match_pol:

del_payload = self.dcnm_policy_get_delete_payload(pol)
Expand Down Expand Up @@ -1136,9 +1147,9 @@ def dcnm_policy_update_policy(self, policy, command):
def dcnm_policy_delete_policy(self, policy, mark_del):

if mark_del is True:
path = self.paths["POLICY_MARK_DELETE"].format(policy["policyId"])
path = self.paths["POLICY_DELETE"].format(policy["policyId"])
json_payload = ""
command = "PUT"
command = "DELETE"
else:
path = self.paths["POLICY_WITH_POLICY_ID"].format(policy)
json_payload = ""
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/targets/dcnm_policy/tasks/dcnm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
tags: sanity

- name: run test cases (connection=httpapi)
include: "{{ test_case_to_run }}"
ansible.builtin.include_tasks: "{{ test_case_to_run }}"
with_items: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/targets/dcnm_policy/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
- { include: dcnm.yaml, tags: ['dcnm'] }
- { ansible.builtin.include_tasks: dcnm.yaml, tags: ['dcnm'] }
Original file line number Diff line number Diff line change
Expand Up @@ -458,28 +458,37 @@
- 'result["response"][0]["RETURN_CODE"] == 200'
- 'result["response"][0]["MESSAGE"] == "OK"'

- name: Delete policy using template name - with deploy
cisco.dcnm.dcnm_policy:
fabric: "{{ ansible_it_fabric }}"
state: deleted # only choose form [merged, deleted, query]
config:
- name: template_101 # This can either be a policy name like POLICY-xxxxx or template name
- switch:
- ip: "{{ ansible_switch1 }}"
register: result

- assert:
that:
- 'result.changed == true'
- '(result["diff"][0]["merged"] | length) == 0'
- '(result["diff"][0]["deleted"] | length) == 1'
- '(result["diff"][0]["query"] | length) == 0'
- '(result["response"] | length) == 2'

- assert:
that:
- 'result["response"][0]["RETURN_CODE"] == 200'
- 'result["response"][0]["MESSAGE"] == "OK"'
# TBD: This test case needs to be refactored to test delete with and without deploy
# using a the switch_freeform template. The following order should be verified.
# 1. Create policy with deploy
# 2. Delete policy with deploy
# 3. Create policy with deploy
# 4. Delete policy without deploy and verify policy config is not removed from switch
# 5. Delete policy with deploy and verify policy config is removed from switch


# - name: Delete policy using template name - with deploy
# cisco.dcnm.dcnm_policy:
# fabric: "{{ ansible_it_fabric }}"
# state: deleted # only choose form [merged, deleted, query]
# config:
# - name: template_101 # This can either be a policy name like POLICY-xxxxx or template name
# - switch:
# - ip: "{{ ansible_switch1 }}"
# register: result

# - assert:
# that:
# - 'result.changed == true'
# - '(result["diff"][0]["merged"] | length) == 0'
# - '(result["diff"][0]["deleted"] | length) == 1'
# - '(result["diff"][0]["query"] | length) == 0'
# - '(result["response"] | length) == 2'

# - assert:
# that:
# - 'result["response"][0]["RETURN_CODE"] == 200'
# - 'result["response"][0]["MESSAGE"] == "OK"'

##############################################
## CLEANUP ##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@
state: merged
config:
- name: my_base_ospf # This must be a valid template name
create_additional_policy: false
create_additional_policy: true
priority: 111
description: "Changed description - 1"
policy_vars:
OSPF_TAG: 2000
LOOPBACK_IP: 10.122.84.108

- name: my_base_ospf # This must be a valid template name
create_additional_policy: false
create_additional_policy: true
priority: 121
description: "Changed description - 2"
policy_vars:
Expand Down