|
|
package com.monitor.awstools.controller;
|
|
|
|
|
|
import com.monitor.awstools.constant.Constants;
|
|
|
import com.monitor.awstools.constant.JobType;
|
|
|
import com.monitor.awstools.constant.TaskStatus;
|
|
|
import com.monitor.awstools.constant.TaskType;
|
|
|
import com.monitor.awstools.job.AwsTask;
|
|
|
import com.monitor.awstools.model.*;
|
|
|
import com.monitor.awstools.service.AwsQueryService;
|
|
|
import com.monitor.awstools.service.TaskService;
|
|
|
import com.monitor.model.response.BaseResponse;
|
|
|
import com.sun.javafx.collections.MappingChange;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
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.RestController;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Created by yoho on 2016/8/30.
|
|
|
*/
|
|
|
|
|
|
@RestController
|
|
|
@RequestMapping(value = "/aws")
|
|
|
public class AwsToolController {
|
|
|
public static final Logger DEBUG = LoggerFactory.getLogger(AwsToolController.class);
|
|
|
|
|
|
@Autowired
|
|
|
AwsQueryService queryService;
|
|
|
|
|
|
@Autowired
|
|
|
TaskService taskService;
|
|
|
|
|
|
@RequestMapping(value = "/snapshot/check")
|
|
|
public BaseResponse checkSnapShot(@RequestBody AwsEbsReq task) {
|
|
|
List<M_SnapShot> snapShotList = queryService.querySnapShotList(task.getVolumeId());
|
|
|
|
|
|
BaseResponse response = new BaseResponse();
|
|
|
|
|
|
response.setData(snapShotList);
|
|
|
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* volume Id校验
|
|
|
*
|
|
|
* @param task
|
|
|
* @return
|
|
|
*/
|
|
|
@RequestMapping(value = "/volume/check")
|
|
|
public BaseResponse checkVolume(@RequestBody AwsEbsReq task) {
|
|
|
|
|
|
M_Volume m_volume = queryService.queryVolumeInfo(task.getVolumeId());
|
|
|
|
|
|
if (null == m_volume) {
|
|
|
return new BaseResponse(400, "未找到相关volume,请确认id...");
|
|
|
}
|
|
|
|
|
|
String instanceId = m_volume.getInstanceId();
|
|
|
|
|
|
AwsEbsResp awsEbsTaskResp = new AwsEbsResp();
|
|
|
|
|
|
if (StringUtils.isBlank(instanceId)) {
|
|
|
|
|
|
awsEbsTaskResp.setM_instance(new M_Instance());
|
|
|
|
|
|
awsEbsTaskResp.setM_volume(m_volume);
|
|
|
|
|
|
} else {
|
|
|
M_Instance m_instance = queryService.queryInstanceInfo(instanceId);
|
|
|
|
|
|
awsEbsTaskResp.setM_volume(m_volume);
|
|
|
|
|
|
awsEbsTaskResp.setM_instance(m_instance);
|
|
|
}
|
|
|
|
|
|
BaseResponse response = new BaseResponse();
|
|
|
|
|
|
response.setData(awsEbsTaskResp);
|
|
|
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 查询所有的task信息
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
@RequestMapping(value = "/task/all")
|
|
|
public BaseResponse queryTasks() {
|
|
|
|
|
|
List<AwsEbsTaskResp> awsEbsTaskRespList = new ArrayList<>();
|
|
|
|
|
|
List<AwsTask> awsTaskList = taskService.queryAllTasks();
|
|
|
|
|
|
for (AwsTask awsTask : awsTaskList) {
|
|
|
awsEbsTaskRespList.add(converAwsTask(awsTask));
|
|
|
}
|
|
|
BaseResponse baseResponse = new BaseResponse();
|
|
|
|
|
|
baseResponse.setData(awsEbsTaskRespList);
|
|
|
|
|
|
return baseResponse;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建task
|
|
|
*
|
|
|
* @param task
|
|
|
* @return
|
|
|
*/
|
|
|
@RequestMapping(value = "/task/create")
|
|
|
public BaseResponse createTask(@RequestBody AwsEbsReq task) {
|
|
|
Map<String, String> proMaps = convertProMap(task);
|
|
|
//插入创建时间
|
|
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy/MM/dd HH:mm");
|
|
|
|
|
|
proMaps.put(Constants.CREATETIME, simpleDateFormat.format(new Date()));
|
|
|
|
|
|
try {
|
|
|
taskService.addTask(proMaps.get(Constants.TASKNAME), proMaps.get(Constants.TASKGROUP), JobType.valueOf(proMaps.get(Constants.JOBTYPE)), proMaps);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
DEBUG.error("Failed to create task.... error {}", e);
|
|
|
|
|
|
return new BaseResponse(400, e.getMessage());
|
|
|
}
|
|
|
|
|
|
return new BaseResponse();
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 修改task
|
|
|
*
|
|
|
* @param task
|
|
|
* @return
|
|
|
*/
|
|
|
@RequestMapping(value = "/task/edit")
|
|
|
public BaseResponse editTask(@RequestBody AwsEbsReq task) {
|
|
|
Map<String, String> proMap = convertProMap(task);
|
|
|
|
|
|
try {
|
|
|
taskService.configTask(proMap.get(Constants.TASKNAME), proMap.get(Constants.TASKGROUP), proMap);
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
DEBUG.error("Failed to re-config task .... error {}", e);
|
|
|
|
|
|
return new BaseResponse(400, e.getMessage());
|
|
|
}
|
|
|
return new BaseResponse();
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 暂停task
|
|
|
*
|
|
|
* @param task
|
|
|
* @return
|
|
|
*/
|
|
|
@RequestMapping(value = "/task/stop")
|
|
|
public BaseResponse stopTask(@RequestBody AwsEbsReq task) {
|
|
|
try {
|
|
|
taskService.pauseTask(task.getTaskName(), Constants.GROUP);
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
DEBUG.error("Failed to stop task .... error {}", e);
|
|
|
|
|
|
return new BaseResponse(400, e.getMessage());
|
|
|
}
|
|
|
return new BaseResponse();
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 启动task
|
|
|
*
|
|
|
* @param task
|
|
|
* @return
|
|
|
*/
|
|
|
@RequestMapping(value = "/task/start")
|
|
|
public BaseResponse startTask(@RequestBody AwsEbsReq task) {
|
|
|
|
|
|
try {
|
|
|
taskService.resumeTask(task.getTaskName(), Constants.GROUP);
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
DEBUG.error("Failed to start task .... error {}", e);
|
|
|
|
|
|
return new BaseResponse(400, e.getMessage());
|
|
|
}
|
|
|
|
|
|
return new BaseResponse();
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 删除task
|
|
|
*
|
|
|
* @param task
|
|
|
* @return
|
|
|
*/
|
|
|
@RequestMapping(value = "/task/delete")
|
|
|
public BaseResponse deleteTask(@RequestBody AwsEbsReq task) {
|
|
|
try {
|
|
|
taskService.deleteTask(task.getTaskName(), Constants.GROUP);
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
DEBUG.error("Failed to start task .... error {}", e);
|
|
|
|
|
|
return new BaseResponse(400, e.getMessage());
|
|
|
}
|
|
|
|
|
|
return new BaseResponse();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询task
|
|
|
*
|
|
|
* @param task
|
|
|
* @return
|
|
|
*/
|
|
|
@RequestMapping(value = "/task/query")
|
|
|
public BaseResponse queryTask(@RequestBody AwsEbsReq task) {
|
|
|
AwsTask awsTask;
|
|
|
|
|
|
try {
|
|
|
awsTask = taskService.queryTask(task.getTaskName(), Constants.GROUP);
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
DEBUG.error("Failed to start task .... error {}", e);
|
|
|
|
|
|
return new BaseResponse(400, e.getMessage());
|
|
|
}
|
|
|
|
|
|
BaseResponse response = new BaseResponse();
|
|
|
|
|
|
response.setData(converAwsTask(awsTask));
|
|
|
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 查询task运行详情
|
|
|
*
|
|
|
* @param task
|
|
|
* @return
|
|
|
*/
|
|
|
public BaseResponse infoTask(@RequestBody AwsEbsReq task) {
|
|
|
try {
|
|
|
taskService.deleteTask(task.getTaskName(), Constants.GROUP);
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
DEBUG.error("Failed to start task .... error {}", e);
|
|
|
|
|
|
return new BaseResponse(400, e.getMessage());
|
|
|
}
|
|
|
|
|
|
return new BaseResponse();
|
|
|
}
|
|
|
|
|
|
|
|
|
private Map<String, String> convertProMap(AwsEbsReq awsEbsReq) {
|
|
|
Map<String, String> proMap = new HashMap<>();
|
|
|
|
|
|
proMap.put(Constants.TASKNAME, awsEbsReq.getTaskName());
|
|
|
|
|
|
proMap.put(Constants.TASKGROUP, Constants.GROUP);
|
|
|
|
|
|
JobType jobType;
|
|
|
|
|
|
switch (awsEbsReq.getTaskType()) {
|
|
|
case "0":
|
|
|
jobType = JobType.ONCE;
|
|
|
break;
|
|
|
case "1":
|
|
|
jobType = JobType.INTERVAL;
|
|
|
break;
|
|
|
case "2":
|
|
|
jobType = JobType.CRON;
|
|
|
break;
|
|
|
default:
|
|
|
jobType = JobType.ONCE;
|
|
|
break;
|
|
|
}
|
|
|
proMap.put(Constants.JOBTYPE, jobType.name());
|
|
|
|
|
|
proMap.put(Constants.EXPRESSION, awsEbsReq.getTaskExp());
|
|
|
|
|
|
proMap.put(Constants.VOLUME, awsEbsReq.getVolumeId());
|
|
|
|
|
|
proMap.put(Constants.DESCRPTION, awsEbsReq.getSanpShotDesc());
|
|
|
|
|
|
proMap.put(Constants.TASKSTATUS, TaskStatus.START.name());
|
|
|
|
|
|
proMap.put(Constants.TASKTYPE, TaskType.CREATESNAPSHOT.name());
|
|
|
|
|
|
return proMap;
|
|
|
}
|
|
|
|
|
|
public AwsEbsTaskResp converAwsTask(AwsTask awsTask) {
|
|
|
|
|
|
AwsEbsTaskResp ebsTaskResp = new AwsEbsTaskResp();
|
|
|
|
|
|
ebsTaskResp.setTaskName(awsTask.getJobName());
|
|
|
|
|
|
ebsTaskResp.setTaskType(String.valueOf(awsTask.getJobType().ordinal()));
|
|
|
|
|
|
ebsTaskResp.setTaskCtime(awsTask.getProMaps().get(Constants.CREATETIME));
|
|
|
|
|
|
ebsTaskResp.setTaskState(awsTask.getProMaps().get(Constants.TASKSTATUS));
|
|
|
|
|
|
ebsTaskResp.setSanpShotDesc(awsTask.getProMaps().get(Constants.DESCRPTION));
|
|
|
|
|
|
ebsTaskResp.setTaskExp(awsTask.getProMaps().get(Constants.EXPRESSION));
|
|
|
|
|
|
ebsTaskResp.setVolumeId(awsTask.getProMaps().get(Constants.VOLUME));
|
|
|
|
|
|
return ebsTaskResp;
|
|
|
}
|
|
|
} |
...
|
...
|
|