Debian Codegraph @

ansible_collections/netapp/storagegrid/plugins/modules/na_sg_grid_vlan_interface.py

raw
1 #!/usr/bin/python
2
3 # (c) 2025, NetApp Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 """NetApp StorageGRID - Manage VLAN Interface Configuration"""
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12
13 ANSIBLE_METADATA = {
14 "metadata_version": "1.0",
15 "status": ["preview"],
16 "supported_by": "community",
17 }
18
19
20 DOCUMENTATION = """
21 module: na_sg_grid_vlan_interface
22 short_description: Configure VLAN interface on StorageGRID.
23 extends_documentation_fragment:
24 - netapp.storagegrid.netapp.sg
25 version_added: '21.15.0'
26 author: NetApp Ansible Team (@vinaykus) <[email protected]>
27 description:
28 - Configure VLAN interface on NetApp StorageGRID.
29 options:
30 state:
31 description:
32 - Whether the vlan interface should be present or absent.
33 choices: ['present', 'absent']
34 default: 'present'
35 type: str
36 vlan_id:
37 description:
38 - The numeric ID of the VLAN used for VLAN tagging.
39 type: int
40 description:
41 description:
42 - Description for this VLAN interface.
43 type: str
44 interfaces:
45 description:
46 - List of node interface pairs.
47 type: list
48 elements: dict
49 suboptions:
50 node_id:
51 description:
52 - The node UUID.
53 type: str
54 required: true
55 interface_name:
56 description:
57 - The name of the interface.
58 type: str
59 required: true
60 """
61
62 EXAMPLES = """
63 - name: create vlan interface
64 na_sg_grid_vlan_interface:
65 api_url: "https://<storagegrid-endpoint-url>"
66 auth_token: "storagegrid-auth-token"
67 vlan_id: 428
68 description: "vlan interface"
69 interfaces:
70 - node_id: 6562d5d8-f218-45ff-a466-5bb39b729288
71 interface_name: eth0
72
73 - name: modify vlan interface
74 na_sg_grid_vlan_interface:
75 api_url: "https://<storagegrid-endpoint-url>"
76 auth_token: "storagegrid-auth-token"
77 vlan_id: 428
78 description: "vlan interface is modified"
79 interfaces:
80 - node_id: 6562d5d8-f218-45ff-a466-5bb39b729288
81 interface_name: eth2
82
83 - name: delete vlan interface
84 na_sg_grid_vlan_interface:
85 api_url: "https://<storagegrid-endpoint-url>"
86 auth_token: "storagegrid-auth-token"
87 vlan_id: 428
88 """
89
90 RETURN = """
91 resp:
92 description: Returns information about the StorageGRID VLAN Interface.
93 returned: If state is 'present'.
94 type: dict
95 sample: {
96 "id": "12bcw2a2-7028-4d98-89f8-972414278e3c",
97 "vlanId": 430,
98 "description": "vlan interface",
99 "interfaces": [
100 {
101 "nodeId": "6562d5d8-f718-45ff-a716-5bb39b729238",
102 "interface": "eth0"
103 }
104 ]
105 }
106 """
107
108 import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils
109 from ansible.module_utils.basic import AnsibleModule
110 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule
111 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI
112
113
114 class SgVLANInterface:
115 """
116 Configure VLAN interface on StorageGRID
117 """
118
119 def __init__(self):
120 """
121 Parse arguments, setup state variables,
122 check parameters and ensure request module is installed
123 """
124 self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec()
125 self.argument_spec.update(
126 dict(
127 state=dict(type="str", choices=["present", "absent"], default="present"),
128 vlan_id=dict(required=False, type="int"),
129 description=dict(required=False, type="str"),
130 interfaces=dict(
131 required=False,
132 type="list",
133 elements="dict",
134 options=dict(
135 node_id=dict(required=True, type="str"),
136 interface_name=dict(required=True, type="str"),
137 )
138 )
139 )
140 )
141
142 self.module = AnsibleModule(
143 argument_spec=self.argument_spec,
144 required_if=[("state", "present", ["vlan_id", "interfaces"])],
145 supports_check_mode=True
146 )
147 self.na_helper = NetAppModule()
148
149 # set up state variables
150 self.parameters = self.na_helper.set_parameters(self.module.params)
151 # Calling generic SG rest_api class
152 self.rest_api = SGRestAPI(self.module)
153 # Get API version
154 self.rest_api.get_sg_product_version()
155 self.api_version = self.rest_api.get_api_version()
156
157 # Checking for the parameters passed and create new parameters list
158 self.data = {}
159 self.data["vlanId"] = self.parameters["vlan_id"]
160 if self.parameters.get("description"):
161 self.data["description"] = self.parameters["description"]
162 if self.parameters.get("interfaces"):
163 self.data["interfaces"] = [
164 {
165 "nodeId": interface["node_id"],
166 "interface": interface["interface_name"]
167 }
168 for interface in self.parameters["interfaces"]
169 ]
170
171 def get_vlan_interfaces(self):
172 """ Get vlan interfaces """
173 api = "api/%s/private/vlan-interfaces" % self.api_version
174 response, error = self.rest_api.get(api)
175
176 if error:
177 self.module.fail_json(msg=error)
178 # if vlan interface with 'vlan_id' exists, return it, else none
179 for vlan_interface in response["data"]:
180 if vlan_interface["vlanId"] == self.parameters["vlan_id"]:
181 self.id = vlan_interface["id"]
182 self.data.update({"id": vlan_interface["id"]})
183 return vlan_interface
184
185 return None
186
187 def delete_vlan_interfaces(self):
188 """ Delete vlan interface """
189 api = "api/%s/private/vlan-interfaces/%s" % (self.api_version, self.id)
190 response, error = self.rest_api.delete(api, self.data)
191 if error:
192 self.module.fail_json(msg=error)
193
194 def create_vlan_interface(self):
195 """ create vlan interface """
196 api = "api/%s/private/vlan-interfaces" % self.api_version
197 response, error = self.rest_api.post(api, self.data)
198
199 if error:
200 self.module.fail_json(msg=error)
201 else:
202 return response["data"]
203
204 def update_vlan_interfaces(self):
205 """ Update vlan interface """
206 api = "api/%s/private/vlan-interfaces/%s" % (self.api_version, self.id)
207 response, error = self.rest_api.put(api, self.data)
208
209 if error:
210 self.module.fail_json(msg=error)
211 else:
212 return response["data"]
213
214 def apply(self):
215 ''' Apply vlan interface configuration '''
216
217 current_vlan_interfaces = self.get_vlan_interfaces()
218
219 cd_action = self.na_helper.get_cd_action(current_vlan_interfaces, self.parameters)
220
221 if cd_action is None and self.parameters["state"] == "present":
222 # let's see if we need to update parameters
223 modify = self.na_helper.get_modified_attributes(current_vlan_interfaces, self.data)
224
225 result_message = ""
226 resp_data = current_vlan_interfaces
227 if self.na_helper.changed:
228 if self.module.check_mode:
229 pass
230 else:
231 if cd_action == "delete":
232 self.delete_vlan_interfaces()
233 resp_data = None
234 result_message = "VLAN Interface deleted"
235 elif cd_action == "create":
236 resp_data = self.create_vlan_interface()
237 result_message = "VLAN Interface created"
238 elif modify:
239 resp_data = self.update_vlan_interfaces()
240 result_message = "VLAN Interface updated"
241
242 self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data)
243
244
245 def main():
246 """
247 Main function
248 """
249 na_sg_grid_vlan_interface = SgVLANInterface()
250 na_sg_grid_vlan_interface.apply()
251
252
253 if __name__ == "__main__":
254 main()