def main(): module = KatelloEntityAnsibleModule( argument_spec=dict( manifest_path=dict(type='path'), state=dict(default='present', choices=['absent', 'present', 'refreshed']), repository_url=dict(aliases=['redhat_repository_url']), ), foreman_spec=dict( organization=dict(type='entity', required=True, thin=False), ), required_if=[ ['state', 'present', ['manifest_path']], ], supports_check_mode=False, ) module.task_timeout = 5 * 60 with module.api_connection(): organization = module.lookup_entity('organization') scope = module.scope_for('organization') try: existing_manifest = organization['owner_details']['upstreamConsumer'] except KeyError: existing_manifest = None if module.state == 'present': if 'repository_url' in module.foreman_params: payload = {'redhat_repository_url': module.foreman_params['repository_url']} org_spec = dict(id=dict(), redhat_repository_url=dict()) organization = module.ensure_entity('organizations', payload, organization, state='present', foreman_spec=org_spec) try: with open(module.foreman_params['manifest_path'], 'rb') as manifest_file: files = {'content': (module.foreman_params['manifest_path'], manifest_file, 'application/zip')} params = {} if 'repository_url' in module.foreman_params: params['repository_url'] = module.foreman_params['repository_url'] params.update(scope) result = module.resource_action('subscriptions', 'upload', params, files=files, record_change=False, ignore_task_errors=True) for error in result['humanized']['errors']: if "same as existing data" in error: # Nothing changed, but everything ok break if "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) else: module.set_changed() except IOError as e: module.fail_json(msg="Unable to read the manifest file: %s" % e) elif module.desired_absent and existing_manifest: module.resource_action('subscriptions', 'delete_manifest', scope) elif module.state == 'refreshed': if existing_manifest: module.resource_action('subscriptions', 'refresh_manifest', scope) else: module.fail_json(msg="No manifest found to refresh.")
def main(): module = KatelloEntityAnsibleModule( entity_spec=dict( name=dict(required=True), updated_name=dict(), login=dict(), scc_account_password=dict(no_log=True, flat_name='password'), base_url=dict(), sync_date=dict(), interval=dict(choices=['never', 'daily', 'weekly', 'monthly']), state=dict(default='present', choices=['present', 'absent', 'synced']), ), argument_spec=dict( test_connection=dict(type='bool', default=False), ), ) entity_dict = module.clean_params() module.task_timeout = 4 * 60 module.connect() entity_dict, scope = module.handle_organization_param(entity_dict) failsafe = (module.state != 'synced') entity = module.find_resource_by_name('scc_accounts', name=entity_dict['name'], params=scope, failsafe=failsafe) if not module.desired_absent: if not entity: if 'login' not in entity_dict: module.fail_json(msg="scc account login not provided") if 'scc_account_password' not in entity_dict: module.fail_json(msg="Scc account password not provided") if entity_dict['test_connection']: scc_account_credentials = {} if entity: scc_account_credentials['id'] = entity['id'] if 'login' in entity_dict: scc_account_credentials['login'] = entity_dict['login'] if 'scc_account_password' in entity_dict: scc_account_credentials['password'] = entity_dict['scc_account_password'] if 'base_url' in entity_dict: scc_account_credentials['base_url'] = entity_dict['base_url'] module.resource_action('scc_accounts', 'test_connection', scc_account_credentials, ignore_check_mode=True) if 'updated_name' in entity_dict: entity_dict['name'] = entity_dict.pop('updated_name') if module.state == 'synced': module.resource_action('scc_accounts', 'sync', {'id': entity['id']}) else: module.ensure_entity('scc_accounts', entity_dict, entity, params=scope) module.exit_json()
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( argument_spec=dict( manifest_path=dict(type='path'), state=dict(default='present', choices=['absent', 'present', 'refreshed']), repository_url=dict(aliases=['redhat_repository_url']), ), required_if=[ ['state', 'present', ['manifest_path']], ], ) module.task_timeout = 5 * 60 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, files=files) for error in result['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) elif module.state == 'refreshed': if existing_manifest: changed, result = module.resource_action('subscriptions', 'refresh_manifest', scope) else: module.fail_json(msg="No manifest found to refresh.") module.exit_json(changed=changed)