Skip to content

Commit

Permalink
Merge pull request #208 from tmobile/release/1.3
Browse files Browse the repository at this point in the history
Release/1.3
  • Loading branch information
kaykumar authored May 8, 2019
2 parents 743f925 + 85a12d8 commit 8cbcccc
Show file tree
Hide file tree
Showing 204 changed files with 22,023 additions and 12,465 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ private AdminConstants() {
public static final String RESOURCE_ACCESS_DENIED = "You don't have sufficient privileges to access this resource";
public static final String FAILED = "failed";
public static final String ENABLED_CAPS = "ENABLED";
public static final String RULE = "rule";
public static final String JOB = "job";
public static final String ENABLE = "enable";

public static final String DATE_FORMAT = "MM/dd/yyyy HH:mm";

Expand All @@ -47,6 +50,8 @@ private AdminConstants() {
public static final String UNEXPECTED_ERROR_OCCURRED = "Unexpected error occurred!!";
public static final String LAMBDA_LINKING_EXCEPTION = "Failed in linking the lambda function to the rule";
public static final String CLOUDWATCH_RULE_DELETION_FAILURE = "Failed in deleting the cloudwatch rule while disabling the rule";
public static final String CLOUDWATCH_RULE_DISABLE_FAILURE = "Failed in disabling the cloudwatch rule";
public static final String CLOUDWATCH_RULE_ENABLE_FAILURE = "Failed in enabling the cloudwatch rule";

public static final String DOMAIN_CREATION_SUCCESS = "Domain has been successfully created";
public static final String DOMAIN_NAME_EXITS = "Domain name already exits!!!";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.web.bind.annotation.RestController;

import com.tmobile.pacman.api.admin.domain.Response;
import com.tmobile.pacman.api.admin.repository.service.AdminService;
import com.tmobile.pacman.api.admin.repository.service.JobExecutionManagerService;
import com.tmobile.pacman.api.admin.repository.service.RuleService;
import com.tmobile.pacman.api.commons.utils.ResponseUtils;
Expand All @@ -56,6 +57,9 @@ public class AdminController {

@Autowired
private JobExecutionManagerService jobService;

@Autowired
private AdminService adminService;

/**
* API to enable disable rule or job
Expand Down Expand Up @@ -86,4 +90,36 @@ public ResponseEntity<Object> enableDisableRuleOrJob(@AuthenticationPrincipal Pr
return ResponseUtils.buildFailureResponse(new Exception(UNEXPECTED_ERROR_OCCURRED), exception.getMessage());
}
}

@ApiOperation(httpMethod = "POST", value = "API to shutdown all operations", response = Response.class, consumes = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping(path = "/operations", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> shutDownAllOperations(@AuthenticationPrincipal Principal user,
@ApiParam(value = "select operation ", required = true) @RequestParam("operation") Operation operation,
@ApiParam(value = "select job to perform operation ", required = true) @RequestParam("job") Job job) {
try {
return ResponseUtils.buildSucessResponse(adminService.shutDownAlloperations(operation.toString(),job.toString()));
} catch (Exception exception) {
log.error(UNEXPECTED_ERROR_OCCURRED, exception);
return ResponseUtils.buildFailureResponse(new Exception(UNEXPECTED_ERROR_OCCURRED), exception.getMessage());
}
}

@ApiOperation(httpMethod = "GET", value = "API to get status of all jobs", response = Response.class, produces = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping(path = "/system/status", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> statusOfSystem() {
try {
return ResponseUtils.buildSucessResponse(adminService.statusOfSystem());
} catch (Exception exception) {
log.error(UNEXPECTED_ERROR_OCCURRED, exception);
return ResponseUtils.buildFailureResponse(new Exception(UNEXPECTED_ERROR_OCCURRED), exception.getMessage());
}
}
}

enum Job {
all,job,rule;
}

enum Operation {
enable,disable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package com.tmobile.pacman.api.admin.repository.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.amazonaws.services.cloudwatchevents.model.DisableRuleRequest;
import com.amazonaws.services.cloudwatchevents.model.EnableRuleRequest;
import com.amazonaws.services.cloudwatchevents.model.ListRulesRequest;
import com.amazonaws.services.cloudwatchevents.model.ListRulesResult;
import com.amazonaws.services.cloudwatchevents.model.RuleState;
import com.tmobile.pacman.api.admin.common.AdminConstants;
import com.tmobile.pacman.api.admin.config.PacmanConfiguration;
import com.tmobile.pacman.api.admin.exceptions.PacManException;
import com.tmobile.pacman.api.admin.repository.JobExecutionManagerRepository;
import com.tmobile.pacman.api.admin.repository.RuleRepository;
import com.tmobile.pacman.api.admin.repository.model.JobExecutionManager;
import com.tmobile.pacman.api.admin.repository.model.Rule;
import com.tmobile.pacman.api.admin.service.AmazonClientBuilderService;

@Service
public class AdminService {

private static final Logger log = LoggerFactory.getLogger(AdminService.class);

@Autowired
private RuleRepository ruleRepository;

@Autowired
private JobExecutionManagerRepository jobRepository;

@Autowired
private AmazonClientBuilderService amazonClient;

@Autowired
private PacmanConfiguration config;

public String shutDownAlloperations(String operation, String job) throws PacManException {

String nextToken = null;
ListRulesResult listRulesResult ;
List<String> rules = new ArrayList<>();
do{
listRulesResult = amazonClient.getAmazonCloudWatchEvents(config.getRule().getLambda().getRegion()).listRules(new ListRulesRequest().withNextToken(nextToken));
rules.addAll(listRulesResult.getRules().parallelStream().map(rule->rule.getName()).collect(Collectors.toList()));
nextToken = listRulesResult.getNextToken();
}while(nextToken!=null);

if(operation.equals(AdminConstants.ENABLE)) {
if(job.equals(AdminConstants.RULE)) {
if(enableRules(rules)) {
return "All Rules has been sucessfully enabled";
}
} else if(job.equals(AdminConstants.JOB)) {
if(enableJobs(rules)) {
return "All Jobs has been sucessfully enabled";
}
} else {
if(enableRules(rules) && enableJobs(rules)) {
return "All Rules and Jobs has been sucessfully enabled";
}
}
throw new PacManException("Enabling operation failed");
} else {
if(job.equals(AdminConstants.RULE)) {
if(disableRules(rules)) {
return "All Rules has been sucessfully disabled";
}
} else if(job.equals(AdminConstants.JOB)) {
if(disableJobs(rules)) {
return "All Jobs has been sucessfully disabled";
}
} else {
if(disableRules(rules) && disableJobs(rules)) {
return "All Rules and Jobs has been sucessfully disabled";
}
}
throw new PacManException("Disabling operation failed");
}
}

private boolean disableRules(List<String> rules) {
List<Rule> ruleIds = ruleRepository.findAll();
try {
for(Rule rule : ruleIds) {
if(rules.contains(rule.getRuleUUID())) {
amazonClient.getAmazonCloudWatchEvents(config.getRule().getLambda().getRegion())
.disableRule(new DisableRuleRequest().withName(rule.getRuleUUID()));
rule.setStatus(RuleState.DISABLED.name());
ruleRepository.save(rule);
}
}
return true;
} catch(Exception e) {
log.error("Error in disable rules",e);
return false;
}

}

private boolean disableJobs(List<String> rules) {
List<JobExecutionManager> jobs = jobRepository.findAll();
try {
for(JobExecutionManager job : jobs) {
if(rules.contains(job.getJobUUID())) {
job.getJobUUID();
amazonClient.getAmazonCloudWatchEvents(config.getRule().getLambda().getRegion())
.disableRule(new DisableRuleRequest().withName(job.getJobUUID()));
job.setStatus(RuleState.DISABLED.name());
jobRepository.save(job);
}
}
return true;
} catch(Exception e) {
log.error("Error in disable jobs",e);
return false;
}
}

private boolean enableRules(List<String> rules) {
List<Rule> ruleIds = ruleRepository.findAll();
try {
for(Rule rule : ruleIds) {
if(rules.contains(rule.getRuleUUID())) {
amazonClient.getAmazonCloudWatchEvents(config.getRule().getLambda().getRegion())
.enableRule(new EnableRuleRequest().withName(rule.getRuleUUID()));
rule.setStatus(RuleState.ENABLED.name());
ruleRepository.save(rule);
}
}
return true;
} catch(Exception e) {
log.error("Error in enable rules",e);
return false;
}
}

private boolean enableJobs(List<String> rules) {
List<JobExecutionManager> jobs = jobRepository.findAll();
try {
for(JobExecutionManager job : jobs) {
if(rules.contains(job.getJobUUID())) {
amazonClient.getAmazonCloudWatchEvents(config.getRule().getLambda().getRegion())
.enableRule(new EnableRuleRequest().withName(job.getJobUUID()));
job.setStatus(RuleState.ENABLED.name());
jobRepository.save(job);
}
}
return true;
} catch(Exception e) {
log.error("Error in enable jobs",e);
return false;
}
}

public Map<String,String> statusOfSystem() throws PacManException{

Map<String,String> status = new HashMap<>();
try {
List<Rule> rules = ruleRepository.findAll();
List<JobExecutionManager> jobs = jobRepository.findAll();

boolean rulesEnabled = false;
boolean jobsEnabled = false;

for(Rule rule : rules) {
if(rule.getStatus().equals(RuleState.ENABLED.name())) {
rulesEnabled = true;
break;
}
}

for(JobExecutionManager job : jobs) {
if(job.getStatus().equals(RuleState.ENABLED.name())) {
jobsEnabled = true;
break;
}
}

if(rulesEnabled) {
status.put("rule", RuleState.ENABLED.name());
} else {
status.put("rule", RuleState.DISABLED.name());
}

if(jobsEnabled) {
status.put("job", RuleState.ENABLED.name());
} else {
status.put("job", RuleState.DISABLED.name());
}
return status;
} catch(Exception e) {
log.error("Error in fetching status of system",e);
throw new PacManException("Error in fetching the status of system");
}
}

}
Loading

0 comments on commit 8cbcccc

Please sign in to comment.