Authored by qinchao

aws和qcloud伸缩接口.。。。3333

1 package com.monitor.cmdb.ctrl; 1 package com.monitor.cmdb.ctrl;
2 2
  3 +import com.monitor.model.response.BaseResponse;
3 import com.yoho.ops.cmdb.aws.autoscaling.AutoScalingFetcher; 4 import com.yoho.ops.cmdb.aws.autoscaling.AutoScalingFetcher;
4 -import com.yoho.ops.cmdb.domain.BaseResponse;  
5 import com.yoho.ops.cmdb.models.AutoScalingGroup; 5 import com.yoho.ops.cmdb.models.AutoScalingGroup;
6 import com.yoho.ops.cmdb.models.AutoScalingHost; 6 import com.yoho.ops.cmdb.models.AutoScalingHost;
7 import com.yoho.ops.cmdb.models.CommAutoModifyScalingGroupReq; 7 import com.yoho.ops.cmdb.models.CommAutoModifyScalingGroupReq;
@@ -16,7 +16,9 @@ import org.springframework.web.bind.annotation.RequestMapping; @@ -16,7 +16,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
16 import org.springframework.web.bind.annotation.ResponseBody; 16 import org.springframework.web.bind.annotation.ResponseBody;
17 17
18 import java.util.ArrayList; 18 import java.util.ArrayList;
  19 +import java.util.HashMap;
19 import java.util.List; 20 import java.util.List;
  21 +import java.util.Map;
20 22
21 /** 23 /**
22 * 对外提供接口 24 * 对外提供接口
@@ -59,33 +61,68 @@ public class AutoScalingCtrl { @@ -59,33 +61,68 @@ public class AutoScalingCtrl {
59 } 61 }
60 62
61 /** 63 /**
62 - * 腾讯云:根据组id获取instanceIP  
63 - * 亚马逊云:待定 64 + * 获取伸缩组内机器的ips
64 * @param groupId 伸缩组id 65 * @param groupId 伸缩组id
65 * @return 66 * @return
66 */ 67 */
67 @RequestMapping("/getIpsByScalingGroupId") 68 @RequestMapping("/getIpsByScalingGroupId")
68 @ResponseBody 69 @ResponseBody
69 - public com.monitor.model.response.BaseResponse getIpsByScalingGroupId(int cloudType, String groupName , String groupId){  
70 - com.monitor.model.response.BaseResponse response = new com.monitor.model.response.BaseResponse();  
71 - List<String> ipList =new ArrayList<String>();; 70 + public BaseResponse getIpsByScalingGroupId(int cloudType, String groupName , String groupId){
  71 + BaseResponse response = new BaseResponse();
  72 + List<String> ipList =new ArrayList<String>();
  73 + AutoScalingGroup group=this.getGroup(cloudType, groupName , groupId);
  74 + if(group!=null&&group.getAutoScalingHosts()!=null){
  75 + for(AutoScalingHost host:group.getAutoScalingHosts()){
  76 + ipList.add(host.getIp());
  77 + }
  78 + }
  79 + response.setData(ipList);
  80 + return response;
  81 + }
  82 +
  83 + private AutoScalingGroup getGroup(int cloudType, String groupName , String groupId){
72 AutoScalingGroup group=null; 84 AutoScalingGroup group=null;
73 if(1==cloudType){ 85 if(1==cloudType){
74 if(groupName==null||groupName.length()<=0){ 86 if(groupName==null||groupName.length()<=0){
75 - response.setCode(300);  
76 - response.setMessage("aws伸缩组名字为空");  
77 - return response; 87 + return null;
78 } 88 }
79 group=awsFetcher.getAutoScalingByGroupName(groupName); 89 group=awsFetcher.getAutoScalingByGroupName(groupName);
80 }else if(2==cloudType){ 90 }else if(2==cloudType){
  91 + if(groupId==null||groupId.length()<=0){
  92 + return null;
  93 + }
81 group=qcloudFetcher.getAutoScalingGroupByScalingGroupId(groupId); 94 group=qcloudFetcher.getAutoScalingGroupByScalingGroupId(groupId);
82 } 95 }
  96 + return group;
  97 + }
  98 +
  99 + /**
  100 + * 获取伸缩组内的机器ips
  101 + * 并按照保护和非保护分类
  102 + * @param groupId 伸缩组id
  103 + * @return
  104 + */
  105 + @RequestMapping("/getIpsProtectedAndNoneByScalingGroupId")
  106 + @ResponseBody
  107 + public BaseResponse getIpsProtectedAndNoneByScalingGroupId(int cloudType, String groupName , String groupId){
  108 + BaseResponse response = new BaseResponse();
  109 + List<String> ipList_protected =new ArrayList<String>();
  110 + List<String> ipList_none_protected =new ArrayList<String>();
  111 + AutoScalingGroup group=this.getGroup(cloudType, groupName , groupId);
83 if(group!=null&&group.getAutoScalingHosts()!=null){ 112 if(group!=null&&group.getAutoScalingHosts()!=null){
84 for(AutoScalingHost host:group.getAutoScalingHosts()){ 113 for(AutoScalingHost host:group.getAutoScalingHosts()){
85 - ipList.add(host.getIp()); 114 + if(host.isProtected()){
  115 + ipList_protected.add(host.getIp());
  116 + }else{
  117 + ipList_none_protected.add(host.getIp());
  118 + }
  119 +
86 } 120 }
87 } 121 }
88 - response.setData(ipList); 122 + Map<String,List<String>> map=new HashMap<>();
  123 + map.put("protected",ipList_protected);
  124 + map.put("noneProtected",ipList_none_protected);
  125 + response.setData(map);
89 return response; 126 return response;
90 } 127 }
91 128
1 package com.monitor.cmdb.ctrl; 1 package com.monitor.cmdb.ctrl;
2 2
3 -import com.amazonaws.regions.Regions;  
4 -import com.amazonaws.services.autoscaling.AmazonAutoScaling;  
5 -import com.amazonaws.services.autoscaling.AmazonAutoScalingClient;  
6 import com.amazonaws.services.elasticloadbalancingv2.model.SetSecurityGroupsRequest; 3 import com.amazonaws.services.elasticloadbalancingv2.model.SetSecurityGroupsRequest;
  4 +import com.monitor.model.response.BaseResponse;
7 import com.yoho.ops.cmdb.aws.AwsClientFactory; 5 import com.yoho.ops.cmdb.aws.AwsClientFactory;
8 import com.yoho.ops.cmdb.aws.lb.AwsApplicationLoadBalanceFetcher; 6 import com.yoho.ops.cmdb.aws.lb.AwsApplicationLoadBalanceFetcher;
9 -import com.yoho.ops.cmdb.domain.BaseResponse;  
10 import com.yoho.ops.cmdb.models.LoadBalance; 7 import com.yoho.ops.cmdb.models.LoadBalance;
11 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.stereotype.Controller; 9 import org.springframework.stereotype.Controller;
1 -package com.yoho.ops.cmdb.domain;  
2 -  
3 -/**  
4 - * service返回信息对象  
5 - * @author hp  
6 - * 2014-03-11  
7 - */  
8 -public class BaseResponse<T> {  
9 -  
10 - private int code = 200;  
11 - private String message = "success";  
12 -  
13 - private T data;  
14 -  
15 - public BaseResponse() {}  
16 -  
17 - public BaseResponse(ErrorCode errorCode) {  
18 - this.code = errorCode.getCode();  
19 - this.message = errorCode.getMessage();  
20 - }  
21 -  
22 - public BaseResponse(int code, String message) {  
23 - this.code = code;  
24 - this.message = message;  
25 - }  
26 -  
27 - public BaseResponse(String message) {//Exception  
28 - this.code = 201;  
29 - this.message = message;  
30 - }  
31 -  
32 - public BaseResponse(T data) {  
33 - this.data = data;  
34 - }  
35 -  
36 - public BaseResponse(int code, String message, T data) {  
37 - this(code, message);  
38 - this.data = data;  
39 - }  
40 -  
41 - public int getCode() {  
42 - return code;  
43 - }  
44 -  
45 - public void setCode(int code) {  
46 - this.code = code;  
47 - }  
48 -  
49 - public String getMessage() {  
50 - return message;  
51 - }  
52 -  
53 - public void setMessage(String message) {  
54 - this.message = message;  
55 - }  
56 -  
57 - public T getData() {  
58 - return data;  
59 - }  
60 -  
61 - public void setData(T data) {  
62 - this.data = data;  
63 - }  
64 -  
65 -}  
66 -  
1 -/**  
2 - *  
3 - */  
4 -package com.yoho.ops.cmdb.domain;  
5 -  
6 -import lombok.Data;  
7 -  
8 -/**  
9 - * 描述:  
10 - *  
11 - * @author ping.huang  
12 - * 2016年3月31日  
13 - */  
14 -@Data  
15 -public class ErrorCode {  
16 -  
17 - public ErrorCode(int code, String message) {  
18 - super();  
19 - this.code = code;  
20 - this.message = message;  
21 - }  
22 -  
23 - private int code;  
24 - private String message;  
25 -  
26 -}