Authored by qinchao

aws和qcloud伸缩接口

Showing 12 changed files with 130 additions and 141 deletions
package com.monitor.awstools.service;
import com.alibaba.fastjson.JSON;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.autoscaling.AmazonAutoScaling;
import com.amazonaws.services.autoscaling.AmazonAutoScalingClient;
import com.amazonaws.services.autoscaling.model.*;
import com.amazonaws.services.autoscaling.model.AutoScalingGroup;
import com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest;
import com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult;
import com.amazonaws.services.autoscaling.model.TagDescription;
import com.amazonaws.services.ec2.model.*;
import com.amazonaws.services.ec2.model.Filter;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.Tag;
import com.monitor.awstools.comp.AWSClientComp;
import com.monitor.awstools.model.AwsModifyScalingGroupReq;
import com.monitor.awstools.model.M_Instance;
import com.monitor.awstools.model.M_SnapShot;
import com.monitor.awstools.model.M_Volume;
import com.monitor.cloudtools.model.CommAutoScalingGroup;
import com.monitor.cloudtools.model.CommAutoScalingInstances;
import com.monitor.cloudtools.util.DateTimeUtil;
import com.monitor.model.response.BaseResponse;
import com.monitor.qcloudtools.constant.QcloudConstant;
import com.monitor.qcloudtools.model.QcloudDescribeScalingActivitySet;
import com.monitor.qcloudtools.util.QcloudSdkUtil;
import com.qcloud.Module.Scaling;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
... ... @@ -60,12 +52,10 @@ public class AwsQueryService {
}
/**
/*
* 修改伸缩组信息
* @param req
* @return
*/
public BaseResponse modifyScalingGroup(AwsModifyScalingGroupReq req){
/*public BaseResponse modifyScalingGroup(AwsModifyScalingGroupReq req){
BaseResponse response = new BaseResponse();
if(req==null){
return null;
... ... @@ -87,7 +77,7 @@ public class AwsQueryService {
response.setMessage(e.getMessage());
}
return response;
}
}*/
public BaseResponse describeScalingActivity(String scalingGroupName) {
BaseResponse response = new BaseResponse();
... ...
package com.monitor.cloudtools.controller;
import com.monitor.awstools.service.AwsQueryService;
import com.monitor.cloudtools.model.AutoScalingInfoReq;
import com.monitor.cloudtools.model.CommAutoModifyScalingGroupReq;
import com.monitor.cloudtools.model.CommAutoScalingGroup;
import com.monitor.cloudtools.model.CommAutoScalingInstances;
import com.monitor.cloudtools.service.CloudToolService;
import com.monitor.model.response.BaseResponse;
import com.monitor.qcloudtools.service.QcloudToolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* 云伸缩组入口
* Created by xh on 2017/5/9.
... ... @@ -26,62 +18,11 @@ import java.util.List;
public class CloudToolController {
/**
* 腾讯云
*/
@Autowired
private QcloudToolService qCloudToolService;
/**
* 亚马逊云
*/
@Autowired
private AwsQueryService awsQueryService;
/**
* 通用
*/
@Autowired
private CloudToolService cloudToolService;
/**
* 修改伸缩组信息
* @param req 通用请求参数 云类型 1:AWS 2:QCLOUD
* @return
*/
@RequestMapping("/modifyScalingGroup")
@ResponseBody
public BaseResponse modifyScalingGroup(@RequestBody CommAutoModifyScalingGroupReq req){
BaseResponse response = null;
if(null == req){
return null;
}
if(1 == req.getCloudType()){
if(req.getAwsModifyScalingGroupReq() != null){
response=awsQueryService.modifyScalingGroup(req.getAwsModifyScalingGroupReq());
}
}else if(2 == req.getCloudType()){
if(req.getQCloudParms() != null){
response = qCloudToolService.modifyScalingGroup(req.getQCloudParms());
}
}else{
return null;
}
return response;
}
/**
* 腾讯云:根据实例id获取ip相关信息
* 亚马逊云:待定
* @param instanceIds 实例id集合
* @return
*/
@RequestMapping("/describeInstances")
@ResponseBody
public BaseResponse describeInstances(String instanceIds){
BaseResponse response = null;
response = qCloudToolService.describeInstances(instanceIds);
return response;
}
/**
* 项目发布后集成测试
... ...
... ... @@ -2,7 +2,6 @@ package com.monitor.qcloudtools.service;
import com.monitor.cloudtools.model.CommAutoScalingGroup;
import com.monitor.model.response.BaseResponse;
import com.monitor.qcloudtools.model.QcloudModifyScalingGroupReq;
import java.util.List;
... ... @@ -15,12 +14,6 @@ public interface QcloudToolService {
*/
List<CommAutoScalingGroup> getAutoScalingConfig(String equalsName);
/**
* 修改伸缩组信息
* @param req
* @return
*/
BaseResponse modifyScalingGroup(QcloudModifyScalingGroupReq req);
/**
* 修改伸缩组后的下一个流程是查询
... ...
... ... @@ -8,7 +8,6 @@ import com.monitor.model.response.BaseResponse;
import com.monitor.qcloudtools.constant.QcloudConstant;
import com.monitor.qcloudtools.model.*;
import com.monitor.qcloudtools.service.QcloudToolService;
import com.monitor.qcloudtools.util.CommQcloudUtil;
import com.monitor.qcloudtools.util.QcloudSdkUtil;
import com.qcloud.Module.Cvm;
import com.qcloud.Module.Scaling;
... ... @@ -114,50 +113,6 @@ public class QcloudToolServiceImpl implements QcloudToolService {
}
/**
* 修改腾讯云的伸缩组信息
* @return
*/
@Override
public BaseResponse modifyScalingGroup(QcloudModifyScalingGroupReq req) {
BaseResponse response = new BaseResponse();
if(null == req){
return null;
}
try{
TreeMap<String, Object> map = CommQcloudUtil.getTreeMapParam(req);
String rootJson = QcloudSdkUtil.getCommonData(new Scaling(), "GET", QcloudConstant.QCLOUD_API_MODIFY_SCALING_GROUP_FUN, map, true);
logger.info("QcloudToolServiceImpl-modifyScalingGroup"+map);
logger.info("QcloudToolServiceImpl-modifyScalingGroup"+rootJson);
if(StringUtils.isBlank(rootJson)){
return null;
}
QcloudResponse rootObj = JSON.parseObject(rootJson, QcloudResponse.class);
if(rootObj != null){
//我们系统只默认200是成功,腾讯云默认是0是成功
if(0 != rootObj.getCode()){
int errorCode=rootObj.getCode();
if(errorCode==200){
errorCode=201;
}
response.setCode(errorCode);
}
response.setMessage(rootObj.getMessage());
}
/*response = new BaseResponse();
response.setCode(200);
response.setMessage("因为匹配期望实例数,扩容2台");*/
}catch (Exception e){
logger.error(" - QcloudToolServiceImpl - modifyScalingGroup- error", e);
response.setCode(300);
response.setMessage(e.getMessage());
}
return response;
}
/**
* 修改伸缩组后的下一个流程是查询
* @param scalingGroupId 待查询的伸缩组Id
* @param startTime 开始时间,如果不填,则默认是当前时间前10秒之后发生的活动
... ...
... ... @@ -4,12 +4,14 @@ import com.yoho.ops.cmdb.aws.autoscaling.AutoScalingFetcher;
import com.yoho.ops.cmdb.domain.BaseResponse;
import com.yoho.ops.cmdb.models.AutoScalingGroup;
import com.yoho.ops.cmdb.models.AutoScalingHost;
import com.yoho.ops.cmdb.models.CommAutoModifyScalingGroupReq;
import com.yoho.ops.cmdb.qcloud.autoscaling.QcloudAutoScalingFetcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
... ... @@ -87,5 +89,31 @@ public class AutoScalingCtrl {
return response;
}
/**
* 修改伸缩组信息
* @param req 通用请求参数 云类型 1:AWS 2:QCLOUD
* @return
*/
@RequestMapping("/modifyScalingGroup")
@ResponseBody
public com.monitor.model.response.BaseResponse modifyScalingGroup(@RequestBody CommAutoModifyScalingGroupReq req){
com.monitor.model.response.BaseResponse response = null;
if(null == req){
return null;
}
if(1 == req.getCloudType()){
if(req.getAwsModifyScalingGroupReq() != null){
response=awsFetcher.modifyScalingGroup(req.getAwsModifyScalingGroupReq());
}
}else if(2 == req.getCloudType()){
if(req.getQCloudParms() != null){
response = qcloudFetcher.modifyScalingGroup(req.getQCloudParms());
}
}else{
return null;
}
return response;
}
}
... ...
package com.yoho.ops.cmdb.aws.autoscaling;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.autoscaling.AmazonAutoScaling;
import com.amazonaws.services.autoscaling.AmazonAutoScalingClient;
import com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest;
import com.amazonaws.services.autoscaling.model.TagDescription;
import com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest;
import com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupResult;
import com.monitor.model.response.BaseResponse;
import com.yoho.ops.cmdb.aws.AwsClientFactory;
import com.yoho.ops.cmdb.aws.ec2.AwsEc2Fetcher;
import com.yoho.ops.cmdb.models.AutoScalingGroup;
import com.yoho.ops.cmdb.models.AutoScalingHost;
import com.yoho.ops.cmdb.models.DataCenter;
import com.yoho.ops.cmdb.models.Host;
import com.yoho.ops.cmdb.models.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
... ... @@ -88,6 +91,35 @@ public class AutoScalingFetcher {
return null;
}
/**
* 修改伸缩组信息
* @param req
* @return
*/
public BaseResponse modifyScalingGroup(AwsModifyScalingGroupReq req){
BaseResponse response = new BaseResponse();
if(req==null){
return null;
}
try{
//AmazonAutoScaling client = new AmazonAutoScalingClient(awsClientComp.getAWSCredentials());
//client.setRegion(awsClientComp.getRegion(Regions.CN_NORTH_1));
UpdateAutoScalingGroupRequest request = new UpdateAutoScalingGroupRequest()
.withAutoScalingGroupName(req.getScalingGroupName())
.withMaxSize(req.getMaxSize()).withMinSize(req.getMinSize()).withDesiredCapacity(req.getDesiredCapacity());
UpdateAutoScalingGroupResult awsResponse = this.awsClientFactory.getAutoScaling().updateAutoScalingGroup(request);
if(awsResponse==null){
response.setCode(300);
response.setMessage("伸缩结果未知");
}
}catch (Exception e){
response.setCode(300);
response.setMessage(e.getMessage());
}
return response;
}
private String getGroupNameForTag(com.amazonaws.services.autoscaling.model.AutoScalingGroup awsAutoScalingGroup){
String name="";
List<TagDescription> tags=awsAutoScalingGroup.getTags();
... ...
package com.monitor.cloudtools.model;
package com.yoho.ops.cmdb.models;
import com.monitor.awstools.model.AwsModifyScalingGroupReq;
import com.monitor.qcloudtools.model.QcloudModifyScalingGroupReq;
import lombok.Data;
/**
... ...
... ... @@ -3,16 +3,16 @@ package com.yoho.ops.cmdb.qcloud;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.monitor.model.response.BaseResponse;
import com.qcloud.Module.Cvm;
import com.qcloud.Module.Scaling;
import com.yoho.ops.cmdb.models.AutoScalingGroup;
import com.yoho.ops.cmdb.models.AutoScalingHost;
import com.yoho.ops.cmdb.models.DataCenter;
import com.yoho.ops.cmdb.models.Host;
import com.yoho.ops.cmdb.models.*;
import com.yoho.ops.cmdb.qcloud.constant.QcloudConstant;
import com.yoho.ops.cmdb.qcloud.model.QcloudAutoScalingGroup;
import com.yoho.ops.cmdb.qcloud.model.QcloudInstanceSet;
import com.yoho.ops.cmdb.qcloud.model.QcloudResponse;
import com.yoho.ops.cmdb.qcloud.model.QcloudScalingInstancesSet;
import com.yoho.ops.cmdb.qcloud.util.CommQcloudUtil;
import com.yoho.ops.cmdb.qcloud.util.QcloudSdkUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
... ... @@ -112,6 +112,49 @@ public class QcloudClientFactory {
return null;
}
/**
* 修改腾讯云的伸缩组信息
* @return
*/
public BaseResponse modifyScalingGroup(QcloudModifyScalingGroupReq req) {
BaseResponse response = new BaseResponse();
if(null == req){
return null;
}
try{
TreeMap<String, Object> map = CommQcloudUtil.getTreeMapParam(req);
String rootJson = QcloudSdkUtil.getCommonData(new Scaling(), "GET", QcloudConstant.QCLOUD_API_MODIFY_SCALING_GROUP_FUN, map, true);
logger.info("QcloudToolServiceImpl-modifyScalingGroup"+map);
logger.info("QcloudToolServiceImpl-modifyScalingGroup"+rootJson);
if(StringUtils.isBlank(rootJson)){
return null;
}
QcloudResponse rootObj = JSON.parseObject(rootJson, QcloudResponse.class);
if(rootObj != null){
//我们系统只默认200是成功,腾讯云默认是0是成功
if(0 != rootObj.getCode()){
int errorCode=rootObj.getCode();
if(errorCode==200){
errorCode=201;
}
response.setCode(errorCode);
}
response.setMessage(rootObj.getMessage());
}
/*response = new BaseResponse();
response.setCode(200);
response.setMessage("因为匹配期望实例数,扩容2台");*/
}catch (Exception e){
logger.error(" - QcloudToolServiceImpl - modifyScalingGroup- error", e);
response.setCode(300);
response.setMessage(e.getMessage());
}
return response;
}
public static void main(String[] args){
QcloudClientFactory f=new QcloudClientFactory();
List<AutoScalingGroup> g=f.getAutoScalingGroup();
... ...
package com.yoho.ops.cmdb.qcloud.autoscaling;
import com.monitor.model.response.BaseResponse;
import com.yoho.ops.cmdb.models.AutoScalingGroup;
import com.yoho.ops.cmdb.models.Host;
import com.yoho.ops.cmdb.models.QcloudModifyScalingGroupReq;
import com.yoho.ops.cmdb.qcloud.QcloudClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
... ... @@ -30,6 +32,13 @@ public class QcloudAutoScalingFetcher {
return qcloudClientFactory.getAllInstancesSet();
}
public BaseResponse modifyScalingGroup(QcloudModifyScalingGroupReq req){
return qcloudClientFactory.modifyScalingGroup(req);
}
}
... ...
package com.monitor.qcloudtools.util;
package com.yoho.ops.cmdb.qcloud.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ...