Debian Codegraph @

ansible_collections/cisco/nxos/plugins/modules/nxos_vsan.py

raw
1 #!/usr/bin/python
2 # Copyright: Ansible Project
3 # GNU General Public License v3.0+ (see COPYING or
4 # https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7
8
9 DOCUMENTATION = """
10 module: nxos_vsan
11 short_description: Configuration of vsan for Cisco NXOS MDS Switches.
12 description:
13 - Configuration of vsan for Cisco MDS NXOS.
14 version_added: 1.0.0
15 author:
16 - Suhas Bharadwaj (@srbharadwaj) ([email protected])
17 notes:
18 - Tested against Cisco MDS NX-OS 8.4(1)
19 options:
20 vsan:
21 description:
22 - List of vsan details to be added or removed
23 type: list
24 elements: dict
25 suboptions:
26 id:
27 description:
28 - Vsan id
29 required: true
30 type: int
31 name:
32 description:
33 - Name of the vsan
34 type: str
35 suspend:
36 description:
37 - suspend the vsan if True
38 type: bool
39 remove:
40 description:
41 - Removes the vsan if True
42 type: bool
43 interface:
44 description:
45 - List of vsan's interfaces to be added
46 type: list
47 elements: str
48 """
49
50 EXAMPLES = """
51 - name: Test that vsan module works
52 cisco.nxos.nxos_vsan:
53 vsan:
54 - id: 922
55 interface:
56 - fc1/1
57 - fc1/2
58 - port-channel 1
59 name: vsan-SAN-A
60 remove: false
61 suspend: false
62 - id: 923
63 interface:
64 - fc1/11
65 - fc1/21
66 - port-channel 2
67 name: vsan-SAN-B
68 remove: false
69 suspend: true
70 - id: 1923
71 name: vsan-SAN-Old
72 remove: true
73 """
74
75 RETURN = """
76 commands:
77 description: commands sent to the device
78 returned: always
79 type: list
80 sample:
81 - terminal dont-ask
82 - vsan database
83 - vsan 922 interface fc1/40
84 - vsan 922 interface port-channel 155
85 - no terminal dont-ask
86 """
87
88 import re
89
90 from ansible.module_utils.basic import AnsibleModule
91
92 from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import (
93 load_config,
94 run_commands,
95 )
96
97
98 __metaclass__ = type
99
100
101 class Vsan(object):
102 def __init__(self, vsanid):
103 self.vsanid = vsanid
104 self.vsanname = None
105 self.vsanstate = None
106 self.vsanoperstate = None
107 self.vsaninterfaces = []
108
109
110 class GetVsanInfoFromSwitch(object):
111 """docstring for GetVsanInfoFromSwitch"""
112
113 def __init__(self, module):
114 self.module = module
115 self.vsaninfo = {}
116 self.processShowVsan()
117 self.processShowVsanMembership()
118
119 def execute_show_vsan_cmd(self):
120 output = execute_show_command("show vsan", self.module)[0]
121 return output
122
123 def execute_show_vsan_mem_cmd(self):
124 output = execute_show_command("show vsan membership", self.module)[0]
125 return output
126
127 def processShowVsan(self):
128 patv = r"^vsan\s+(\d+)\s+information"
129 patnamestate = "name:(.*)state:(.*)"
130 patoperstate = "operational state:(.*)"
131
132 output = self.execute_show_vsan_cmd().split("\n")
133 for o in output:
134 z = re.match(patv, o.strip())
135 if z:
136 v = z.group(1).strip()
137 self.vsaninfo[v] = Vsan(v)
138
139 z1 = re.match(patnamestate, o.strip())
140 if z1:
141 n = z1.group(1).strip()
142 s = z1.group(2).strip()
143 self.vsaninfo[v].vsanname = n
144 self.vsaninfo[v].vsanstate = s
145
146 z2 = re.match(patoperstate, o.strip())
147 if z2:
148 oper = z2.group(1).strip()
149 self.vsaninfo[v].vsanoperstate = oper
150
151 # 4094/4079 vsan is always present
152 self.vsaninfo["4079"] = Vsan("4079")
153 self.vsaninfo["4094"] = Vsan("4094")
154
155 def processShowVsanMembership(self):
156 patv = r"^vsan\s+(\d+).*"
157 output = self.execute_show_vsan_mem_cmd().split("\n")
158 memlist = []
159 v = None
160 for o in output:
161 z = re.match(patv, o.strip())
162 if z:
163 if v is not None:
164 self.vsaninfo[v].vsaninterfaces = memlist
165 memlist = []
166 v = z.group(1)
167 if "interfaces" not in o:
168 llist = o.strip().split()
169 memlist = memlist + llist
170 self.vsaninfo[v].vsaninterfaces = memlist
171
172 def getVsanInfoObjects(self):
173 return self.vsaninfo
174
175
176 def execute_show_command(command, module, command_type="cli_show"):
177 output = "text"
178 commands = [{"command": command, "output": output}]
179 return run_commands(module, commands)
180
181
182 def flatten_list(command_lists):
183 flat_command_list = []
184 for command in command_lists:
185 if isinstance(command, list):
186 flat_command_list.extend(command)
187 else:
188 flat_command_list.append(command)
189 return flat_command_list
190
191
192 def main():
193 vsan_element_spec = dict(
194 id=dict(required=True, type="int"),
195 name=dict(type="str"),
196 remove=dict(type="bool"),
197 suspend=dict(type="bool"),
198 interface=dict(type="list", elements="str"),
199 )
200
201 argument_spec = dict(
202 vsan=dict(type="list", elements="dict", options=vsan_element_spec),
203 )
204
205 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
206 warnings = list()
207 messages = list()
208 commands_executed = list()
209 result = {"changed": False}
210
211 obj = GetVsanInfoFromSwitch(module)
212 dictSwVsanObjs = obj.getVsanInfoObjects()
213
214 commands = []
215 vsan_list = module.params["vsan"]
216
217 for eachvsan in vsan_list:
218 vsanid = str(eachvsan["id"])
219 vsanname = eachvsan["name"]
220 vsanremove = eachvsan["remove"]
221 vsansuspend = eachvsan["suspend"]
222 vsaninterface_list = eachvsan["interface"]
223
224 if int(vsanid) < 1 or int(vsanid) >= 4095:
225 module.fail_json(
226 msg=vsanid + " - This is an invalid vsan. Supported vsan range is 1-4094",
227 )
228
229 if vsanid in dictSwVsanObjs.keys():
230 sw_vsanid = vsanid
231 sw_vsanname = dictSwVsanObjs[vsanid].vsanname
232 sw_vsanstate = dictSwVsanObjs[vsanid].vsanstate
233 sw_vsaninterfaces = dictSwVsanObjs[vsanid].vsaninterfaces
234 else:
235 sw_vsanid = None
236 sw_vsanname = None
237 sw_vsanstate = None
238 sw_vsaninterfaces = []
239
240 if vsanremove:
241 # Negative case:
242 if vsanid == "4079" or vsanid == "4094":
243 messages.append(
244 str(vsanid) + " is a reserved vsan, hence cannot be removed",
245 )
246 continue
247 if vsanid == sw_vsanid:
248 commands.append("no vsan " + str(vsanid))
249 messages.append("deleting the vsan " + str(vsanid))
250 else:
251 messages.append(
252 "There is no vsan "
253 + str(vsanid)
254 + " present in the switch. Hence there is nothing to delete",
255 )
256 continue
257 else:
258 # Negative case:
259 if vsanid == "4079" or vsanid == "4094":
260 messages.append(
261 str(vsanid) + " is a reserved vsan, and always present on the switch",
262 )
263 else:
264 if vsanid == sw_vsanid:
265 messages.append(
266 "There is already a vsan "
267 + str(vsanid)
268 + " present in the switch. Hence there is nothing to configure",
269 )
270 else:
271 commands.append("vsan " + str(vsanid))
272 messages.append("creating vsan " + str(vsanid))
273
274 if vsanname is not None:
275 # Negative case:
276 if vsanid == "4079" or vsanid == "4094":
277 messages.append(
278 str(vsanid) + " is a reserved vsan, and cannot be renamed",
279 )
280 else:
281 if vsanname == sw_vsanname:
282 messages.append(
283 "There is already a vsan "
284 + str(vsanid)
285 + " present in the switch, which has the name "
286 + vsanname
287 + " Hence there is nothing to configure",
288 )
289 else:
290 commands.append("vsan " + str(vsanid) + " name " + vsanname)
291 messages.append(
292 "setting vsan name to " + vsanname + " for vsan " + str(vsanid),
293 )
294
295 if vsansuspend:
296 # Negative case:
297 if vsanid == "4079" or vsanid == "4094":
298 messages.append(
299 str(vsanid) + " is a reserved vsan, and cannot be suspended",
300 )
301 else:
302 if sw_vsanstate == "suspended":
303 messages.append(
304 "There is already a vsan "
305 + str(vsanid)
306 + " present in the switch, which is in suspended state ",
307 )
308 else:
309 commands.append("vsan " + str(vsanid) + " suspend")
310 messages.append("suspending the vsan " + str(vsanid))
311 else:
312 if sw_vsanstate == "active":
313 messages.append(
314 "There is already a vsan "
315 + str(vsanid)
316 + " present in the switch, which is in active state ",
317 )
318 else:
319 commands.append("no vsan " + str(vsanid) + " suspend")
320 messages.append("no suspending the vsan " + str(vsanid))
321
322 if vsaninterface_list is not None:
323 for each_interface_name in vsaninterface_list:
324 # For fcip,port-channel,vfc-port-channel need to remove the
325 # extra space to compare
326 temp = re.sub(" +", "", each_interface_name)
327 if temp in sw_vsaninterfaces:
328 messages.append(
329 each_interface_name
330 + " is already present in the vsan "
331 + str(vsanid)
332 + " interface list",
333 )
334 else:
335 commands.append(
336 "vsan " + str(vsanid) + " interface " + each_interface_name,
337 )
338 messages.append(
339 "adding interface " + each_interface_name + " to vsan " + str(vsanid),
340 )
341
342 if len(commands) != 0:
343 commands = ["terminal dont-ask"] + ["vsan database"] + commands + ["no terminal dont-ask"]
344
345 cmds = flatten_list(commands)
346 commands_executed = cmds
347
348 if commands_executed:
349 if module.check_mode:
350 module.exit_json(
351 changed=False,
352 commands=commands_executed,
353 msg="Check Mode: No cmds issued to the hosts",
354 )
355 else:
356 result["changed"] = True
357 load_config(module, commands_executed)
358
359 result["messages"] = messages
360 result["commands"] = commands_executed
361 result["warnings"] = warnings
362 module.exit_json(**result)
363
364
365 if __name__ == "__main__":
366 main()