Exemple #1
0
def main():
    argument_spec = mso_argument_spec()
    argument_spec.update(
        tenant=dict(type='str', required=True),
        schema=dict(type='str', required=True),
        template=dict(type='str', aliases=['name']),
        display_name=dict(type='str'),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

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

    tenant = module.params.get('tenant')
    schema = module.params.get('schema')
    template = module.params.get('template')
    display_name = module.params.get('display_name')
    state = module.params.get('state')

    mso = MSOModule(module)

    # Get schema
    schema_obj = mso.get_obj('schemas', displayName=schema)

    mso.existing = {}
    if schema_obj:
        # Schema exists
        schema_path = 'schemas/{id}'.format(**schema_obj)

        # Get template
        templates = [t.get('name') for t in schema_obj.get('templates')]
        if template:
            if template in templates:
                template_idx = templates.index(template)
                mso.existing = schema_obj.get('templates')[template_idx]
        else:
            mso.existing = schema_obj.get('templates')
    else:
        schema_path = 'schemas'

    if state == 'query':
        if not mso.existing:
            if template:
                mso.fail_json(msg="Template '{0}' not found".format(template))
            else:
                mso.existing = []
        mso.exit_json()

    template_path = '/templates/{0}'.format(template)
    ops = []

    mso.previous = mso.existing
    if state == 'absent':
        mso.proposed = mso.sent = {}

        if not schema_obj:
            # There was no schema to begin with
            pass
        elif len(templates) == 1:
            # There is only one tenant, remove schema
            mso.existing = {}
            if not module.check_mode:
                mso.request(schema_path, method='DELETE')
        elif mso.existing:
            # Remove existing template
            mso.existing = {}
            ops.append(dict(op='remove', path=template_path))
        else:
            # There was no template to begin with
            pass

    elif state == 'present':
        tenant_id = mso.lookup_tenant(tenant)

        if display_name is None:
            display_name = mso.existing.get('displayName', template)

        if not schema_obj:
            # Schema does not exist, so we have to create it
            payload = dict(
                displayName=schema,
                templates=[
                    dict(
                        name=template,
                        displayName=display_name,
                        tenantId=tenant_id,
                    )
                ],
                sites=[],
            )

            mso.existing = payload.get('templates')[0]

            if not module.check_mode:
                mso.request(schema_path, method='POST', data=payload)

        elif mso.existing:
            # Template exists, so we have to update it
            payload = dict(
                name=template,
                displayName=display_name,
                tenantId=tenant_id,
            )

            mso.sanitize(payload, collate=True)

            ops.append(
                dict(op='replace',
                     path=template_path + '/displayName',
                     value=display_name))
            ops.append(
                dict(op='replace',
                     path=template_path + '/tenantId',
                     value=tenant_id))

            mso.existing = mso.proposed
        else:
            # Template does not exist, so we have to add it
            payload = dict(
                name=template,
                displayName=display_name,
                tenantId=tenant_id,
            )

            mso.sanitize(payload, collate=True)

            ops.append(dict(op='add', path='/templates/-', value=payload))

            mso.existing = mso.proposed

    if not module.check_mode:
        mso.request(schema_path, method='PATCH', data=ops)

    mso.exit_json()
Exemple #2
0
def main():
    argument_spec = mso_argument_spec()
    argument_spec.update(
        tenant=dict(type='str', required=True),
        schema=dict(type='str', required=True),
        template=dict(type='str', required=False, aliases=['name']),
        display_name=dict(type='str'),
        state=dict(type='str',
                   default='present',
                   choices=['absent', 'present', 'query']),
    )

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

    tenant = module.params['tenant']
    schema = module.params['schema']
    template = module.params['template']
    display_name = module.params['display_name']
    state = module.params['state']

    mso = MSOModule(module)

    # Get schema
    schema_obj = mso.get_obj('schemas', displayName=schema)
    if schema_obj:
        # Schema exists
        path = 'schemas/{id}'.format(**schema_obj)

        # Get template
        templates = [t['name'] for t in schema_obj['templates']]
        if template is None:
            mso.existing = schema_obj['templates']
        elif template in templates:
            template_idx = templates.index(template)
            mso.existing = schema_obj['templates'][template_idx]
        else:
            mso.existing = {}
    else:
        path = 'schemas'

        if template is None:
            mso.existing = []
        else:
            mso.existing = {}

    if state == 'query':
        mso.exit_json()

    mso.previous = mso.existing
    if state == 'absent':
        mso.proposed = mso.sent = {}

        if not schema_obj:
            # There was no schema to begin with
            pass
        elif len(templates) == 1:
            # There is only one tenant, remove schema
            mso.existing = {}
            if not module.check_mode:
                mso.request(path, method='DELETE')
        elif mso.existing:
            # Remove existing template
            mso.existing = {}
            operation = [
                dict(op='remove',
                     path='/templates/{template}'.format(template=template)),
            ]
            if not module.check_mode:
                mso.request(path, method='PATCH', data=operation)
        else:
            # There was no template to begin with
            pass

    elif state == 'present':
        tenant_id = mso.lookup_tenant(tenant)

        if display_name is None:
            display_name = mso.existing.get('displayName', template)

        if not schema_obj:
            # Schema does not exist, so we have to create it
            payload = dict(
                displayName=schema,
                templates=[
                    dict(
                        name=template,
                        displayName=display_name,
                        tenantId=tenant_id,
                    )
                ],
                sites=[],
            )

            mso.existing = payload['templates'][0]

            if not module.check_mode:
                mso.request(path, method='POST', data=payload)

        elif mso.existing:
            # Template exists, so we have to update it
            payload = dict(
                name=template,
                displayName=display_name,
                tenantId=tenant_id,
            )

            mso.sanitize(payload, collate=True)

            mso.existing = payload
            operations = [
                dict(op='replace',
                     path='/templates/{template}/displayName'.format(
                         template=template),
                     value=display_name),
                dict(op='replace',
                     path='/templates/{template}/tenantId'.format(
                         template=template),
                     value=tenant_id),
            ]

            if not module.check_mode:
                mso.request(path, method='PATCH', data=operations)
        else:
            # Template does not exist, so we have to add it
            payload = dict(
                name=template,
                displayName=display_name,
                tenantId=tenant_id,
            )

            mso.existing = payload
            operations = [
                dict(op='add', path='/templates/-', value=payload),
            ]

            if not module.check_mode:
                mso.request(path, method='PATCH', data=operations)

    mso.exit_json()