Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2019, 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_firewalled_services
19 short_description: Edit firewall policies for administrative network services
20 description:
21 - Allows for setting policy firewalled services for Meraki network devices.
22
23 deprecated:
24 removed_in: '3.0.0'
25 why: Updated modules released with increased functionality
26 alternative: cisco.meraki.networks_appliance_firewall_firewalled_services
27 options:
28 auth_key:
29 description:
30 - Authentication key provided by the dashboard. Required if environmental variable MERAKI_KEY is not set.
31 type: str
32 net_name:
33 description:
34 - Name of a network.
35 aliases: [ network ]
36 type: str
37 net_id:
38 description:
39 - ID number of a network.
40 type: str
41 org_name:
42 description:
43 - Name of organization associated to a network.
44 type: str
45 org_id:
46 description:
47 - ID of organization associated to a network.
48 type: str
49 state:
50 description:
51 - States that a policy should be created or modified.
52 choices: [present, query]
53 default: present
54 type: str
55 service:
56 description:
57 - Network service to query or modify.
58 choices: [ICMP, SNMP, web]
59 type: str
60 access:
61 description:
62 - Network service to query or modify.
63 choices: [blocked, restricted, unrestricted]
64 type: str
65 allowed_ips:
66 description:
67 - List of IP addresses allowed to access a service.
68 - Only used when C(access) is set to restricted.
69 type: list
70 elements: str
71
72 author:
73 - Kevin Breit (@kbreit)
74 extends_documentation_fragment: cisco.meraki.meraki
75 '''
76
77 EXAMPLES = r'''
78 - name: Set icmp service to blocked
79 meraki_firewalled_services:
80 auth_key: '{{ auth_key }}'
81 state: present
82 org_name: '{{ test_org_name }}'
83 net_name: IntTestNetworkAppliance
84 service: ICMP
85 access: blocked
86 delegate_to: localhost
87
88 - name: Set icmp service to restricted
89 meraki_firewalled_services:
90 auth_key: abc123
91 state: present
92 org_name: YourOrg
93 net_name: YourNet
94 service: web
95 access: restricted
96 allowed_ips:
97 - 192.0.1.1
98 - 192.0.1.2
99 delegate_to: localhost
100
101 - name: Query appliance services
102 meraki_firewalled_services:
103 auth_key: abc123
104 state: query
105 org_name: YourOrg
106 net_name: YourNet
107 delegate_to: localhost
108
109 - name: Query services
110 meraki_firewalled_services:
111 auth_key: abc123
112 state: query
113 org_name: YourOrg
114 net_name: YourNet
115 service: ICMP
116 delegate_to: localhost
117 '''
118
119 RETURN = r'''
120 data:
121 description: List of network services.
122 returned: info
123 type: complex
124 contains:
125 access:
126 description: Access assigned to a service type.
127 returned: success
128 type: str
129 sample: unrestricted
130 service:
131 description: Service to apply policy to.
132 returned: success
133 type: str
134 sample: ICMP
135 allowed_ips:
136 description: List of IP addresses to have access to service.
137 returned: success
138 type: str
139 sample: 192.0.1.0
140 '''
141
142 from ansible.module_utils.basic import AnsibleModule, json
143 from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec
144
145
146 def main():
147
148 # define the available arguments/parameters that a user can pass to
149 # the module
150
151 argument_spec = meraki_argument_spec()
152 argument_spec.update(
153 net_id=dict(type='str'),
154 net_name=dict(type='str', aliases=['network']),
155 state=dict(type='str', default='present', choices=['query', 'present']),
156 service=dict(type='str', default=None, choices=['ICMP', 'SNMP', 'web']),
157 access=dict(type='str', choices=['blocked', 'restricted', 'unrestricted']),
158 allowed_ips=dict(type='list', elements='str'),
159 )
160
161 mutually_exclusive = [('net_name', 'net_id')]
162
163 # the AnsibleModule object will be our abstraction working with Ansible
164 # this includes instantiation, a couple of common attr would be the
165 # args/params passed to the execution, as well as if the module
166 # supports check mode
167 module = AnsibleModule(argument_spec=argument_spec,
168 supports_check_mode=True,
169 mutually_exclusive=mutually_exclusive
170 )
171
172 meraki = MerakiModule(module, function='firewalled_services')
173 module.params['follow_redirects'] = 'all'
174
175 net_services_urls = {'firewalled_services': '/networks/{net_id}/appliance/firewall/firewalledServices'}
176 services_urls = {'firewalled_services': '/networks/{net_id}/appliance/firewall/firewalledServices/{service}'}
177
178 meraki.url_catalog['network_services'] = net_services_urls
179 meraki.url_catalog['service'] = services_urls
180
181 # manipulate or modify the state as needed (this is going to be the
182 # part where your module will do what it needs to do)
183
184 org_id = meraki.params['org_id']
185 if not org_id:
186 org_id = meraki.get_org_id(meraki.params['org_name'])
187 net_id = meraki.params['net_id']
188 if net_id is None:
189 nets = meraki.get_nets(org_id=org_id)
190 net_id = meraki.get_net_id(org_id, meraki.params['net_name'], data=nets)
191
192 if meraki.params['state'] == 'present':
193 if meraki.params['access'] != 'restricted' and meraki.params['allowed_ips'] is not None:
194 meraki.fail_json(msg="allowed_ips is only allowed when access is restricted.")
195 payload = {'access': meraki.params['access']}
196 if meraki.params['access'] == 'restricted':
197 payload['allowedIps'] = meraki.params['allowed_ips']
198
199 if meraki.params['state'] == 'query':
200 if meraki.params['service'] is None:
201 path = meraki.construct_path('network_services', net_id=net_id)
202 response = meraki.request(path, method='GET')
203 meraki.result['data'] = response
204 meraki.exit_json(**meraki.result)
205 else:
206 path = meraki.construct_path('service', net_id=net_id, custom={'service': meraki.params['service']})
207 response = meraki.request(path, method='GET')
208 meraki.result['data'] = response
209 meraki.exit_json(**meraki.result)
210 elif meraki.params['state'] == 'present':
211 path = meraki.construct_path('service', net_id=net_id, custom={'service': meraki.params['service']})
212 original = meraki.request(path, method='GET')
213 if meraki.is_update_required(original, payload, optional_ignore=['service']):
214 if meraki.check_mode is True:
215 diff_payload = {'service': meraki.params['service']} # Need to add service as it's not in payload
216 diff_payload.update(payload)
217 meraki.generate_diff(original, diff_payload)
218 original.update(payload)
219 meraki.result['data'] = original
220 meraki.result['changed'] = True
221 meraki.exit_json(**meraki.result)
222 path = meraki.construct_path('service', net_id=net_id, custom={'service': meraki.params['service']})
223 response = meraki.request(path, method='PUT', payload=json.dumps(payload))
224 if meraki.status == 200:
225 meraki.generate_diff(original, response)
226 meraki.result['data'] = response
227 meraki.result['changed'] = True
228 else:
229 meraki.result['data'] = original
230
231 # in the event of a successful module execution, you will want to
232 # simple AnsibleModule.exit_json(), passing the key/value results
233 meraki.exit_json(**meraki.result)
234
235
236 if __name__ == '__main__':
237 main()