def main():
    module = AnsibleModule(
        argument_spec=dict(
            server_url=dict(required=True),
            username=dict(required=True, no_log=True),
            password=dict(required=True, no_log=True),
            verify_ssl=dict(type='bool', default=True),
            name=dict(required=True),
            organization=dict(required=True),
            repositories=dict(type='list'),
            state=dict(default='present',
                       choices=['present_with_defaults', 'present', 'absent']),
        ),
        supports_check_mode=True,
    )

    if has_import_error:
        module.fail_json(msg=import_error_msg)

    entity_dict = dict([(k, v) for (k, v) in module.params.items()
                        if v is not None])

    server_url = entity_dict.pop('server_url')
    username = entity_dict.pop('username')
    password = entity_dict.pop('password')
    verify_ssl = entity_dict.pop('verify_ssl')
    state = entity_dict.pop('state')

    try:
        create_server(server_url, (username, password), verify_ssl)
    except Exception as e:
        module.fail_json(msg="Failed to connect to Foreman server: %s " % e)

    ping_server(module)

    entity_dict['organization'] = find_organization(
        module, name=entity_dict['organization'])
    if 'repositories' in entity_dict:
        entity_dict['repositories'] = find_repositories(
            module, entity_dict['repositories'], entity_dict['organization'])

    content_view_entity = find_content_view(
        module,
        name=entity_dict['name'],
        organization=entity_dict['organization'],
        failsafe=True)
    content_view_dict = sanitize_entity_dict(entity_dict, name_map)

    changed = naildown_entity_state(ContentView, content_view_dict,
                                    content_view_entity, state, module)

    module.exit_json(changed=changed)
def main():
    module = KatelloEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            composite=dict(type='bool', default=False),
            auto_publish=dict(type='bool', default=False),
            components=dict(type='list'),
            repositories=dict(type='list'),
            state=dict(default='present',
                       choices=['present_with_defaults', 'present', 'absent']),
        ),
        mutually_exclusive=[['repositories', 'components']],
    )

    (entity_dict, state) = module.parse_params()

    module.connect()

    entity_dict['organization'] = find_organization(
        module, name=entity_dict['organization'])
    if 'repositories' in entity_dict and not entity_dict['composite']:
        entity_dict['repositories'] = find_repositories(
            module, entity_dict['repositories'], entity_dict['organization'])

    content_view_entity = find_content_view(
        module,
        name=entity_dict['name'],
        organization=entity_dict['organization'],
        failsafe=True)
    content_view_dict = sanitize_entity_dict(entity_dict, name_map)

    changed, content_view_entity = naildown_entity(ContentView,
                                                   content_view_dict,
                                                   content_view_entity, state,
                                                   module)

    # only update CVC's of newly created or updated CCV's
    if state == 'present' or (state == 'present_with_defaults' and changed):
        current_cvcs = []
        if hasattr(content_view_entity, 'content_view_component'):
            current_cvcs = [
                cvc.read()
                for cvc in content_view_entity.content_view_component
            ]
        if 'components' in entity_dict and content_view_entity.composite:
            for component in entity_dict['components']:
                cvc = component.copy()
                cvc['content_view'] = find_content_view(
                    module,
                    name=component['content_view'],
                    organization=entity_dict['organization'])
                cvc_matched = None
                for _cvc in current_cvcs:
                    if _cvc.content_view.id == cvc['content_view'].id:
                        cvc_matched = _cvc
                force_update = list()
                if 'version' in component:
                    cvc['version'] = find_content_view_version(
                        module,
                        cvc['content_view'],
                        version=component['version'])
                    cvc['latest'] = False
                    if cvc_matched and cvc_matched.latest:
                        # When changing to latest=False & version is the latest we must send 'content_view_version' to the server
                        force_update.append('content_view_version')
                if cvc_matched:
                    cvc['composite_content_view'] = content_view_entity
                    cvc_dict = sanitize_entity_dict(cvc, cvc_map)
                    cvc_changed = naildown_entity_state(
                        ContentViewComponent,
                        cvc_dict,
                        cvc_matched,
                        'present',
                        module,
                        force_update=force_update)
                    current_cvcs.remove(cvc_matched)
                    if cvc_changed:
                        changed = cvc_changed
                else:
                    for attr in ['latest', 'version']:
                        if attr not in cvc:
                            cvc[attr] = None
                    ContentViewComponent(
                        composite_content_view=content_view_entity,
                        content_view=cvc['content_view'],
                        latest=cvc['latest'],
                        content_view_version=cvc['version']).add()
                    changed = True
        for cvc in current_cvcs:
            # desired cvcs have already been updated and removed from `current_cvcs`
            cvc.remove()
            changed = True

    module.exit_json(changed=changed)
def main():
    module = AnsibleModule(
        argument_spec=dict(
            server_url=dict(required=True),
            username=dict(required=True, no_log=True),
            password=dict(required=True, no_log=True),
            verify_ssl=dict(type='bool', default=True),
            name=dict(required=True),
            organization=dict(required=True),
            composite=dict(type='bool', default=False),
            auto_publish=dict(type='bool', default=False),
            components=dict(type='list'),
            repositories=dict(type='list'),
            state=dict(default='present', choices=['present_with_defaults', 'present', 'absent']),
        ),
        supports_check_mode=True,
        mutually_exclusive=[['repositories', 'components']],
    )

    if has_import_error:
        module.fail_json(msg=import_error_msg)

    entity_dict = dict(
        [(k, v) for (k, v) in module.params.items() if v is not None])

    server_url = entity_dict.pop('server_url')
    username = entity_dict.pop('username')
    password = entity_dict.pop('password')
    verify_ssl = entity_dict.pop('verify_ssl')
    state = entity_dict.pop('state')

    try:
        create_server(server_url, (username, password), verify_ssl)
    except Exception as e:
        module.fail_json(msg="Failed to connect to Foreman server: %s " % e)

    ping_server(module)

    entity_dict['organization'] = find_organization(module, name=entity_dict['organization'])
    if 'repositories' in entity_dict and not entity_dict['composite']:
        entity_dict['repositories'] = find_repositories(module, entity_dict['repositories'], entity_dict['organization'])

    content_view_entity = find_content_view(module, name=entity_dict['name'], organization=entity_dict['organization'], failsafe=True)
    content_view_dict = sanitize_entity_dict(entity_dict, name_map)

    changed, content_view_entity = naildown_entity(ContentView, content_view_dict, content_view_entity, state, module)

    # only update CVC's of newly created or updated CCV's
    if state == 'present' or (state == 'present_with_defaults' and changed):
        current_cvcs = []
        if hasattr(content_view_entity, 'content_view_component'):
            current_cvcs = [cvc.read() for cvc in content_view_entity.content_view_component]
        if 'components' in entity_dict and content_view_entity.composite:
            for component in entity_dict['components']:
                cvc = component.copy()
                cvc['content_view'] = find_content_view(module, name=component['content_view'], organization=entity_dict['organization'])
                cvc_matched = None
                for _cvc in current_cvcs:
                    if _cvc.content_view.id == cvc['content_view'].id:
                        cvc_matched = _cvc
                force_update = list()
                if 'version' in component:
                    cvc['version'] = find_content_view_version(module, cvc['content_view'], version=component['version'])
                    cvc['latest'] = False
                    if cvc_matched and cvc_matched.latest:
                        # When changing to latest=False & version is the latest we must send 'content_view_version' to the server
                        force_update.append('content_view_version')
                if cvc_matched:
                    cvc['composite_content_view'] = content_view_entity
                    cvc_dict = sanitize_entity_dict(cvc, cvc_map)
                    cvc_changed = naildown_entity_state(ContentViewComponent, cvc_dict, cvc_matched, 'present', module, force_update=force_update)
                    current_cvcs.remove(cvc_matched)
                    if cvc_changed:
                        changed = cvc_changed
                else:
                    for attr in ['latest', 'version']:
                        if attr not in cvc:
                            cvc[attr] = None
                    ContentViewComponent(composite_content_view=content_view_entity, content_view=cvc['content_view'],
                                         latest=cvc['latest'], content_view_version=cvc['version']).add()
                    changed = True
        for cvc in current_cvcs:
            # desired cvcs have already been updated and removed from `current_cvcs`
            cvc.remove()
            changed = True

    module.exit_json(changed=changed)
Exemple #4
0
def main():
    module = ForemanAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            description=dict(),
            repositories=dict(type='list', default=[]),
            inclusion=dict(type='bool', default=False),
            content_view=dict(required=True),
            filter_type=dict(required=True, choices=['rpm', 'package_group', 'erratum', 'docker']),
            organization=dict(required=True),
            filter_state=dict(default='present', choices=['present', 'absent']),
            rule_state=dict(default='present', choices=['present', 'absent']),
            rule_name=dict(aliases=['package_name', 'package_group', 'tag']),
            date_type=dict(default='updated', choices=['issued', 'updated']),
            end_date=dict(),
            errata_id=dict(),
            max_version=dict(),
            min_version=dict(),
            start_date=dict(),
            types=dict(default=["bugfix", "enhancement", "security"], type='list'),
            version=dict(),
        ),
        supports_check_mode=False,
    )

    entity_dict = module.parse_params()
    filter_state = entity_dict.pop('filter_state')
    rule_state = entity_dict.pop('rule_state')

    module.connect()

    organization = find_organization(module, name=entity_dict.pop('organization'))
    entity_dict['content_view'] = find_content_view(module, name=entity_dict['content_view'], organization=organization)
    if len(entity_dict['repositories']) > 0:
        entity_dict['repositories'] = find_repositories(module, entity_dict['repositories'], organization)

    content_view_filter_entity = find_content_view_filter(module, name=entity_dict['name'], content_view=entity_dict['content_view'], failsafe=True)
    content_view_filter_dict = sanitize_entity_dict(entity_dict, content_filter_map)

    content_view_filter_changed = naildown_entity_state(AbstractContentViewFilter, content_view_filter_dict, content_view_filter_entity, filter_state, module)

    if entity_dict['filter_type'] == 'erratum':
        entity_dict['rule_name'] = None
    elif 'rule_name' not in entity_dict:
        entity_dict['rule_name'] = entity_dict['name']

    # Find content_view_filter again as it may have just been created
    entity_dict['content_view_filter'] = find_content_view_filter(module, name=entity_dict['name'], content_view=entity_dict['content_view'], failsafe=True)

    if entity_dict['content_view_filter'] is not None:
        if 'errata_id' in entity_dict:
            rule_map = content_filter_rule_erratum_id_map
            entity_dict['errata'] = find_errata(module, id=entity_dict['errata_id'], organization=organization)
            content_view_filter_rule_entity = find_content_view_filter_rule(module, content_view_filter=entity_dict['content_view_filter'],
                                                                            errata=entity_dict['errata'], failsafe=True)
        else:
            rule_map = globals()['content_filter_rule_%s_map' % (entity_dict['filter_type'])]
            content_view_filter_rule_entity = find_content_view_filter_rule(module, content_view_filter=entity_dict['content_view_filter'],
                                                                            name=entity_dict['rule_name'], failsafe=True)

        if entity_dict['filter_type'] == 'package_group':
            entity_dict['uuid'] = find_package_group(module, name=entity_dict['rule_name']).uuid

        content_view_filter_rule_dict = sanitize_entity_dict(entity_dict, rule_map)
        check_missing = ['min_version', 'max_version', 'version', 'start_date', 'end_date', 'architecture', 'date_type']
        content_view_filter_rule_changed = naildown_entity_state(ContentViewFilterRule, content_view_filter_rule_dict, content_view_filter_rule_entity,
                                                                 rule_state, module, check_missing)
        changed = content_view_filter_changed or content_view_filter_rule_changed
    else:
        changed = content_view_filter_changed

    module.exit_json(changed=changed)
def main():
    module = AnsibleModule(
        argument_spec=dict(
            server_url=dict(required=True),
            username=dict(required=True, no_log=True),
            password=dict(required=True, no_log=True),
            verify_ssl=dict(type='bool', default=True),
            name=dict(required=True),
            description=dict(),
            repositories=dict(type='list', default=[]),
            inclusion=dict(type='bool', default=False),
            content_view=dict(required=True),
            filter_type=dict(
                required=True,
                choices=['rpm', 'package_group', 'erratum', 'docker']),
            organization=dict(required=True),
            filter_state=dict(default='present', choices=['present',
                                                          'absent']),
            rule_state=dict(default='present', choices=['present', 'absent']),
            rule_name=dict(aliases=['package_name', 'package_group', 'tag']),
            date_type=dict(default='updated', choices=['issued', 'updated']),
            end_date=dict(),
            errata_id=dict(),
            max_version=dict(),
            min_version=dict(),
            start_date=dict(),
            types=dict(default=["bugfix", "enhancement", "security"],
                       type='list'),
            version=dict(),
        ),
        supports_check_mode=False,
    )

    if has_import_error:
        module.fail_json(msg=import_error_msg)

    entity_dict = dict([(k, v) for (k, v) in module.params.items()
                        if v is not None])

    server_url = entity_dict.pop('server_url')
    username = entity_dict.pop('username')
    password = entity_dict.pop('password')
    verify_ssl = entity_dict.pop('verify_ssl')
    filter_state = entity_dict.pop('filter_state')
    rule_state = entity_dict.pop('rule_state')

    try:
        create_server(server_url, (username, password), verify_ssl)
    except Exception as e:
        module.fail_json(msg="Failed to connect to Foreman server: %s " % e)

    ping_server(module)

    organization = find_organization(module,
                                     name=entity_dict.pop('organization'))
    entity_dict['content_view'] = find_content_view(
        module, name=entity_dict['content_view'], organization=organization)
    if len(entity_dict['repositories']) > 0:
        entity_dict['repositories'] = find_repositories(
            module, entity_dict['repositories'], organization)

    content_view_filter_entity = find_content_view_filter(
        module,
        name=entity_dict['name'],
        content_view=entity_dict['content_view'],
        failsafe=True)
    content_view_filter_dict = sanitize_entity_dict(entity_dict,
                                                    content_filter_map)

    content_view_filter_changed = naildown_entity_state(
        AbstractContentViewFilter, content_view_filter_dict,
        content_view_filter_entity, filter_state, module)

    if entity_dict['filter_type'] == 'erratum':
        entity_dict['rule_name'] = None
    elif 'rule_name' not in entity_dict:
        entity_dict['rule_name'] = entity_dict['name']

    # Find content_view_filter again as it may have just been created
    entity_dict['content_view_filter'] = find_content_view_filter(
        module,
        name=entity_dict['name'],
        content_view=entity_dict['content_view'],
        failsafe=True)

    if entity_dict['content_view_filter'] is not None:
        if 'errata_id' in entity_dict:
            rule_map = content_filter_rule_erratum_id_map
            entity_dict['errata'] = find_errata(module,
                                                id=entity_dict['errata_id'],
                                                organization=organization)
            content_view_filter_rule_entity = find_content_view_filter_rule(
                module,
                content_view_filter=entity_dict['content_view_filter'],
                errata=entity_dict['errata'],
                failsafe=True)
        else:
            rule_map = globals()['content_filter_rule_%s_map' %
                                 (entity_dict['filter_type'])]
            content_view_filter_rule_entity = find_content_view_filter_rule(
                module,
                content_view_filter=entity_dict['content_view_filter'],
                name=entity_dict['rule_name'],
                failsafe=True)

        if entity_dict['filter_type'] == 'package_group':
            entity_dict['uuid'] = find_package_group(
                module, name=entity_dict['rule_name']).uuid

        content_view_filter_rule_dict = sanitize_entity_dict(
            entity_dict, rule_map)
        check_missing = [
            'min_version', 'max_version', 'version', 'start_date', 'end_date',
            'architecture', 'date_type'
        ]
        content_view_filter_rule_changed = naildown_entity_state(
            ContentViewFilterRule, content_view_filter_rule_dict,
            content_view_filter_rule_entity, rule_state, module, check_missing)
        changed = content_view_filter_changed or content_view_filter_rule_changed
    else:
        changed = content_view_filter_changed

    module.exit_json(changed=changed)