Esempio n. 1
0
def compute_attribute(module, compute_profile, attributes):
    # If we don't work on a copy, we hit a TypeError: Value of unknown type: <class 'nailgun.entities.ComputeProfile'> exception
    # from module.exit_json(changed=changed) method
    compute_attribute_dict = attributes.copy()
    for key in ['compute_resource', 'vm_attrs']:
        if key not in compute_attribute_dict:
            module.fail_json(msg='compute_attribute must have %s.' % key)

    try:
        compute_attribute = find_compute_attribute(
            module,
            compute_profile_name=compute_profile.name,
            compute_resource_name=compute_attribute_dict['compute_resource'],
            failsafe=True)
        compute_attribute_dict['compute_resource'] = find_compute_resource(
            module, name=compute_attribute_dict['compute_resource'])
    except Exception as e:
        module.fail_json(msg='Failed to find entity: %s ' % e)

    if 'compute_profile' not in compute_attribute_dict:
        compute_attribute_dict['compute_profile'] = compute_profile

    changed = naildown_entity_state(ComputeAttribute, compute_attribute_dict,
                                    compute_attribute, 'present', module)

    return changed
def main(module):
    if has_import_error:
        module.fail_json(msg=import_error_msg)

    name = module.params.get('name')
    state = module.params.get('state')
    provider = module.params.get('provider').title()
    description = module.params.get('description')
    locations = module.params.get('locations')
    organizations = module.params.get('organizations')

    cement.create_server(
        server_url=module.params.get('server_url'),
        auth=(module.params.get('username'), module.params.get('password')),
        verify_ssl=module.params.get('verify_ssl'),
    )

    cement.ping_server(module)

    data = {'name': name, 'description': description}

    compute_resource = cement.find_compute_resource(module,
                                                    name=data.get('name'),
                                                    failsafe=True)

    if organizations:
        data['organization'] = [
            cement.find_organization(module, organization)
            for organization in organizations
        ]

    if locations:
        data['location'] = [
            cement.find_location(module, location) for location in locations
        ]

    data['provider'] = provider
    provider_params = get_provider_params(provider=provider)

    if state in ['present', 'present_with_defaults']:
        if not provider_params and not compute_resource:
            module.fail_json(
                msg=
                'To create a compute resource a valid provider must be supplied'
            )

        for key in provider_params.get('credentials'):
            data.__setitem__(key, module.params.get('provider_auth').get(key))

        for key in provider_params.get('params'):
            if key not in module.params:
                module.fail_json(msg='missing required param {}'.format(key))

            data.__setitem__(key, module.params.get(key))

    changed = cement.naildown_entity_state(provider_params.get('class'), data,
                                           compute_resource, state, module)

    return changed
Esempio n. 3
0
def main():
    module = ForemanEntityAnsibleModule(
        argument_spec=dict(
            name=dict(type='str', required=True),
            updated_name=dict(type='str'),
            description=dict(type='str'),
            provider=dict(type='str', choices=['vmware', 'libvirt', 'ovirt']),
            provider_params=dict(type='dict'),
            locations=dict(type='list'),
            organizations=dict(type='list'),
            state=dict(type='str',
                       default='present',
                       choices=['present', 'absent', 'present_with_defaults']),

            # Deprecated provider's specific params, use nested keys in provider_params param instead
            provider_auth=dict(type='dict'),
            url=dict(type='str'),
            display_type=dict(type='str'),
            datacenter=dict(type='str'),
        ),
        required_if=(['state', 'present', ['provider']], ),
        supports_check_mode=True,
    )

    (entity_dict, state) = module.parse_params()

    if 'provider' in entity_dict:
        entity_dict['provider'] = entity_dict['provider'].title()

    provider_infos = get_provider_infos(
        provider=entity_dict.get('provider', ''))
    provider_params = entity_dict.pop('provider_params', dict())
    provider_auth = entity_dict.pop('provider_auth', dict())

    module.connect()

    try:
        # Try to find the compute_resource to work on
        entity = find_compute_resource(module,
                                       name=entity_dict['name'],
                                       failsafe=True)
    except Exception as e:
        module.fail_json(msg='Failed to find entity: %s ' % e)

    if 'updated_name' in entity_dict and state == 'present':
        entity_dict['name'] = entity_dict['updated_name']

    if 'organizations' in entity_dict:
        entity_dict['organizations'] = find_organizations(
            module, entity_dict['organizations'])

    if 'locations' in entity_dict:
        entity_dict['locations'] = find_locations(module,
                                                  entity_dict['locations'])

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    # Add provider specific params
    if state in ['present', 'present_with_defaults']:
        if not provider_infos and not entity:
            module.fail_json(
                msg=
                'To create a compute resource a valid provider must be supplied'
            )

        for key in provider_infos['params']:
            # Manage deprecated params
            if key in provider_auth:
                entity_dict[key] = provider_auth[key]

            if key in provider_params:
                entity_dict[key] = provider_params[key]

    changed = naildown_entity_state(provider_infos['class'], entity_dict,
                                    entity, state, module)

    module.exit_json(changed=changed)