java/com.sap.sse.landscape.aws.test/src/com/sap/sse/landscape/aws/MongoUriParserTest.java
... ...
@@ -36,12 +36,12 @@ public class MongoUriParserTest {
36 36
when(host1.getPublicAddress()).thenReturn(wwwExampleCom);
37 37
when(host1.getPrivateAddress()).thenReturn(wwwExampleCom);
38 38
when(host1.getHostname()).thenReturn(WWW_EXAMPLE_COM);
39
- when(landscape.getHostByPrivateIpAddress(ArgumentMatchers.any(Region.class), ArgumentMatchers.contains(wwwExampleCom.getHostAddress()), ArgumentMatchers.any(HostSupplier.class))).thenReturn(host1);
39
+ when(landscape.getHostByPrivateDnsNameOrIpAddress(ArgumentMatchers.any(Region.class), ArgumentMatchers.contains(wwwExampleCom.getHostAddress()), ArgumentMatchers.any(HostSupplier.class))).thenReturn(host1);
40 40
final InetAddress loopback = InetAddress.getLoopbackAddress();
41 41
final AwsInstance<String> host2 = mock(AwsInstance.class);
42 42
when(host2.getPrivateAddress()).thenReturn(loopback);
43 43
when(host2.getHostname()).thenReturn(loopback.getHostAddress());
44
- when(landscape.getHostByPrivateIpAddress(ArgumentMatchers.any(Region.class), ArgumentMatchers.contains(loopback.getHostAddress()), ArgumentMatchers.any(HostSupplier.class))).thenReturn(host2);
44
+ when(landscape.getHostByPrivateDnsNameOrIpAddress(ArgumentMatchers.any(Region.class), ArgumentMatchers.contains(loopback.getHostAddress()), ArgumentMatchers.any(HostSupplier.class))).thenReturn(host2);
45 45
parser = new MongoUriParser<String>(landscape, new AwsRegion("eu-west-2", landscape));
46 46
}
47 47
java/com.sap.sse.landscape.aws/src/com/sap/sse/landscape/aws/AwsLandscape.java
... ...
@@ -329,7 +329,7 @@ public interface AwsLandscape<ShardingKey> extends Landscape<ShardingKey> {
329 329
330 330
Instance getInstanceByPublicIpAddress(Region region, String publicIpAddress);
331 331
332
- Instance getInstanceByPrivateIpAddress(Region region, String publicIpAddress);
332
+ Instance getInstanceByPrivateDnsOrIpAddress(Region region, String publicIpAddress);
333 333
334 334
/**
335 335
* @param hostname
java/com.sap.sse.landscape.aws/src/com/sap/sse/landscape/aws/MongoUriParser.java
... ...
@@ -1,6 +1,5 @@
1 1
package com.sap.sse.landscape.aws;
2 2
3
-import java.net.InetAddress;
4 3
import java.net.URI;
5 4
import java.net.URISyntaxException;
6 5
import java.net.UnknownHostException;
... ...
@@ -156,8 +155,7 @@ public class MongoUriParser<ShardingKey> {
156 155
* If the host isn't found in the landscape, the {@link Pair#getA()} component of the pair returned will be {@code null}.
157 156
*/
158 157
private Pair<AwsInstance<ShardingKey>, Integer> getHostAndPort(String hostname, Integer optionalPort) throws UnknownHostException {
159
- final InetAddress address = InetAddress.getByName(hostname);
160
- final AwsInstance<ShardingKey> hostByPrivateIp = landscape.getHostByPrivateIpAddress(region, address.getHostAddress(), AwsInstanceImpl::new);
161
- return new Pair<>(hostByPrivateIp, optionalPort);
158
+ final AwsInstance<ShardingKey> hostByPrivateDnsOrIp = landscape.getHostByPrivateDnsNameOrIpAddress(region, hostname, AwsInstanceImpl::new);
159
+ return new Pair<>(hostByPrivateDnsOrIp, optionalPort);
162 160
}
163 161
}
java/com.sap.sse.landscape.aws/src/com/sap/sse/landscape/aws/impl/AwsApplicationProcessImpl.java
... ...
@@ -155,7 +155,7 @@ implements AwsApplicationProcess<ShardingKey, MetricsT, ProcessT> {
155 155
} catch (Exception e) {
156 156
logger.info("Unable to find master by public IP "+ipAddressOrHostname+" ("+e.getMessage()+"); trying to look up master assuming "+ipAddressOrHostname+" is the private IP");
157 157
try {
158
- host = landscape.getHostByPrivateIpAddress(getHost().getRegion(), ipAddressOrHostname, hostSupplier);
158
+ host = landscape.getHostByPrivateDnsNameOrIpAddress(getHost().getRegion(), ipAddressOrHostname, hostSupplier);
159 159
} catch (Exception f) {
160 160
logger.info("Unable to find master by private IP "+ipAddressOrHostname+" ("+f.getMessage()+") either. Returning null.");
161 161
host = null;
java/com.sap.sse.landscape.aws/src/com/sap/sse/landscape/aws/impl/AwsLandscapeImpl.java
... ...
@@ -645,24 +645,55 @@ public class AwsLandscapeImpl<ShardingKey> implements AwsLandscape<ShardingKey>
645 645
public <HostT extends AwsInstance<ShardingKey>> HostT getHostByPublicIpAddress(com.sap.sse.landscape.Region region, String publicIpAddress, HostSupplier<ShardingKey, HostT> hostSupplier) {
646 646
return getHost(region, getInstanceByPublicIpAddress(region, publicIpAddress), hostSupplier);
647 647
}
648
-
648
+
649
+ private Instance getFirstInstance(DescribeInstancesResponse response) {
650
+ for (Reservation reservation : response.reservations()) {
651
+ for (Instance instance : reservation.instances()) {
652
+ return instance;
653
+ }
654
+ }
655
+ return null;
656
+ }
657
+
658
+ /**
659
+ * Searches for an instance that matches the private dns name if this fails it tries the to resolve the private ip address
660
+ */
649 661
@Override
650
- public Instance getInstanceByPrivateIpAddress(com.sap.sse.landscape.Region region, String privateIpAddress) {
662
+ public Instance getInstanceByPrivateDnsNameOrIpAddress(com.sap.sse.landscape.Region region, String privateDnsNameOrIpAddress) {
651 663
InetAddress inetAddress;
652 664
try {
653
- inetAddress = InetAddress.getByName(privateIpAddress);
654
- return getEc2Client(getRegion(region))
655
- .describeInstances(b->b.filters(Filter.builder().name("private-ip-address").values(inetAddress.getHostAddress()).build())).reservations()
656
- .iterator().next().instances().iterator().next();
665
+ DescribeInstancesResponse response = getEc2Client(getRegion(region))
666
+ .describeInstances(b -> b.filters(
667
+ Filter.builder().name("private-dns-name").values(privateDnsNameOrIpAddress).build())
668
+ );
669
+ Instance instance = getFirstInstance(response);
670
+ if (instance != null) {
671
+ return instance;
672
+ }
673
+
674
+ inetAddress = InetAddress.getByName(privateDnsNameOrIpAddress);
675
+ response = getEc2Client(getRegion(region))
676
+ .describeInstances(b -> b.filters(
677
+ Filter.builder().name("private-ip-address").values(inetAddress.getHostAddress()).build())
678
+ );
679
+ instance = getFirstInstance(response);
680
+ if (instance != null) {
681
+ return instance;
682
+ }
657 683
} catch (UnknownHostException | NoSuchElementException e) {
658
- logger.warning("IP address for "+privateIpAddress+" not found");
659
- return null;
684
+ logger.severe("An error occurred while trying to find the instance: " + e.getMessage());
660 685
}
661
- }
662 686
687
+ logger.warning("Instance for " + privateDnsNameOrIpAddress + " not found");
688
+ return null;
689
+ }
690
+
691
+ /**
692
+ * Returns a host first by private dns name and alternatively by private ip address
693
+ */
663 694
@Override
664
- public <HostT extends AwsInstance<ShardingKey>> HostT getHostByPrivateIpAddress(com.sap.sse.landscape.Region region, String privateIpAddress, HostSupplier<ShardingKey, HostT> hostSupplier) {
665
- return getHost(region, getInstanceByPrivateIpAddress(region, privateIpAddress), hostSupplier);
695
+ public <HostT extends AwsInstance<ShardingKey>> HostT getHostByPrivateDnsNameOrIpAddress(com.sap.sse.landscape.Region region, String privateIpAddress, HostSupplier<ShardingKey, HostT> hostSupplier) {
696
+ return getHost(region, getInstanceByPrivateDnsNameOrIpAddress(region, privateIpAddress), hostSupplier);
666 697
}
667 698
668 699
private Route53Client getRoute53Client() {
... ...
@@ -1196,7 +1227,7 @@ public class AwsLandscapeImpl<ShardingKey> implements AwsLandscape<ShardingKey>
1196 1227
DescribeTargetHealthRequest.builder().targetGroupArn(targetGroup.getTargetGroupArn()).build())
1197 1228
.targetHealthDescriptions().forEach(targetHealthDescription -> {
1198 1229
if (targetHealthDescription.target().id().matches("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+")) {
1199
- AwsInstance<ShardingKey> awsInstance = getHostByPrivateIpAddress(targetGroup.getRegion(), targetHealthDescription.target().id().trim(),
1230
+ AwsInstance<ShardingKey> awsInstance = getHostByPrivateDnsNameOrIpAddress(targetGroup.getRegion(), targetHealthDescription.target().id().trim(),
1200 1231
AwsInstanceImpl::new);
1201 1232
result.put(awsInstance, targetHealthDescription.targetHealth());
1202 1233
} else {