def main():
    module = KatelloEntityAnsibleModule(
        argument_spec=dict(
            name=dict(default=None),
            product=dict(default=None),
            label=dict(default=None),
            repositories=dict(required=True, type='list'),
            state=dict(default='enabled', choices=['disabled', 'enabled']),
        ),
        required_one_of=[['label', 'name']],
    )

    (module_params, state) = module.parse_params()
    name = module_params.get('name')
    product = module_params.get('product')
    label = module_params.get('label')
    organization = module_params.get('organization')
    repositories = module_params.get('repositories')

    module.connect()

    try:
        changed = repository_set(module, name, organization, product, label, state, repositories=repositories)
        module.exit_json(changed=changed)
    except Exception as e:
        module.fail_json(msg=e)
def main():
    module = KatelloEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            content_type=dict(required=True, choices=['gpg_key', 'cert']),
            content=dict(required=True),
        ),
        supports_check_mode=True,
    )

    (server_params, entity_dict, state) = module.parse_params()

    try:
        (server_url, username, password, verify_ssl) = server_params
        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'])
    entity = find_content_credential(module,
                                     name=entity_dict['name'],
                                     organization=entity_dict['organization'],
                                     failsafe=True)

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    changed = naildown_entity_state(ContentCredential, entity_dict, entity,
                                    state, module)

    module.exit_json(changed=changed)
Пример #3
0
def main():
    module = KatelloEntityAnsibleModule(
        argument_spec=dict(
            product=dict(required=True),
            label=dict(),
            name=dict(required=True),
            content_type=dict(
                required=True,
                choices=['docker', 'ostree', 'yum', 'puppet', 'file', 'deb']),
            url=dict(),
            gpg_key=dict(),
            docker_upstream_name=dict(),
            download_policy=dict(
                choices=['background', 'immediate', 'on_demand']),
            mirror_on_sync=dict(type='bool', default=True),
            state=dict(default='present',
                       choices=['present_with_defaults', 'present', 'absent']),
        ),
        supports_check_mode=True,
    )

    (entity_dict, state) = module.parse_params()

    if entity_dict[
            'content_type'] != 'docker' and 'docker_upstream_name' in entity_dict:
        module.fail_json(
            msg=
            "docker_upstream_name should not be set unless content_type: docker"
        )

    module.connect()

    entity_dict['organization'] = find_organization(
        module, name=entity_dict['organization'])

    entity_dict['product'] = find_product(
        module,
        name=entity_dict['product'],
        organization=entity_dict['organization'])

    if 'gpg_key' in entity_dict:
        entity_dict['gpg_key'] = find_content_credential(
            module,
            name=entity_dict['gpg_key'],
            organization=entity_dict['organization'])

    entity = find_repository(module,
                             name=entity_dict['name'],
                             product=entity_dict['product'],
                             failsafe=True)

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    changed = naildown_entity_state(Repository, entity_dict, entity, state,
                                    module)

    module.exit_json(changed=changed)
def main():
    module = KatelloEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            label=dict(),
            gpg_key=dict(),
            sync_plan=dict(),
            description=dict(),
            state=dict(default='present',
                       choices=['present_with_defaults', 'present', 'absent']),
        ),
        supports_check_mode=True,
    )

    (entity_dict, state) = module.parse_params()

    module.connect()

    entity_dict['organization'] = find_organization(
        module, name=entity_dict['organization'])

    if 'gpg_key' in entity_dict:
        entity_dict['gpg_key'] = find_content_credential(
            module,
            name=entity_dict['gpg_key'],
            organization=entity_dict['organization'])

    if 'sync_plan' in entity_dict:
        entity_dict['sync_plan'] = find_sync_plan(
            module,
            name=entity_dict['sync_plan'],
            organization=entity_dict['organization'])

    entity = find_product(module,
                          name=entity_dict['name'],
                          organization=entity_dict['organization'],
                          failsafe=True)

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    changed = naildown_entity_state(Product, entity_dict, entity, state,
                                    module)

    module.exit_json(changed=changed)
Пример #5
0
def main():
    module = KatelloEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            label=dict(),
            description=dict(),
            prior=dict(),
        ),
        supports_check_mode=True,
    )

    (server_params, module_params, state) = module.parse_params()
    name = module_params.get('name')
    label = module_params.get(
        'label') if module_params.get('label') != '' else None
    description = module_params.get('description')
    prior = None if module_params.get('prior') == '' else module_params.get(
        'prior')
    organization = module_params.get('organization')

    (server_url, username, password, verify_ssl) = server_params
    create_server(server_url, (username, password), verify_ssl)
    ping_server(module)
    validate_params(module,
                    state,
                    label=label,
                    description=description,
                    prior=prior)

    try:
        changed = lifecycle_environment(module,
                                        name,
                                        organization,
                                        state,
                                        label=label,
                                        description=description,
                                        prior=prior)
        module.exit_json(changed=changed)
    except Exception as e:
        module.fail_json(msg=e)
def main():
    module = KatelloEntityAnsibleModule(argument_spec=dict(
        name=dict(required=True),
        label=dict(),
        description=dict(),
        prior=dict(),
    ), )

    (module_params, state) = module.parse_params()
    name = module_params.get('name')
    label = module_params.get(
        'label') if module_params.get('label') != '' else None
    description = module_params.get('description')
    prior = None if module_params.get('prior') == '' else module_params.get(
        'prior')
    organization = module_params.get('organization')

    module.connect()

    validate_params(module,
                    state,
                    label=label,
                    description=description,
                    prior=prior)

    try:
        changed = lifecycle_environment(module,
                                        name,
                                        organization,
                                        state,
                                        label=label,
                                        description=description,
                                        prior=prior)
        module.exit_json(changed=changed)
    except Exception as e:
        module.fail_json(msg=e)
def main():
    module = KatelloEntityAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            new_name=dict(),
            lifecycle_environment=dict(),
            content_view=dict(),
            subscriptions=dict(type='list'),
            host_collections=dict(type='list'),
            content_overrides=dict(type='list'),
            auto_attach=dict(type='bool', default=True),
            state=dict(default='present',
                       choices=[
                           'present', 'present_with_defaults', 'absent',
                           'copied'
                       ]),
        ),
        supports_check_mode=True,
        required_if=[
            ['state', 'copied', ['new_name']],
        ],
    )

    (entity_dict, state) = module.parse_params()

    module.connect()

    entity_dict['organization'] = find_organization(
        module, name=entity_dict['organization'])
    if 'lifecycle_environment' in entity_dict:
        entity_dict['lifecycle_environment'] = find_lifecycle_environment(
            module, entity_dict['lifecycle_environment'],
            entity_dict['organization'])

    if 'content_view' in entity_dict:
        entity_dict['content_view'] = find_content_view(
            module, entity_dict['content_view'], entity_dict['organization'])

    if 'host_collections' in entity_dict:
        entity_dict['host_collections'] = find_host_collections(
            module, entity_dict['host_collections'],
            entity_dict['organization'])

    activation_key_entity = find_activation_key(
        module,
        name=entity_dict['name'],
        organization=entity_dict['organization'],
        failsafe=True)

    activation_key_dict = sanitize_entity_dict(entity_dict, name_map)

    try:
        changed, activation_key_entity = naildown_entity(
            ActivationKey, activation_key_dict, activation_key_entity, state,
            module)

        # only update subscriptions of newly created or updated AKs
        # copied keys inherit the subscriptions of the origin, so one would not have to specify them again
        # deleted keys don't need subscriptions anymore either
        if state == 'present' or (state == 'present_with_defaults'
                                  and changed):
            if 'subscriptions' in entity_dict:
                subscriptions = entity_dict['subscriptions']
                desired_subscription_ids = set(
                    s.id for s in find_subscriptions(
                        module, subscriptions, entity_dict['organization']))
                current_subscriptions = [
                    Subscription(**result)
                    for result in Subscription().search_normalize(
                        activation_key_entity.subscriptions()['results'])
                ]
                current_subscription_ids = set(s.id
                                               for s in current_subscriptions)

                if desired_subscription_ids != current_subscription_ids:
                    if not module.check_mode:
                        for subscription_id in (desired_subscription_ids -
                                                current_subscription_ids):
                            activation_key_entity.add_subscriptions(
                                data={
                                    'quantity': 1,
                                    'subscription_id': subscription_id
                                })
                        for subscription_id in (current_subscription_ids -
                                                desired_subscription_ids):
                            activation_key_entity.remove_subscriptions(
                                data={'subscription_id': subscription_id})
                    changed = True

            if 'content_overrides' in entity_dict:
                content_overrides = entity_dict['content_overrides']
                product_content = activation_key_entity.product_content()
                current_content_overrides = set(
                    (product['content']['label'],
                     product['enabled_content_override'])
                    for product in product_content['results']
                    if product['enabled_content_override'] is not None)
                desired_content_overrides = set(
                    (product['label'],
                     override_to_boolnone(product['override']))
                    for product in content_overrides)

                if desired_content_overrides != current_content_overrides:
                    if not module.check_mode:
                        for (
                                label, override
                        ) in current_content_overrides - desired_content_overrides:
                            activation_key_entity.content_override(
                                data={
                                    'content_override': {
                                        'content_label': label,
                                        'value': 'default'
                                    }
                                })
                        for (
                                label, override
                        ) in desired_content_overrides - current_content_overrides:
                            activation_key_entity.content_override(
                                data={
                                    'content_override': {
                                        'content_label': label,
                                        'value': str(override).lower()
                                    }
                                })
                    changed = True

        module.exit_json(changed=changed)
    except Exception as e:
        module.fail_json(msg=e)
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)
Пример #9
0
def main():
    module = KatelloEntityAnsibleModule(
        argument_spec=dict(
            product=dict(required=True),
            label=dict(),
            name=dict(required=True),
            content_type=dict(
                required=True,
                choices=['docker', 'ostree', 'yum', 'puppet', 'file', 'deb']),
            url=dict(),
            gpg_key=dict(),
            docker_upstream_name=dict(),
            download_policy=dict(
                choices=['background', 'immediate', 'on_demand']),
            state=dict(default='present',
                       choices=['present_with_defaults', 'present', 'absent']),
        ),
        supports_check_mode=True,
    )

    (server_params, entity_dict, state) = module.parse_params()

    if entity_dict[
            'content_type'] != 'docker' and 'docker_upstream_name' in entity_dict:
        module.fail_json(
            msg=
            "docker_upstream_name should not be set unless content_type: docker"
        )

    try:
        (server_url, username, password, verify_ssl) = server_params
        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'])

    entity_dict['product'] = find_product(
        module,
        name=entity_dict['product'],
        organization=entity_dict['organization'])

    if 'gpg_key' in entity_dict:
        entity_dict['gpg_key'] = find_content_credential(
            module,
            name=entity_dict['gpg_key'],
            organization=entity_dict['organization'])

    entity = find_repository(module,
                             name=entity_dict['name'],
                             product=entity_dict['product'],
                             failsafe=True)

    entity_dict = sanitize_entity_dict(entity_dict, name_map)

    changed = naildown_entity_state(Repository, entity_dict, entity, state,
                                    module)

    module.exit_json(changed=changed)