Skip to content

Commit

Permalink
Merge pull request #2 from mbaris/improvement/import-contains
Browse files Browse the repository at this point in the history
Add 'is already exists' logic to routing rules and notification rules
  • Loading branch information
mbaris authored Aug 22, 2017
2 parents c3eeb43 + 4ce3b2f commit ee5df74
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
11 changes: 6 additions & 5 deletions src/main/java/com/opsgenie/tools/backup/ImportMain.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.opsgenie.tools.backup;

import com.beust.jcommander.JCommander;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.opsgenie.client.ApiClient;
import com.opsgenie.client.Configuration;
Expand Down Expand Up @@ -69,9 +69,9 @@ public static void main(String[] args) throws Exception {
defaultApiClient.setDebugging(debug);

ObjectMapper mapper = defaultApiClient.getJSON().getContext(Object.class);
mapper.addMixIn(Filter.TypeEnum.class, Ignored.class);
mapper.addMixIn(TimeRestrictionInterval.TypeEnum.class, Ignored.class);
mapper.addMixIn(Recipient.TypeEnum.class, Ignored.class);
mapper.addMixIn(Filter.class, Ignored.class);
mapper.addMixIn(TimeRestrictionInterval.class, Ignored.class);
mapper.addMixIn(Recipient.class, Ignored.class);

AccountApi accountApi = new AccountApi();
final GetAccountInfoResponse info = accountApi.getInfo();
Expand Down Expand Up @@ -241,7 +241,8 @@ public static ImportConfig extractRestoreConfig() throws IOException {
return null;
}

@JsonIgnoreType
abstract class Ignored {
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
String type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import com.opsgenie.client.model.*;
import com.opsgenie.tools.backup.BackupUtils;
import com.opsgenie.tools.backup.RestoreException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.util.List;

public class TeamRoutingRuleImporter extends BaseImporter<TeamRoutingRule> {

Expand Down Expand Up @@ -74,14 +73,38 @@ private void importRoutingRulesForTeam(Team team, File teamDirectory) throws Api
teamName = team.getName();
String[] files = BackupUtils.getFileListOf(teamDirectory);

List<TeamRoutingRule> teamRoutingRules = this.listRoutingRulesWithoutId();

for (String fileName : files) {
TeamRoutingRule bean = readEntity(teamDirectory.getName() + "/" + fileName);
if (bean != null) {
importEntity(bean);
TeamRoutingRule entity = readEntity(teamDirectory.getName() + "/" + fileName);
TeamRoutingRule cloneEntity = readEntity(teamDirectory.getName() + "/" + fileName);
unsetIds(cloneEntity);

if (entity != null && !teamRoutingRules.contains(cloneEntity)) {
importEntity(entity);
}
}
}

private List<TeamRoutingRule> listRoutingRulesWithoutId() throws ApiException {
ListTeamRoutingRulesRequest request = new ListTeamRoutingRulesRequest()
.identifier(teamName)
.teamIdentifierType(ListTeamRoutingRulesRequest.TeamIdentifierTypeEnum.NAME);
ListTeamRoutingRulesResponse response = teamRoutingRuleApi.listTeamRoutingRules(request);

for (TeamRoutingRule routingRule : response.getData()) {
unsetIds(routingRule);
}

return response.getData();
}

private void unsetIds(TeamRoutingRule routingRule) {
routingRule.setId(null);
routingRule.getNotify().setId(null);
routingRule.setIsDefault(false);
}

@Override
protected void addBean(TeamRoutingRule bean) throws ApiException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import com.opsgenie.client.model.*;
import com.opsgenie.tools.backup.BackupUtils;
import com.opsgenie.tools.backup.RestoreException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.util.ArrayList;
Expand Down Expand Up @@ -72,14 +70,51 @@ private User findUser(File notificationDirectory) throws ApiException {
private void importNotificationsForUser(User user, File notificationDirectory) throws ApiException {
username = user.getUsername();
String[] files = BackupUtils.getFileListOf(notificationDirectory);

List<NotificationRule> notificationRules = this.listNotificationRulesWithoutId();

for (String fileName : files) {
NotificationRule bean = readEntity(notificationDirectory.getName() + "/" + fileName);
if (bean != null) {
importEntity(bean);
NotificationRule entity = readEntity(notificationDirectory.getName() + "/" + fileName);
NotificationRule cloneEntity = readEntity(notificationDirectory.getName() + "/" + fileName);
unsetIds(cloneEntity);

if (entity != null && !notificationRules.contains(cloneEntity)) {
importEntity(entity);
}
}
}

private List<NotificationRule> listNotificationRulesWithoutId() throws ApiException {
ListNotificationRulesResponse listResponse = notificationRuleApi.listNotificationRules(username);
List<NotificationRuleMeta> ruleMetaList = listResponse.getData();

List<NotificationRule> result = new ArrayList<NotificationRule>();

for (NotificationRuleMeta meta : ruleMetaList) {
GetNotificationRuleRequest request = new GetNotificationRuleRequest()
.identifier(username)
.ruleId(meta.getId());

GetNotificationRuleResponse response = notificationRuleApi.getNotificationRule(request);

NotificationRule notificationRule = response.getData();
unsetIds(notificationRule);

result.add(notificationRule);
}

return result;
}

private void unsetIds(NotificationRule notificationRule) {
notificationRule.setId(null);

for (NotificationRuleStep step : notificationRule.getSteps()) {
step.setId(null);
step.getParent().setId(null);
}
}

@Override
protected void addBean(NotificationRule bean) throws ApiException {
CreateNotificationRulePayload payload = new CreateNotificationRulePayload();
Expand Down

0 comments on commit ee5df74

Please sign in to comment.