package com.bocloud.ims.booter.controller.resource.hmc;

import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.ims.entity.resource.hmc.HmcVolumeGroup;
import com.bocloud.ims.entity.resource.hmc.params.HmcVolumeGroupAssignParams;
import com.bocloud.ims.entity.resource.hmc.params.HmcVolumeGroupCreateParams;
import com.bocloud.ims.entity.resource.hmc.params.HmcVolumeGroupParams;
import com.bocloud.ims.entity.resource.hmc.params.HmcVolumeGroupUpdateParams;
import com.bocloud.ims.interfaces.resource.hmc.HmcVolumeGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * hmc volume group controller
 * 
 * @author lkx007
 *
 */
@RestController
@RequestMapping("/v1/hmc")
@Api(value = "HMC虚拟存储器", tags = "HMC虚拟存储器")
public class HmcVolumeGroupController {
    @Autowired
    private HmcVolumeGroupService hmcVolumeGroupService;

    /**
     * 列表存储池信息
     *
     *
     * @return 列表结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC存储池", operation = "查询")
    @RequestMapping(value = "/vgs", method = { RequestMethod.GET })
    @ApiOperation(tags={"CMC","CSC"},value = "查询存储池",  httpMethod = "GET")
    public BocloudResult<List<HmcVolumeGroup>> list(
            @RequestParam(value = "serverId") Long serverId,
            @RequestParam(value = "vendorId") Long vendorId,
            @RequestParam(value = "viosId") Long viosId) {
        return hmcVolumeGroupService.list(serverId, vendorId, viosId);
    }

    /**
     * 创建存储池
     *
     * @param params
     * @param context
     *
     * @return 列表结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC存储池", operation = "创建")
    @RequestMapping(value = "/vgs", method = { RequestMethod.POST })
    @ApiOperation(tags={"CMC","CSC"},value = "创建存储池",  httpMethod = "POST")
    public BocloudResult create(
            @RequestBody HmcVolumeGroupCreateParams params,
            @Value(Common.REQ_CONTEXT) RequestContext context) {
        return hmcVolumeGroupService.createVG(params, context);
    }

    /**
     * 删除存储池
     *
     * @param params
     * @param context
     *
     * @return 列表结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC存储池", operation = "删除")
    @RequestMapping(value = "/vgs/{id}", method = { RequestMethod.DELETE })
    @ApiOperation(tags={"CMC","CSC"},value = "删除存储池",  httpMethod = "DELETE")
    public BocloudResult remove(
            @PathVariable(value = Common.ID) Long id,
            @Value(Common.REQ_CONTEXT) RequestContext context) {
        return hmcVolumeGroupService.removeVG(id, context);
    }

    /**
     * 在存储池中创建虚拟磁盘
     *
     * @param params
     * @param context
     * 
     * @return 创建结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC虚拟磁盘", operation = "创建")
    @RequestMapping(value = "/vgs/{id}/vdisks", method = { RequestMethod.POST })
    @ApiOperation(tags={"CMC","CSC"},value = "创建虚拟磁盘",  httpMethod = "POST")
    public BocloudResult createVd(
                                  @RequestBody HmcVolumeGroupParams params,
                                  @PathVariable(value = Common.ID, required = true) Long vgsId,
            @Value(Common.REQ_CONTEXT) RequestContext context) {
        return hmcVolumeGroupService.createVd(params, context);
    }

    /**
     * 删除虚拟磁盘
     *
     * @param params
     * @param context
     * 
     * @return 删除结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC虚拟磁盘", operation = "删除")
    @RequestMapping(value = "/vgs/{vgsId}/vdisks/{id}", method = { RequestMethod.DELETE })
    @ApiOperation(tags={"CMC","CSC"},value = "删除虚拟磁盘",  httpMethod = "DELETE")
    public BocloudResult deleteVd(
                                  @RequestBody HmcVolumeGroupParams params,
                                  @PathVariable(value = "vgsId", required = true) Long vgsId,
                                  @PathVariable(value = Common.ID, required = true) Long vdiskId,
            @Value(Common.REQ_CONTEXT) RequestContext context) {
        return hmcVolumeGroupService.deleteVd(params, context);
    }

    /**
     * 查询所有的虚拟插槽
     *
     * @return 列表结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC虚拟磁盘", operation = "查询分区中所有的虚拟插槽")
    @RequestMapping(value = "/partitions/{id}/slots", method = { RequestMethod.GET })
    @ApiOperation(tags={"CMC","CSC"},value = "查询分区中所有的虚拟插槽",  httpMethod = "GET")
    public BocloudResult<List<Object>> virtualSlotList(@RequestParam(value = "viosId", required = false) Long viosId,
                                                       @RequestParam(value = "partitionId", required = false) Long partitionId) {
        return hmcVolumeGroupService.virtualSlotList(viosId, partitionId);
    }

    /**
     * 分配虚拟磁盘
     * 
     * @param params
     * @param context
     * 
     * @return 列表结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC虚拟磁盘", operation = "分配")
    @RequestMapping(value = "/vgs/{vgsId}/vdisks/{id}", method = { RequestMethod.POST })
    @ApiOperation(tags={"CMC","CSC"},value = "分配虚拟磁盘",  httpMethod = "POST")
    public BocloudResult assign(
            @RequestBody HmcVolumeGroupAssignParams params,
            @Value(Common.REQ_CONTEXT) RequestContext context) {
        String volumeName = params.getVolumeName();
        String diskName = params.getDiskName();
        if (volumeName != null) {
            return hmcVolumeGroupService.assignPd(params, context);
        }
        if (diskName != null) {
            return hmcVolumeGroupService.assignVd(params, context);
        }
        return ResultTools.formatErrResult();
    }

    /**
     * 虚拟磁盘的扩容
     * 
     * @param params
     * @param context
     * 
     * @return 列表结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC虚拟磁盘", operation = "扩容")
    @RequestMapping(value = "/vgs/{vgsId}/vdisks/{id}", method = { RequestMethod.PUT })
    @ApiOperation(tags={"CMC","CSC"},value = "扩容虚拟磁盘",  httpMethod = "PUT")
    public BocloudResult update(
                                @RequestBody HmcVolumeGroupUpdateParams params,
            @Value(Common.REQ_CONTEXT) RequestContext context) {
        return hmcVolumeGroupService.update(params, context);
    }
}
