Skip to content

Commit bb88644

Browse files
committed
dcnm_vrf now raises an error if networks are attached during deletion
1 parent cf970b6 commit bb88644

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

plugins/modules/dcnm_vrf.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -583,13 +583,15 @@
583583
"GET_VRF_SWITCH": "/rest/top-down/fabrics/{}/vrfs/switches?vrf-names={}&serial-numbers={}",
584584
"GET_VRF_ID": "/rest/managed-pool/fabrics/{}/partitions/ids",
585585
"GET_VLAN": "/rest/resource-manager/vlan/{}?vlanUsageType=TOP_DOWN_VRF_VLAN",
586+
"GET_NET_VRF": "/rest/resource-manager/fabrics/{}/networks?vrf-name={}"
586587
},
587588
12: {
588589
"GET_VRF": "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/top-down/fabrics/{}/vrfs",
589590
"GET_VRF_ATTACH": "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/top-down/fabrics/{}/vrfs/attachments?vrf-names={}",
590591
"GET_VRF_SWITCH": "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/top-down/fabrics/{}/vrfs/switches?vrf-names={}&serial-numbers={}",
591592
"GET_VRF_ID": "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/top-down/fabrics/{}/vrfinfo",
592593
"GET_VLAN": "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/resource-manager/vlan/{}?vlanUsageType=TOP_DOWN_VRF_VLAN",
594+
"GET_NET_VRF": "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/top-down/fabrics/{}/networks?vrf-name={}",
593595
},
594596
}
595597

@@ -3758,6 +3760,18 @@ def push_to_remote(self, is_rollback=False):
37583760
# attachment being deleted is re-used on a new vrf attachment being
37593761
# created. This is needed specially for state: overridden
37603762

3763+
for vrf_name in self.diff_delete:
3764+
path = self.paths["GET_NET_VRF"].format(self.fabric, vrf_name)
3765+
resp = dcnm_send(self.module, "GET", path)
3766+
if resp.get("DATA") is None:
3767+
msg = f"Invalid Response from Controller. {resp}"
3768+
self.module.fail_json(msg=msg)
3769+
elif resp["DATA"] != []:
3770+
msg = "VRF has associated network attachments. "
3771+
msg += "Please remove the network attachments "
3772+
msg += "before deleting the VRF. (maybe using dcnm_network module)"
3773+
self.module.fail_json(msg=msg)
3774+
37613775
self.push_diff_detach(is_rollback)
37623776
self.push_diff_undeploy(is_rollback)
37633777

@@ -3791,7 +3805,7 @@ def wait_for_vrf_del_ready(self, vrf_name="not_supplied"):
37913805
for vrf in self.diff_delete:
37923806
ok_to_delete = False
37933807
path = self.paths["GET_VRF_ATTACH"].format(self.fabric, vrf)
3794-
3808+
retry_count = 20
37953809
while not ok_to_delete:
37963810
resp = dcnm_send(self.module, "GET", path)
37973811
ok_to_delete = True
@@ -3811,31 +3825,18 @@ def wait_for_vrf_del_ready(self, vrf_name="not_supplied"):
38113825
):
38123826
self.diff_delete.update({vrf: "OUT-OF-SYNC"})
38133827
break
3814-
if (
3815-
attach["lanAttachState"] == "DEPLOYED"
3816-
and attach["isLanAttached"] is True
3817-
):
3818-
vrf_name = attach.get("vrfName", "unknown")
3819-
fabric_name = attach.get("fabricName", "unknown")
3820-
switch_ip = attach.get("ipAddress", "unknown")
3821-
switch_name = attach.get("switchName", "unknown")
3822-
vlan_id = attach.get("vlanId", "unknown")
3823-
msg = f"Network attachments associated with vrf {vrf_name} "
3824-
msg += "must be removed (e.g. using the dcnm_network module) "
3825-
msg += "prior to deleting the vrf. "
3826-
msg += f"Details: fabric_name: {fabric_name}, "
3827-
msg += f"vrf_name: {vrf_name}. "
3828-
msg += "Network attachments found on "
3829-
msg += f"switch_ip: {switch_ip}, "
3830-
msg += f"switch_name: {switch_name}, "
3831-
msg += f"vlan_id: {vlan_id}"
3832-
self.module.fail_json(msg=msg)
38333828
if attach["lanAttachState"] != "NA":
38343829
time.sleep(self.WAIT_TIME_FOR_DELETE_LOOP)
38353830
self.diff_delete.update({vrf: "DEPLOYED"})
38363831
ok_to_delete = False
38373832
break
38383833
self.diff_delete.update({vrf: "NA"})
3834+
if retry_count <= 0:
3835+
msg = "Timeout waiting for VRF to be ready for deletion. "
3836+
msg += f"vrf: {vrf}, "
3837+
msg += f"resp: {resp}"
3838+
self.module.fail_json(msg=msg)
3839+
retry_count -= 1
38393840

38403841
def attach_spec(self):
38413842
"""

tests/unit/modules/dcnm/fixtures/dcnm_vrf.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,12 @@
10581058
}
10591059
]
10601060
},
1061+
"mock_net_from_vrf_empty": {
1062+
"ERROR": "",
1063+
"RETURN_CODE": 200,
1064+
"MESSAGE": "OK",
1065+
"DATA": []
1066+
},
10611067
"mock_vrf_attach_object_dcnm_only": {
10621068
"MESSAGE": "OK",
10631069
"METHOD": "POST",

tests/unit/modules/dcnm/test_dcnm_vrf.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class TestDcnmVrfModule(TestDcnmModule):
4343
fabric_details_mfd = test_data.get("fabric_details_mfd")
4444
fabric_details_vxlan = test_data.get("fabric_details_vxlan")
4545

46+
mock_net_from_vrf_empty = test_data.get("mock_net_from_vrf_empty")
4647
mock_vrf_attach_object_del_not_ready = test_data.get(
4748
"mock_vrf_attach_object_del_not_ready"
4849
)
@@ -451,6 +452,7 @@ def load_fixtures(self, response=None, device=""):
451452
self.mock_vrf_object,
452453
self.mock_vrf_attach_get_ext_object_ov_att1_only,
453454
self.mock_vrf_attach_get_ext_object_ov_att2_only,
455+
self.mock_net_from_vrf_empty,
454456
self.attach_success_resp,
455457
self.deploy_success_resp,
456458
self.mock_vrf_attach_object_del_not_ready,
@@ -488,6 +490,7 @@ def load_fixtures(self, response=None, device=""):
488490
self.mock_vrf_object,
489491
self.mock_vrf_attach_get_ext_object_dcnm_att1_only,
490492
self.mock_vrf_attach_get_ext_object_dcnm_att2_only,
493+
self.mock_net_from_vrf_empty,
491494
self.attach_success_resp,
492495
self.deploy_success_resp,
493496
self.mock_vrf_attach_object_del_not_ready,
@@ -519,6 +522,7 @@ def load_fixtures(self, response=None, device=""):
519522
self.mock_vrf_object,
520523
self.mock_vrf_attach_get_ext_object_dcnm_att1_only,
521524
self.mock_vrf_attach_get_ext_object_dcnm_att2_only,
525+
self.mock_net_from_vrf_empty,
522526
self.attach_success_resp,
523527
self.deploy_success_resp,
524528
self.mock_vrf_attach_object_del_not_ready,
@@ -539,6 +543,7 @@ def load_fixtures(self, response=None, device=""):
539543
self.mock_vrf_object_dcnm_only,
540544
self.mock_vrf_attach_get_ext_object_dcnm_att1_only,
541545
self.mock_vrf_attach_get_ext_object_dcnm_att2_only,
546+
self.mock_net_from_vrf_empty,
542547
self.attach_success_resp,
543548
self.deploy_success_resp,
544549
obj1,

0 commit comments

Comments
 (0)