Debian Codegraph @

ansible_collections/purestorage/flasharray/plugins/modules/purefa_vlan.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2020, Simon Dodsley ([email protected])
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11
12 ANSIBLE_METADATA = {
13 "metadata_version": "1.1",
14 "status": ["preview"],
15 "supported_by": "community",
16 }
17
18
19 DOCUMENTATION = """
20 ---
21 module: purefa_vlan
22 version_added: '1.0.0'
23 short_description: Manage network VLAN interfaces in a Pure Storage FlashArray
24 description:
25 - This module manages the VLAN network interfaces on a Pure Storage FlashArray.
26 author: Pure Storage Ansible Team (@sdodsley) <[email protected]>
27 options:
28 name:
29 description:
30 - Interface name, including controller identifier.
31 - VLANs are only supported on iSCSI, NVMe-RoCE and file
32 physical interfaces
33 required: true
34 type: str
35 state:
36 description:
37 - State of existing interface (on/off).
38 required: false
39 default: present
40 choices: [ "present", "absent" ]
41 type: str
42 enabled:
43 description:
44 - Define if VLAN interface is enabled or not.
45 required: false
46 default: true
47 type: bool
48 address:
49 description:
50 - IPv4 or IPv6 address of interface.
51 required: false
52 type: str
53 subnet:
54 description:
55 - Name of subnet interface associated with.
56 required: true
57 type: str
58 extends_documentation_fragment:
59 - purestorage.flasharray.purestorage.fa
60 """
61
62 EXAMPLES = """
63 - name: Configure and enable VLAN interface ct0.eth8 for subnet test
64 purestorage.flasharray.purefa_vlan:
65 name: ct0.eth8
66 subnet: test
67 address: 10.21.200.18
68 state: present
69 fa_url: 10.10.10.2
70 api_token: c6033033-fe69-2515-a9e8-966bb7fe4b40
71
72 - name: Disable VLAN interface for subnet test on ct1.eth2
73 purestorage.flasharray.purefa_vlan:
74 name: ct1.eth2
75 subnet: test
76 enabled: false
77 fa_url: 10.10.10.2
78 api_token: c6033033-fe69-2515-a9e8-966bb7fe4b40
79
80 - name: Delete VLAN interface for subnet test on ct0.eth4
81 purestorage.flasharray.purefa_vlan:
82 name: ct0.eth4
83 subnet: test
84 state: absent
85 fa_url: 10.10.10.2
86 api_token: c6033033-fe69-2515-a9e8-966bb7fe4b40
87 """
88
89 RETURN = """
90 """
91
92 HAS_PURESTORAGE = True
93 try:
94 from pypureclient.flasharray import (
95 NetworkInterfacePatch,
96 NetworkInterfacePost,
97 ReferenceNoId,
98 NetworkinterfacepatchEth,
99 NetworkinterfacepostEth,
100 )
101 except ImportError:
102 HAS_PURESTORAGE = False
103
104 from ansible.module_utils.basic import AnsibleModule
105 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
106 get_array,
107 purefa_argument_spec,
108 )
109
110
111 def _get_subnet(module, array):
112 """Return subnet or None"""
113 res = array.get_subnets(names=[module.params["subnet"]])
114 if res.status_code != 200:
115 return None
116 return list(res.items)[0]
117
118
119 def _get_interface(module, array):
120 """Return Interface or None"""
121 res = array.get_network_interfaces(names=[module.params["name"]])
122 if res.status_code != 200:
123 return None
124 return list(res.items)[0]
125
126
127 def _get_vif(array, interface, subnet):
128 """Return VLAN Interface or None"""
129 vif_name = interface["name"] + "." + str(subnet["vlan"])
130 res = array.get_network_interfaces(names=[vif_name])
131 if res.status_code != 200:
132 return None
133 return list(res.items)[0]
134
135
136 def create_vif(module, array, interface, subnet):
137 """Create VLAN Interface"""
138 changed = True
139 if not module.check_mode:
140 vif_name = interface["name"] + "." + str(subnet["vlan"])
141 if module.params["address"]:
142 res = array.post_network_interfaces(
143 names=[vif_name],
144 network=NetworkInterfacePost(
145 eth=(
146 NetworkinterfacepostEth(
147 subtype="vif",
148 subnet=ReferenceNoId(name=module.params["subnet"]),
149 address=module.params["address"],
150 )
151 )
152 ),
153 )
154 if res.status_code != 200:
155 module.fail_json(
156 msg="Failed to create VLAN interface {0}. Error: {1}".format(
157 vif_name, res.errors[0].message
158 )
159 )
160 else:
161 res = array.post_network_interfaces(
162 names=[vif_name],
163 network=NetworkInterfacePost(
164 eth=(
165 NetworkinterfacepostEth(
166 subtype="vif",
167 subnet=ReferenceNoId(name=module.params["subnet"]),
168 )
169 )
170 ),
171 )
172 if res.status_code != 200:
173 module.fail_json(
174 msg="Failed to create VLAN interface {0}. Error: {1}".format(
175 vif_name, res.errors[0].message
176 )
177 )
178 if not module.params["enabled"]:
179 res = array.patch_network_interfaces(
180 names=[vif_name], network=NetworkInterfacePatch(enabled=False)
181 )
182 if res.status_code != 200:
183 module.fail_json(
184 msg="Failed to disable VLAN interface {0}. Error: {1}".format(
185 vif_name, res.errors[0].message
186 )
187 )
188 else:
189 res = array.patch_network_interfaces(
190 names=[vif_name], network=NetworkInterfacePatch(enabled=True)
191 )
192 if res.status_code != 200:
193 module.fail_json(
194 msg="Failed to disable VLAN interface {0}. Error: {1}".format(
195 vif_name, res.errors[0].message
196 )
197 )
198 module.exit_json(changed=changed)
199
200
201 def update_vif(module, array, interface, subnet):
202 """Modify VLAN Interface settings"""
203 changed = False
204 vif_info = _get_vif(array, interface, subnet)
205 vif_name = vif_info["name"]
206 if module.params["address"]:
207 if module.params["address"] != vif_info["eth"]["address"]:
208 changed = True
209 if not module.check_mode:
210 res = array.patch_network_interfaces(
211 names=[vif_name],
212 network=NetworkInterfacePatch(
213 eth=NetworkinterfacepatchEth(address=module.params["address"])
214 ),
215 )
216 if res.status_code != 200:
217 module.fail_json(
218 msg="Failed to change IP address for VLAN interface {0}. Error: {1}".format(
219 subnet, res.errors[0].message
220 )
221 )
222
223 if module.params["enabled"] != vif_info["enabled"]:
224 if module.params["enabled"]:
225 changed = True
226 if not module.check_mode:
227 res = array.patch_network_interfaces(
228 names=[vif_name], network=NetworkInterfacePatch(enabled=True)
229 )
230 if res.status_code != 200:
231 module.fail_json(
232 msg="Failed to enable VLAN interface {0}. Error: {1}".format(
233 vif_name, res.errors[0].message
234 )
235 )
236 else:
237 changed = True
238 if not module.check_mode:
239 res = array.patch_network_interfaces(
240 names=[vif_name], network=NetworkInterfacePatch(enabled=False)
241 )
242 if res.status_code != 200:
243 module.fail_json(
244 msg="Failed to disable VLAN interface {0}. Error: {1}".format(
245 vif_name, res.errors[0].message
246 )
247 )
248
249 module.exit_json(changed=changed)
250
251
252 def delete_vif(module, array, subnet):
253 """Delete VLAN Interface"""
254 changed = True
255 if not module.check_mode:
256 vif_name = module.params["name"] + "." + str(subnet["vlan"])
257 res = array.delete_network_interfaces(names=[vif_name])
258 if res.status_code != 200:
259 module.fail_json(msg="Failed to delete VLAN interface {0}".format(vif_name))
260 module.exit_json(changed=changed)
261
262
263 def main():
264 argument_spec = purefa_argument_spec()
265 argument_spec.update(
266 dict(
267 name=dict(type="str", required=True),
268 subnet=dict(type="str", required=True),
269 enabled=dict(type="bool", default=True),
270 state=dict(type="str", default="present", choices=["present", "absent"]),
271 address=dict(type="str"),
272 )
273 )
274
275 module = AnsibleModule(argument_spec, supports_check_mode=True)
276
277 state = module.params["state"]
278 array = get_array(module)
279 subnet = _get_subnet(module, array)
280 interface = _get_interface(module, array)
281 if not subnet:
282 module.fail_json(msg="Invalid subnet specified.")
283 if not interface:
284 module.fail_json(msg="Invalid interface specified.")
285 if subnet["vlan"]:
286 vif_name = module.params["name"] + "." + str(subnet["vlan"])
287 vif = bool(array.get_network_interfaces(names=[vif_name]).status_code == 200)
288
289 if state == "present" and not vif:
290 create_vif(module, array, interface, subnet)
291 elif state == "present" and vif:
292 update_vif(module, array, interface, subnet)
293 elif state == "absent" and vif:
294 delete_vif(module, array, subnet)
295
296 module.exit_json(changed=False)
297
298
299 if __name__ == "__main__":
300 main()