Exemplo n.º 1
0
def main():
    module = ForemanHostgroupModule(
        foreman_spec=dict(
            name=dict(required=True),
            description=dict(),
            parent=dict(type='entity'),
            organization=dict(type='entity', required=False, ensure=False),
        ),
        argument_spec=dict(updated_name=dict(), ),
        required_by=dict(
            content_source=('organization', ),
            content_view=('organization', ),
            lifecycle_environment=('organization', ),
        ),
    )

    module_params = module.foreman_params
    with module.api_connection():
        if not module.desired_absent:
            if 'organization' in module_params:
                if 'organizations' in module_params:
                    if module_params['organization'] not in module_params[
                            'organizations']:
                        module_params['organizations'].append(
                            module_params['organization'])
                else:
                    module_params['organizations'] = [
                        module_params['organization']
                    ]
        expected_puppetclasses = module_params.pop('puppetclasses', None)
        entity = module.run()
        if not module.desired_absent and 'environment_id' in entity:
            ensure_puppetclasses(module, 'hostgroup', entity,
                                 expected_puppetclasses)
Exemplo n.º 2
0
def main():
    module = ForemanHostModule(
        foreman_spec=dict(
            name=dict(required=True),
            hostgroup=dict(type='entity'),
            location=dict(type='entity'),
            organization=dict(type='entity'),
            enabled=dict(type='bool'),
            managed=dict(type='bool'),
            build=dict(type='bool'),
            ip=dict(),
            mac=dict(),
            comment=dict(),
            owner=dict(type='entity',
                       resource_type='users',
                       flat_name='owner_id'),
            owner_group=dict(type='entity',
                             resource_type='usergroups',
                             flat_name='owner_id'),
            owner_type=dict(invisible=True),
            provision_method=dict(choices=['build', 'image', 'bootdisk']),
            image=dict(type='entity', scope=['compute_resource']),
            compute_attributes=dict(type='dict'),
        ),
        mutually_exclusive=[['owner', 'owner_group']],
        required_by=dict(image=('compute_resource', ), ),
    )

    # additional param validation
    if '.' not in module.foreman_params['name']:
        module.fail_json(msg="The hostname must be FQDN")

    if not module.desired_absent:
        if 'build' in module.foreman_params and module.foreman_params['build']:
            # When 'build'=True, 'managed' has to be True. Assuming that user's priority is to build.
            if 'managed' in module.foreman_params and not module.foreman_params[
                    'managed']:
                module.warn(
                    "when 'build'=True, 'managed' is ignored and forced to True"
                )
            module.foreman_params['managed'] = True
        elif 'build' not in module.foreman_params and 'managed' in module.foreman_params and not module.foreman_params[
                'managed']:
            # When 'build' is not given and 'managed'=False, have to clear 'build' context that might exist on the server.
            module.foreman_params['build'] = False

        if 'mac' in module.foreman_params:
            module.foreman_params['mac'] = module.foreman_params['mac'].lower()

        if 'owner' in module.foreman_params:
            module.foreman_params['owner_type'] = 'User'
        elif 'owner_group' in module.foreman_params:
            module.foreman_params['owner_type'] = 'Usergroup'

    with module.api_connection():
        if not module.desired_absent:
            module.auto_lookup_entities()
        expected_puppetclasses = module.foreman_params.pop(
            'puppetclasses', None)
        entity = module.run()
        if not module.desired_absent and 'environment_id' in entity:
            ensure_puppetclasses(module, 'host', entity,
                                 expected_puppetclasses)
Exemplo n.º 3
0
def main():
    module = ForemanHostModule(
        foreman_spec=dict(
            name=dict(required=True),
            hostgroup=dict(type='entity'),
            location=dict(type='entity'),
            organization=dict(type='entity'),
            enabled=dict(type='bool'),
            managed=dict(type='bool'),
            build=dict(type='bool'),
            ip=dict(),
            mac=dict(),
            comment=dict(),
            owner=dict(type='entity',
                       resource_type='users',
                       flat_name='owner_id'),
            owner_group=dict(type='entity',
                             resource_type='usergroups',
                             flat_name='owner_id'),
            owner_type=dict(invisible=True),
            provision_method=dict(choices=['build', 'image', 'bootdisk']),
            image=dict(type='entity', scope=['compute_resource']),
            compute_attributes=dict(type='dict'),
            interfaces_attributes=dict(type='nested_list',
                                       foreman_spec=interfaces_spec,
                                       ensure=True),
        ),
        mutually_exclusive=[['owner', 'owner_group']],
        required_by=dict(image=('compute_resource', ), ),
    )

    # additional param validation
    if '.' not in module.foreman_params['name']:
        module.fail_json(msg="The hostname must be FQDN")

    if not module.desired_absent:
        if 'build' in module.foreman_params and module.foreman_params['build']:
            # When 'build'=True, 'managed' has to be True. Assuming that user's priority is to build.
            if 'managed' in module.foreman_params and not module.foreman_params[
                    'managed']:
                module.warn(
                    "when 'build'=True, 'managed' is ignored and forced to True"
                )
            module.foreman_params['managed'] = True
        elif 'build' not in module.foreman_params and 'managed' in module.foreman_params and not module.foreman_params[
                'managed']:
            # When 'build' is not given and 'managed'=False, have to clear 'build' context that might exist on the server.
            module.foreman_params['build'] = False

        if 'mac' in module.foreman_params:
            module.foreman_params['mac'] = module.foreman_params['mac'].lower()

        if 'owner' in module.foreman_params:
            module.foreman_params['owner_type'] = 'User'
        elif 'owner_group' in module.foreman_params:
            module.foreman_params['owner_type'] = 'Usergroup'

    with module.api_connection():
        entity = module.lookup_entity('entity')

        if not module.desired_absent:
            module.auto_lookup_entities()

        if 'compute_resource' in module.foreman_params:
            compute_resource = module.foreman_params['compute_resource']
            if 'compute_attributes' in module.foreman_params and 'cluster' in module.foreman_params[
                    'compute_attributes']:
                cluster = module.find_cluster(
                    module.foreman_params['compute_attributes']['cluster'],
                    compute_resource)
                module.foreman_params['compute_attributes'][
                    'cluster'] = cluster['_api_identifier']
            else:
                cluster = None

            if 'interfaces_attributes' in module.foreman_params:
                for interface in module.foreman_params[
                        'interfaces_attributes']:
                    if 'compute_attributes' in interface and 'network' in interface[
                            'compute_attributes']:
                        network = module.find_network(
                            interface['compute_attributes']['network'],
                            compute_resource, cluster)
                        interface['compute_attributes']['network'] = network[
                            'id']

        # We use different APIs for creating a host with interfaces
        # and updating it, so let's differentiate based on entity being present or not
        if entity and 'interfaces_attributes' in module.foreman_params:
            interfaces = module.foreman_params.pop('interfaces_attributes')
        else:
            interfaces = None

        expected_puppetclasses = module.foreman_params.pop(
            'puppetclasses', None)

        entity = module.run()

        if not module.desired_absent:
            if 'environment_id' in entity:
                ensure_puppetclasses(module, 'host', entity,
                                     expected_puppetclasses)
            if interfaces is not None:
                ensure_host_interfaces(module, entity, interfaces)