Пример #1
0
def main():
    argument_spec = mso_argument_spec()
    argument_spec.update(
        role=dict(type='str', required=False, aliases=['name', 'role_name']),
        role_id=dict(type='str', required=False),
        display_name=dict(type='str'),
        description=dict(type='str'),
        permissions=dict(type='list',
                         choices=[
                             'backup-db',
                             'manage-audit-records',
                             'manage-labels',
                             'manage-roles',
                             'manage-schemas',
                             'manage-sites',
                             'manage-tenants',
                             'manage-tenant-schemas',
                             'manage-users',
                             'platform-logs',
                             'view-all-audit-records',
                             'view-labels',
                             'view-roles',
                             'view-schemas',
                             'view-sites',
                             'view-tenants',
                             'view-tenant-schemas',
                             'view-users',
                         ]),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

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

    role = module.params['role']
    role_id = module.params['role_id']
    description = module.params['description']
    permissions = module.params['permissions']
    state = module.params['state']

    mso = MSOModule(module)

    path = 'roles'

    # Query for existing object(s)
    if role_id is None and role is None:
        mso.existing = mso.query_objs(path)
    elif role_id is None:
        mso.existing = mso.get_obj(path, name=role)
        if mso.existing:
            role_id = mso.existing['id']
    elif role is None:
        mso.existing = mso.get_obj(path, id=role_id)
    else:
        mso.existing = mso.get_obj(path, id=role_id)
        existing_by_name = mso.get_obj(path, name=role)
        if existing_by_name and role_id != existing_by_name['id']:
            mso.fail_json(
                msg=
                "Provided role '{0}' with id '{1}' does not match existing id '{2}'."
                .format(role, role_id, existing_by_name['id']))

    # If we found an existing object, continue with it
    if role_id:
        path = 'roles/{id}'.format(id=role_id)

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

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

        payload = dict(
            id=role_id,
            name=role,
            displayName=role,
            description=description,
            permissions=permissions,
        )

        mso.sanitize(payload, collate=True)

        if mso.existing:
            if not issubset(mso.sent, mso.existing):
                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)

    mso.exit_json()
Пример #2
0
def main():
    argument_spec = mso_argument_spec()
    argument_spec.update(
        label=dict(type='str', aliases=['name']),
        type=dict(type='str', default='site', choices=['site']),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

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

    label = module.params.get('label')
    label_type = module.params.get('type')
    state = module.params.get('state')

    mso = MSOModule(module)

    label_id = None
    path = 'labels'

    # Query for existing object(s)
    if label:
        mso.existing = mso.get_obj(path, displayName=label)
        if mso.existing:
            label_id = mso.existing.get('id')
            # If we found an existing object, continue with it
            path = 'labels/{id}'.format(id=label_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')

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

        payload = dict(
            id=label_id,
            displayName=label,
            type=label_type,
        )

        mso.sanitize(payload, collate=True)

        if mso.existing:
            if not issubset(mso.sent, mso.existing):
                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)

    mso.exit_json()
Пример #3
0
def main():
    argument_spec = mso_argument_spec()
    argument_spec.update(
        description=dict(type='str'),
        display_name=dict(type='str'),
        tenant=dict(type='str', aliases=['name']),
        users=dict(type='list'),
        sites=dict(type='list'),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

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

    description = module.params['description']
    display_name = module.params['display_name']
    tenant = module.params['tenant']
    state = module.params['state']

    mso = MSOModule(module)

    # Convert sites and users
    sites = mso.lookup_sites(module.params['sites'])
    users = mso.lookup_users(module.params['users'])

    tenant_id = None
    path = 'tenants'

    # Query for existing object(s)
    if tenant:
        mso.existing = mso.get_obj(path, name=tenant)
        if mso.existing:
            tenant_id = mso.existing['id']
            # If we found an existing object, continue with it
            path = 'tenants/{id}'.format(id=tenant_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')

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

        payload = dict(
            description=description,
            id=tenant_id,
            name=tenant,
            displayName=display_name,
            siteAssociations=sites,
            userAssociations=users,
        )

        mso.sanitize(payload, collate=True)

        # Ensure displayName is not undefined
        if mso.sent.get('displayName') is None:
            mso.sent['displayName'] = tenant

        # Ensure tenant has at least admin user
        if mso.sent.get('userAssociations') is None:
            mso.sent['userAssociations'] = [
                dict(userId="0000ffff0000000000000020")
            ]

        if mso.existing:
            if not issubset(mso.sent, mso.existing):
                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)

    mso.exit_json()
Пример #4
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 not issubset(mso.sent, mso.existing):
                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()
Пример #5
0
def main():
    argument_spec = mso_argument_spec()
    argument_spec.update(
        schema=dict(type='str', aliases=['name']),
        templates=dict(type='list'),
        sites=dict(type='list'),
        # messages=dict(type='dict'),
        # associations=dict(type='list'),
        # health_faults=dict(type='list'),
        # references=dict(type='dict'),
        # policy_states=dict(type='list'),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

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

    schema = module.params['schema']
    templates = module.params['templates']
    sites = module.params['sites']
    state = module.params['state']

    mso = MSOModule(module)

    schema_id = None
    path = 'schemas'

    # Query for existing object(s)
    if schema:
        mso.existing = mso.get_obj(path, displayName=schema)
        if mso.existing:
            schema_id = mso.existing['id']
            path = 'schemas/{id}'.format(id=schema_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')

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

        payload = dict(
            id=schema_id,
            displayName=schema,
            templates=templates,
            sites=sites,
        )

        mso.sanitize(payload, collate=True)

        if mso.existing:
            if not issubset(mso.sent, mso.existing):
                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)

    mso.exit_json()
Пример #6
0
def main():
    argument_spec = mso_argument_spec()
    argument_spec.update(
        label=dict(type='str', aliases=['name']),
        label_id=dict(type='str'),
        type=dict(type='str', default='site', choices=['site']),
        state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
    )

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

    label = module.params['label']
    label_id = module.params['label_id']
    label_type = module.params['type']
    state = module.params['state']

    mso = MSOModule(module)

    path = 'labels'

    # Query for existing object(s)
    if label_id is None and label is None:
        mso.existing = mso.query_objs(path)
    elif label_id is None:
        mso.existing = mso.get_obj(path, displayName=label)
        if mso.existing:
            label_id = mso.existing['id']
    elif label is None:
        mso.existing = mso.get_obj(path, id=label_id)
    else:
        mso.existing = mso.get_obj(path, id=label_id)
        existing_by_name = mso.get_obj(path, displayName=label)
        if existing_by_name and label_id != existing_by_name['id']:
            mso.fail_json(msg="Provided label '{0}' with id '{1}' does not match existing id '{2}'.".format(label, label_id, existing_by_name['id']))

    # If we found an existing object, continue with it
    if label_id:
        path = 'labels/{id}'.format(id=label_id)

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

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

        payload = dict(
            id=label_id,
            displayName=label,
            type=label_type,
        )

        mso.sanitize(payload, collate=True)

        if mso.existing:
            if not issubset(mso.sent, mso.existing):
                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)

    mso.exit_json()
Пример #7
0
def main():
    argument_spec = mso_argument_spec()
    argument_spec.update(
        user_id=dict(type='str', required=False),
        user=dict(type='str', required=False, aliases=['name', 'user_name']),
        user_password=dict(type='str', no_log=True),
        first_name=dict(type='str'),
        last_name=dict(type='str'),
        email=dict(type='str'),
        phone=dict(type='str'),
        # TODO: What possible options do we have ?
        account_status=dict(type='str', choices=['active']),
        domain=dict(type='str'),
        roles=dict(type='list'),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

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

    user_id = module.params['user_id']
    user_name = module.params['user']
    user_password = module.params['user_password']
    first_name = module.params['first_name']
    last_name = module.params['last_name']
    email = module.params['email']
    phone = module.params['phone']
    account_status = module.params['account_status']
    state = module.params['state']

    mso = MSOModule(module)

    roles = mso.lookup_roles(module.params['roles'])
    domain = mso.lookup_domain(module.params['domain'])

    path = 'users'

    # Query for existing object(s)
    if user_id is None and user_name is None:
        mso.existing = mso.query_objs(path)
    elif user_id is None:
        mso.existing = mso.get_obj(path, username=user_name)
        if mso.existing:
            user_id = mso.existing['id']
    elif user_name is None:
        mso.existing = mso.get_obj(path, id=user_id)
    else:
        mso.existing = mso.get_obj(path, id=user_id)
        existing_by_name = mso.get_obj(path, username=user_name)
        if existing_by_name and user_id != existing_by_name['id']:
            mso.fail_json(
                msg=
                "Provided user '{0}' with id '{1}' does not match existing id '{2}'."
                .format(user_name, user_id, existing_by_name['id']))

    # If we found an existing object, continue with it
    if user_id:
        path = 'users/{id}'.format(id=user_id)

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

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

        payload = dict(
            id=user_id,
            username=user_name,
            password=user_password,
            firstName=first_name,
            lastName=last_name,
            emailAddress=email,
            phoneNumber=phone,
            accountStatus=account_status,
            domainId=domain,
            roles=roles,
            # active=True,
            # remote=True,
        )

        mso.sanitize(payload, collate=True)

        if mso.sent.get('accountStatus') is None:
            mso.sent['accountStatus'] = 'active'

        if mso.existing:
            if not issubset(mso.sent, mso.existing):
                # NOTE: Since MSO always returns '******' as password, we need to assume a change
                if 'password' in mso.proposed:
                    mso.module.warn(
                        "A password change is assumed, as the MSO REST API does not return passwords we do not know."
                    )
                    mso.result['changed'] = True

                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)

    mso.exit_json()