Skip to content
This repository has been archived by the owner on Mar 4, 2021. It is now read-only.

Allows overriding the SimpleDB region #307

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/main/java/com/netflix/simianarmy/aws/SimpleDBRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.amazonaws.services.simpledb.model.ReplaceableAttribute;
import com.amazonaws.services.simpledb.model.SelectRequest;
import com.amazonaws.services.simpledb.model.SelectResult;
import com.amazonaws.regions.Region;
import com.netflix.simianarmy.EventType;
import com.netflix.simianarmy.MonkeyRecorder;
import com.netflix.simianarmy.MonkeyType;
Expand All @@ -58,7 +59,8 @@ public class SimpleDBRecorder implements MonkeyRecorder {

private final AmazonSimpleDB simpleDBClient;

private final String region;
/** The region. */
private final Region region;

/** The domain. */
private final String domain;
Expand Down Expand Up @@ -96,14 +98,18 @@ private enum Keys {
*
* @param awsClient
* the AWS client
* @param region
* the AWS region
* @param domain
* the domain
*/
public SimpleDBRecorder(AWSClient awsClient, String domain) {
public SimpleDBRecorder(AWSClient awsClient, Region region, String domain) {
Validate.notNull(awsClient);
Validate.notNull(region);
Validate.notNull(domain);
this.simpleDBClient = awsClient.sdbClient();
this.region = awsClient.region();
this.simpleDBClient.setRegion(region);
this.region = region;
this.domain = domain;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.amazonaws.services.simpledb.model.ReplaceableAttribute;
import com.amazonaws.services.simpledb.model.SelectRequest;
import com.amazonaws.services.simpledb.model.SelectResult;
import com.amazonaws.regions.Region;
import com.google.common.collect.Lists;
import com.netflix.simianarmy.client.aws.AWSClient;
import com.netflix.simianarmy.conformity.Cluster;
Expand All @@ -47,6 +48,9 @@ public class SimpleDBConformityClusterTracker implements ConformityClusterTracke
/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleDBConformityClusterTracker.class);

/** The region. */
private final Region region;

/** The domain. */
private final String domain;

Expand All @@ -60,14 +64,19 @@ public class SimpleDBConformityClusterTracker implements ConformityClusterTracke
*
* @param awsClient
* the AWS Client
* @param region
* the AWS region
* @param domain
* the domain
*/
public SimpleDBConformityClusterTracker(AWSClient awsClient, String domain) {
public SimpleDBConformityClusterTracker(AWSClient awsClient, Region region, String domain) {
Validate.notNull(awsClient);
Validate.notNull(region);
Validate.notNull(domain);
this.region = region;
this.domain = domain;
this.simpleDBClient = awsClient.sdbClient();
this.simpleDBClient.setRegion(region);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.amazonaws.services.simpledb.AmazonSimpleDB;
import com.amazonaws.services.simpledb.model.*;
import com.amazonaws.regions.Region;
import com.netflix.simianarmy.Resource;
import com.netflix.simianarmy.Resource.CleanupState;
import com.netflix.simianarmy.ResourceType;
Expand All @@ -42,6 +43,9 @@ public class SimpleDBJanitorResourceTracker implements JanitorResourceTracker {
/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleDBJanitorResourceTracker.class);

/** The region. */
private final Region region;

/** The domain. */
private final String domain;

Expand All @@ -53,12 +57,16 @@ public class SimpleDBJanitorResourceTracker implements JanitorResourceTracker {
*
* @param awsClient
* the AWS Client
* @param region
* the AWS region
* @param domain
* the domain
*/
public SimpleDBJanitorResourceTracker(AWSClient awsClient, String domain) {
public SimpleDBJanitorResourceTracker(AWSClient awsClient, Region region, String domain) {
this.region = region;
this.domain = domain;
this.simpleDBClient = awsClient.sdbClient();
this.simpleDBClient.setRegion(region);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,18 @@ private void createRecorder() {
setRecorder(rdsRecorder);
} else if (recorderClass == null || recorderClass.equals(SimpleDBRecorder.class)) {
String domain = config.getStrOrElse("simianarmy.recorder.sdb.domain", "SIMIAN_ARMY");
String region = config.getStr("simianarmy.recorder.sdb.region");
if (region == null) {
region = "us-east-1";
Region currentRegion = Regions.getCurrentRegion();

if (currentRegion != null) {
region = currentRegion.getName();
}
}
Region awsRegion = Region.getRegion(Regions.fromName(region));
if (client != null) {
SimpleDBRecorder simpleDbRecorder = new SimpleDBRecorder(client, domain);
SimpleDBRecorder simpleDbRecorder = new SimpleDBRecorder(client, awsRegion, domain);
simpleDbRecorder.init();
setRecorder(simpleDbRecorder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public BasicConformityMonkeyContext() {

LOGGER.info(String.format("Conformity Monkey is running in: %s", regions));

String sdbRegion = configuration().getStr("simianarmy.conformity.sdb.region");
if (sdbRegion == null) {
sdbRegion = awsClient().region();
}
String sdbDomain = configuration().getStrOrElse("simianarmy.conformity.sdb.domain", "SIMIAN_ARMY");

String dbDriver = configuration().getStr("simianarmy.recorder.db.driver");
Expand All @@ -109,8 +113,9 @@ public BasicConformityMonkeyContext() {
String dbUrl = configuration().getStr("simianarmy.recorder.db.url");
String dbTable = configuration().getStr("simianarmy.conformity.resources.db.table");

if (dbDriver == null) {
clusterTracker = new SimpleDBConformityClusterTracker(awsClient(), sdbDomain);
if (dbDriver == null) {
Region awsRegion = Region.getRegion(Regions.fromName(sdbRegion));
clusterTracker = new SimpleDBConformityClusterTracker(awsClient(), awsRegion, sdbDomain);
} else {
RDSConformityClusterTracker rdsClusterTracker = new RDSConformityClusterTracker(dbDriver, dbUser, dbPass, dbUrl, dbTable);
rdsClusterTracker.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public BasicJanitorMonkeyContext() {
monkeyRegion = region();
monkeyCalendar = calendar();

String resourceRegion = configuration().getStr("simianarmy.janitor.resources.sdb.region");
if (resourceRegion == null) {
resourceRegion = awsClient().region();
}
String resourceDomain = configuration().getStrOrElse("simianarmy.janitor.resources.sdb.domain", "SIMIAN_ARMY");

Set<String> enabledResourceSet = getEnabledResourceSet();
Expand All @@ -105,8 +109,9 @@ public BasicJanitorMonkeyContext() {
String dbUrl = configuration().getStr("simianarmy.recorder.db.url");
String dbTable = configuration().getStr("simianarmy.janitor.resources.db.table");

if (dbDriver == null) {
janitorResourceTracker = new SimpleDBJanitorResourceTracker(awsClient(), resourceDomain);
if (dbDriver == null) {
Region awsRegion = Region.getRegion(Regions.fromName(resourceRegion));
janitorResourceTracker = new SimpleDBJanitorResourceTracker(awsClient(), awsRegion, resourceDomain);
} else {
RDSJanitorResourceTracker rdsTracker = new RDSJanitorResourceTracker(dbDriver, dbUser, dbPass, dbUrl, dbTable);
rdsTracker.init();
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/conformity.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ simianarmy.calendar.timezone = America/Los_Angeles
simianarmy.conformity.notification.openHour = 0
simianarmy.conformity.notification.closeHour = 24

#simianarmy.conformity.sdb.region = us-east-1
simianarmy.conformity.sdb.domain = SIMIAN_ARMY

# The property below needs to be a valid email address to receive the summary email of Conformity Monkey
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/janitor.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ simianarmy.janitor.enabled = true
# Don't allow Janitor Monkey to change resources (dryrun mode)
simianarmy.janitor.leashed = true

# The SDB region for storing the resources managed by the Janitor Monkey.
#simianarmy.janitor.resources.sdb.region = us-east-1

# The SDB domain for storing the resources managed by the Janitor Monkey.
simianarmy.janitor.resources.sdb.domain = SIMIAN_ARMY

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/simianarmy.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# see documentation at:
# https://github.com/Netflix/SimianArmy/wiki/Configuration

#simianarmy.recorder.sdb.region = us-east-1
simianarmy.recorder.sdb.domain = SIMIAN_ARMY

# If using a non-SimbleDB recorder (LocalDB), these settings tweak defaults.
Expand Down
18 changes: 10 additions & 8 deletions src/test/java/com/netflix/simianarmy/aws/TestSimpleDBRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import com.amazonaws.services.simpledb.model.ReplaceableAttribute;
import com.amazonaws.services.simpledb.model.SelectRequest;
import com.amazonaws.services.simpledb.model.SelectResult;
import com.amazonaws.regions.Regions;
import com.amazonaws.regions.Region;
import com.netflix.simianarmy.EventType;
import com.netflix.simianarmy.MonkeyType;
import com.netflix.simianarmy.client.aws.AWSClient;
Expand All @@ -54,12 +56,12 @@ private static AWSClient makeMockAWSClient() {
AmazonSimpleDB sdbMock = mock(AmazonSimpleDB.class);
AWSClient awsClient = mock(AWSClient.class);
when(awsClient.sdbClient()).thenReturn(sdbMock);
when(awsClient.region()).thenReturn("region");
when(awsClient.region()).thenReturn("us-east-1");
return awsClient;
}

public TestSimpleDBRecorder() {
super(makeMockAWSClient(), "DOMAIN");
super(makeMockAWSClient(), Region.getRegion(Regions.fromName("us-east-1")), "DOMAIN");
sdbMock = super.sdbClient();
}

Expand Down Expand Up @@ -95,7 +97,7 @@ public enum EventTypes implements EventType {
public void testRecordEvent() {
ArgumentCaptor<PutAttributesRequest> arg = ArgumentCaptor.forClass(PutAttributesRequest.class);

Event evt = newEvent(Type.MONKEY, EventTypes.EVENT, "region", "testId");
Event evt = newEvent(Type.MONKEY, EventTypes.EVENT, "us-east-1", "testId");
evt.addField("field1", "value1");
evt.addField("field2", "value2");
// this will be ignored as it conflicts with reserved key
Expand All @@ -107,15 +109,15 @@ public void testRecordEvent() {

PutAttributesRequest req = arg.getValue();
Assert.assertEquals(req.getDomainName(), "DOMAIN");
Assert.assertEquals(req.getItemName(), "MONKEY-testId-region-" + evt.eventTime().getTime());
Assert.assertEquals(req.getItemName(), "MONKEY-testId-us-east-1-" + evt.eventTime().getTime());
Map<String, String> map = new HashMap<String, String>();
for (ReplaceableAttribute attr : req.getAttributes()) {
map.put(attr.getName(), attr.getValue());
}

Assert.assertEquals(map.remove("id"), "testId");
Assert.assertEquals(map.remove("eventTime"), String.valueOf(evt.eventTime().getTime()));
Assert.assertEquals(map.remove("region"), "region");
Assert.assertEquals(map.remove("region"), "us-east-1");
Assert.assertEquals(map.remove("recordType"), "MonkeyEvent");
Assert.assertEquals(map.remove("monkeyType"), "MONKEY|com.netflix.simianarmy.aws.TestSimpleDBRecorder$Type");
Assert.assertEquals(map.remove("eventType"),
Expand All @@ -130,14 +132,14 @@ private SelectResult mkSelectResult(String id) {
List<Attribute> attrs = new LinkedList<Attribute>();
attrs.add(new Attribute("id", id));
attrs.add(new Attribute("eventTime", "1330538400000"));
attrs.add(new Attribute("region", "region"));
attrs.add(new Attribute("region", "us-east-1"));
attrs.add(new Attribute("recordType", "MonkeyEvent"));
attrs.add(new Attribute("monkeyType", "MONKEY|com.netflix.simianarmy.aws.TestSimpleDBRecorder$Type"));
attrs.add(new Attribute("eventType", "EVENT|com.netflix.simianarmy.aws.TestSimpleDBRecorder$EventTypes"));
attrs.add(new Attribute("field1", "value1"));
attrs.add(new Attribute("field2", "value2"));
item.setAttributes(attrs);
item.setName("MONKEY-" + id + "-region");
item.setName("MONKEY-" + id + "-us-east-1");
SelectResult result = new SelectResult();
result.setItems(Arrays.asList(item));
return result;
Expand All @@ -161,7 +163,7 @@ public void testFindEvent() {
verify(sdbMock, times(2)).select(arg.capture());
SelectRequest req = arg.getValue();
StringBuilder sb = new StringBuilder();
sb.append("select * from `DOMAIN` where region = 'region'");
sb.append("select * from `DOMAIN` where region = 'us-east-1'");
sb.append(" and instanceId = 'testId1'");

Assert.assertEquals(req.getSelectExpression(), sb.toString() + " and eventTime > '0' order by eventTime desc");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import com.amazonaws.services.simpledb.model.ReplaceableAttribute;
import com.amazonaws.services.simpledb.model.SelectRequest;
import com.amazonaws.services.simpledb.model.SelectResult;
import com.amazonaws.regions.Regions;
import com.amazonaws.regions.Region;
import com.netflix.simianarmy.Resource;
import com.netflix.simianarmy.aws.AWSResource;
import com.netflix.simianarmy.aws.AWSResourceType;
Expand All @@ -56,11 +58,12 @@ private static AWSClient makeMockAWSClient() {
AmazonSimpleDB sdbMock = mock(AmazonSimpleDB.class);
AWSClient awsClient = mock(AWSClient.class);
when(awsClient.sdbClient()).thenReturn(sdbMock);
when(awsClient.region()).thenReturn("us-east-1");
return awsClient;
}

public TestSimpleDBJanitorResourceTracker() {
super(makeMockAWSClient(), "DOMAIN");
super(makeMockAWSClient(), Region.getRegion(Regions.fromName("us-east-1")), "DOMAIN");
sdbMock = super.getSimpleDBClient();
}

Expand Down