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()
Esempio n. 2
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()