Ejemplo n.º 1
0
def main():
    module = KatelloEntityApypieAnsibleModule(entity_spec=dict(
        name=dict(required=True),
        content_type=dict(required=True, choices=['gpg_key', 'cert']),
        content=dict(required=True),
    ), )

    entity_dict = module.clean_params()

    module.connect()

    entity_dict['organization'] = module.find_resource_by_name(
        'organizations', entity_dict['organization'], thin=True)
    scope = {'organization_id': entity_dict['organization']['id']}
    entity = module.find_resource_by_name('content_credentials',
                                          name=entity_dict['name'],
                                          params=scope,
                                          failsafe=True)

    changed = module.ensure_entity_state('content_credentials',
                                         entity_dict,
                                         entity,
                                         params=scope)

    module.exit_json(changed=changed)
Ejemplo n.º 2
0
def main():
    module = KatelloEntityApypieAnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            content_type=dict(required=True, choices=['gpg_key', 'cert']),
            content=dict(required=True),
        ),
        supports_check_mode=True,
    )

    (entity_dict, state) = module.parse_params()

    module.connect()

    entity_dict['organization'] = module.find_resource_by_name(
        'organizations', entity_dict['organization'], thin=True)
    search_params = {'organization_id': entity_dict['organization']['id']}
    entity = module.find_resource_by_name('content_credentials',
                                          name=entity_dict['name'],
                                          params=search_params,
                                          failsafe=True)

    changed = module.ensure_resource_state('content_credentials', entity_dict,
                                           entity, state, name_map)

    module.exit_json(changed=changed)
def main():
    module = KatelloEntityApypieAnsibleModule(entity_spec=dict(
        name=dict(required=True),
        description=dict(),
    ), )

    entity_dict = module.clean_params()

    module.connect()

    entity_dict['organization'] = module.find_resource_by_name(
        'organizations', entity_dict['organization'], thin=True)
    scope = {'organization_id': entity_dict['organization']['id']}
    entity = module.find_resource_by_name('host_collections',
                                          name=entity_dict['name'],
                                          params=scope,
                                          failsafe=True)

    changed = module.ensure_entity_state('host_collections',
                                         entity_dict,
                                         entity,
                                         params=scope)

    module.exit_json(changed=changed)
def main():
    module = KatelloEntityApypieAnsibleModule(
        argument_spec=dict(
            manifest_path=dict(),
            state=dict(default='present',
                       choices=['absent', 'present', 'refreshed']),
            repository_url=dict(aliases=['redhat_repository_url']),
        ),
        required_if=[
            ['state', 'present', ['manifest_path']],
        ],
    )

    entity_dict = module.clean_params()

    module.connect()

    organization = module.find_resource_by_name(
        'organizations', name=entity_dict['organization'], thin=False)
    scope = {'organization_id': organization['id']}

    try:
        existing_manifest = organization['owner_details']['upstreamConsumer']
    except KeyError:
        existing_manifest = None

    changed = False
    if module.state == 'present':
        if 'repository_url' in entity_dict:
            payload = {'redhat_repository_url': entity_dict['repository_url']}
            org_spec = dict(redhat_repository_url=dict())
            changed_url, organization = module.ensure_entity(
                'organizations',
                payload,
                organization,
                state='present',
                entity_spec=org_spec)
        else:
            changed_url = False

        try:
            with open(entity_dict['manifest_path'], 'rb') as manifest_file:
                files = {
                    'content': (entity_dict['manifest_path'], manifest_file,
                                'application/zip')
                }
                params = {}
                if 'repository_url' in entity_dict:
                    params['repository_url'] = entity_dict['repository_url']
                params.update(scope)
                changed, result = module.resource_action(
                    'subscriptions',
                    'upload',
                    params,
                    options={'skip_validation': True},
                    files=files)
                task = module.wait_for_task(result, 5 * 60)
                for error in task['humanized']['errors']:
                    if "same as existing data" in error:
                        changed = False
                    elif "older than existing data" in error:
                        module.fail_json(
                            msg="Manifest is older than existing data.")
                    else:
                        module.fail_json(
                            msg="Upload of the manifest failed: %s" % error)
                changed |= changed_url
        except IOError as e:
            module.fail_json(msg="Unable to read the manifest file: %s" % e)
    elif module.desired_absent and existing_manifest:
        changed, result = module.resource_action('subscriptions',
                                                 'delete_manifest', scope)
        task = module.wait_for_task(result)
    elif module.state == 'refreshed':
        if existing_manifest:
            changed, result = module.resource_action('subscriptions',
                                                     'refresh_manifest', scope)
            task = module.wait_for_task(result)
        else:
            module.fail_json(msg="No manifest found to refresh.")

    module.exit_json(changed=changed)
def main():
    module = KatelloEntityApypieAnsibleModule(
        entity_spec=dict(
            name=dict(required=True),
            label=dict(),
            description=dict(),
            prior=dict(type='entity', flat_name='prior_id'),
        ),
    )

    entity_dict = module.clean_params()

    module.connect()

    entity_dict['organization'] = module.find_resource_by_name('organizations', entity_dict['organization'], thin=True)
    scope = {'organization_id': entity_dict['organization']['id']}
    entity = module.find_resource_by_name('lifecycle_environments', name=entity_dict['name'], params=scope, failsafe=True)
    if not module.desired_absent:
        if 'prior' in entity_dict:
            entity_dict['prior'] = module.find_resource_by_name('lifecycle_environments', entity_dict['prior'], params=scope, thin=True)
        # Default to 'Library' for new env with no 'prior' provided
        elif not entity:
            entity_dict['prior'] = module.find_resource_by_name('lifecycle_environments', 'Library', params=scope, thin=True)

    if entity:
        if 'label' in entity_dict and entity_dict['label'] and entity['label'] != entity_dict['label']:
            module.fail_json(msg="Label cannot be updated on a lifecycle environment.")

        if 'prior' in entity_dict and entity['prior']['id'] != entity_dict['prior']['id']:
            module.fail_json(msg="Prior cannot be updated on a lifecycle environment.")

    changed = module.ensure_entity_state('lifecycle_environments', entity_dict, entity, params=scope)

    module.exit_json(changed=changed)
def main():
    module = KatelloEntityApypieAnsibleModule(
        entity_spec=dict(
            name=dict(required=True),
            description=dict(),
            interval=dict(choices=['hourly', 'daily', 'weekly', 'custom cron'], required=True),
            enabled=dict(type='bool', required=True),
            sync_date=dict(required=True),
            cron_expression=dict(),
        ),
        argument_spec=dict(
            products=dict(type='list'),
        ),
        required_if=[
            ['interval', 'custom cron', ['cron_expression']],
        ],
    )

    entity_dict = module.clean_params()

    if (entity_dict['interval'] != 'custom cron') and ('cron_expression' in entity_dict):
        module.fail_json(msg='"cron_expression" cannot be combined with "interval"!="custom cron".')

    module.connect()

    entity_dict['organization'] = module.find_resource_by_name('organizations', name=entity_dict['organization'], thin=True)
    scope = {'organization_id': entity_dict['organization']['id']}
    entity = module.find_resource_by_name('sync_plans', name=entity_dict['name'], params=scope, failsafe=True)

    products = entity_dict.pop('products', None)

    changed, sync_plan = module.ensure_entity('sync_plans', entity_dict, entity, params=scope)

    if not module.desired_absent and products is not None:
        products = module.find_resources_by_name('products', products, params=scope, thin=True)
        desired_product_ids = set(product['id'] for product in products)
        current_product_ids = set(product['id'] for product in entity['products']) if entity else set()

        if desired_product_ids != current_product_ids:
            if not module.check_mode:
                product_ids_to_add = desired_product_ids - current_product_ids
                if product_ids_to_add:
                    payload = {
                        'id': sync_plan['id'],
                        'product_ids': list(product_ids_to_add),
                    }
                    payload.update(scope)
                    module.resource_action('sync_plans', 'add_products', payload)
                product_ids_to_remove = current_product_ids - desired_product_ids
                if product_ids_to_remove:
                    payload = {
                        'id': sync_plan['id'],
                        'product_ids': list(product_ids_to_remove),
                    }
                    payload.update(scope)
                    module.resource_action('sync_plans', 'remove_products', payload)
            changed = True

    module.exit_json(changed=changed)
Ejemplo n.º 7
0
def main():
    module = KatelloEntityApypieAnsibleModule(
        entity_spec=dict(
            name=dict(required=True),
            new_name=dict(),
            lifecycle_environment=dict(type='entity',
                                       flat_name='environment_id'),
            content_view=dict(type='entity', flat_name='content_view_id'),
            host_collections=dict(type='entity_list',
                                  flat_name='host_collection_ids'),
            auto_attach=dict(type='bool'),
        ),
        argument_spec=dict(
            subscriptions=dict(type='list',
                               elements='dict',
                               options=dict(name=dict(required=True), )),
            content_overrides=dict(
                type='list',
                elements='dict',
                options=dict(
                    label=dict(required=True),
                    override=dict(choises=['enabled', 'disabled']),
                )),
            state=dict(default='present',
                       choices=[
                           'present', 'present_with_defaults', 'absent',
                           'copied'
                       ]),
        ),
        required_if=[
            ['state', 'copied', ['new_name']],
        ],
    )

    entity_dict = module.clean_params()

    module.connect()

    entity_dict['organization'] = module.find_resource_by_name(
        'organizations', entity_dict['organization'], thin=True)
    scope = {'organization_id': entity_dict['organization']['id']}
    if not module.desired_absent:
        if 'lifecycle_environment' in entity_dict:
            entity_dict[
                'lifecycle_environment'] = module.find_resource_by_name(
                    'lifecycle_environments',
                    entity_dict['lifecycle_environment'],
                    params=scope,
                    thin=True)

        if 'content_view' in entity_dict:
            entity_dict['content_view'] = module.find_resource_by_name(
                'content_views',
                entity_dict['content_view'],
                params=scope,
                thin=True)

    entity = module.find_resource_by_name('activation_keys',
                                          name=entity_dict['name'],
                                          params=scope,
                                          failsafe=True)

    if module.state == 'copied':
        new_entity = module.find_resource_by_name('activation_keys',
                                                  name=entity_dict['new_name'],
                                                  params=scope,
                                                  failsafe=True)
        if new_entity is not None:
            module.warn("Activation Key '{}' already exists.".format(
                entity_dict['new_name']))
            module.exit_json(changed=False)

    subscriptions = entity_dict.pop('subscriptions', None)
    content_overrides = entity_dict.pop('content_overrides', None)
    host_collections = entity_dict.pop('host_collections', None)
    changed, activation_key = module.ensure_entity('activation_keys',
                                                   entity_dict,
                                                   entity,
                                                   params=scope)

    # 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 module.state == 'present' or (module.state == 'present_with_defaults'
                                     and changed):
        ak_scope = {'activation_key_id': activation_key['id']}
        if subscriptions is not None:
            subscription_names = [
                subscription['name'] for subscription in subscriptions
            ]
            desired_subscriptions = module.find_resources_by_name(
                'subscriptions', subscription_names, params=scope, thin=True)
            desired_subscription_ids = set(item['id']
                                           for item in desired_subscriptions)
            current_subscriptions = module.list_resource('subscriptions',
                                                         params=ak_scope)
            current_subscription_ids = set(item['id']
                                           for item in current_subscriptions)

            if desired_subscription_ids != current_subscription_ids:
                ids_to_remove = current_subscription_ids - desired_subscription_ids
                if ids_to_remove:
                    payload = {
                        'id': activation_key['id'],
                        'subscriptions': [{
                            'id': item
                        } for item in ids_to_remove],
                    }
                    payload.update(scope)
                    module.resource_action('activation_keys',
                                           'remove_subscriptions', payload)
                ids_to_add = desired_subscription_ids - current_subscription_ids
                if ids_to_add:
                    payload = {
                        'id':
                        activation_key['id'],
                        'subscriptions': [{
                            'id': item,
                            'quantity': 1
                        } for item in ids_to_add],
                    }
                    payload.update(scope)
                    module.resource_action('activation_keys',
                                           'add_subscriptions', payload)
                changed = True

        if content_overrides is not None:
            _, product_content = module.resource_action(
                'activation_keys',
                'product_content',
                params={'id': activation_key['id']})
            current_content_overrides = {
                product['content']['label']:
                product['enabled_content_override']
                for product in product_content['results']
                if product['enabled_content_override'] is not None
            }
            desired_content_overrides = {
                product['label']: override_to_boolnone(product['override'])
                for product in content_overrides
            }
            changed_content_overrides = []

            for label, override in desired_content_overrides.items():
                if override != current_content_overrides.pop(label, None):
                    changed_content_overrides.append({
                        'content_label': label,
                        'value': override
                    })
            for label in current_content_overrides.keys():
                changed_content_overrides.append({
                    'content_label': label,
                    'reset': True
                })

            if changed_content_overrides:
                payload = {
                    'id': activation_key['id'],
                    'content_overrides': changed_content_overrides,
                }
                module.resource_action('activation_keys', 'content_override',
                                       payload)
                changed = True

        if host_collections is not None:
            if 'host_collection_ids' in activation_key:
                current_host_collection_ids = set(
                    activation_key['host_collection_ids'])
            else:
                current_host_collection_ids = set(
                    item['id'] for item in activation_key['host_collections'])
            desired_host_collections = module.find_resources_by_name(
                'host_collections', host_collections, params=scope, thin=True)
            desired_host_collection_ids = set(
                item['id'] for item in desired_host_collections)

            if desired_host_collection_ids != current_host_collection_ids:
                ids_to_remove = current_host_collection_ids - desired_host_collection_ids
                if ids_to_remove:
                    payload = {
                        'id': activation_key['id'],
                        'host_collection_ids': list(ids_to_remove),
                    }
                    module.resource_action('activation_keys',
                                           'remove_host_collections', payload)
                ids_to_add = desired_host_collection_ids - current_host_collection_ids
                if ids_to_add:
                    payload = {
                        'id': activation_key['id'],
                        'host_collection_ids': list(ids_to_add),
                    }
                    module.resource_action('activation_keys',
                                           'add_host_collections', payload)
                changed = True

    module.exit_json(changed=changed)
Ejemplo n.º 8
0
def main():
    module = KatelloEntityApypieAnsibleModule(
        argument_spec=dict(product=dict(required=True), ),
        entity_spec=dict(
            label=dict(),
            name=dict(required=True),
            content_type=dict(
                required=True,
                choices=['docker', 'ostree', 'yum', 'puppet', 'file', 'deb']),
            url=dict(),
            gpg_key=dict(type='entity', flat_name='gpg_key_id'),
            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']),
        ),
    )

    entity_dict = module.clean_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'] = module.find_resource_by_name(
        'organizations', name=entity_dict['organization'], thin=True)
    scope = {'organization_id': entity_dict['organization']['id']}
    entity_dict['product'] = module.find_resource_by_name(
        'products', name=entity_dict['product'], params=scope, thin=True)

    if not module.desired_absent:
        if 'gpg_key' in entity_dict:
            entity_dict['gpg_key'] = module.find_resource_by_name(
                'content_credentials',
                name=entity_dict['gpg_key'],
                params=scope,
                thin=True)

    scope['product_id'] = entity_dict['product']['id']
    entity = module.find_resource_by_name('repositories',
                                          name=entity_dict['name'],
                                          params=scope,
                                          failsafe=True)

    changed = module.ensure_entity_state('repositories',
                                         entity_dict,
                                         entity,
                                         params=scope)

    module.exit_json(changed=changed)
Ejemplo n.º 9
0
def main():
    module = KatelloEntityApypieAnsibleModule(entity_spec=dict(
        name=dict(required=True),
        label=dict(),
        gpg_key=dict(type='entity', flat_name='gpg_key_id'),
        sync_plan=dict(type='entity', flat_name='sync_plan_id'),
        description=dict(),
        state=dict(default='present',
                   choices=['present_with_defaults', 'present', 'absent']),
    ), )

    entity_dict = module.clean_params()

    module.connect()

    entity_dict['organization'] = module.find_resource_by_name(
        'organizations', name=entity_dict['organization'], thin=True)
    scope = {'organization_id': entity_dict['organization']['id']}
    entity = module.find_resource_by_name('products',
                                          name=entity_dict['name'],
                                          params=scope,
                                          failsafe=True)

    if not module.desired_absent:
        if 'gpg_key' in entity_dict:
            entity_dict['gpg_key'] = module.find_resource_by_name(
                'content_credentials',
                name=entity_dict['gpg_key'],
                params=scope,
                thin=True)

        if 'sync_plan' in entity_dict:
            entity_dict['sync_plan'] = module.find_resource_by_name(
                'sync_plans',
                name=entity_dict['sync_plan'],
                params=scope,
                thin=True)

    changed = module.ensure_entity_state('products',
                                         entity_dict,
                                         entity,
                                         params=scope)

    module.exit_json(changed=changed)