|
1 | | -# Copyright (c) 2024 Cisco Systems, Inc. and its affiliates |
| 1 | +# Copyright (c) 2025 Cisco Systems, Inc. and its affiliates |
2 | 2 | # |
3 | 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy of |
4 | 4 | # this software and associated documentation files (the "Software"), to deal in |
|
24 | 24 |
|
25 | 25 | __metaclass__ = type |
26 | 26 |
|
| 27 | +import json |
| 28 | + |
27 | 29 | from ansible.plugins.action import ActionBase |
28 | 30 | from ansible_collections.cisco.nac_dc_vxlan.plugins.plugin_utils.helper_functions import ndfc_get_switch_policy_using_template |
29 | 31 |
|
30 | 32 |
|
31 | 33 | class ActionModule(ActionBase): |
| 34 | + """ |
| 35 | + Action plugin to manage switch hostname policy, host_11_1, |
| 36 | + in Nexus Dashboard (ND) through comparison with the desired |
| 37 | + switch name in the data model to switch hostname policy in ND |
| 38 | + if it exists, or create it if it does not exist. |
| 39 | + """ |
| 40 | + def __init__(self, *args, **kwargs): |
| 41 | + super(ActionModule, self).__init__(*args, **kwargs) |
| 42 | + self.tmp = None |
| 43 | + self.task_vars = None |
| 44 | + self.results = {} |
| 45 | + self.results['failed'] = False |
| 46 | + self.results['changed'] = False |
| 47 | + # self.policy_add = {} |
| 48 | + self.policy_update = {} |
| 49 | + |
| 50 | + def _get_switch_policy(self, switch_serial_number, template_name): |
| 51 | + """ |
| 52 | + Get switch hostname policy from Nexus Dashboard using |
| 53 | + template name (host_11_1) and switch serial number. |
| 54 | + """ |
| 55 | + return ndfc_get_switch_policy_using_template( |
| 56 | + self=self, |
| 57 | + task_vars=self.task_vars, |
| 58 | + tmp=self.tmp, |
| 59 | + switch_serial_number=switch_serial_number, |
| 60 | + template_name=template_name |
| 61 | + ) |
| 62 | + |
| 63 | + def nd_policy_add(self, switch_name, switch_serial_number): |
| 64 | + """ |
| 65 | + Add switch hostname policy in Nexus Dashboard. |
| 66 | + """ |
| 67 | + policy = { |
| 68 | + "nvPairs": { |
| 69 | + "SWITCH_NAME": switch_name |
| 70 | + }, |
| 71 | + "entityName": "SWITCH", |
| 72 | + "entityType": "SWITCH", |
| 73 | + "source": "", |
| 74 | + "priority": "100", |
| 75 | + "description": "", |
| 76 | + "templateName": "host_11_1", |
| 77 | + "serialNumber": switch_serial_number |
| 78 | + } |
| 79 | + |
| 80 | + nd_policy_add = self._execute_module( |
| 81 | + module_name="cisco.dcnm.dcnm_rest", |
| 82 | + module_args={ |
| 83 | + "method": "POST", |
| 84 | + "path": "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/policies", |
| 85 | + "data": json.dumps(policy) |
| 86 | + }, |
| 87 | + task_vars=self.task_vars, |
| 88 | + tmp=self.tmp |
| 89 | + ) |
| 90 | + |
| 91 | + if nd_policy_add.get('response'): |
| 92 | + if nd_policy_add['response']['RETURN_CODE'] == 200: |
| 93 | + self.results['changed'] = True |
| 94 | + |
| 95 | + if nd_policy_add.get('msg'): |
| 96 | + if nd_policy_add['msg']['RETURN_CODE'] != 200: |
| 97 | + self.results['failed'] = True |
| 98 | + self.results['msg'] = f"For switch {switch_name} addition; {nd_policy_add['msg']['DATA']['message']}" |
| 99 | + |
| 100 | + def build_policy_update(self, policy, switch_name, switch_serial_number): |
| 101 | + """ |
| 102 | + Build switch hostname policy update data structure. |
| 103 | + """ |
| 104 | + policy["nvPairs"]["SWITCH_NAME"] = switch_name |
| 105 | + self.policy_update.update({switch_serial_number: policy}) |
| 106 | + |
| 107 | + def nd_policy_update(self): |
| 108 | + """ |
| 109 | + Bulk update switch hostname policy in Nexus Dashboard. |
| 110 | + """ |
| 111 | + policy_ids = ",".join([str(value["policyId"]) for key, value in self.policy_update.items()]) |
| 112 | + |
| 113 | + nd_policy_update = self._execute_module( |
| 114 | + module_name="cisco.dcnm.dcnm_rest", |
| 115 | + module_args={ |
| 116 | + "method": "PUT", |
| 117 | + "path": f"/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/policies/{policy_ids}/bulk", |
| 118 | + "data": json.dumps(list(self.policy_update.values())) |
| 119 | + }, |
| 120 | + task_vars=self.task_vars, |
| 121 | + tmp=self.tmp |
| 122 | + ) |
| 123 | + |
| 124 | + if nd_policy_update.get('response'): |
| 125 | + if nd_policy_update['response']['RETURN_CODE'] == 200: |
| 126 | + self.results['changed'] = True |
| 127 | + |
| 128 | + if nd_policy_update.get('msg'): |
| 129 | + if nd_policy_update['msg']['RETURN_CODE'] != 200: |
| 130 | + self.results['failed'] = True |
| 131 | + self.results['msg'] = f"Bulk update failed; {nd_policy_update['msg']['DATA']['message']}" |
32 | 132 |
|
33 | 133 | def run(self, tmp=None, task_vars=None): |
34 | 134 | results = super(ActionModule, self).run(tmp, task_vars) |
35 | 135 | results['changed'] = False |
36 | 136 |
|
| 137 | + self.task_vars = task_vars |
| 138 | + self.tmp = tmp |
| 139 | + |
37 | 140 | model_data = self._task.args["model_data"] |
38 | | - switch_serial_numbers = self._task.args["switch_serial_numbers"] |
39 | 141 | template_name = self._task.args["template_name"] |
40 | 142 |
|
41 | | - policy_update = {} |
| 143 | + dm_switches = [] |
| 144 | + switch_serial_numbers = [] |
| 145 | + if model_data["vxlan"]["fabric"]["type"] in ('VXLAN_EVPN', 'eBGP_VXLAN', 'ISN', 'External'): |
| 146 | + dm_switches = model_data["vxlan"]["topology"]["switches"] |
| 147 | + switch_serial_numbers = [dm_switch['serial_number'] for dm_switch in dm_switches] |
42 | 148 |
|
43 | 149 | for switch_serial_number in switch_serial_numbers: |
44 | | - policy_match = ndfc_get_switch_policy_using_template( |
45 | | - self=self, |
46 | | - task_vars=task_vars, |
47 | | - tmp=tmp, |
48 | | - switch_serial_number=switch_serial_number, |
49 | | - template_name=template_name |
50 | | - ) |
51 | | - |
52 | | - dm_switches = [] |
53 | | - if model_data["vxlan"]["fabric"]["type"] in ('VXLAN_EVPN', 'eBGP_VXLAN', 'ISN', 'External'): |
54 | | - dm_switches = model_data["vxlan"]["topology"]["switches"] |
55 | | - # elif model_data["vxlan"]["fabric"]["type"] in ('ISN'): |
56 | | - # dm_switches = model_data["vxlan"]["multisite"]["isn"]["topology"]["switches"] |
| 150 | + policy_match = self._get_switch_policy(switch_serial_number, template_name) |
57 | 151 |
|
58 | 152 | switch_match = next((item for item in dm_switches if item["serial_number"] == switch_serial_number)) |
59 | 153 |
|
60 | | - if policy_match["nvPairs"]["SWITCH_NAME"] != switch_match["name"]: |
61 | | - results['changed'] = True |
62 | | - policy_match["nvPairs"]["SWITCH_NAME"] = switch_match["name"] |
63 | | - policy_update.update({switch_serial_number: policy_match}) |
64 | | - |
65 | | - if policy_update: |
66 | | - results['changed'] = True |
67 | | - |
68 | | - results['policy_update'] = policy_update |
| 154 | + if not policy_match: |
| 155 | + self.nd_policy_add( |
| 156 | + switch_name=switch_match['name'], |
| 157 | + switch_serial_number=switch_serial_number |
| 158 | + ) |
| 159 | + |
| 160 | + if self.results['failed']: |
| 161 | + results['failed'] = self.results['failed'] |
| 162 | + results['msg'] = self.results['msg'] |
| 163 | + return results |
| 164 | + |
| 165 | + if self.results['changed']: |
| 166 | + results['changed'] = self.results['changed'] |
| 167 | + |
| 168 | + if policy_match: |
| 169 | + if policy_match["nvPairs"]["SWITCH_NAME"] != switch_match['name']: |
| 170 | + self.build_policy_update( |
| 171 | + policy=policy_match, |
| 172 | + switch_name=switch_match['name'], |
| 173 | + switch_serial_number=switch_serial_number |
| 174 | + ) |
| 175 | + |
| 176 | + if self.policy_update: |
| 177 | + self.nd_policy_update() |
| 178 | + |
| 179 | + if self.results['failed']: |
| 180 | + results['failed'] = self.results['failed'] |
| 181 | + results['msg'] = self.results['msg'] |
| 182 | + return results |
| 183 | + |
| 184 | + if self.results['changed']: |
| 185 | + results['changed'] = self.results['changed'] |
69 | 186 |
|
70 | 187 | return results |
0 commit comments