Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2020, 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 __metaclass__ = type
9
10 ANSIBLE_METADATA = {
11 'metadata_version': '1.1',
12 "status": ['deprecated'],
13 'supported_by': 'community'
14 }
15
16 DOCUMENTATION = r'''
17 ---
18 module: meraki_mx_l2_interface
19 short_description: Configure MX layer 2 interfaces
20 version_added: "2.1.0"
21 description:
22 - Allows for management and visibility of Merkai MX layer 2 ports.
23
24 deprecated:
25 removed_in: '3.0.0'
26 why: Updated modules released with increased functionality
27 alternative: cisco.meraki.networks_appliance_ports
28 options:
29 state:
30 description:
31 - Modify or query an port.
32 choices: [present, query]
33 default: present
34 type: str
35 net_name:
36 description:
37 - Name of a network.
38 aliases: [name, network]
39 type: str
40 net_id:
41 description:
42 - ID number of a network.
43 type: str
44 org_id:
45 description:
46 - ID of organization associated to a network.
47 type: str
48 number:
49 description:
50 - ID number of MX port.
51 aliases: [port, port_id]
52 type: int
53 vlan:
54 description:
55 - Native VLAN when the port is in Trunk mode.
56 - Access VLAN when the port is in Access mode.
57 type: int
58 access_policy:
59 description:
60 - The name of the policy. Only applicable to access ports.
61 choices: [open, 8021x-radius, mac-radius, hybris-radius]
62 type: str
63 allowed_vlans:
64 description:
65 - Comma-delimited list of the VLAN ID's allowed on the port, or 'all' to permit all VLAN's on the port.
66 type: str
67 port_type:
68 description:
69 - Type of port.
70 choices: [access, trunk]
71 type: str
72 drop_untagged_traffic:
73 description:
74 - Trunk port can Drop all Untagged traffic. When true, no VLAN is required.
75 - Access ports cannot have dropUntaggedTraffic set to true.
76 type: bool
77 enabled:
78 description:
79 - Enabled state of port.
80 type: bool
81
82 author:
83 - Kevin Breit (@kbreit)
84 extends_documentation_fragment: cisco.meraki.meraki
85 '''
86
87 EXAMPLES = r'''
88 - name: Query layer 2 interface settings
89 meraki_mx_l2_interface:
90 auth_key: abc123
91 org_name: YourOrg
92 net_name: YourNet
93 state: query
94 delegate_to: localhost
95
96 - name: Query a single layer 2 interface settings
97 meraki_mx_l2_interface:
98 auth_key: abc123
99 org_name: YourOrg
100 net_name: YourNet
101 state: query
102 number: 2
103 delegate_to: localhost
104
105 - name: Update interface configuration
106 meraki_mx_l2_interface:
107 auth_key: abc123
108 org_name: YourOrg
109 net_name: YourNet
110 state: present
111 number: 2
112 port_type: access
113 vlan: 10
114 delegate_to: localhost
115 '''
116
117 RETURN = r'''
118 data:
119 description: Information about the created or manipulated object.
120 returned: success
121 type: complex
122 contains:
123 number:
124 description:
125 - ID number of MX port.
126 type: int
127 returned: success
128 sample: 4
129 vlan:
130 description:
131 - Native VLAN when the port is in Trunk mode.
132 - Access VLAN when the port is in Access mode.
133 type: int
134 returned: success
135 sample: 1
136 access_policy:
137 description:
138 - The name of the policy. Only applicable to access ports.
139 type: str
140 returned: success
141 sample: guestUsers
142 allowed_vlans:
143 description:
144 - Comma-delimited list of the VLAN ID's allowed on the port, or 'all' to permit all VLAN's on the port.
145 type: str
146 returned: success
147 sample: 1,5,10
148 type:
149 description:
150 - Type of port.
151 type: str
152 returned: success
153 sample: access
154 drop_untagged_traffic:
155 description:
156 - Trunk port can Drop all Untagged traffic. When true, no VLAN is required.
157 - Access ports cannot have dropUntaggedTraffic set to true.
158 type: bool
159 returned: success
160 sample: true
161 enabled:
162 description:
163 - Enabled state of port.
164 type: bool
165 returned: success
166 sample: true
167 '''
168
169 from ansible.module_utils.basic import AnsibleModule, json
170 from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec
171
172
173 def construct_payload(meraki):
174 payload = {}
175 if meraki.params['vlan'] is not None:
176 payload['vlan'] = meraki.params['vlan']
177 if meraki.params['access_policy'] is not None:
178 payload['accessPolicy'] = meraki.params['access_policy']
179 if meraki.params['allowed_vlans'] is not None:
180 payload['allowedVlans'] = meraki.params['allowed_vlans']
181 if meraki.params['port_type'] is not None:
182 payload['type'] = meraki.params['port_type']
183 if meraki.params['drop_untagged_traffic'] is not None:
184 payload['dropUntaggedTraffic'] = meraki.params['drop_untagged_traffic']
185 if meraki.params['enabled'] is not None:
186 payload['enabled'] = meraki.params['enabled']
187 return payload
188
189
190 def main():
191
192 # define the available arguments/parameters that a user can pass to
193 # the module
194
195 argument_spec = meraki_argument_spec()
196 argument_spec.update(
197 net_id=dict(type='str'),
198 net_name=dict(type='str', aliases=['name', 'network']),
199 state=dict(type='str', choices=['present', 'query'], default='present'),
200 number=dict(type='int', aliases=['port', 'port_id']),
201 vlan=dict(type='int'),
202 access_policy=dict(type='str', choices=['open', '8021x-radius', 'mac-radius', 'hybris-radius']),
203 allowed_vlans=dict(type='str'),
204 port_type=dict(type='str', choices=['access', 'trunk']),
205 drop_untagged_traffic=dict(type='bool'),
206 enabled=dict(type='bool'),
207 )
208
209 # the AnsibleModule object will be our abstraction working with Ansible
210 # this includes instantiation, a couple of common attr would be the
211 # args/params passed to the execution, as well as if the module
212 # supports check mode
213 module = AnsibleModule(argument_spec=argument_spec,
214 supports_check_mode=True,
215 )
216
217 meraki = MerakiModule(module, function='mx_l2_interface')
218 module.params['follow_redirects'] = 'all'
219
220 get_all_urls = {'mx_l2_interface': '/networks/{net_id}/appliance/ports'}
221 get_one_urls = {'mx_l2_interface': '/networks/{net_id}/appliance/ports/{port_id}'}
222 update_urls = {'mx_l2_interface': '/networks/{net_id}/appliance/ports/{port_id}'}
223 meraki.url_catalog['query_all'] = get_all_urls
224 meraki.url_catalog['query_one'] = get_one_urls
225 meraki.url_catalog['update'] = update_urls
226
227 if meraki.params['net_name'] and meraki.params['net_id']:
228 meraki.fail_json(msg='net_name and net_id are mutually exclusive.')
229 if meraki.params['port_type'] == 'access':
230 if meraki.params['allowed_vlans'] is not None:
231 meraki.meraki.fail_json(msg='allowed_vlans is mutually exclusive with port type trunk.')
232
233 org_id = meraki.params['org_id']
234 if not org_id:
235 org_id = meraki.get_org_id(meraki.params['org_name'])
236 net_id = meraki.params['net_id']
237 if net_id is None:
238 nets = meraki.get_nets(org_id=org_id)
239 net_id = meraki.get_net_id(org_id, meraki.params['net_name'], data=nets)
240
241 if meraki.params['state'] == 'query':
242 if meraki.params['number'] is not None:
243 path = meraki.construct_path('query_one', net_id=net_id, custom={'port_id': meraki.params['number']})
244 else:
245 path = meraki.construct_path('query_all', net_id=net_id)
246 response = meraki.request(path, method='GET')
247 meraki.result['data'] = response
248 meraki.exit_json(**meraki.result)
249 elif meraki.params['state'] == 'present':
250 path = meraki.construct_path('query_one', net_id=net_id, custom={'port_id': meraki.params['number']})
251 original = meraki.request(path, method='GET')
252 payload = construct_payload(meraki)
253 if meraki.is_update_required(original, payload):
254 meraki.generate_diff(original, payload)
255 if meraki.check_mode is True:
256 original.update(payload)
257 meraki.result['data'] = original
258 meraki.result['changed'] = True
259 meraki.exit_json(**meraki.result)
260 path = meraki.construct_path('update', net_id=net_id, custom={'port_id': meraki.params['number']})
261 response = meraki.request(path, method='PUT', payload=json.dumps(payload))
262 if meraki.status == 200:
263 meraki.result['data'] = response
264 meraki.result['changed'] = True
265 meraki.exit_json(**meraki.result)
266 else:
267 meraki.result['data'] = original
268 meraki.exit_json(**meraki.result)
269
270 # in the event of a successful module execution, you will want to
271 # simple AnsibleModule.exit_json(), passing the key/value results
272 meraki.exit_json(**meraki.result)
273
274
275 if __name__ == '__main__':
276 main()