Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, 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_site_to_site_vpn
19 short_description: Manage AutoVPN connections in Meraki
20 version_added: "1.1.0"
21 description:
22 - Allows for creation, management, and visibility into AutoVPNs implemented on Meraki MX firewalls.
23 deprecated:
24 removed_in: '3.0.0'
25 why: Updated modules released with increased functionality
26 alternative: cisco.meraki.networks_appliance_vpn_site_to_site_vpn
27 options:
28 state:
29 description:
30 - Create or modify an organization.
31 choices: ['present', 'query']
32 default: present
33 type: str
34 net_name:
35 description:
36 - Name of network which MX firewall is in.
37 type: str
38 net_id:
39 description:
40 - ID of network which MX firewall is in.
41 type: str
42 mode:
43 description:
44 - Set VPN mode for network
45 choices: ['none', 'hub', 'spoke']
46 type: str
47 hubs:
48 description:
49 - List of hubs to assign to a spoke.
50 type: list
51 elements: dict
52 suboptions:
53 hub_id:
54 description:
55 - Network ID of hub
56 type: str
57 use_default_route:
58 description:
59 - Indicates whether deafult troute traffic should be sent to this hub.
60 - Only valid in spoke mode.
61 type: bool
62 subnets:
63 description:
64 - List of subnets to advertise over VPN.
65 type: list
66 elements: dict
67 suboptions:
68 local_subnet:
69 description:
70 - CIDR formatted subnet.
71 type: str
72 use_vpn:
73 description:
74 - Whether to advertise over VPN.
75 type: bool
76 author:
77 - Kevin Breit (@kbreit)
78 extends_documentation_fragment: cisco.meraki.meraki
79 '''
80
81 EXAMPLES = r'''
82 - name: Set hub mode
83 meraki_site_to_site_vpn:
84 auth_key: abc123
85 state: present
86 org_name: YourOrg
87 net_name: hub_network
88 mode: hub
89 delegate_to: localhost
90 register: set_hub
91
92 - name: Set spoke mode
93 meraki_site_to_site_vpn:
94 auth_key: abc123
95 state: present
96 org_name: YourOrg
97 net_name: spoke_network
98 mode: spoke
99 hubs:
100 - hub_id: N_1234
101 use_default_route: false
102 delegate_to: localhost
103 register: set_spoke
104
105 - name: Add subnet to hub for VPN. Hub is required.
106 meraki_site_to_site_vpn:
107 auth_key: abc123
108 state: present
109 org_name: YourOrg
110 net_name: hub_network
111 mode: hub
112 hubs:
113 - hub_id: N_1234
114 use_default_route: false
115 subnets:
116 - local_subnet: 192.168.1.0/24
117 use_vpn: true
118 delegate_to: localhost
119 register: set_hub
120
121 - name: Query rules for hub
122 meraki_site_to_site_vpn:
123 auth_key: abc123
124 state: query
125 org_name: YourOrg
126 net_name: hub_network
127 delegate_to: localhost
128 register: query_all_hub
129 '''
130
131 RETURN = r'''
132 data:
133 description: VPN settings.
134 returned: success
135 type: complex
136 contains:
137 mode:
138 description: Mode assigned to network.
139 returned: always
140 type: str
141 sample: spoke
142 hubs:
143 description: Hub networks to associate to.
144 returned: always
145 type: complex
146 contains:
147 hub_id:
148 description: ID of hub network.
149 returned: always
150 type: complex
151 sample: N_12345
152 use_default_route:
153 description: Whether to send all default route traffic over VPN.
154 returned: always
155 type: bool
156 sample: true
157 subnets:
158 description: List of subnets to advertise over VPN.
159 returned: always
160 type: complex
161 contains:
162 local_subnet:
163 description: CIDR formatted subnet.
164 returned: always
165 type: str
166 sample: 192.168.1.0/24
167 use_vpn:
168 description: Whether subnet should use the VPN.
169 returned: always
170 type: bool
171 sample: true
172 '''
173
174 from ansible.module_utils.basic import AnsibleModule, json
175 from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec
176 from copy import deepcopy
177
178
179 def assemble_payload(meraki):
180 payload = {'mode': meraki.params['mode']}
181 if meraki.params['hubs'] is not None:
182 payload['hubs'] = meraki.params['hubs']
183 for hub in payload['hubs']:
184 hub['hubId'] = hub.pop('hub_id')
185 hub['useDefaultRoute'] = hub.pop('use_default_route')
186 if meraki.params['subnets'] is not None:
187 payload['subnets'] = meraki.params['subnets']
188 for subnet in payload['subnets']:
189 subnet['localSubnet'] = subnet.pop('local_subnet')
190 subnet['useVpn'] = subnet.pop('use_vpn')
191 return payload
192
193
194 def main():
195 # define the available arguments/parameters that a user can pass to
196 # the module
197
198 hubs_args = dict(hub_id=dict(type='str'),
199 use_default_route=dict(type='bool'),
200 )
201 subnets_args = dict(local_subnet=dict(type='str'),
202 use_vpn=dict(type='bool'),
203 )
204
205 argument_spec = meraki_argument_spec()
206 argument_spec.update(state=dict(type='str', choices=['present', 'query'], default='present'),
207 net_name=dict(type='str'),
208 net_id=dict(type='str'),
209 hubs=dict(type='list', default=None, elements='dict', options=hubs_args),
210 subnets=dict(type='list', default=None, elements='dict', options=subnets_args),
211 mode=dict(type='str', choices=['none', 'hub', 'spoke']),
212 )
213
214 # the AnsibleModule object will be our abstraction working with Ansible
215 # this includes instantiation, a couple of common attr would be the
216 # args/params passed to the execution, as well as if the module
217 # supports check mode
218 module = AnsibleModule(argument_spec=argument_spec,
219 supports_check_mode=True,
220 )
221 meraki = MerakiModule(module, function='site_to_site_vpn')
222
223 meraki.params['follow_redirects'] = 'all'
224
225 query_urls = {'site_to_site_vpn': '/networks/{net_id}/appliance/vpn/siteToSiteVpn/'}
226 update_urls = {'site_to_site_vpn': '/networks/{net_id}/appliance/vpn/siteToSiteVpn/'}
227
228 meraki.url_catalog['get_all'].update(query_urls)
229 meraki.url_catalog['update'] = update_urls
230
231 payload = None
232
233 # manipulate or modify the state as needed (this is going to be the
234 # part where your module will do what it needs to do)
235 org_id = meraki.params['org_id']
236 if org_id is None:
237 orgs = meraki.get_orgs()
238 for org in orgs:
239 if org['name'] == meraki.params['org_name']:
240 org_id = org['id']
241 net_id = meraki.params['net_id']
242 if net_id is None:
243 net_id = meraki.get_net_id(net_name=meraki.params['net_name'],
244 data=meraki.get_nets(org_id=org_id))
245
246 if meraki.params['state'] == 'query':
247 path = meraki.construct_path('get_all', net_id=net_id)
248 response = meraki.request(path, method='GET')
249 meraki.result['data'] = response
250 elif meraki.params['state'] == 'present':
251 path = meraki.construct_path('get_all', net_id=net_id)
252 original = meraki.request(path, method='GET')
253 payload = assemble_payload(meraki)
254 comparable = deepcopy(original)
255 comparable.update(payload)
256 if meraki.is_update_required(original, payload):
257 if meraki.check_mode is True:
258 meraki.result['changed'] = True
259 meraki.result['data'] = payload
260 meraki.exit_json(**meraki.result)
261 path = meraki.construct_path('update', net_id=net_id)
262 response = meraki.request(path, method='PUT', payload=json.dumps(payload))
263 meraki.result['changed'] = True
264 meraki.result['data'] = response
265 else:
266 meraki.result['data'] = original
267
268 # in the event of a successful module execution, you will want to
269 # simple AnsibleModule.exit_json(), passing the key/value results
270 meraki.exit_json(**meraki.result)
271
272
273 if __name__ == '__main__':
274 main()