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

import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.ims.entity.resource.SecurityGroupRule;
import com.bocloud.ims.interfaces.resource.fusionsphere.FusionSphereSecurityGroupRuleService;
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;

@RestController
@RequestMapping("/v1/fc/sgroups/rules")
@Api(value = "安全组规则", tags = "安全组规则")
public class FusionSphereSecurityGroupRuleController {

    @Autowired
    private FusionSphereSecurityGroupRuleService securityGroupRuleService;

    /**
     * 列表
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询安全组规则列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "安全组规则", operation = "查看安全组规则列表")
    @GetMapping
    public BocloudResult<GridBean<SecurityGroupRule>> list(Pager pager,
                                                           @Value(Common.REQ_CONTEXT) RequestContext context) {
        return securityGroupRuleService.list(pager, context);
    }

    /**
     * 创建
     *
     * @param rule
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建安全组规则", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "安全组规则", operation = "创建安全组规则")
    @PostMapping
    public BocloudResult<List<SecurityGroupRule>> create(@RequestBody SecurityGroupRule rule,
                                                         @Value(Common.REQ_CONTEXT) RequestContext context) {
        return securityGroupRuleService.create(rule, context);
    }

    /**
     * 删除
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除安全组规则", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "安全组规则", operation = "删除安全组规则")
    @DeleteMapping(value = "/{id}")
    public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return securityGroupRuleService.remove(id, context);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询安全组规则详情", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "安全组规则", operation = "查看安全组规则详情")
    @GetMapping(value = "/{id}")
    public BocloudResult<SecurityGroupRule> detail(@PathVariable(value = Common.ID) Long id,
                                                   @Value(Common.REQ_CONTEXT) RequestContext context) {
        return securityGroupRuleService.detail(id, context);
    }

    /**
     * 批量删除安全组规则
     *
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "安全组规则批量删除", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "安全组规则", operation = "批量删除安全组规则")
    @DeleteMapping
    public BocloudResult batchRemove(@RequestBody List<Long> idArray,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        return securityGroupRuleService.batchRemove(idArray, context);
    }
}
