def main():

    # define the available arguments/parameters that a user can pass to
    # the module

    argument_spec = meraki_argument_spec()
    argument_spec.update(
        net_id=dict(type='str'),
        type=dict(type='list', choices=['wireless', 'switch', 'appliance'], aliases=['net_type']),
        tags=dict(type='list'),
        timezone=dict(type='str'),
        net_name=dict(type='str', aliases=['name', 'network']),
        state=dict(type='str', choices=['present', 'query', 'absent'], default='present'),
        enable_vlans=dict(type='bool'),
        disable_my_meraki=dict(type='bool', removed_in_version=2.13),
        enable_my_meraki=dict(type='bool'),
        enable_remote_status_page=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=False,
                           mutually_exclusive=[('disable_my_meraki', 'enable_my_meraki'),
                                               ]
                           )

    meraki = MerakiModule(module, function='network')
    module.params['follow_redirects'] = 'all'
    payload = None

    create_urls = {'network': '/organizations/{org_id}/networks'}
    update_urls = {'network': '/networks/{net_id}'}
    delete_urls = {'network': '/networks/{net_id}'}
    enable_vlans_urls = {'network': '/networks/{net_id}/vlansEnabledState'}
    get_vlan_status_urls = {'network': '/networks/{net_id}/vlansEnabledState'}
    meraki.url_catalog['create'] = create_urls
    meraki.url_catalog['update'] = update_urls
    meraki.url_catalog['delete'] = delete_urls
    meraki.url_catalog['enable_vlans'] = enable_vlans_urls
    meraki.url_catalog['status_vlans'] = get_vlan_status_urls

    if not meraki.params['org_name'] and not meraki.params['org_id']:
        meraki.fail_json(msg='org_name or org_id parameters are required')
    if meraki.params['state'] != 'query':
        if not meraki.params['net_name'] and not meraki.params['net_id']:
            meraki.fail_json(msg='net_name or net_id is required for present or absent states')
    if meraki.params['net_name'] and meraki.params['net_id']:
        meraki.fail_json(msg='net_name and net_id are mutually exclusive')
    if not meraki.params['net_name'] and not meraki.params['net_id']:
        if meraki.params['enable_vlans']:
            meraki.fail_json(msg="The parameter 'enable_vlans' requires 'net_name' or 'net_id' to be specified")
    if meraki.params['enable_my_meraki'] is True and meraki.params['enable_remote_status_page'] is False:
        meraki.fail_json(msg='enable_my_meraki must be true when setting enable_remote_status_page')

    # 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
    if module.check_mode:
        return meraki.result

    # Construct payload
    if meraki.params['state'] == 'present':
        payload = dict()
        if meraki.params['net_name']:
            payload['name'] = meraki.params['net_name']
        if meraki.params['type']:
            payload['type'] = list_to_string(meraki.params['type'])
        if meraki.params['tags']:
            payload['tags'] = construct_tags(meraki.params['tags'])
        if meraki.params['timezone']:
            payload['timeZone'] = meraki.params['timezone']
        if meraki.params['enable_my_meraki'] is not None:
            if meraki.params['enable_my_meraki'] is True:
                payload['disableMyMerakiCom'] = False
            else:
                payload['disableMyMerakiCom'] = True
        elif meraki.params['disable_my_meraki'] is not None:
            payload['disableMyMerakiCom'] = meraki.params['disable_my_meraki']
        if meraki.params['enable_remote_status_page'] is not None:
            if meraki.params['enable_remote_status_page'] is True:
                payload['disableRemoteStatusPage'] = False
                # meraki.fail_json(msg="Debug", payload=payload)
            else:
                payload['disableRemoteStatusPage'] = True

    # 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 not org_id:
        org_id = meraki.get_org_id(meraki.params['org_name'])
    nets = meraki.get_nets(org_id=org_id)

    # check if network is created
    net_id = meraki.params['net_id']
    net_exists = False
    if net_id is not None:
        if is_net_valid(nets, net_id=net_id) is False:
            meraki.fail_json(msg="Network specified by net_id does not exist.")
        net_exists = True
    elif meraki.params['net_name']:
        if is_net_valid(nets, net_name=meraki.params['net_name']) is True:
            net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets)
            net_exists = True

    if meraki.params['state'] == 'query':
        if not meraki.params['net_name'] and not meraki.params['net_id']:
            meraki.result['data'] = nets
        elif meraki.params['net_name'] or meraki.params['net_id'] is not None:
            meraki.result['data'] = meraki.get_net(meraki.params['org_name'],
                                                   meraki.params['net_name'],
                                                   data=nets
                                                   )
    elif meraki.params['state'] == 'present':
        if net_exists is False:  # Network needs to be created
            if 'type' not in meraki.params or meraki.params['type'] is None:
                meraki.fail_json(msg="type parameter is required when creating a network.")
            path = meraki.construct_path('create',
                                         org_id=org_id
                                         )
            r = meraki.request(path,
                               method='POST',
                               payload=json.dumps(payload)
                               )
            if meraki.status == 201:
                meraki.result['data'] = r
                meraki.result['changed'] = True
        else:  # Network exists, make changes
            # meraki.fail_json(msg="nets", nets=nets, net_id=net_id)
            # meraki.fail_json(msg="compare", original=net, payload=payload)
            if meraki.params['enable_vlans'] is not None:  # Modify VLANs configuration
                status_path = meraki.construct_path('status_vlans', net_id=net_id)
                status = meraki.request(status_path, method='GET')
                payload = {'enabled': meraki.params['enable_vlans']}
                # meraki.fail_json(msg="here", payload=payload)
                if meraki.is_update_required(status, payload):
                    path = meraki.construct_path('enable_vlans', net_id=net_id)
                    r = meraki.request(path,
                                       method='PUT',
                                       payload=json.dumps(payload))
                    if meraki.status == 200:
                        meraki.result['data'] = r
                        meraki.result['changed'] = True
                        meraki.exit_json(**meraki.result)
                else:
                    meraki.result['data'] = status
                    meraki.exit_json(**meraki.result)
            net = meraki.get_net(meraki.params['org_name'], net_id=net_id, data=nets)
            if meraki.is_update_required(net, payload):
                path = meraki.construct_path('update', net_id=net_id)
                # meraki.fail_json(msg="Payload", path=path, payload=payload)
                r = meraki.request(path,
                                   method='PUT',
                                   payload=json.dumps(payload))
                if meraki.status == 200:
                    meraki.result['data'] = r
                    meraki.result['changed'] = True
            else:
                meraki.result['data'] = net
    elif meraki.params['state'] == 'absent':
        if is_net_valid(nets, net_id=net_id) is True:
            path = meraki.construct_path('delete', net_id=net_id)
            r = meraki.request(path, method='DELETE')
            if meraki.status == 204:
                meraki.result['changed'] = True

    # 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)
示例#2
0
def main():

    # define the available arguments/parameters that a user can pass to
    # the module

    argument_spec = meraki_argument_spec()
    argument_spec.update(
        net_id=dict(type='str'),
        type=dict(type='str',
                  choices=['wireless', 'switch', 'appliance', 'combined'],
                  aliases=['net_type']),
        tags=dict(type='str'),
        timezone=dict(type='str'),
        net_name=dict(type='str', aliases=['name', 'network']),
        state=dict(type='str',
                   choices=['present', 'query', 'absent'],
                   default='present'),
    )

    # 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=False,
    )

    meraki = MerakiModule(module, function='network')
    module.params['follow_redirects'] = 'all'
    payload = None

    create_urls = {'network': '/organizations/{org_id}/networks'}
    update_urls = {'network': '/networks/{net_id}'}
    delete_urls = {'network': '/networks/{net_id}'}
    meraki.url_catalog['create'] = create_urls
    meraki.url_catalog['update'] = update_urls
    meraki.url_catalog['delete'] = delete_urls

    if not meraki.params['org_name'] and not meraki.params['org_id']:
        meraki.fail_json(msg='org_name or org_id parameters are required')
    if meraki.params['state'] != 'query':
        if not meraki.params['net_name'] or meraki.params['net_id']:
            meraki.fail_json(
                msg=
                'net_name or net_id is required for present or absent states')
    if meraki.params['net_name'] and meraki.params['net_id']:
        meraki.fail_json(msg='net_name and net_id are mutually exclusive')

    # 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
    if module.check_mode:
        return meraki.result

    # Construct payload
    if meraki.params['state'] == 'present':
        payload = {
            'name': meraki.params['net_name'],
            'type': meraki.params['type'],
        }
        if meraki.params['tags']:
            payload['tags'] = construct_tags(meraki.params['tags'])
        if meraki.params['timezone']:
            payload['timeZone'] = meraki.params['timezone']
        if meraki.params['type'] == 'combined':
            payload['type'] = 'switch wireless appliance'

    # manipulate or modify the state as needed (this is going to be the
    # part where your module will do what it needs to do)

    if meraki.params['org_name']:
        nets = meraki.get_nets(org_name=meraki.params['org_name'])
    elif meraki.params['org_id']:
        nets = meraki.get_nets(org_id=meraki.params['org_id'])

    if meraki.params['state'] == 'query':
        if not meraki.params['net_name'] and not meraki.params['net_id']:
            meraki.result['data'] = nets
        elif meraki.params['net_name'] or meraki.params['net_id'] is not None:
            meraki.result['data'] = meraki.get_net(meraki.params['org_name'],
                                                   meraki.params['net_name'],
                                                   nets)
    elif meraki.params['state'] == 'present':
        if meraki.params[
                'net_name']:  # FIXME: Idempotency check is ugly here, improve
            if is_net_valid(meraki, meraki.params['net_name'], nets) is False:
                if meraki.params[
                        'org_name']:  # FIXME: This can be cleaned up...maybe
                    path = meraki.construct_path(
                        'create', org_name=meraki.params['org_name'])
                elif meraki.params['org_id']:
                    path = meraki.construct_path(
                        'create', org_id=meraki.params['org_id'])
                r = meraki.request(path,
                                   method='POST',
                                   payload=json.dumps(payload))
                meraki.result['data'] = r
                meraki.result['changed'] = True
            else:
                net = meraki.get_net(meraki.params['org_name'],
                                     meraki.params['net_name'],
                                     data=nets)
                proposed = payload
                if meraki.params['timezone']:
                    proposed['timeZone'] = meraki.params['timezone']
                else:
                    proposed['timeZone'] = 'America/Los_Angeles'
                if not meraki.params['tags']:
                    proposed['tags'] = None
                if not proposed['type']:
                    proposed['type'] = net['type']

                if meraki.is_update_required(net, payload):
                    path = meraki.construct_path(
                        'update',
                        net_id=meraki.get_net_id(
                            net_name=meraki.params['net_name'], data=nets))
                    r = meraki.request(path,
                                       method='PUT',
                                       payload=json.dumps(payload))
                    meraki.result['data'] = r
                    meraki.result['changed'] = True
    elif meraki.params['state'] == 'absent':
        if is_net_valid(meraki, meraki.params['net_name'], nets) is True:
            net_id = meraki.get_net_id(org_name=meraki.params['org_name'],
                                       net_name=meraki.params['net_name'],
                                       data=nets)
            path = meraki.construct_path('delete', net_id=net_id)
            r = meraki.request(path, method='DELETE')
            if meraki.status == 204:
                meraki.result['changed'] = True

    # 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)