def main():
    module = KatelloEntityAnsibleModule(
        argument_spec=dict(
            updated_name=dict(),
        ),
        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)

    if entity and 'updated_name' in entity_dict:
        entity_dict['name'] = entity_dict.pop('updated_name')

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

    module.exit_json(changed=changed)
Ejemplo n.º 2
0
def main():
    module = KatelloEntityAnsibleModule(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.º 3
0
def main():
    module = KatelloEntityAnsibleModule(
        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.º 4
0
def main():
    module = KatelloEntityAnsibleModule(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)
Ejemplo n.º 5
0
def main():
    module = KatelloEntityAnsibleModule(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)
def main():
    module = KatelloEntityAnsibleModule(
        entity_spec=dict(
            name=dict(required=True),
            description=dict(),
            composite=dict(type='bool', default=False),
            auto_publish=dict(type='bool', default=False),
            components=dict(type='nested_list', entity_spec=cvc_entity_spec),
            repositories=dict(type='entity_list',
                              flat_name='repository_ids',
                              elements='dict',
                              options=dict(
                                  name=dict(required=True),
                                  product=dict(required=True),
                              )),
        ),
        argument_spec=dict(state=dict(
            default='present',
            choices=['present_with_defaults', 'present', 'absent']), ),
        mutually_exclusive=[['repositories', 'components']],
    )

    entity_dict = module.clean_params()

    # components is None when we're managing a CCV but don't want to adjust its components
    components = entity_dict.pop('components', None)
    if components:
        for component in components:
            if not component['latest'] and component.get(
                    'content_view_version') is None:
                module.fail_json(
                    msg=
                    "Content View Component must either have latest=True or provide a Content View Version."
                )

    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 'repositories' in entity_dict:
            if entity_dict['composite']:
                module.fail_json(
                    msg=
                    "Repositories cannot be parts of a Composite Content View."
                )
            else:
                repositories = []
                for repository in entity_dict['repositories']:
                    product = module.find_resource_by_name(
                        'products',
                        repository['product'],
                        params=scope,
                        thin=True)
                    repositories.append(
                        module.find_resource_by_name(
                            'repositories',
                            repository['name'],
                            params={'product_id': product['id']},
                            thin=True))
                entity_dict['repositories'] = repositories

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

    changed, content_view_entity = module.ensure_entity('content_views',
                                                        entity_dict,
                                                        entity,
                                                        params=scope)

    # only update CVC's of newly created or updated CV's that are composite if components are specified
    update_dependent_entities = (module.state == 'present'
                                 or (module.state == 'present_with_defaults'
                                     and changed))
    if update_dependent_entities and content_view_entity[
            'composite'] and components is not None:
        if not changed:
            content_view_entity['content_view_components'] = entity[
                'content_view_components']
        current_cvcs = content_view_entity.get('content_view_components', [])

        components_to_add = []
        ccv_scope = {'composite_content_view_id': content_view_entity['id']}
        for component in components:
            cvc = {
                'content_view':
                module.find_resource_by_name('content_views',
                                             name=component['content_view'],
                                             params=scope),
                'latest':
                component['latest'],
            }
            cvc_matched = next(
                (item for item in current_cvcs
                 if item['content_view']['id'] == cvc['content_view']['id']),
                None)
            if not cvc['latest']:
                search = "content_view_id={},version={}".format(
                    cvc['content_view']['id'],
                    component['content_view_version'])
                cvc['content_view_version'] = module.find_resource(
                    'content_view_versions', search=search, thin=True)
                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
                    # Let's fake, it wasn't there...
                    cvc_matched.pop('content_view_version', None)
                    cvc_matched.pop('content_view_version_id', None)
            if cvc_matched:
                changed |= module.ensure_entity_state(
                    'content_view_components',
                    cvc,
                    cvc_matched,
                    state='present',
                    entity_spec=cvc_entity_spec,
                    params=ccv_scope)
                current_cvcs.remove(cvc_matched)
            else:
                cvc['content_view_id'] = cvc.pop('content_view')['id']
                if 'content_view_version' in cvc:
                    cvc['content_view_version_id'] = cvc.pop(
                        'content_view_version')['id']
                components_to_add.append(cvc)
        if components_to_add:
            payload = {
                'composite_content_view_id': content_view_entity['id'],
                'components': components_to_add,
            }
            module.resource_action('content_view_components', 'add_components',
                                   payload)
            changed = True

        if current_cvcs:
            # desired cvcs have already been updated and removed from `current_cvcs`
            payload = {
                'composite_content_view_id': content_view_entity['id'],
                'component_ids': [item['id'] for item in current_cvcs],
            }
            module.resource_action('content_view_components',
                                   'remove_components', payload)
            changed = True

    module.exit_json(changed=changed)
Ejemplo n.º 7
0
def main():
    module = KatelloEntityAnsibleModule(
        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'),
            download_policy=dict(
                choices=['background', 'immediate', 'on_demand']),
            mirror_on_sync=dict(type='bool', default=True),
            upstream_username=dict(),
            upstream_password=dict(nolog=True),
            docker_upstream_name=dict(),
            docker_tags_whitelist=dict(type='list'),
            deb_releases=dict(),
            deb_components=dict(),
            deb_architectures=dict(),
            state=dict(default='present',
                       choices=['present_with_defaults', 'present', 'absent']),
        ),
    )

    entity_dict = module.clean_params()

    if entity_dict['content_type'] != 'docker':
        invalid_list = [
            key for key in ['docker_upstream_name', 'docker_tags_whitelist']
            if key in entity_dict
        ]
        if invalid_list:
            module.fail_json(
                msg="({0}) can only be used with content_type 'docker'".format(
                    ",".join(invalid_list)))

    if entity_dict['content_type'] != 'deb':
        invalid_list = [
            key
            for key in ['deb_releases', 'deb_components', 'deb_architectures']
            if key in entity_dict
        ]
        if invalid_list:
            module.fail_json(
                msg="({0}) can only be used with content_type 'deb'".format(
                    ",".join(invalid_list)))

    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)
def main():
    module = KatelloEntityAnsibleModule(
        entity_spec=dict(
            content_view=dict(type='entity', flat_name='content_view_id', required=True),
            description=dict(),
            version=dict(),
            lifecycle_environments=dict(type='list'),
            force_promote=dict(type='bool', aliases=['force'], default=False),
            force_yum_metadata_regeneration=dict(type='bool', default=False),
            synchronous=dict(type='bool', default=True),
            current_lifecycle_environment=dict(),
        ),
        mutually_exclusive=[['current_lifecycle_environment', 'version']],
    )

    module.task_timeout = 60 * 60

    entity_dict = module.clean_params()

    # Do an early (not exhaustive) sanity check, whether we can perform this non-synchronous
    if (
        not module.desired_absent and 'lifecycle_environments' in entity_dict
        and 'version' not in entity_dict and 'current_lifecycle_environment' not in entity_dict
        and not entity_dict['synchronous']
    ):
        module.fail_json(msg="Cannot perform non-blocking publishing and promoting in the same module call.")

    module.connect()

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

    content_view = module.find_resource_by_name('content_views', name=entity_dict['content_view'], params=scope)

    if 'current_lifecycle_environment' in entity_dict:
        entity_dict['current_lifecycle_environment'] = module.find_resource_by_name(
            'lifecycle_environments', name=entity_dict['current_lifecycle_environment'], params=scope)
        search_scope = {'content_view_id': content_view['id'], 'environment_id': entity_dict['current_lifecycle_environment']['id']}
        content_view_version = module.find_resource('content_view_versions', search=None, params=search_scope)
    elif 'version' in entity_dict:
        search = "content_view_id={0},version={1}".format(content_view['id'], entity_dict['version'])
        content_view_version = module.find_resource('content_view_versions', search=search, failsafe=True)
    else:
        content_view_version = None

    changed = False
    le_changed = False
    if module.desired_absent:
        changed = module.ensure_entity_state('content_view_versions', None, content_view_version, params=scope)
    else:
        if content_view_version is None:
            # Do a sanity check, whether we can perform this non-synchronous
            if 'lifecycle_environments' in entity_dict and not entity_dict['synchronous']:
                module.fail_json(msg="Cannot perform non-blocking publishing and promoting in the same module call.")

            payload = {
                'id': content_view['id'],
            }
            if 'description' in entity_dict:
                payload['description'] = entity_dict['description']
            if 'force_yum_metadata_regeneration' in entity_dict:
                payload['force_yum_metadata_regeneration'] = entity_dict['force_yum_metadata_regeneration']
            if 'version' in entity_dict:
                split_version = list(map(int, str(entity_dict['version']).split('.')))
                payload['major'] = split_version[0]
                payload['minor'] = split_version[1]

            changed, response = module.resource_action('content_views', 'publish', params=payload, synchronous=entity_dict['synchronous'])
            content_view_version = module.show_resource('content_view_versions', response['output']['content_view_version_id'])

        if 'lifecycle_environments' in entity_dict:
            lifecycle_environments = module.find_resources_by_name('lifecycle_environments', names=entity_dict['lifecycle_environments'], params=scope)
            le_changed = promote_content_view_version(
                module, content_view_version, lifecycle_environments, synchronous=entity_dict['synchronous'],
                force=entity_dict['force_promote'],
                force_yum_metadata_regeneration=entity_dict['force_yum_metadata_regeneration'],
            )

    module.exit_json(changed=changed or le_changed)