Authored by qinchao

aws伸缩

package com.monitor.awstools.model;
import lombok.Data;
/**
* aws接口:updateAutoScalingGroup
* 请求参数
*/
@Data
public class AwsModifyScalingGroupReq {
/**
* 必填
* 用户定义的伸缩组名称,若不传,表示不修改名称
*/
private String scalingGroupName;
/**
* 必填
* 修改后的伸缩组的最小伸缩数,即伸缩组内最小CVM实例数,范围0-30,且不大于maxSize。当伸缩组CVM实例数小于minSize时,AS会自动添加CVM实例,使得伸缩组内的当前实例数等于minSize
*/
private int minSize;
/**
* 必填
* 修改后的伸缩组的最大伸缩数,即伸缩组内最大CVM实例数,范围0-30,不小于minSize。当伸缩组CVM实例数大于maxSize时,AS会自动移出CVM实例,使得伸缩组内的当前实例数等于maxSize
*/
private int maxSize;
/**
* 不必填
* 期望实例数,期望实例数是指伸缩组当前合理的实例数量,大小介于最小实例数和最大实例数之间, 它的值可以被手动调整,当定时任务和告警伸缩任务触发时也会被调整。伸缩组会自动调整实际实例数, 使之与期望实例数相等
*/
private int desiredCapacity;
}
\ No newline at end of file
... ...
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.AutoScalingGroup;
import com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest;
import com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult;
import com.amazonaws.services.autoscaling.model.*;
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;
... ... @@ -42,13 +51,62 @@ public class AwsQueryService {
//获取AutoScaling组信息
commAutoScalingGroups = this.getAwsAutoScaling(equalsName);
} catch (Exception e) {
DEBUG.error(" - AwsController - autoScalingConfig - error", e);
DEBUG.error(" - AwsQueryService - autoScalingConfig - error", e);
}
return commAutoScalingGroups;
}
/**
* 修改伸缩组信息
* @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 = client.updateAutoScalingGroup(request);
if(awsResponse==null){
response.setCode(300);
response.setMessage("伸缩结果未知");
}
}catch (Exception e){
DEBUG.error(" - AwsQueryService - modifyScalingGroup- error", e);
response.setCode(300);
response.setMessage(e.getMessage());
}
return response;
}
public BaseResponse describeScalingActivity(String scalingGroupName) {
BaseResponse response = new BaseResponse();
if(org.apache.commons.lang3.StringUtils.isBlank(scalingGroupName)){
return null;
}
try{
List<CommAutoScalingGroup> commAutoScalingGroups = this.getAwsAutoScaling(scalingGroupName);
if(commAutoScalingGroups == null||commAutoScalingGroups.size()<=0){
return null;
}
response.setData(commAutoScalingGroups.get(0).getScalingInstancess());
}catch (Exception e){
DEBUG.error(" - AwsQueryService - describeScalingActivity- error", e);
response.setCode(300);
response.setMessage(e.getMessage());
}
return response;
}
/**
* 获取AutoScaling组的信息
* @return
*/
... ... @@ -57,17 +115,23 @@ public class AwsQueryService {
try {
AmazonAutoScaling client = new AmazonAutoScalingClient(awsClientComp.getAWSCredentials());
client.setRegion(awsClientComp.getRegion(Regions.CN_NORTH_1));
DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest()
.withAutoScalingGroupNames();
DescribeAutoScalingGroupsRequest request = null;
if(StringUtils.isNotBlank(equalsName)){
request = new DescribeAutoScalingGroupsRequest()
.withAutoScalingGroupNames(equalsName);
}else{
request = new DescribeAutoScalingGroupsRequest()
.withAutoScalingGroupNames();
}
DescribeAutoScalingGroupsResult result = client .describeAutoScalingGroups(request);
if(result == null || CollectionUtils.isEmpty(result.getAutoScalingGroups())){
if(result == null){
return null;
}
commGroups = new ArrayList<>();
CommAutoScalingGroup commGroup;
for(AutoScalingGroup group : result.getAutoScalingGroups()){
if(equalsName != null){
if(group.getAutoScalingGroupName().toLowerCase().indexOf(equalsName) == -1){
if(StringUtils.isNotBlank(equalsName)){
if(!group.getAutoScalingGroupName().toLowerCase().equals(equalsName)){
continue;
}
}
... ...
... ... @@ -3,6 +3,7 @@ package com.monitor.cloudtools.controller;
import com.monitor.awstools.service.AwsQueryService;
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;
... ... @@ -12,6 +13,7 @@ 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;
/**
... ... @@ -76,7 +78,9 @@ public class CloudToolController {
return null;
}
if(1 == req.getCloudType()){
return null;
if(req.getAwsModifyScalingGroupReq() != null){
response=awsQueryService.modifyScalingGroup(req.getAwsModifyScalingGroupReq());
}
}else if(2 == req.getCloudType()){
if(req.getQCloudParms() != null){
response = qCloudToolService.modifyScalingGroup(req.getQCloudParms());
... ... @@ -90,15 +94,34 @@ public class CloudToolController {
/**
* 腾讯云:修改伸缩组后的下一个流程是查询伸缩活动
* 亚马逊云:修改伸缩组后的下一个流程待定,因为不清楚
* @param scalingGroupName 待查询的伸缩组名字,aws要求
* @param scalingGroupId 待查询的伸缩组Id
* @param startTime 开始时间,如果不填,则默认是当前时间前10秒之后发生的活动
* @return
*/
@RequestMapping("/describeScalingActivity")
@ResponseBody
public BaseResponse describeScalingActivity(String scalingGroupId, String startTime){
BaseResponse response = null;
response = qCloudToolService.describeScalingActivity(scalingGroupId, startTime);
public BaseResponse describeScalingActivity(int cloudType,String scalingGroupName,String scalingGroupId, String startTime){
/*BaseResponse response = null;
if(1==cloudType){
response = awsQueryService.describeScalingActivity(scalingGroupName);
}else if(2==cloudType){
response = qCloudToolService.describeScalingActivity(scalingGroupId, startTime);
}*/
BaseResponse response;
List<CommAutoScalingGroup> autoScalingGroups = null;
if(1 == cloudType){
autoScalingGroups = awsQueryService.autoScalingConfig(scalingGroupName);
}else if(2 == cloudType){
autoScalingGroups = qCloudToolService.getAutoScalingConfig(scalingGroupName);
}else{
return null;
}
response = new BaseResponse();
response.setData(autoScalingGroups);
return response;
}
... ... @@ -110,9 +133,20 @@ public class CloudToolController {
*/
@RequestMapping("/getIpsByScalingGroupId")
@ResponseBody
public BaseResponse getIpsByScalingGroupId(String groupId){
public BaseResponse getIpsByScalingGroupId(int cloudType,String groupName ,String groupId){
BaseResponse response = new BaseResponse();
List<String> ipList = qCloudToolService.getIpsByScalingGroupId(groupId);
List<String> ipList = null;
if(1==cloudType){
List<CommAutoScalingGroup> autoScalingGroups = awsQueryService.autoScalingConfig(groupName);
if(autoScalingGroups!=null&&autoScalingGroups.get(0)!=null&&autoScalingGroups.get(0).getScalingInstancess()!=null){
ipList=new ArrayList<String>();
for(CommAutoScalingInstances ins:autoScalingGroups.get(0).getScalingInstancess()){
ipList.add(ins.getIp());
}
}
}else if(2==cloudType){
ipList = qCloudToolService.getIpsByScalingGroupId(groupId);
}
response.setData(ipList);
return response;
}
... ...
package com.monitor.cloudtools.model;
import com.monitor.awstools.model.AwsModifyScalingGroupReq;
import com.monitor.qcloudtools.model.QcloudModifyScalingGroupReq;
import lombok.Data;
... ... @@ -23,4 +24,5 @@ public class CommAutoModifyScalingGroupReq {
private QcloudModifyScalingGroupReq qCloudParms;
//亚马逊云
private AwsModifyScalingGroupReq awsModifyScalingGroupReq;
}
\ No newline at end of file
... ...