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

import com.bocloud.cmp.model.ResourcePoolBean;
import com.bocloud.cmp.model.ResourcePoolModel;
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.ims.entity.resource.VmResourcePool;
import com.bocloud.ims.interfaces.resource.VmResourcePoolService;
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;

/**
 * vcenter资源池控制器
 *
 * @author tyl
 */
@RestController
@RequestMapping("/v1/vrps")
@Api(value = "vcenter资源池", tags = "vcenter资源池")
public class VmResourcePoolController {

    @Autowired
    private VmResourcePoolService vmResourcePoolService;

    /**
     * 查询资源池
     *
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询资源池列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "查询资源池列表")
    @GetMapping
    public BocloudResult<List<VmResourcePool>> list() {
        return vmResourcePoolService.list(null, null);
    }

    /**
     * 根据condition查询资源池列表
     *
     * @param condition
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "根据condition查询资源池列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "查询资源池列表")
    @GetMapping("/condition")
    public BocloudResult condition(@RequestParam(value = Common.CONDITION, required = false) String condition) {
        return vmResourcePoolService.vrps(condition);
    }

    /**
     * 创建资源池
     *
     * @param model
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建资源池",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "创建资源池")
    @PostMapping
    public BocloudResult<String> create(@RequestBody ResourcePoolModel model,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vmResourcePoolService.create(model, context);
    }

    /**
     * 修改资源池
     *
     * @param bean
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "修改资源池",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "修改资源池")
    @PutMapping(value = "/{id}")
    public BocloudResult<String> modify(@RequestBody ResourcePoolBean bean,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vmResourcePoolService.modify(bean, context);
    }

    /**
     * 删除资源池
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除资源池",  httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "删除资源池")
    @DeleteMapping(value = "/{id}")
    public BocloudResult<String> remove(@PathVariable(value = Common.ID) Long id,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vmResourcePoolService.remove(id, context.getTarget());
    }

    /**
     * 查询资源池详情
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询资源池详情",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "资源池", operation = "查询资源池详情")
    @GetMapping(value = "/{id}")
    public BocloudResult<VmResourcePool> detail(@PathVariable(value = Common.ID) Long id,
                                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return vmResourcePoolService.detail(id);
    }

}
