コード例 #1
0
def main():
    argument_spec = msc_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=[
                             'manage-roles',
                             'manage-schemas',
                             'manage-sites',
                             'manage-tenants',
                             'manage-users',
                             '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']

    msc = MSCModule(module)

    path = 'roles'

    # Query for existing object(s)
    if role_id is None and role is None:
        msc.existing = msc.query_objs(path)
    elif role_id is None:
        msc.existing = msc.get_obj(path, name=role)
        if msc.existing:
            role_id = msc.existing['id']
    elif role is None:
        msc.existing = msc.get_obj(path, id=role_id)
    else:
        msc.existing = msc.get_obj(path, id=role_id)
        existing_by_name = msc.get_obj(path, name=role)
        if existing_by_name and role_id != existing_by_name['id']:
            msc.fail_json(
                msg=
                "Provided role '{1}' with id '{2}' does not match existing id '{3}'."
                .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':
        msc.previous = msc.existing
        if msc.existing:
            if module.check_mode:
                msc.existing = {}
            else:
                msc.existing = msc.request(path, method='DELETE')

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

        msc.sanitize(dict(
            id=role_id,
            name=role,
            displayName=role,
            description=description,
            permissions=permissions,
        ),
                     collate=True)

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

    msc.exit_json()
コード例 #2
0
ファイル: msc_tenant.py プロジェクト: zenglin521/ansible
def main():
    argument_spec = msc_argument_spec()
    argument_spec.update(
        description=dict(type='str'),
        display_name=dict(type='str'),
        tenant=dict(type='str',
                    required=False,
                    aliases=['name', 'tenant_name']),
        tenant_id=dict(type='str', required=False),
        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']
    tenant_id = module.params['tenant_id']
    state = module.params['state']

    msc = MSCModule(module)

    path = 'tenants'

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

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

    if state == 'query':
        pass

    elif state == 'absent':
        msc.previous = msc.existing
        if msc.existing:
            if module.check_mode:
                msc.existing = {}
            else:
                msc.existing = msc.request(path, method='DELETE')

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

        msc.sanitize(
            dict(
                description=description,
                id=tenant_id,
                name=tenant,
                displayName=display_name,
                siteAssociations=[],
                userAssociations=[dict(userId="0000ffff0000000000000020")],
            ))

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

    msc.exit_json()
コード例 #3
0
def main():
    argument_spec = msc_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'),
        site=dict(type='str', required=False, aliases=['name', 'site_name']),
        site_id=dict(type='str', required=False),
        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']
    site_id = module.params['site_id']
    state = module.params['state']
    urls = module.params['urls']

    msc = MSCModule(module)

    path = 'sites'

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

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

    if state == 'query':
        pass

    elif state == 'absent':
        msc.previous = msc.existing
        if msc.existing:
            if module.check_mode:
                msc.existing = {}
            else:
                msc.existing = msc.request(path, method='DELETE')

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

        msc.sanitize(dict(
            apicSiteId=apic_site_id,
            id=site_id,
            name=site,
            urls=urls,
            username=apic_username,
            password=apic_password,
        ),
                     collate=True)

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

    msc.exit_json()
コード例 #4
0
def main():
    argument_spec = msc_argument_spec()
    argument_spec.update(
        label=dict(type='str', required=False, aliases=['name', 'label_name']),
        label_id=dict(type='str', required=False),
        display_name=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']
    display_name = module.params['display_name']
    state = module.params['state']

    msc = MSCModule(module)

    path = 'labels'

    # Query for existing object(s)
    if label_id is None and label is None:
        msc.existing = msc.query_objs(path)
    elif label_id is None:
        msc.existing = msc.get_obj(path, displayName=label)
        if msc.existing:
            label_id = msc.existing['id']
    elif label is None:
        msc.existing = msc.get_obj(path, id=label_id)
    else:
        msc.existing = msc.get_obj(path, id=label_id)
        existing_by_name = msc.get_obj(path, displayName=label)
        if existing_by_name and label_id != existing_by_name['id']:
            msc.fail_json(
                msg=
                "Provided label '{1}' with id '{2}' does not match existing id '{3}'."
                .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':
        msc.previous = msc.existing
        if msc.existing:
            if module.check_mode:
                msc.existing = {}
            else:
                msc.existing = msc.request(path, method='DELETE')

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

        msc.sanitize(dict(
            id=label_id,
            displayName=display_name,
            type=label_type,
        ),
                     collate=True)

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

    msc.exit_json()
コード例 #5
0
def main():
    argument_spec = msc_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'),
        # FIXME: 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', 'password', 'first_name', 'last_name',
                    'email', 'phone', 'account_status'
                ]
            ],
        ],
    )

    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']
    # FIXME: Look up domain
    domain = module.params['domain']
    # FIXME: Look up roles
    roles = module.params['roles']
    roles_dict = list()
    if roles:
        for role in roles:
            roles_dict.append(dict(roleId=role))
    state = module.params['state']

    msc = MSCModule(module)

    path = 'users'

    # Query for existing object(s)
    if user_id is None and user_name is None:
        msc.existing = msc.query_objs(path)
    elif user_id is None:
        msc.existing = msc.get_obj(path, username=user_name)
        if msc.existing:
            user_id = msc.existing['id']
    elif user_name is None:
        msc.existing = msc.get_obj(path, id=user_id)
    else:
        msc.existing = msc.get_obj(path, id=user_id)
        existing_by_name = msc.get_obj(path, username=user_name)
        if existing_by_name and user_id != existing_by_name['id']:
            msc.fail_json(
                msg=
                "Provided user '{1}' with id '{2}' does not match existing id '{3}'."
                .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':
        msc.previous = msc.existing
        if msc.existing:
            if module.check_mode:
                msc.existing = {}
            else:
                msc.existing = msc.request(path, method='DELETE')

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

        msc.sanitize(
            dict(
                id=user_id,
                username=user_name,
                password=user_password,
                firstName=first_name,
                lastName=last_name,
                emailAddress=email,
                phoneNumber=phone,
                # accountStatus={},
                accountStatus=account_status,
                needsPasswordUpdate=False,
                domainId=domain,
                roles=roles_dict,
                # active=True,
                # remote=True,
            ),
            collate=True)

        if msc.existing:
            if not issubset(msc.sent, msc.existing):
                # NOTE: Since MSC always returns '******' as password, we need to assume a change
                if 'password' in msc.sent:
                    msc.result['changed'] = True

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

    msc.exit_json()
コード例 #6
0
def main():
    argument_spec = msc_argument_spec()
    argument_spec.update(
        schema=dict(type='str',
                    required=False,
                    aliases=['name', 'schema_name']),
        schema_id=dict(type='str', required=False),
        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']],
        ],
    )

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

    msc = MSCModule(module)

    path = 'schemas'

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

    if schema_id:
        path = 'schemas/{id}'.format(id=schema_id)

    if state == 'query':
        pass

    elif state == 'absent':
        msc.previous = msc.existing
        if msc.existing:
            if module.check_mode:
                msc.existing = {}
            else:
                msc.existing = msc.request(path, method='DELETE')

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

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

        msc.sanitize(payload, collate=True)

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

    msc.exit_json()