summaryrefslogtreecommitdiffstats
path: root/ansible_collections/cisco/meraki/plugins/modules/meraki_config_template.py
blob: bb6ae22898822d02b8bea053028c38d0ba4d5dcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright: (c) 2018, Kevin Breit (@kbreit) <kevin.breit@kevinbreit.net>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {
    'metadata_version': '1.1',
    "status": ['deprecated'],
    'supported_by': 'community'
}

DOCUMENTATION = r'''
---
module: meraki_config_template
short_description: Manage configuration templates in the Meraki cloud
version_added: "1.0.0"
description:
- Allows for querying, deleting, binding, and unbinding of configuration templates.
notes:
- Module is not idempotent as the Meraki API is limited in what information it provides about configuration templates.
- Meraki's API does not support creating new configuration templates.
- To use the configuration template, simply pass its ID via C(net_id) parameters in Meraki modules.
deprecated:
  removed_in: '3.0.0'
  why: Updated modules released with increased functionality
  alternative: cisco.meraki.organizations_config_templates
options:
    state:
        description:
        - Specifies whether configuration template information should be queried, modified, or deleted.
        choices: ['absent', 'query', 'present']
        default: query
        type: str
    org_name:
        description:
        - Name of organization containing the configuration template.
        type: str
    org_id:
        description:
        - ID of organization associated to a configuration template.
        type: str
    config_template:
        description:
        - Name of the configuration template within an organization to manipulate.
        aliases: ['name']
        type: str
    net_name:
        description:
        - Name of the network to bind or unbind configuration template to.
        type: str
    net_id:
        description:
        - ID of the network to bind or unbind configuration template to.
        type: str
    auto_bind:
        description:
        - Optional boolean indicating whether the network's switches should automatically bind to profiles of the same model.
        - This option only affects switch networks and switch templates.
        - Auto-bind is not valid unless the switch template has at least one profile and has at most one profile per switch model.
        type: bool

author:
- Kevin Breit (@kbreit)
extends_documentation_fragment: cisco.meraki.meraki
'''

EXAMPLES = r'''
- name: Query configuration templates
  meraki_config_template:
    auth_key: abc12345
    org_name: YourOrg
    state: query
  delegate_to: localhost

- name: Bind a template from a network
  meraki_config_template:
    auth_key: abc123
    state: present
    org_name: YourOrg
    net_name: YourNet
    config_template: DevConfigTemplate
  delegate_to: localhost

- name: Unbind a template from a network
  meraki_config_template:
    auth_key: abc123
    state: absent
    org_name: YourOrg
    net_name: YourNet
    config_template: DevConfigTemplate
  delegate_to: localhost

- name: Delete a configuration template
  meraki_config_template:
    auth_key: abc123
    state: absent
    org_name: YourOrg
    config_template: DevConfigTemplate
  delegate_to: localhost
'''

RETURN = r'''
data:
    description: Information about queried object.
    returned: success
    type: complex
    contains:
        id:
          description: Unique identification number of organization.
          returned: success
          type: int
          sample: L_2930418
        name:
          description: Name of configuration template.
          returned: success
          type: str
          sample: YourTemplate
        product_types:
          description: List of products which can exist in the network.
          returned: success
          type: list
          sample: [ "appliance", "switch" ]
        time_zone:
          description: Timezone applied to each associated network.
          returned: success
          type: str
          sample: "America/Chicago"

'''

from ansible.module_utils.basic import AnsibleModule, json
from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec


def get_config_templates(meraki, org_id):
    path = meraki.construct_path('get_all', org_id=org_id)
    response = meraki.request(path, 'GET')
    if meraki.status != 200:
        meraki.fail_json(msg='Unable to get configuration templates')
    return response


def get_template_id(meraki, name, data):
    for template in data:
        if name == template['name']:
            return template['id']
    meraki.fail_json(msg='No configuration template named {0} found'.format(name))


def is_template_valid(meraki, nets, template_id):
    for net in nets:
        if net['id'] == template_id:
            return True
    return False


def is_network_bound(meraki, nets, net_id, template_id):
    for net in nets:
        if net['id'] == net_id:
            try:
                if net['configTemplateId'] == template_id:
                    return True
            except KeyError:
                pass
    return False


def delete_template(meraki, org_id, name, data):
    template_id = get_template_id(meraki, name, data)
    path = meraki.construct_path('delete', org_id=org_id)
    path = path + '/' + template_id
    response = meraki.request(path, 'DELETE')
    if meraki.status != 204:
        meraki.fail_json(msg='Unable to remove configuration template')
    return response


def bind(meraki, net_id, template_id):
    path = meraki.construct_path('bind', net_id=net_id)
    payload = {'configTemplateId': template_id}
    if meraki.params['auto_bind']:
        payload['autoBind'] = meraki.params['auto_bind']
    r = meraki.request(path, method='POST', payload=json.dumps(payload))
    return r


def unbind(meraki, net_id):
    path = meraki.construct_path('unbind', net_id=net_id)
    meraki.result['changed'] = True
    return meraki.request(path, method='POST')


def main():

    # define the available arguments/parameters that a user can pass to
    # the module
    argument_spec = meraki_argument_spec()
    argument_spec.update(state=dict(type='str', choices=['absent', 'query', 'present'], default='query'),
                         config_template=dict(type='str', aliases=['name']),
                         net_name=dict(type='str'),
                         net_id=dict(type='str'),
                         # config_template_id=dict(type='str', aliases=['id']),
                         auto_bind=dict(type='bool'),
                         )

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True,
                           )
    meraki = MerakiModule(module, function='config_template')
    meraki.params['follow_redirects'] = 'all'

    query_urls = {'config_template': '/organizations/{org_id}/configTemplates'}
    delete_urls = {'config_template': '/organizations/{org_id}/configTemplates'}
    bind_urls = {'config_template': '/networks/{net_id}/bind'}
    unbind_urls = {'config_template': '/networks/{net_id}/unbind'}

    meraki.url_catalog['get_all'].update(query_urls)
    meraki.url_catalog['delete'] = delete_urls
    meraki.url_catalog['bind'] = bind_urls
    meraki.url_catalog['unbind'] = unbind_urls

    # if the user is working with this module in only check mode we do not
    # want to make any changes to the environment, just return the current
    # state with no modifications

    # execute checks for argument completeness

    # manipulate or modify the state as needed (this is going to be the
    # part where your module will do what it needs to do)
    org_id = meraki.params['org_id']
    if meraki.params['org_name']:
        org_id = meraki.get_org_id(meraki.params['org_name'])
    net_id = meraki.params['net_id']
    nets = None
    if net_id is None:
        if meraki.params['net_name'] is not None:
            nets = meraki.get_nets(org_id=org_id)
            net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets)
        else:
            nets = meraki.get_nets(org_id=org_id)

    if meraki.params['state'] == 'query':
        meraki.result['data'] = get_config_templates(meraki, org_id)
    elif meraki.params['state'] == 'present':
        template_id = get_template_id(meraki,
                                      meraki.params['config_template'],
                                      get_config_templates(meraki, org_id))
        if nets is None:
            nets = meraki.get_nets(org_id=org_id)
        if is_network_bound(meraki, nets, net_id, template_id) is False:  # Bind template
            if meraki.check_mode is True:
                meraki.result['data'] = {}
                meraki.result['changed'] = True
                meraki.exit_json(**meraki.result)
            template_bind = bind(meraki,
                                 net_id,
                                 template_id)
            if meraki.status != 200:
                meraki.fail_json(msg='Unable to bind configuration template to network')
            meraki.result['changed'] = True
            meraki.result['data'] = template_bind
        else:  # Network is already bound, being explicit
            if meraki.check_mode is True:  # Include to be explicit
                meraki.result['data'] = {}
                meraki.result['changed'] = False
                meraki.exit_json(**meraki.result)
            meraki.result['data'] = {}
            meraki.result['changed'] = False
            meraki.exit_json(**meraki.result)
    elif meraki.params['state'] == 'absent':
        template_id = get_template_id(meraki,
                                      meraki.params['config_template'],
                                      get_config_templates(meraki, org_id))
        if not meraki.params['net_name'] and not meraki.params['net_id']:  # Delete template
            if is_template_valid(meraki, nets, template_id) is True:
                if meraki.check_mode is True:
                    meraki.result['data'] = {}
                    meraki.result['changed'] = True
                    meraki.exit_json(**meraki.result)
                meraki.result['data'] = delete_template(meraki,
                                                        org_id,
                                                        meraki.params['config_template'],
                                                        get_config_templates(meraki, org_id))
                if meraki.status == 204:
                    meraki.result['data'] = {}
                    meraki.result['changed'] = True
            else:
                meraki.fail_json(msg="No template named {0} found.".format(meraki.params['config_template']))
        else:  # Unbind template
            if nets is None:
                nets = meraki.get_nets(org_id=org_id)
            if meraki.check_mode is True:
                meraki.result['data'] = {}
                if is_template_valid(meraki, nets, template_id) is True:
                    meraki.result['changed'] = True
                else:
                    meraki.result['changed'] = False
                meraki.exit_json(**meraki.result)
            template_id = get_template_id(meraki,
                                          meraki.params['config_template'],
                                          get_config_templates(meraki, org_id))
            if is_network_bound(meraki, nets, net_id, template_id) is True:
                if meraki.check_mode is True:
                    meraki.result['data'] = {}
                    meraki.result['changed'] = True
                    meraki.exit_json(**meraki.result)
                config_unbind = unbind(meraki,
                                       net_id)
                if meraki.status != 200:
                    meraki.fail_json(msg='Unable to unbind configuration template from network')
                meraki.result['changed'] = True
                meraki.result['data'] = config_unbind
            else:  # No network is bound, nothing to do
                if meraki.check_mode is True:  # Include to be explicit
                    meraki.result['data'] = {}
                    meraki.result['changed'] = False
                    meraki.exit_json(**meraki.result)
                meraki.result['data'] = {}
                meraki.result['changed'] = False

    # in the event of a successful module execution, you will want to
    # simple AnsibleModule.exit_json(), passing the key/value results
    meraki.exit_json(**meraki.result)


if __name__ == '__main__':
    main()