def query_private_nsfs(ws_id, vendor, name, version, is_vnf): """ Finds a function in the private catalogue :param ws_id: :param is_vnf: :param vendor: :param name: :param version: :return: """ session = db_session() if is_vnf: descriptor = session.query(PrivateFunction).filter( PrivateFunction.name == name and PrivateFunction.vendor == vendor and PrivateFunction.version == version and PrivateFunction.workspace.id == ws_id and PrivateFunction.workspace.owner == get_user( session['user_data'])).first() else: descriptor = session.query(PrivateService).filter( PrivateService.name == name and PrivateService.vendor == vendor and PrivateService.version == version and PrivateFunction.workspace.id == ws_id and PrivateFunction.workspace.owner == get_user( session['user_data'])).first() return descriptor
def query_private_nsfs(ws_id, vendor, name, version, is_vnf): """ Finds a function in the private catalogue :param ws_id: The workspace ID :param is_vnf: if descriptor is a VNF :param vendor: the descriptors vendor :param name: the descriptors name :param version: the descriptors version :return: The requested descriptor if found, None if nothing found """ session = db_session() if is_vnf: descriptor = session.query(PrivateFunction).filter( PrivateFunction.name == name and PrivateFunction.vendor == vendor and PrivateFunction.version == version and PrivateFunction.workspace.id == ws_id and PrivateFunction.workspace.owner == get_user( session['user_data'])).first() else: descriptor = session.query(PrivateService).filter( PrivateService.name == name and PrivateService.vendor == vendor and PrivateService.version == version and PrivateFunction.workspace.id == ws_id and PrivateFunction.workspace.owner == get_user( session['user_data'])).first() return descriptor
def query_private_nsfs(ws_id, vendor, name, version, is_vnf): """ Finds a function in the private catalogue :param ws_id: The workspace ID :param is_vnf: if descriptor is a VNF :param vendor: the descriptors vendor :param name: the descriptors name :param version: the descriptors version :return: The requested descriptor if found, None if nothing found """ session = db_session() if is_vnf: descriptor = session.query(PrivateFunction).filter(PrivateFunction.name == name and PrivateFunction.vendor == vendor and PrivateFunction.version == version and PrivateFunction.workspace.id == ws_id and PrivateFunction.workspace.owner == get_user( session['user_data'])).first() else: descriptor = session.query(PrivateService).filter( PrivateService.name == name and PrivateService.vendor == vendor and PrivateService.version == version and PrivateFunction.workspace.id == ws_id and PrivateFunction.workspace.owner == get_user(session['user_data'])).first() return descriptor
def query_private_nsfs(ws_id, vendor, name, version, is_vnf): """ Finds a function in the private catalogue :param ws_id: :param is_vnf: :param vendor: :param name: :param version: :return: """ session = db_session() if is_vnf: descriptor = session.query(PrivateFunction).filter(PrivateFunction.name == name and PrivateFunction.vendor == vendor and PrivateFunction.version == version and PrivateFunction.workspace.id == ws_id and PrivateFunction.workspace.owner == get_user( session['user_data'])).first() else: descriptor = session.query(PrivateService).filter( PrivateService.name == name and PrivateService.vendor == vendor and PrivateService.version == version and PrivateFunction.workspace.id == ws_id and PrivateFunction.workspace.owner == get_user(session['user_data'])).first() return descriptor
def create_workspace(login: str, workspace_data: dict) -> dict: """ Creates a workspace (on disk and in the database) from the given workspace data :param workspace_data: The workspace configuration data :return: The created workspace """ wsName = shlex.quote(workspace_data["name"]) session = db_session() # test if ws Name exists in database user = get_user(login) existingWorkspaces = list(session.query(Workspace) .filter(Workspace.owner == user) .filter(Workspace.name == wsName)) if len(existingWorkspaces) > 0: raise NameConflict("Workspace with name " + wsName + " already exists") wsPath = path.join(WORKSPACES_DIR, user.name, wsName) # prepare db insert try: ws = Workspace(name=wsName, path=wsPath, owner=user) session.add(ws) if 'platforms' in workspace_data: for platform in workspace_data['platforms']: ptf = Platform(platform['name'], platform['url'], True, ws) if 'token' in platform: ptf.token_path = create_token_file(platform['token']) session.add(ptf) test_url(platform['name'], platform['url'] + "/api/v2/packages") if 'catalogues' in workspace_data: for catalogue in workspace_data['catalogues']: session.add(Catalogue(catalogue['name'], catalogue['url'], True, ws)) test_url(catalogue['name'], catalogue['url']) except Exception as e: logger.exception(e) session.rollback() raise # create workspace on disk proc = Popen(['son-workspace', '--init', '--workspace', wsPath], stdout=PIPE, stderr=PIPE) out, err = proc.communicate() exitcode = proc.returncode if out.decode().find('existing') >= 0: workspace_exists = True else: workspace_exists = False if exitcode == 0 and not workspace_exists: update_workspace_descriptor(ws) session.commit() return ws.as_dict() else: session.rollback() if workspace_exists: raise NameConflict(out.decode()) raise Exception(err, out)
def create_workspace(login: str, workspace_data: dict) -> dict: """ Creates a workspace (on disk and in the database) from the given workspace data :param workspace_data: The workspace configuration data :return: The created workspace """ wsName = shlex.quote(workspace_data["name"]) session = db_session() # test if ws Name exists in database user = get_user(login) existingWorkspaces = list( session.query(Workspace).filter(Workspace.owner == user).filter( Workspace.name == wsName)) if len(existingWorkspaces) > 0: raise NameConflict("Workspace with name " + wsName + " already exists") wsPath = path.join(WORKSPACES_DIR, user.name, wsName) # prepare db insert try: ws = Workspace(name=wsName, path=wsPath, owner=user) session.add(ws) if 'platforms' in workspace_data: for platform in workspace_data['platforms']: session.add(Platform(platform['name'], platform['url'], ws)) test_url(platform['name'], platform['url'] + "/packages") if 'catalogues' in workspace_data: for catalogue in workspace_data['catalogues']: session.add(Catalogue(catalogue['name'], catalogue['url'], ws)) test_url(catalogue['name'], catalogue['url']) except: logger.exception() session.rollback() raise # create workspace on disk proc = Popen(['son-workspace', '--init', '--workspace', wsPath], stdout=PIPE, stderr=PIPE) out, err = proc.communicate() exitcode = proc.returncode if out.decode().find('existing') >= 0: workspace_exists = True else: workspace_exists = False if exitcode == 0 and not workspace_exists: synchronize_workspace_descriptor(ws, session) session.commit() return ws.as_dict() else: session.rollback() if workspace_exists: raise NameConflict(out.decode()) raise Exception(err, out)
def setup_git_user_email(project_full_path: str): """ Setting up the git user in the local git config to be able to make commits and push :param project_full_path: The absolute project path """ user = usermanagement.get_user(session['user_data']['login']) git_command(['config', 'user.name', user.name], cwd=project_full_path) git_command(['config', 'user.email', user.email], cwd=project_full_path) git_command(['config', 'push.default', 'simple'], cwd=project_full_path)
def get_workspaces(login: str) -> list: """ Get all workspaces for the current user :return: A list wof workspace dictionaries """ session = db_session() user = get_user(login) workspaces = session.query(Workspace). \ filter(Workspace.owner == user).all() session.commit() return list(map(lambda x: x.as_dict(), workspaces))
def check_access(request): """ checks if the current user is allowed to access a given resource. Session will be invalidated if the login information cannot be found :param request: The http request made to the server :return: nothing if access granted :raises UnauthorizedException: if user not logged in """ if not all(key in session for key in ['user_data', 'access_token']): session.clear() raise UnauthorizedException( "Session invalid. Please try logging in again") # Access parsed values of the url # Check if wsID was in the url if request.view_args is None: return if 'ws_id' in request.view_args: ws_id = request.view_args['ws_id'] data_session = db_session() ws = data_session.query(Workspace).filter_by(id=ws_id).first() # Get current user user = get_user(session['user_data']['login']) # If the requested workspace is in his workspaces, he is allowed to access it if ws in user.workspaces: if 'project_id' in request.view_args: pj_id = request.view_args['project_id'] pj = data_session.query(Project).filter( Project.id == pj_id).first() if pj in ws.projects: return else: raise NotFound("Project not found") elif 'parent_id' in request.view_args and PROJECTS in request.url.split( "/"): pj_id = request.view_args['parent_id'] pj = data_session.query(Project).filter( Project.id == pj_id).first() if pj in ws.projects: return else: raise NotFound("Project not found") return raise NotFound("Workspace not found") return
def check_access(request): """ checks if the current user is allowed to access a given resource. Session will be invalidated if the login information cannot be found :param request: The http request made to the server :return: nothing if access granted :raises UnauthorizedException: if user not logged in """ if not all(key in session for key in ['user_data', 'access_token']): session.clear() raise UnauthorizedException("Session invalid. Please try logging in again") # Access parsed values of the url # Check if wsID was in the url if request.view_args is None: return if 'ws_id' in request.view_args: ws_id = request.view_args['ws_id'] data_session = db_session() ws = data_session.query(Workspace).filter_by(id=ws_id).first() # Get current user user = get_user(session['user_data']['login']) # If the requested workspace is in his workspaces, he is allowed to access it if ws in user.workspaces: if 'project_id' in request.view_args: pj_id = request.view_args['project_id'] pj = data_session.query(Project).filter(Project.id == pj_id).first() if pj in ws.projects: return else: raise NotFound("Project not found") elif 'parent_id' in request.view_args and PROJECTS in request.url.split("/"): pj_id = request.view_args['parent_id'] pj = data_session.query(Project).filter(Project.id == pj_id).first() if pj in ws.projects: return else: raise NotFound("Project not found") return raise NotFound("Workspace not found") return
def setup_git_user_email(project_full_path: str): user = usermanagement.get_user(session['user_data']['login']) git_command(['config', 'user.name', user.name], cwd=project_full_path) git_command(['config', 'user.email', user.email], cwd=project_full_path) git_command(['config', 'push.default', 'simple'], cwd=project_full_path)