-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
806 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...itial/aws-workshop-common/src/main/java/com/gofore/aws/workshop/common/asg/AsgClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.gofore.aws.workshop.common.asg; | ||
|
||
import com.amazonaws.handlers.AsyncHandler; | ||
import com.amazonaws.services.autoscaling.AmazonAutoScalingAsync; | ||
import com.amazonaws.services.autoscaling.model.AutoScalingGroup; | ||
import com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest; | ||
import com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult; | ||
import com.amazonaws.services.autoscaling.model.Instance; | ||
import com.gofore.aws.workshop.common.functional.Lists; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class AsgClient { | ||
|
||
private final AmazonAutoScalingAsync asg; | ||
|
||
public AsgClient(AmazonAutoScalingAsync asg) { | ||
this.asg = asg; | ||
} | ||
|
||
public CompletableFuture<DescribeAutoScalingGroupsResult> describeAutoScalingGroups(DescribeAutoScalingGroupsRequest request) { | ||
CompletableFuture<DescribeAutoScalingGroupsResult> future = new CompletableFuture<>(); | ||
asg.describeAutoScalingGroupsAsync(request, new AsyncHandler<DescribeAutoScalingGroupsRequest, DescribeAutoScalingGroupsResult>() { | ||
@Override | ||
public void onError(Exception exception) { | ||
future.completeExceptionally(exception); | ||
} | ||
|
||
@Override | ||
public void onSuccess(DescribeAutoScalingGroupsRequest request, DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult) { | ||
future.complete(describeAutoScalingGroupsResult); | ||
} | ||
}); | ||
return future; | ||
} | ||
|
||
public CompletableFuture<List<Instance>> getInstances(String asgName) { | ||
return describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(asgName)) | ||
.thenApply(DescribeAutoScalingGroupsResult::getAutoScalingGroups) | ||
.thenApply(Lists.findFirst()) | ||
.thenApply(AutoScalingGroup::getInstances); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...itial/aws-workshop-common/src/main/java/com/gofore/aws/workshop/common/ec2/Ec2Client.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.gofore.aws.workshop.common.ec2; | ||
|
||
import com.amazonaws.handlers.AsyncHandler; | ||
import com.amazonaws.services.ec2.AmazonEC2Async; | ||
import com.amazonaws.services.ec2.model.*; | ||
import com.amazonaws.util.EC2MetadataUtils; | ||
import com.gofore.aws.workshop.common.functional.Lists; | ||
import com.google.common.base.Strings; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
public class Ec2Client { | ||
|
||
private final AmazonEC2Async ec2; | ||
|
||
public Ec2Client(AmazonEC2Async ec2) { | ||
this.ec2 = ec2; | ||
} | ||
|
||
public String getInstanceId() { | ||
return EC2MetadataUtils.getInstanceId(); | ||
} | ||
|
||
public Optional<Tag> getTag(Instance instance, String name) { | ||
return Lists.findFirst(instance.getTags(), t -> t.getKey().equals(name)); | ||
} | ||
|
||
public CompletableFuture<DescribeInstancesResult> describeInstances(DescribeInstancesRequest request) { | ||
CompletableFuture<DescribeInstancesResult> future = new CompletableFuture<>(); | ||
ec2.describeInstancesAsync(request, new AsyncHandler<DescribeInstancesRequest, DescribeInstancesResult>() { | ||
@Override | ||
public void onError(Exception exception) { | ||
future.completeExceptionally(exception); | ||
} | ||
|
||
@Override | ||
public void onSuccess(DescribeInstancesRequest request, DescribeInstancesResult describeInstancesResult) { | ||
future.complete(describeInstancesResult); | ||
} | ||
}); | ||
return future; | ||
} | ||
|
||
public CompletableFuture<List<Instance>> getInstances(List<String> instanceIds, boolean requirePrivateAccessible) { | ||
return describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceIds)) | ||
.thenApply(DescribeInstancesResult::getReservations) | ||
.thenApply(rs -> rs.stream().flatMap(r -> r.getInstances().stream())) | ||
.thenApply(instances -> instances.filter(accessible(requirePrivateAccessible))) | ||
.thenApply(instances -> instances.collect(Collectors.toList())); | ||
} | ||
|
||
public CompletableFuture<Instance> getInstance(String instanceId, boolean requirePrivateAccessible) { | ||
return getInstances(ImmutableList.of(instanceId), requirePrivateAccessible) | ||
.thenApply(Lists.findFirst()); | ||
} | ||
|
||
private Predicate<Instance> accessible(boolean requirePrivateAccessible) { | ||
if (requirePrivateAccessible) { | ||
return i -> !Strings.isNullOrEmpty(i.getPrivateIpAddress()); | ||
} else { | ||
return i -> true; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 16 additions & 9 deletions
25
...n/java/com/gofore/aws/workshop/common/properties/CloudFormationOutputsPropertyLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,32 @@ | ||
package com.gofore.aws.workshop.common.properties; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import com.amazonaws.services.cloudformation.model.Output; | ||
import com.gofore.aws.workshop.common.cloudformation.CloudFormationClient; | ||
import com.google.common.collect.Maps; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class CloudFormationOutputsPropertyLoader extends AbstractPropertyLoader { | ||
|
||
private final Map<String, Output> outputs; | ||
private final String awsStack; | ||
private final CloudFormationClient cloudFormationClient; | ||
private Map<String, Output> outputs; | ||
|
||
public CloudFormationOutputsPropertyLoader(ApplicationProperties properties, CloudFormationClient cloudFormationClient) { | ||
this.outputs = Maps.uniqueIndex( | ||
cloudFormationClient.getStackOutputs(properties.lookup("aws.stack")).join(), | ||
Output::getOutputKey | ||
); | ||
this.awsStack = properties.lookup("aws.stack"); | ||
this.cloudFormationClient = cloudFormationClient; | ||
} | ||
|
||
@Override | ||
public Optional<String> lookupOptional(String name) { | ||
public synchronized Optional<String> lookupOptional(String name) { | ||
loadOutputs(); | ||
return Optional.ofNullable(outputs.get(name)).map(Output::getOutputValue); | ||
} | ||
|
||
private void loadOutputs() { | ||
if (outputs == null) { | ||
this.outputs = Maps.uniqueIndex(cloudFormationClient.getStackOutputs(awsStack).join(), Output::getOutputKey); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...on/src/main/java/com/gofore/aws/workshop/common/properties/EnvironmentPropertyLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.gofore.aws.workshop.common.properties; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class EnvironmentPropertyLoader extends AbstractPropertyLoader { | ||
|
||
private final Map<String, String> env; | ||
|
||
public EnvironmentPropertyLoader() { | ||
this.env = System.getenv(); | ||
} | ||
|
||
@Override | ||
public Optional<String> lookupOptional(String name) { | ||
return Optional.ofNullable(env.get(name)); | ||
} | ||
} |
Oops, something went wrong.