def _scan_for_functions(function_dir, pj): """ Scan project for functions Scans the given directory for functions and adds them to the project. :param function_dir: The functions directory in the project :param pj: The project model from the database """ session = db_session() from son_editor.models.descriptor import Function try: for function_folder in os.listdir(function_dir): folder_path = os.path.join(function_dir, function_folder) if os.path.isdir(folder_path): yaml_files = [ file for file in os.listdir(folder_path) if file.endswith(".yml") ] if len(yaml_files) == 1: function = Function() file_path = os.path.join(folder_path, yaml_files[0]) load_ns_vnf_from_disk(file_path, function) db_function = session.query(Function). \ filter(Function.project == pj). \ filter(Function.name == function.name). \ filter(Function.vendor == function.vendor). \ filter(Function.version == function.version). \ first() if not db_function: logger.info("Found function in project {}: {}".format( pj.name, function.uid)) function.project = pj session.add(function) session.commit() else: function = db_function # rename folder if necessary target_folder = os.path.normpath( get_file_path("vnf", function).replace( get_file_name(function), '')) if os.path.normpath(folder_path) != target_folder: shutil.move(folder_path, target_folder) file_path = file_path.replace(folder_path, target_folder) # rename file if necessary if not os.path.exists(get_file_path("vnf", function)): shutil.move(file_path, get_file_path("vnf", function)) else: logger.info( "Multiple or no yaml files in folder {}. Ignoring". format(folder_path)) except: logger.exception("Could not load function descriptor:") finally: session.rollback()
def create_function(ws_id: int, project_id: int, function_data: dict) -> dict: """ Creates a new vnf in the project :param ws_id: The workspace ID :param project_id: The Project ID :param function_data: The function data to create :return: The created function as a dict """ try: function_name = shlex.quote(function_data['descriptor']["name"]) vendor_name = shlex.quote(function_data['descriptor']["vendor"]) version = shlex.quote(function_data['descriptor']["version"]) except KeyError as ke: raise InvalidArgument("Missing key {} in function data".format( str(ke))) session = db_session() ws = session.query(Workspace).filter( Workspace.id == ws_id).first() # type: Workspace validate_vnf(ws.schema_index, function_data['descriptor']) # test if function Name exists in database existing_functions = list( session.query(Function).join(Project).join(Workspace).filter( Workspace.id == ws_id).filter( Function.project_id == project_id).filter( Function.vendor == vendor_name).filter( Function.name == function_name).filter( Function.version == version)) if len(existing_functions) > 0: raise NameConflict("Function with name " + function_name + " already exists") project = session.query(Project).filter(Project.id == project_id).first() if project is None: raise NotFound("No project with id " + project_id + " was found") function = Function(name=function_name, project=project, vendor=vendor_name, version=version, descriptor=json.dumps(function_data['descriptor'])) session.add(function) try: write_ns_vnf_to_disk("vnf", function) except: logger.exception("Could not write data to disk:") session.rollback() raise session.commit() return function.as_dict()
def create_function(ws_id: int, project_id: int, function_data: dict) -> dict: """ Creates a new vnf in the project :param ws_id: :param project_id: :param function_data: :return: The created function as a dict """ try: function_name = shlex.quote(function_data['descriptor']["name"]) vendor_name = shlex.quote(function_data['descriptor']["vendor"]) version = shlex.quote(function_data['descriptor']["version"]) except KeyError as ke: raise InvalidArgument("Missing key {} in function data".format(str(ke))) session = db_session() ws = session.query(Workspace).filter(Workspace.id == ws_id).first() # type: Workspace validate_vnf(ws.vnf_schema_index, function_data['descriptor']) # test if function Name exists in database existing_functions = list(session.query(Function) .join(Project) .join(Workspace) .filter(Workspace.id == ws_id) .filter(Function.project_id == project_id) .filter(Function.vendor == vendor_name) .filter(Function.name == function_name) .filter(Function.version == version)) if len(existing_functions) > 0: raise NameConflict("Function with name " + function_name + " already exists") project = session.query(Project).filter(Project.id == project_id).first() if project is None: raise NotFound("No project with id " + project_id + " was found") function = Function(name=function_name, project=project, vendor=vendor_name, version=version, descriptor=json.dumps(function_data['descriptor'])) session.add(function) try: write_ns_vnf_to_disk("vnf", function) except: logger.exception("Could not write data to disk:") session.rollback() raise session.commit() return function.as_dict()
def _scan_for_functions(function_dir, pj): """ Scan project for functions Scans the given directory for functions and adds them to the project. :param function_dir: The functions directory in the project :param pj: The project model from the database """ session = db_session() from son_editor.models.descriptor import Function try: for function_folder in os.listdir(function_dir): folder_path = os.path.join(function_dir, function_folder) if os.path.isdir(folder_path): yaml_files = [file for file in os.listdir(folder_path) if file.endswith(".yml")] if len(yaml_files) == 1: function = Function() file_path = os.path.join(folder_path, yaml_files[0]) load_ns_vnf_from_disk(file_path, function) db_function = session.query(Function). \ filter(Function.project == pj). \ filter(Function.name == function.name). \ filter(Function.vendor == function.vendor). \ filter(Function.version == function.version). \ first() if not db_function: logger.info("Found function in project {}: {}".format(pj.name, function.uid)) function.project = pj session.add(function) session.commit() else: function = db_function # rename folder if necessary target_folder = os.path.normpath( get_file_path("vnf", function).replace(get_file_name(function), '')) if os.path.normpath(folder_path) != target_folder: shutil.move(folder_path, target_folder) file_path = file_path.replace(folder_path, target_folder) # rename file if necessary if not os.path.exists(get_file_path("vnf", function)): shutil.move(file_path, get_file_path("vnf", function)) else: logger.info("Multiple or no yaml files in folder {}. Ignoring".format(folder_path)) except: logger.exception("Could not load function descriptor:") finally: session.rollback()
def setUp(self): # Initializes test context self.app = init_test_context() self.project = Project(name="Project A") self.workspace = Workspace(name="Workspace A") self.service = Service(name="Service A") self.function = Function(name="Function A") self.catalogue = Catalogue(name="Catalogue A", url="") self.platform = Catalogue(name="Platform A", url="") self.user = User(name="User A")
def update_function(ws_id: int, prj_id: int, func_id: int, func_data: dict) -> dict: """ Update the function descriptor :param ws_id: :param prj_id: :param func_id: :param func_data: :return: The updated function descriptor """ session = db_session() ws = session.query(Workspace).filter(Workspace.id == ws_id).first() validate_vnf(ws.vnf_schema_index, func_data['descriptor']) edit_mode = func_data['edit_mode'] # test if function exists in database function = session.query(Function). \ join(Project). \ join(Workspace). \ filter(Workspace.id == ws_id). \ filter(Project.id == prj_id). \ filter(Function.id == func_id).first() if function is None: session.rollback() raise NotFound("Function with id {} does not exist".format(func_id)) old_file_name = get_file_path("vnf", function) old_folder_path = old_file_name.replace(get_file_name(function), "") old_uid = get_uid(function.vendor, function.name, function.version) try: new_name = shlex.quote(func_data['descriptor']["name"]) new_vendor = shlex.quote(func_data['descriptor']["vendor"]) new_version = shlex.quote(func_data['descriptor']["version"]) except KeyError as ke: session.rollback() raise InvalidArgument("Missing key {} in function data".format(str(ke))) # check if new name already exists function_dup = session.query(Function). \ join(Project). \ join(Workspace). \ filter(Workspace.id == ws_id). \ filter(Project.id == prj_id). \ filter(Function.name == new_name). \ filter(Function.vendor == new_vendor). \ filter(Function.version == new_version). \ filter(Function.id != func_id).first() if function_dup: session.rollback() raise NameConflict("A function with that name, vendor and version already exists") new_uid = get_uid(new_vendor, new_name, new_version) refs = get_references(function, session) if old_uid != new_uid: if refs: if edit_mode == "create_new": function = Function(project=function.project) session.add(function) else: replace_function_refs(refs, function.vendor, function.name, function.version, new_vendor, new_name, new_version) function.vendor = new_vendor function.name = new_name function.version = new_version function.uid = new_uid function.descriptor = json.dumps(func_data['descriptor']) try: if old_uid != new_uid: new_file_name = get_file_path("vnf", function) new_folder_path = new_file_name.replace(get_file_name(function), "") if old_folder_path != new_folder_path: # move old files to new location os.makedirs(new_folder_path) for file in os.listdir(old_folder_path): if not file.endswith(".yml"): # don't move descriptor yet if refs and edit_mode == "create_new": if os.path.isdir(os.path.join(old_folder_path, file)): shutil.copytree(os.path.join(old_folder_path, file), os.path.join(new_folder_path, file)) else: shutil.copy(os.path.join(old_folder_path, file), os.path.join(new_folder_path, file)) else: shutil.move(os.path.join(old_folder_path, file), os.path.join(new_folder_path, file)) if refs and edit_mode == "create_new": shutil.copy(old_file_name, new_file_name) else: shutil.move(old_file_name, new_file_name) if old_folder_path != new_folder_path and not (refs and edit_mode == "create_new"): # cleanup old folder if no other descriptor exists if not os.listdir(old_folder_path): shutil.rmtree(old_folder_path) write_ns_vnf_to_disk("vnf", function) if refs and old_uid != new_uid and edit_mode == 'replace_refs': for service in refs: write_ns_vnf_to_disk("ns", service) except: session.rollback() logger.exception("Could not update descriptor file:") raise session.commit() return function.as_dict()
def update_function(ws_id: int, prj_id: int, func_id: int, func_data: dict) -> dict: """ Update the function descriptor :param ws_id: The Workspace ID :param prj_id: The Project ID :param func_id: The function ID :param func_data: The funtion Data for updating :return: The updated function descriptor """ session = db_session() ws = session.query(Workspace).filter(Workspace.id == ws_id).first() validate_vnf(ws.schema_index, func_data['descriptor']) edit_mode = func_data['edit_mode'] # test if function exists in database function = session.query(Function). \ join(Project). \ join(Workspace). \ filter(Workspace.id == ws_id). \ filter(Project.id == prj_id). \ filter(Function.id == func_id).first() if function is None: session.rollback() raise NotFound("Function with id {} does not exist".format(func_id)) old_file_name = get_file_path("vnf", function) old_folder_path = old_file_name.replace(get_file_name(function), "") old_uid = get_uid(function.vendor, function.name, function.version) try: new_name = shlex.quote(func_data['descriptor']["name"]) new_vendor = shlex.quote(func_data['descriptor']["vendor"]) new_version = shlex.quote(func_data['descriptor']["version"]) except KeyError as ke: session.rollback() raise InvalidArgument("Missing key {} in function data".format( str(ke))) # check if new name already exists function_dup = session.query(Function). \ join(Project). \ join(Workspace). \ filter(Workspace.id == ws_id). \ filter(Project.id == prj_id). \ filter(Function.name == new_name). \ filter(Function.vendor == new_vendor). \ filter(Function.version == new_version). \ filter(Function.id != func_id).first() if function_dup: session.rollback() raise NameConflict( "A function with that name, vendor and version already exists") new_uid = get_uid(new_vendor, new_name, new_version) refs = get_references(function, session) if old_uid != new_uid: if refs: if edit_mode == "create_new": function = Function(project=function.project) session.add(function) else: replace_function_refs(refs, function.vendor, function.name, function.version, new_vendor, new_name, new_version) function.vendor = new_vendor function.name = new_name function.version = new_version function.uid = new_uid function.descriptor = json.dumps(func_data['descriptor']) try: if old_uid != new_uid: new_file_name = get_file_path("vnf", function) new_folder_path = new_file_name.replace(get_file_name(function), "") if old_folder_path != new_folder_path: # move old files to new location os.makedirs(new_folder_path) for file in os.listdir(old_folder_path): if not file.endswith(".yml"): # don't move descriptor yet if refs and edit_mode == "create_new": if os.path.isdir( os.path.join(old_folder_path, file)): shutil.copytree( os.path.join(old_folder_path, file), os.path.join(new_folder_path, file)) else: shutil.copy( os.path.join(old_folder_path, file), os.path.join(new_folder_path, file)) else: shutil.move(os.path.join(old_folder_path, file), os.path.join(new_folder_path, file)) if refs and edit_mode == "create_new": shutil.copy(old_file_name, new_file_name) else: shutil.move(old_file_name, new_file_name) if old_folder_path != new_folder_path and not (refs and edit_mode == "create_new"): # cleanup old folder if no other descriptor exists if not os.listdir(old_folder_path): shutil.rmtree(old_folder_path) write_ns_vnf_to_disk("vnf", function) if refs and old_uid != new_uid and edit_mode == 'replace_refs': for service in refs: write_ns_vnf_to_disk("ns", service) except: session.rollback() logger.exception("Could not update descriptor file:") raise session.commit() return function.as_dict()