Beispiel #1
0
def main():
    location_arg_spec = dict(
        latitude=dict(type='float'),
        longitude=dict(type='float'),
    )

    argument_spec = mso_argument_spec()
    argument_spec.update(
        apic_password=dict(type='str', no_log=True),
        apic_site_id=dict(type='str'),
        apic_username=dict(type='str', default='admin'),
        apic_login_domain=dict(type='str'),
        labels=dict(type='list', elements='str'),
        location=dict(type='dict', options=location_arg_spec),
        site=dict(type='str', aliases=['name']),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
        urls=dict(type='list', elements='str'),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['site']],
            ['state', 'present', ['apic_site_id', 'site']],
        ],
    )

    apic_username = module.params.get('apic_username')
    apic_password = module.params.get('apic_password')
    apic_site_id = module.params.get('apic_site_id')
    site = module.params.get('site')
    location = module.params.get('location')
    if location is not None:
        latitude = module.params.get('location')['latitude']
        longitude = module.params.get('location')['longitude']
    state = module.params.get('state')
    urls = module.params.get('urls')
    apic_login_domain = module.params.get('apic_login_domain')

    mso = MSOModule(module)

    site_id = None
    path = 'sites'
    api_version = 'v1'
    if mso.platform == 'nd':
        api_version = 'v2'

    # Convert labels
    labels = mso.lookup_labels(module.params.get('labels'), 'site')

    # Query for mso.existing object(s)
    if site:
        if mso.platform == 'nd':
            site_info = mso.get_obj(path,
                                    api_version=api_version,
                                    common=dict(name=site))
            path = 'sites/manage'
            if site_info:
                # If we found an existing object, continue with it
                site_id = site_info.get('id')
                if site_id is not None and site_id != '':
                    # Checking if site is managed by MSO
                    mso.existing = site_info
                    path = 'sites/manage/{id}'.format(id=site_id)
        else:
            mso.existing = mso.get_obj(path, name=site)
            if mso.existing:
                # If we found an existing object, continue with it
                site_id = mso.existing.get('id')
                path = 'sites/{id}'.format(id=site_id)

    else:
        mso.existing = mso.query_objs(path, api_version=api_version)

    if state == 'query':
        pass

    elif state == 'absent':
        mso.previous = mso.existing
        if mso.existing:
            if module.check_mode:
                mso.existing = {}
            else:
                mso.request(path,
                            method='DELETE',
                            qs=dict(force='true'),
                            api_version=api_version)
                mso.existing = {}

    elif state == 'present':
        mso.previous = mso.existing

        if mso.platform == 'nd':
            if mso.existing:
                payload = mso.existing
            else:
                if site_info:
                    payload = site_info
                    payload['common']['siteId'] = apic_site_id
                else:
                    mso.fail_json(
                        msg=
                        "Site '{0}' is not a valid Site configured at ND-level. Add Site to ND first."
                        .format(site))

        else:
            payload = dict(
                apicSiteId=apic_site_id,
                id=site_id,
                name=site,
                urls=urls,
                labels=labels,
                username=apic_username,
                password=apic_password,
            )

            if location is not None:
                payload['location'] = dict(
                    lat=latitude,
                    long=longitude,
                )

            if apic_login_domain is not None and apic_login_domain not in [
                    '', 'local', 'Local'
            ]:
                payload['username'] = '******'.format(
                    apic_login_domain, apic_username)

        mso.sanitize(payload, collate=True)

        if mso.existing:
            if mso.check_changed():
                if module.check_mode:
                    mso.existing = mso.proposed
                else:
                    mso.existing = mso.request(path,
                                               method='PUT',
                                               data=mso.sent,
                                               api_version=api_version)
        else:
            if module.check_mode:
                mso.existing = mso.proposed
            else:
                mso.existing = mso.request(path,
                                           method='POST',
                                           data=mso.sent,
                                           api_version=api_version)

    if 'password' in mso.existing:
        mso.existing['password'] = '******'

    mso.exit_json()
Beispiel #2
0
def main():
    location_arg_spec = dict(
        latitude=dict(type='float'),
        longitude=dict(type='float'),
    )

    argument_spec = mso_argument_spec()
    argument_spec.update(
        apic_password=dict(type='str', no_log=True),
        apic_site_id=dict(type='str'),
        apic_username=dict(type='str', default='admin'),
        labels=dict(type='list'),
        location=dict(type='dict', options=location_arg_spec),
        site=dict(type='str', aliases=['name']),
        state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
        urls=dict(type='list'),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'absent', ['site']],
            ['state', 'present', ['apic_site_id', 'site']],
        ],
    )

    apic_username = module.params['apic_username']
    apic_password = module.params['apic_password']
    apic_site_id = module.params['apic_site_id']
    site = module.params['site']
    location = module.params['location']
    if location is not None:
        latitude = module.params['location']['latitude']
        longitude = module.params['location']['longitude']
    state = module.params['state']
    urls = module.params['urls']

    mso = MSOModule(module)

    site_id = None
    path = 'sites'

    # Convert labels
    labels = mso.lookup_labels(module.params['labels'], 'site')

    # Query for mso.existing object(s)
    if site:
        mso.existing = mso.get_obj(path, name=site)
        if mso.existing:
            site_id = mso.existing['id']
            # If we found an existing object, continue with it
            path = 'sites/{id}'.format(id=site_id)
    else:
        mso.existing = mso.query_objs(path)

    if state == 'query':
        pass

    elif state == 'absent':
        mso.previous = mso.existing
        if mso.existing:
            if module.check_mode:
                mso.existing = {}
            else:
                mso.existing = mso.request(path, method='DELETE', qs=dict(force='true'))

    elif state == 'present':
        mso.previous = mso.existing

        payload = dict(
            apicSiteId=apic_site_id,
            id=site_id,
            name=site,
            urls=urls,
            labels=labels,
            username=apic_username,
            password=apic_password,
        )

        if location is not None:
            payload['location'] = dict(
                lat=latitude,
                long=longitude,
            )

        mso.sanitize(payload, collate=True)

        if mso.existing:
            if mso.check_changed():
                if module.check_mode:
                    mso.existing = mso.proposed
                else:
                    mso.existing = mso.request(path, method='PUT', data=mso.sent)
        else:
            if module.check_mode:
                mso.existing = mso.proposed
            else:
                mso.existing = mso.request(path, method='POST', data=mso.sent)

    if 'password' in mso.existing:
        mso.existing['password'] = '******'

    mso.exit_json()