Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2022 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_appliance_vlans_settings
22 removed_in: 3.0.0
23 why: Updated modules released with increased functionality
24 description:
25 - Edits VLAN enabled status on a network within Meraki.
26 extends_documentation_fragment: cisco.meraki.meraki
27 module: meraki_mx_network_vlan_settings
28 options:
29 net_id:
30 description:
31 - ID number of a network.
32 type: str
33 net_name:
34 aliases:
35 - name
36 - network
37 description:
38 - Name of a network.
39 type: str
40 state:
41 choices:
42 - present
43 - query
44 description:
45 - Create or modify an alert.
46 type: str
47 vlans_enabled:
48 description:
49 - Whether VLANs are enabled on the network.
50 type: bool
51 short_description: Manage VLAN settings for Meraki Networks
52 """
53
54 EXAMPLES = r"""
55 - name: Update settings
56 meraki_mx_network_vlan_settings:
57 auth_key: abc123
58 org_name: YourOrg
59 net_name: YourNet
60 state: present
61 vlans_enabled: true
62 """
63
64 RETURN = r"""
65 data:
66 description: Information about the created or manipulated object.
67 returned: info
68 type: complex
69 contains:
70 vlans_enabled:
71 description: Whether VLANs are enabled for this network.
72 returned: success
73 type: bool
74 """
75
76 from ansible.module_utils.basic import AnsibleModule, json
77 from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import (
78 MerakiModule,
79 meraki_argument_spec,
80 )
81
82
83 def construct_payload(meraki):
84 payload = {"vlansEnabled": meraki.params["vlans_enabled"]}
85 return payload
86
87
88 def main():
89
90 # define the available arguments/parameters that a user can pass to
91 # the module
92
93 argument_spec = meraki_argument_spec()
94 argument_spec.update(
95 net_id=dict(type="str"),
96 net_name=dict(type="str", aliases=["name", "network"]),
97 state=dict(type="str", choices=["query", "present"]),
98 vlans_enabled=dict(type="bool"),
99 )
100
101 # the AnsibleModule object will be our abstraction working with Ansible
102 # this includes instantiation, a couple of common attr would be the
103 # args/params passed to the execution, as well as if the module
104 # supports check mode
105 module = AnsibleModule(
106 argument_spec=argument_spec,
107 supports_check_mode=True,
108 )
109
110 meraki = MerakiModule(module, function="network_vlan_settings")
111 module.params["follow_redirects"] = "all"
112
113 query_urls = {
114 "network_vlan_settings": "/networks/{net_id}/appliance/vlans/settings"
115 }
116 update_urls = {
117 "network_vlan_settings": "/networks/{net_id}/appliance/vlans/settings"
118 }
119 meraki.url_catalog["get_all"].update(query_urls)
120 meraki.url_catalog["update"] = update_urls
121
122 # manipulate or modify the state as needed (this is going to be the
123 # part where your module will do what it needs to do)
124
125 org_id = meraki.params["org_id"]
126 if org_id is None:
127 org_id = meraki.get_org_id(meraki.params["org_name"])
128 net_id = meraki.params["net_id"]
129 if net_id is None:
130 nets = meraki.get_nets(org_id=org_id)
131 net_id = meraki.get_net_id(net_name=meraki.params["net_name"], data=nets)
132
133 if meraki.params["state"] == "query":
134 path = meraki.construct_path("get_all", net_id=net_id)
135 response = meraki.request(path, method="GET")
136 if meraki.status == 200:
137 meraki.result["data"] = response
138 meraki.exit_json(**meraki.result)
139 elif meraki.params["state"] == "present":
140 path = meraki.construct_path("get_all", net_id=net_id)
141 original = meraki.request(path, method="GET")
142 payload = construct_payload(meraki)
143 if meraki.is_update_required(original, payload):
144 if meraki.check_mode is True:
145 meraki.generate_diff(original, payload)
146 meraki.result["data"] = payload
147 meraki.result["changed"] = True
148 meraki.exit_json(**meraki.result)
149 path = meraki.construct_path("update", net_id=net_id)
150 response = meraki.request(path, method="PUT", payload=json.dumps(payload))
151 if meraki.status == 200:
152 meraki.generate_diff(original, payload)
153 meraki.result["data"] = response
154 meraki.result["changed"] = True
155 meraki.exit_json(**meraki.result)
156 else:
157 meraki.result["data"] = original
158 meraki.exit_json(**meraki.result)
159
160 # in the event of a successful module execution, you will want to
161 # simple AnsibleModule.exit_json(), passing the key/value results
162 meraki.exit_json(**meraki.result)
163
164
165 if __name__ == "__main__":
166 main()