Пример #1
0
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 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()
Пример #4
0
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()