def wait_for_syncplan_tasks(repo_backend_id=None, timeout=10, repo_name=None): """Search the pulp tasks and identify repositories sync tasks with specified name or backend_identifier :param repo_backend_id: The Backend ID for the repository to identify the repo in Pulp environment :param timeout: Value to decided how long to check for the Sync task :param repo_name: If repo_backend_id can not be passed, pass the repo_name """ if repo_name: repo_backend_id = (entities.Repository().search(query={ 'search': f'name="{repo_name}"', 'per_page': '1000' })[0].backend_identifier) # Fetch the Pulp password pulp_pass = ssh.command( 'grep "^default_password" /etc/pulp/server.conf | awk \'{print $2}\'' ).stdout.splitlines()[0] # Set the Timeout value timeup = time.time() + int(timeout) * 60 # Search Filter to filter out the task based on backend-id and sync action filtered_req = { 'criteria': { 'filters': { 'tags': { '$in': [f"pulp:repository:{repo_backend_id}"] }, 'task_type': { '$in': ["pulp.server.managers.repo.sync.sync"] }, } } } while True: if time.time() > timeup: raise entities.APIResponseError( f'Pulp task with repo_id {repo_backend_id} not found') # Send request to pulp API to get the task info req = request( 'POST', f'{get_url()}/pulp/api/v2/tasks/search/', verify=False, auth=('admin', f'{pulp_pass}'), headers={'content-type': 'application/json'}, data=filtered_req, ) # Check Status code of response if req.status_code != 200: raise entities.APIResponseError( f'Pulp task with repo_id {repo_backend_id} not found') # Check content of response # It is '[]' string for empty content when backend_identifier is wrong if len(req.content) > 2: if req.json()[0].get('state') in ['finished']: return True elif req.json()[0].get('error'): raise AssertionError( f"Pulp task with repo_id {repo_backend_id} error or not found: " f"'{req.json().get('error')}'") time.sleep(2)
def create_role_permissions(role, permissions_types_names): # pragma: no cover """Create role permissions found in dict permissions_types_names. :param role: nailgun.entities.Role :param permissions_types_names: a dict containing resource types and permission names to add to the role, example usage. :: permissions_types_names = { None: ['access_dashboard'], 'Organization': ['view_organizations'], 'Location': ['view_locations'], 'Katello::KTEnvironment': [ 'view_lifecycle_environments', 'edit_lifecycle_environments', 'promote_or_remove_content_views_to_environments' ] } role = entities.Role(name='example_role_name').create() create_role_permissions(role, permissions_types_names) """ for resource_type, permissions_name in permissions_types_names.items(): if resource_type is None: permissions_entities = [] for name in permissions_name: result = entities.Permission(name=name).search() if not result: raise entities.APIResponseError( 'permission "{}" not found'.format(name)) if len(result) > 1: raise entities.APIResponseError( 'found more than one entity for permission' ' "{}"'.format(name)) entity_permission = result[0] if entity_permission.name != name: raise entities.APIResponseError( 'the returned permission is different from the' ' requested one "{0} != {1}"'.format( entity_permission.name, name)) permissions_entities.append(entity_permission) else: if not permissions_name: raise ValueError('resource type "{}" empty. You must select at' ' least one permission'.format(resource_type)) resource_type_permissions_entities = entities.Permission( resource_type=resource_type).search() if not resource_type_permissions_entities: raise entities.APIResponseError( 'resource type "{}" permissions not found'.format( resource_type)) permissions_entities = [ entity for entity in resource_type_permissions_entities if entity.name in permissions_name ] # ensure that all the requested permissions entities where # retrieved permissions_entities_names = { entity.name for entity in permissions_entities } not_found_names = set(permissions_name).difference( permissions_entities_names) if not_found_names: raise entities.APIResponseError( 'permissions names entities not found' ' "{}"'.format(not_found_names)) entities.Filter(permission=permissions_entities, role=role, search=None).create()