package com.bocloud.ims.service.resource.azure.slb;

import com.bocloud.cmp.driver.azure.slb.OutboundRuleDriver;
import com.bocloud.cmp.model.azure.slb.SlbModel;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
import com.bocloud.common.model.RequestContext;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.entity.resource.azure.slb.Slb;
import com.bocloud.ims.interfaces.resource.azure.slb.OutboundRuleService;
import com.bocloud.ims.repository.resource.CloudVendorRepository;
import com.bocloud.ims.repository.resource.azure.slb.SlbRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: OutboundRuleServiceImpl
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/5/15 16:37
 */
@Service
public class OutboundRuleServiceImpl implements OutboundRuleService {

    private static Logger logger = LoggerFactory.getLogger(OutboundRuleServiceImpl.class);

    @Autowired
    private CloudVendorRepository cloudVendorRepository;
    @Autowired
    private SlbRepository slbRepository;
    @Autowired
    private OutboundRuleDriver driver;

    @Override
    public BocloudResult<SlbModel> list(Long id, RequestContext context) {
        try {
            Slb slb = slbRepository.query(id);
            Assert.notNull(slb, "资源不存在");
            SlbModel slbModel = new SlbModel();
            slbModel.setName(slb.getName());
            slbModel.setUuid(slb.getUuid());
            slbModel.setResourceGroupName(slb.getResourceGroupName());
            CloudVendor vendor = cloudVendorRepository.query(slb.getVendorId());
            Assert.notNull(vendor, "平台不存在");
            BocloudResult result = driver.list(vendor.getUuid(), slb.getRegionId(), slbModel);
            return result;
        } catch (Exception e) {
            logger.error("list OutboundRule error:", e);
            return new BocloudResult(false, "查询出战规则失败:" + e.getMessage());
        }
    }
}