我是靠谱客的博主 灵巧冬日,最近开发中收集的这篇文章主要介绍ovirt-engine平台vm rest-api新增接口字段,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

 

背景:基于ovirt-engine二次开发,为实现软删除功能,对现有的vm接口进行扩展字段,增加判断软删除字段以及软删除的时间字段。

1、首先要再ovirt-engine-api-model工程中,找到对应的资源类型,添加你需要的字段

types.Vm类中新增如下字段,

    Date softDeletionTime();

    Boolean isSoftDeleted();

新增之后编译出包覆盖maven仓库下的model文件例如:model-4.3.31-sources.jar注意对应的sha1文件记得更新对应新的jar包

sed -i "1c $(sha1sum /home/xx/.m2/repository/org/ovirt/engine/api/model/4.3.31/model-4.3.31-sources.jar |awk -F" " '{print$1}')" /home/xx/.m2/repository/org/ovirt/engine/api/model/4.3.31/model-4.3.31-sources.jar.sha1

2、新增字段

回到ovirt-engine工程中,在packaging/dbscripts/upgrade/04_03_0951_update_soft_delete_to_vm_dynamic.sql新增文件,增加字段

select fn_db_add_column('vm_dynamic', 'is_soft_deleted', 'boolean DEFAULT false NOT NULL');
select fn_db_add_column('vm_dynamic', 'soft_deletion_time', 'timestamp with time zone');

packaging/dbscripts/create_views.sql  视图sql function语句补全,Vm_dynamic类涉及的属性都要添加,可以参考其他同类下的属性添加,例如

 

同理还有数据库接口的实现类org/ovirt/engine/core/dao/VmDynamicDaoImpl.java

更新事务模型,增加数据和set和get方法,org.ovirt.engine.core.common.businessentities.VmDynamic

    public Date getSoftDeletionTime() {
        return this.softDeletionTime;
    }

    public void setSoftDeletionTime(Date softDeletionTime) {
        this.softDeletionTime = softDeletionTime;
    }

    public boolean IsSoftDeleted() {
        return isSoftDeleted;
    }

    public void setSoftDeleted(boolean isSoftDeleted) {
        this.isSoftDeleted = isSoftDeleted;
    }

 同样需要更新org.ovirt.engine.core.common.businessentities.Vm

    public void setSoftDeleted(boolean value){
        vmDynamic.setSoftDeleted(value);
    }

    public boolean isSoftDeleted(){
        return vmDynamic.isSoftDeleted();
    }

    public Date getSoftDeletionTime() {
        return this.vmDynamic.getSoftDeletionTime();
    }

    public void setSoftDeletionTime(Date value) {
        this.vmDynamic.setSoftDeletionTime(value);
    }

到目前为止,查询虚拟机详情接口可以显示两个新增加的字段了。

3、更新接口适配

主要修改这个几个文件

首先PUT api/vms/123 接口到了rest-api后端的BackendVmResource.java中的update方法中,注意这个调用UpdateParametersProvider这个对象中要传入你的新加的数据,这也参数才能传递到数据库后端,实现中涉及org.ovirt.engine.core.common.action.VmManagementParametersBase的修改。

        Vm vm = performUpdate(
            incoming,
            new QueryIdResolver<>(queryType, IdQueryParameters.class),
            ActionType.UpdateVm,
            new UpdateParametersProvider()
        );

到了后端,修改更新命令函数:org.ovirt.engine.core.bll.UpdateVmCommand#executeVmCommand,对应的dal下的数据库接口和实现添加updateVmSoftDeleted方法,最后添加sql function。

    @Override
    protected void executeVmCommand() {
        VM vm = getParameters().getVm();
        if(getParameters().isSetIsSoftDeleted()) {
            vmDynamicDao.updateVmSoftDeleted(vm.getId(), getParameters().isSoftDeleted());
        }
    void updateVmSoftDeleted(Guid vmid, boolean isSoftDeleted);

 

    @Override
    public void updateVmSoftDeleted(Guid vmGuid, boolean isSoftDeleted) {
        MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource()
                .addValue("vm_guid", vmGuid)
                .addValue("is_soft_deleted", isSoftDeleted)
                .addValue("soft_deletion_time", new Date());

        getCallsHandler().executeModification("UpdateVmSoftDeleted", parameterSource);
    }

 vms_sp.sql添加sql function

CREATE OR REPLACE FUNCTION UpdateVmSoftDeleted (
    v_vm_guid UUID,
    v_is_soft_deleted BOOLEAN,
    v_soft_deletion_time TIMESTAMP WITH TIME ZONE
    )
RETURNS VOID AS $PROCEDURE$
BEGIN
    UPDATE vm_dynamic
    SET is_soft_deleted = v_is_soft_deleted,
        soft_deletion_time = v_soft_deletion_time
    WHERE vm_guid = v_vm_guid;
END;$PROCEDURE$
LANGUAGE plpgsql;

测试接口以实现

最后

以上就是灵巧冬日为你收集整理的ovirt-engine平台vm rest-api新增接口字段的全部内容,希望文章能够帮你解决ovirt-engine平台vm rest-api新增接口字段所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部