From ddc6955931eefb8b4ea1d59ed9175b790683a64a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Neuh=C3=A4user?= Date: Wed, 20 Sep 2017 18:12:44 +0200 Subject: [PATCH] Allows overriding the SimpleDB region --- .../simianarmy/aws/SimpleDBRecorder.java | 12 +++++++++--- .../SimpleDBConformityClusterTracker.java | 11 ++++++++++- .../SimpleDBJanitorResourceTracker.java | 10 +++++++++- .../basic/BasicSimianArmyContext.java | 12 +++++++++++- .../BasicConformityMonkeyContext.java | 9 +++++++-- .../janitor/BasicJanitorMonkeyContext.java | 9 +++++++-- src/main/resources/conformity.properties | 1 + src/main/resources/janitor.properties | 3 +++ src/main/resources/simianarmy.properties | 1 + .../simianarmy/aws/TestSimpleDBRecorder.java | 18 ++++++++++-------- .../TestSimpleDBJanitorResourceTracker.java | 5 ++++- 11 files changed, 72 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/netflix/simianarmy/aws/SimpleDBRecorder.java b/src/main/java/com/netflix/simianarmy/aws/SimpleDBRecorder.java index 06120a9e..9f2651aa 100644 --- a/src/main/java/com/netflix/simianarmy/aws/SimpleDBRecorder.java +++ b/src/main/java/com/netflix/simianarmy/aws/SimpleDBRecorder.java @@ -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; @@ -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; @@ -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; } diff --git a/src/main/java/com/netflix/simianarmy/aws/conformity/SimpleDBConformityClusterTracker.java b/src/main/java/com/netflix/simianarmy/aws/conformity/SimpleDBConformityClusterTracker.java index d08cf7bd..a056bb26 100644 --- a/src/main/java/com/netflix/simianarmy/aws/conformity/SimpleDBConformityClusterTracker.java +++ b/src/main/java/com/netflix/simianarmy/aws/conformity/SimpleDBConformityClusterTracker.java @@ -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; @@ -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; @@ -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); } /** diff --git a/src/main/java/com/netflix/simianarmy/aws/janitor/SimpleDBJanitorResourceTracker.java b/src/main/java/com/netflix/simianarmy/aws/janitor/SimpleDBJanitorResourceTracker.java index 653ee5e4..40d1bd82 100644 --- a/src/main/java/com/netflix/simianarmy/aws/janitor/SimpleDBJanitorResourceTracker.java +++ b/src/main/java/com/netflix/simianarmy/aws/janitor/SimpleDBJanitorResourceTracker.java @@ -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; @@ -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; @@ -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); } /** diff --git a/src/main/java/com/netflix/simianarmy/basic/BasicSimianArmyContext.java b/src/main/java/com/netflix/simianarmy/basic/BasicSimianArmyContext.java index 04b1a549..a228765e 100644 --- a/src/main/java/com/netflix/simianarmy/basic/BasicSimianArmyContext.java +++ b/src/main/java/com/netflix/simianarmy/basic/BasicSimianArmyContext.java @@ -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); } diff --git a/src/main/java/com/netflix/simianarmy/basic/conformity/BasicConformityMonkeyContext.java b/src/main/java/com/netflix/simianarmy/basic/conformity/BasicConformityMonkeyContext.java index 43e5e679..db64bfb9 100644 --- a/src/main/java/com/netflix/simianarmy/basic/conformity/BasicConformityMonkeyContext.java +++ b/src/main/java/com/netflix/simianarmy/basic/conformity/BasicConformityMonkeyContext.java @@ -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"); @@ -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(); diff --git a/src/main/java/com/netflix/simianarmy/basic/janitor/BasicJanitorMonkeyContext.java b/src/main/java/com/netflix/simianarmy/basic/janitor/BasicJanitorMonkeyContext.java index 34ce84de..125aa83f 100644 --- a/src/main/java/com/netflix/simianarmy/basic/janitor/BasicJanitorMonkeyContext.java +++ b/src/main/java/com/netflix/simianarmy/basic/janitor/BasicJanitorMonkeyContext.java @@ -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 enabledResourceSet = getEnabledResourceSet(); @@ -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(); diff --git a/src/main/resources/conformity.properties b/src/main/resources/conformity.properties index fb5e908f..3e1dd7c1 100644 --- a/src/main/resources/conformity.properties +++ b/src/main/resources/conformity.properties @@ -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 diff --git a/src/main/resources/janitor.properties b/src/main/resources/janitor.properties index 248b047a..1b7c1b71 100644 --- a/src/main/resources/janitor.properties +++ b/src/main/resources/janitor.properties @@ -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 diff --git a/src/main/resources/simianarmy.properties b/src/main/resources/simianarmy.properties index f99878d1..e16c2853 100644 --- a/src/main/resources/simianarmy.properties +++ b/src/main/resources/simianarmy.properties @@ -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. diff --git a/src/test/java/com/netflix/simianarmy/aws/TestSimpleDBRecorder.java b/src/test/java/com/netflix/simianarmy/aws/TestSimpleDBRecorder.java index 6f3833a6..90d65ad7 100644 --- a/src/test/java/com/netflix/simianarmy/aws/TestSimpleDBRecorder.java +++ b/src/test/java/com/netflix/simianarmy/aws/TestSimpleDBRecorder.java @@ -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; @@ -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(); } @@ -95,7 +97,7 @@ public enum EventTypes implements EventType { public void testRecordEvent() { ArgumentCaptor 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 @@ -107,7 +109,7 @@ 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 map = new HashMap(); for (ReplaceableAttribute attr : req.getAttributes()) { map.put(attr.getName(), attr.getValue()); @@ -115,7 +117,7 @@ public void testRecordEvent() { 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"), @@ -130,14 +132,14 @@ private SelectResult mkSelectResult(String id) { List attrs = new LinkedList(); 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; @@ -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"); diff --git a/src/test/java/com/netflix/simianarmy/aws/janitor/TestSimpleDBJanitorResourceTracker.java b/src/test/java/com/netflix/simianarmy/aws/janitor/TestSimpleDBJanitorResourceTracker.java index d311b3cc..5884210e 100644 --- a/src/test/java/com/netflix/simianarmy/aws/janitor/TestSimpleDBJanitorResourceTracker.java +++ b/src/test/java/com/netflix/simianarmy/aws/janitor/TestSimpleDBJanitorResourceTracker.java @@ -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; @@ -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(); }