def main(): module = KatelloEntityAnsibleModule( 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(), state=dict(default='present', choices=['present_with_defaults', 'present', 'absent']), ), 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 or module.state == 'present_with_defaults') 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)
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 if module.desired_absent: module.ensure_entity('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] response = module.resource_action('content_views', 'publish', params=payload, synchronous=entity_dict['synchronous']) # workaround for https://projects.theforeman.org/issues/28138 if not module.check_mode: content_view_version_id = response['output'].get('content_view_version_id') or response['input'].get('content_view_version_id') content_view_version = module.show_resource('content_view_versions', content_view_version_id) else: content_view_version = {'id': -1, 'environments': []} if 'lifecycle_environments' in entity_dict: lifecycle_environments = module.find_resources_by_name('lifecycle_environments', names=entity_dict['lifecycle_environments'], params=scope) 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()
def main(): module = KatelloEntityAnsibleModule( entity_spec=dict( content_view=dict(type='entity', required=True), description=dict(), version=dict(), lifecycle_environments=dict(type='list', elements='str'), force_promote=dict(type='bool', aliases=['force'], default=False), force_yum_metadata_regeneration=dict(type='bool', default=False), current_lifecycle_environment=dict(), ), mutually_exclusive=[['current_lifecycle_environment', 'version']], ) module.task_timeout = 60 * 60 entity_dict = module.clean_params() with module.api_connection(): entity_dict, scope = module.handle_organization_param(entity_dict) 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 if module.desired_absent: module.ensure_entity('content_view_versions', None, content_view_version, params=scope) else: if content_view_version is None: 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] response = module.resource_action('content_views', 'publish', params=payload) # workaround for https://projects.theforeman.org/issues/28138 if not module.check_mode: content_view_version_id = response['output'].get( 'content_view_version_id') or response['input'].get( 'content_view_version_id') content_view_version = module.show_resource( 'content_view_versions', content_view_version_id) else: content_view_version = {'id': -1, 'environments': []} if 'lifecycle_environments' in entity_dict: lifecycle_environments = module.find_resources_by_name( 'lifecycle_environments', names=entity_dict['lifecycle_environments'], params=scope) promote_content_view_version( module, content_view_version, lifecycle_environments, force=entity_dict['force_promote'], force_yum_metadata_regeneration=entity_dict[ 'force_yum_metadata_regeneration'], )
def main(): module = KatelloEntityAnsibleModule( entity_spec=dict( name=dict(required=True), new_name=dict(), lifecycle_environment=dict(type='entity', flat_name='environment_id'), content_view=dict(type='entity'), host_collections=dict(type='entity_list'), auto_attach=dict(type='bool'), release_version=dict(), service_level=dict( choices=['Self-Support', 'Standard', 'Premium']), max_hosts=dict(type='int'), unlimited_hosts=dict(type='bool'), purpose_usage=dict(), purpose_role=dict(), purpose_addons=dict(type='list', elements='str'), ), argument_spec=dict( subscriptions=dict( type='list', elements='dict', options=dict( name=dict(), pool_id=dict(), ), required_one_of=[['name', 'pool_id']], mutually_exclusive=[['name', 'pool_id']], ), content_overrides=dict( type='list', elements='dict', options=dict( label=dict(required=True), override=dict(required=True, choices=['enabled', 'disabled', 'default']), )), state=dict(default='present', choices=[ 'present', 'present_with_defaults', 'absent', 'copied' ]), ), required_if=[ ['state', 'copied', ['new_name']], ['unlimited_hosts', False, ['max_hosts']], ], ) entity_dict = module.clean_params() with module.api_connection(): entity_dict, scope = module.handle_organization_param(entity_dict) 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 '{0}' already exists.".format( entity_dict['new_name'])) module.exit_json() subscriptions = entity_dict.pop('subscriptions', None) content_overrides = entity_dict.pop('content_overrides', None) host_collections = entity_dict.pop('host_collections', None) 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 module.changed): # the auto_attach, release_version and service_level parameters can only be set on an existing AK with an update, # not during create, so let's force an update. see https://projects.theforeman.org/issues/27632 for details if any(key in entity_dict for key in [ 'auto_attach', 'release_version', 'service_level' ]) and module.changed: activation_key = module.ensure_entity('activation_keys', entity_dict, activation_key, params=scope) ak_scope = {'activation_key_id': activation_key['id']} if subscriptions is not None: desired_subscriptions = [] for subscription in subscriptions: if subscription['name'] is not None and subscription[ 'pool_id'] is None: desired_subscriptions.append( module.find_resource_by_name('subscriptions', subscription['name'], params=scope, thin=True)) if subscription['pool_id'] is not None: desired_subscriptions.append( module.find_resource_by_id('subscriptions', subscription['pool_id'], params=scope, thin=True)) desired_subscription_ids = set( item['id'] for item in desired_subscriptions) current_subscriptions = module.list_resource( 'subscriptions', params=ak_scope) if entity else [] current_subscription_ids = set( item['id'] for item in current_subscriptions) if desired_subscription_ids != current_subscription_ids: module.record_before( 'activation_keys/subscriptions', { 'id': activation_key['id'], 'subscriptions': current_subscription_ids }) module.record_after( 'activation_keys/subscriptions', { 'id': activation_key['id'], 'subscriptions': desired_subscription_ids }) module.record_after_full( 'activation_keys/subscriptions', { 'id': activation_key['id'], 'subscriptions': desired_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) if content_overrides is not None: if entity: product_content = module.resource_action( 'activation_keys', 'product_content', params={ 'id': activation_key['id'], 'content_access_mode_all': True }, ignore_check_mode=True, ) else: product_content = {'results': []} 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 = [] module.record_before( 'activation_keys/content_overrides', { 'id': activation_key['id'], 'content_overrides': current_content_overrides.copy() }) module.record_after( 'activation_keys/content_overrides', { 'id': activation_key['id'], 'content_overrides': desired_content_overrides }) module.record_after_full( 'activation_keys/content_overrides', { 'id': activation_key['id'], 'content_overrides': desired_content_overrides }) for label, override in desired_content_overrides.items(): if override is not None and 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, 'remove': True }) if changed_content_overrides: payload = { 'id': activation_key['id'], 'content_overrides': changed_content_overrides, } module.resource_action('activation_keys', 'content_override', payload) if host_collections is not None: if not entity: current_host_collection_ids = set() elif '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: module.record_before( 'activation_keys/host_collections', { 'id': activation_key['id'], 'host_collections': current_host_collection_ids }) module.record_after( 'activation_keys/host_collections', { 'id': activation_key['id'], 'host_collections': desired_host_collection_ids }) module.record_after_full( 'activation_keys/host_collections', { 'id': activation_key['id'], 'host_collections': desired_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)