Debian Codegraph @

ansible_collections/cisco/meraki/plugins/modules/meraki_network_settings.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023 Kevin Breit (@kbreit) <[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 ANSIBLE_METADATA = {
12 "metadata_version": "1.1",
13 "status": ["deprecated"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 author:
19 - Kevin Breit (@kbreit)
20 deprecated:
21 alternative: cisco.meraki.networks_settings
22 removed_in: 3.0.0
23 why: Updated modules released with increased functionality
24 description:
25 - Allows for management of settings of networks within Meraki.
26 extends_documentation_fragment: cisco.meraki.meraki
27 module: meraki_network_settings
28 options:
29 local_status_page:
30 description:
31 - Configuration stanza of the local status page.
32 suboptions:
33 authentication:
34 description:
35 - Local status page authentication settings.
36 suboptions:
37 enabled:
38 description:
39 - Set whether local status page authentication is enabled.
40 type: bool
41 password:
42 description:
43 - Set password on local status page.
44 type: str
45 type: dict
46 type: dict
47 local_status_page_enabled:
48 description: '- Enables the local device status pages (U[my.meraki.com](my.meraki.com),
49 U[ap.meraki.com](ap.meraki.com), U[switch.meraki.com](switch.meraki.com), U[wired.meraki.com](wired.meraki.com)).
50 - Only can be specified on its own or with C(remote_status_page_enabled). '
51 type: bool
52 net_id:
53 description:
54 - ID number of a network.
55 type: str
56 net_name:
57 aliases:
58 - name
59 - network
60 description:
61 - Name of a network.
62 type: str
63 remote_status_page_enabled:
64 description:
65 - Enables access to the device status page (U(http://device LAN IP)).
66 - Can only be set if C(local_status_page_enabled:) is set to C(yes).
67 - Only can be specified on its own or with C(local_status_page_enabled).
68 type: bool
69 secure_port:
70 description:
71 - Configuration of SecureConnect options applied to the network.
72 suboptions:
73 enabled:
74 description:
75 - Set whether SecureConnect is enabled on the network.
76 type: bool
77 type: dict
78 state:
79 choices:
80 - present
81 - query
82 default: query
83 description:
84 - Create or modify an organization.
85 type: str
86 short_description: Manage the settings of networks in the Meraki cloud
87 """
88
89 EXAMPLES = r"""
90 - name: Get network settings
91 cisco.meraki.meraki_network_settings:
92 auth_key: '{{ auth_key }}'
93 state: query
94 org_name: '{{ test_org_name }}'
95 net_name: NetworkSettingsTestNet
96 delegate_to: localhost
97 - name: Update network settings
98 cisco.meraki.meraki_network_settings:
99 auth_key: '{{ auth_key }}'
100 state: present
101 org_name: '{{ test_org_name }}'
102 net_name: NetworkSettingsTestNet
103 local_status_page_enabled: false
104 delegate_to: localhost
105 - name: Enable password on local page
106 cisco.meraki.meraki_network_settings:
107 auth_key: '{{ auth_key }}'
108 state: present
109 org_name: '{{ test_org_name }}'
110 net_name: NetworkSettingsTestNet
111 local_status_page_enabled: true
112 local_status_page:
113 authentication:
114 enabled: true
115 password: abc123
116 delegate_to: localhost
117 """
118
119 RETURN = r"""
120 data:
121 description: Information about the created or manipulated object.
122 returned: info
123 type: complex
124 contains:
125 local_status_page_enabled:
126 description: States whether U(my.meraki.com) and other device portals should be enabled.
127 returned: success
128 type: bool
129 sample: true
130 remote_status_page_enabled:
131 description: Enables access to the device status page.
132 returned: success
133 type: bool
134 sample: true
135 expire_data_older_than:
136 description: The number of days, weeks, or months in Epoch time to expire the data before
137 returned: success
138 type: int
139 sample: 1234
140 fips:
141 description: A hash of FIPS options applied to the Network.
142 returned: success
143 type: complex
144 contains:
145 enabled:
146 description: Enables/disables FIPS on the network.
147 returned: success
148 type: bool
149 sample: true
150 local_status_page:
151 description: A hash of Local Status Page(s) authentication options applied to the Network.
152 returned: success
153 type: complex
154 contains:
155 authentication:
156 description: A hash of Local Status Pages' authentication options applied to the Network.
157 type: complex
158 contains:
159 username:
160 description: The username used for Local Status Pages.
161 type: str
162 returned: success
163 sample: admin
164 enabled:
165 description: Enables/Disables the authenticaiton on Local Status Pages.
166 type: bool
167 returned: success
168 sample: true
169 secure_port:
170 description: A hash of SecureConnect options applied to the Network.
171 type: complex
172 contains:
173 enabled:
174 description: Enables/disables SecureConnect on the network.
175 type: bool
176 returned: success
177 sample: true
178 named_vlans:
179 description: A hash of Named VLANs options applied to the Network.
180 type: complex
181 contains:
182 enabled:
183 description: Enables/disables Named VLANs on the network.
184 type: bool
185 returned: success
186 sample: true
187 """
188
189 from ansible.module_utils.basic import AnsibleModule, json
190 from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import (
191 MerakiModule,
192 meraki_argument_spec,
193 )
194
195
196 def is_net_valid(data, net_name=None, net_id=None):
197 if net_name is None and net_id is None:
198 return False
199 for n in data:
200 if net_name:
201 if n["name"] == net_name:
202 return True
203 elif net_id:
204 if n["id"] == net_id:
205 return True
206 return False
207
208
209 def get_network_settings(meraki, net_id):
210 path = meraki.construct_path("get_settings", net_id=net_id)
211 response = meraki.request(path, method="GET")
212 return response
213
214
215 def construct_payload(params):
216 payload = dict()
217 if params["local_status_page_enabled"] is not None:
218 payload["localStatusPageEnabled"] = params["local_status_page_enabled"]
219 if params["remote_status_page_enabled"] is not None:
220 payload["remoteStatusPageEnabled"] = params["remote_status_page_enabled"]
221 if params["local_status_page"] is not None:
222 payload["localStatusPage"] = dict()
223 if params["local_status_page"]["authentication"] is not None:
224 payload["localStatusPage"]["authentication"] = {}
225 if params["local_status_page"]["authentication"]["enabled"] is not None:
226 payload["localStatusPage"]["authentication"]["enabled"] = params["local_status_page"]["authentication"]["enabled"]
227 if params["local_status_page"]["authentication"]["password"] is not None:
228 payload["localStatusPage"]["authentication"]["password"] = params["local_status_page"]["authentication"]["password"]
229 if params["secure_port"] is not None:
230 payload["securePort"] = dict()
231 if params["secure_port"]["enabled"] is not None:
232 payload["securePort"]["enabled"] = params["secure_port"]["enabled"]
233 return payload
234
235
236 def main():
237
238 # define the available arguments/parameters that a user can pass to
239 # the module
240
241 auth_args = dict(
242 enabled=dict(type="bool"),
243 password=dict(type="str", no_log=True),
244 )
245
246 local_status_page_args = dict(
247 authentication=dict(type="dict", default=None, options=auth_args),
248 )
249
250 secure_port_args = dict(
251 enabled=dict(type="bool"),
252 )
253
254 argument_spec = meraki_argument_spec()
255 argument_spec.update(
256 state=dict(type="str", choices=["query", "present"], default="query"),
257 net_name=dict(type="str", aliases=["name", "network"]),
258 net_id=dict(type="str"),
259 local_status_page_enabled=dict(type="bool"),
260 remote_status_page_enabled=dict(type="bool"),
261 local_status_page=dict(type="dict", default=None, options=local_status_page_args),
262 secure_port=dict(type="dict", default=None, options=secure_port_args)
263 )
264
265 # the AnsibleModule object will be our abstraction working with Ansible
266 # this includes instantiation, a couple of common attr would be the
267 # args/params passed to the execution, as well as if the module
268 # supports check mode
269 module = AnsibleModule(
270 argument_spec=argument_spec,
271 supports_check_mode=True,
272 )
273
274 meraki = MerakiModule(module, function="network")
275 module.params["follow_redirects"] = "all"
276 payload = None
277
278 update_settings_urls = {"network": "/networks/{net_id}/settings"}
279 get_settings_urls = {"network": "/networks/{net_id}/settings"}
280 meraki.url_catalog["update_settings"] = update_settings_urls
281 meraki.url_catalog["get_settings"] = get_settings_urls
282
283 if not meraki.params["org_name"] and not meraki.params["org_id"]:
284 meraki.fail_json(msg="org_name or org_id parameters are required")
285 if meraki.params["state"] != "query":
286 if not meraki.params["net_name"] and not meraki.params["net_id"]:
287 meraki.fail_json(
288 msg="net_name or net_id is required for present or absent states"
289 )
290 if meraki.params["net_name"] and meraki.params["net_id"]:
291 meraki.fail_json(msg="net_name and net_id are mutually exclusive")
292 if (
293 meraki.params["local_status_page_enabled"] is False
294 and meraki.params["remote_status_page_enabled"] is True
295 ):
296 meraki.fail_json(
297 msg="local_status_page_enabled must be true when setting remote_status_page_enabled"
298 )
299
300 # manipulate or modify the state as needed (this is going to be the
301 # part where your module will do what it needs to do)
302
303 org_id = meraki.params["org_id"]
304 net_id = meraki.params["net_id"]
305 if not org_id:
306 org_id = meraki.get_org_id(meraki.params["org_name"])
307 if net_id is None:
308 nets = meraki.get_nets(org_id=org_id)
309 net_id = meraki.get_net_id(org_id, meraki.params["net_name"], data=nets)
310
311 if meraki.params["state"] == "query":
312 path = meraki.construct_path("get_settings", net_id=net_id)
313 meraki.result["data"] = meraki.request(path, method="GET")
314 meraki.exit_json(**meraki.result)
315 elif meraki.params["state"] == "present":
316 path = meraki.construct_path("get_settings", net_id=net_id)
317 current = meraki.request(path, method="GET")
318 payload = construct_payload(meraki.params)
319 if meraki.is_update_required(current, payload, optional_ignore=["password"]):
320 if meraki.check_mode is True:
321 try:
322 del payload["local_status_page"]["authentication"]["password"]
323 except KeyError:
324 pass
325 meraki.result["data"] = payload
326 meraki.result["changed"] = True
327 meraki.exit_json(**meraki.result)
328 path = meraki.construct_path("update_settings", net_id=net_id)
329 response = meraki.request(path, method="PUT", payload=json.dumps(payload))
330 if meraki.status == 200:
331 meraki.result["changed"] = True
332 meraki.result["data"] = response
333 meraki.exit_json(**meraki.result)
334 meraki.result["data"] = current
335 # in the event of a successful module execution, you will want to
336 # simple AnsibleModule.exit_json(), passing the key/value results
337 meraki.exit_json(**meraki.result)
338
339
340 if __name__ == "__main__":
341 main()