Skip to content

Commit

Permalink
migrate teams and recipients fields to responders
Browse files Browse the repository at this point in the history
  • Loading branch information
emelkomurcu committed May 23, 2019
1 parent 6d94efb commit fc7288b
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 19 deletions.
2 changes: 1 addition & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ dependencies {
compile 'org.codehaus.groovy:groovy-all:1.8.4'
compile 'log4j:log4j:1.2.16'
testCompile project(':test')
compile 'com.opsgenie.oas:opsgenie-sdk-swagger:1.0.1'
compile 'com.opsgenie.oas:opsgenie-sdk-swagger:1.0.10'
}
Original file line number Diff line number Diff line change
Expand Up @@ -285,25 +285,80 @@ public Map createAlert(Map params) throws Exception {
payload.setMessage(ScriptBridgeUtils.getAsString(params, OpsGenieClientConstants.API.MESSAGE));
payload.setPriority(ObjectUtils.defaultIfNull(CreateAlertPayload.PriorityEnum.fromValue(ScriptBridgeUtils.getAsString(params, OpsGenieClientConstants.API.PRIORITY)), payload.getPriority()));
payload.setTags(ScriptBridgeUtils.getAsStringList(params, OpsGenieClientConstants.API.TAGS));
List<TeamRecipient> teamRecipients;

List<Recipient> responders = getResponders(params);
List<Recipient> teamRecipients = getTeamRecipients(params);

if (responders != null && !responders.isEmpty()) {
payload.setResponders(responders);

} else {
payload.setResponders(teamRecipients);
}

List<Recipient> visibleToRecipients = ScriptBridgeUtils.getAsRecipientList(params, OpsGenieClientConstants.API.VISIBLE_TO);
payload.setVisibleTo(visibleToRecipients);

populateCommonParameters(payload, params);

return successToMap(alertApi.createAlert(payload));
}

private List<Recipient> getResponders(Map params) {
List<Recipient> responders = new ArrayList<Recipient>();

List<HashMap> responderEntries = ScriptBridgeUtils.getAsList(params, OpsGenieClientConstants.API.RESPONDERS);
if (responderEntries != null) {
for (HashMap responder : responderEntries) {
if (Recipient.TypeEnum.ESCALATION.getValue().equals(responder.get("type"))) {
EscalationRecipient escalationRecipient = new EscalationRecipient();
escalationRecipient.setName((String) responder.get("name"));
escalationRecipient.setId((String) responder.get("id"));
responders.add(escalationRecipient);
} else if (Recipient.TypeEnum.USER.getValue().equals(responder.get("type"))) {
UserRecipient userRecipient = new UserRecipient();
userRecipient.setUsername((String) responder.get("username"));
userRecipient.setId((String) responder.get("id"));
responders.add(userRecipient);
} else if (Recipient.TypeEnum.TEAM.getValue().equals(responder.get("type"))) {
TeamRecipient teamRecipient = new TeamRecipient();
teamRecipient.setName((String) responder.get("name"));
teamRecipient.setId((String) responder.get("id"));
responders.add(teamRecipient);
} else if (Recipient.TypeEnum.SCHEDULE.getValue().equals(responder.get("type"))) {
ScheduleRecipient scheduleRecipient = new ScheduleRecipient();
scheduleRecipient.setName((String) responder.get("name"));
scheduleRecipient.setId((String) responder.get("id"));
responders.add(scheduleRecipient);
}else if (Recipient.TypeEnum.GROUP.getValue().equals(responder.get("type"))) {
GroupRecipient groupRecipient = new GroupRecipient();
groupRecipient.setName((String) responder.get("name"));
groupRecipient.setId((String) responder.get("id"));
responders.add(groupRecipient);
}
}
}

return responders;
}

private List<Recipient> getTeamRecipients(Map params) throws Exception {
List<Recipient> teamRecipients;
List teamEntries = ScriptBridgeUtils.getAsList(params, OpsGenieClientConstants.API.TEAMS);
teamRecipients = new ArrayList<Recipient>();
if (teamEntries != null && !teamEntries.isEmpty() && teamEntries.get(0) instanceof String) {
teamRecipients = new ArrayList<TeamRecipient>();
for (Object teamName : teamEntries) {
TeamRecipient teamRecipient = new TeamRecipient();
teamRecipient.setName((String) teamName);
teamRecipients.add(teamRecipient);
}
} else {
teamRecipients = ScriptBridgeUtils.getAsObjectList(params, OpsGenieClientConstants.API.TEAMS, TeamRecipient.class);
List<TeamRecipient> teams = ScriptBridgeUtils.getAsObjectList(params, OpsGenieClientConstants.API.TEAMS, TeamRecipient.class);
if (teams != null) {
teamRecipients.addAll(teams);
}
}
payload.setTeams(teamRecipients);
List<Recipient> visibleToRecipients = ScriptBridgeUtils.getAsRecipientList(params, OpsGenieClientConstants.API.VISIBLE_TO);
payload.setVisibleTo(visibleToRecipients);

populateCommonParameters(payload, params);

return successToMap(alertApi.createAlert(payload));
return teamRecipients;
}


Expand Down Expand Up @@ -559,13 +614,13 @@ public Map enableAlertPolicy(Map params) throws Exception {
}
String policyId = ScriptBridgeUtils.getAsString(params, OpsGenieClientConstants.API.IDENTIFIER);

return successToMap(policyApi.enableAlertPolicy(policyId));
return successToMap(policyApi.enablePolicy(policyId, null));
}

public Map disableAlertPolicy(Map params) throws Exception {
String policyId = ScriptBridgeUtils.getAsString(params, OpsGenieClientConstants.API.IDENTIFIER);

return successToMap(policyApi.disableAlertPolicy(policyId));
return successToMap(policyApi.disablePolicy(policyId, null));
}

public Map enableIntegration(Map params) throws Exception {
Expand Down Expand Up @@ -689,9 +744,8 @@ public Map deleteTeam(Map params) throws Exception {
return successToMap(teamApi.deleteTeam(request));
}

public List<Map> listTeams(Map params) throws Exception {
List<String> expand = getExpand(params);
return beansToMap(teamApi.listTeams(expand).getData());
public List<Map> listTeams() throws Exception {
return beansToMap(teamApi.listTeams().getData());
}

public List<Map> listTeamLogs(Map params) throws Exception {
Expand Down Expand Up @@ -1127,7 +1181,7 @@ private static List<TeamMember> parseTeamMembers(Map params) throws Exception {
if (objectList != null) {
for (Map memberEntry : objectList) {
TeamMember teamMember = new TeamMember();
TeamMember.RoleEnum role = getEnumFromValue(TeamMember.RoleEnum.class, ScriptBridgeUtils.getAsString(memberEntry, OpsGenieClientConstants.API.ROLE));
String role = ScriptBridgeUtils.getAsString(memberEntry, OpsGenieClientConstants.API.ROLE);
if (role != null) {
teamMember.setRole(role);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ScriptProxyAlertTest {
}

@Test
public void testCreateAlertSuccessfully() throws Exception {
public void testCreateAlertSuccessfullyWithTeams() throws Exception {
SuccessResponse successResponse = CommonTestUtils.createGenericSuccessResponse();
alertApiMock.setGenericSuccessResponse(successResponse);

Expand Down Expand Up @@ -68,7 +68,62 @@ class ScriptProxyAlertTest {

assertEquals(params.actions, payload.getActions())
assertEquals(params.tags, payload.getTags())
assertEquals(expectedTeamsList, payload.getTeams())
assertEquals(expectedTeamsList, payload.getResponders())
assertEquals(params.details, payload.getDetails())
assertEquals(params.message, payload.getMessage())
assertEquals(params.description, payload.getDescription())
assertEquals(params.source, payload.getSource())
assertEquals(params.entity, payload.getEntity())
assertEquals(params.note, payload.getNote())
assertEquals(params.user, payload.getUser())
}

@Test
public void testCreateAlertSuccessfullyWithResponders() throws Exception {
SuccessResponse successResponse = CommonTestUtils.createGenericSuccessResponse();
alertApiMock.setGenericSuccessResponse(successResponse);

def params = [message : "my message",
description: "my description",
source : "source1", entity: "entity1", alias: "alias1",
note : "alert note",
tags : ["tag1", "tag2"], actions: ["action1", "action2"],
responders : [["name" : "team1", "type":"team"],["username" : "user1", "type":"user"],["id" : "esc1", "type":"escalation"],["name" : "sch1", "type":"schedule"],["name" : "grp1", "type":"group"],["name" : "invalid"]],
details : [param1: "value1", param2: "value2"]];

Map resp = scriptProxy.createAlert(params);

CommonTestUtils.assertGenericResponseWithoutData(resp)

assertEquals(1, alertApiMock.getExecutedRequests().size())

CreateAlertPayload payload = alertApiMock.getExecutedRequests()[0] as CreateAlertPayload
TeamRecipient team1 = new TeamRecipient();
team1.setName("team1");

UserRecipient userRecipient = new UserRecipient();
userRecipient.setUsername("user1");

EscalationRecipient escalationRecipient = new EscalationRecipient();
escalationRecipient.setId("esc1");

ScheduleRecipient scheduleRecipient = new ScheduleRecipient();
scheduleRecipient.setName("sch1");

GroupRecipient groupRecipient = new GroupRecipient();
groupRecipient.setName("grp1");

def listOfRecipients = [
team1,
userRecipient,
escalationRecipient,
scheduleRecipient,
groupRecipient
];

assertEquals(params.actions, payload.getActions())
assertEquals(params.tags, payload.getTags())
assertEquals(listOfRecipients, payload.getResponders())
assertEquals(params.details, payload.getDetails())
assertEquals(params.message, payload.getMessage())
assertEquals(params.description, payload.getDescription())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ interface API {
String TEAMS = "teams";
String TEAM_ID = "teamId";
String RECIPIENT = "recipient";
String RESPONDERS = "responders";
String IDENTIFIER_TYPE = "identifierType";
String SCHEDULE_IDENTIFIER_TYPE = "scheduleIdentifierType";
String SEARCH_IDENTIFIER = "searchIdentifier";
Expand Down

0 comments on commit fc7288b

Please sign in to comment.