use of software.amazon.awssdk.services.ec2.model.Instance in project druid by druid-io.
the class EC2AutoScalerTest method setUp.
@Before
public void setUp() throws Exception {
amazonEC2Client = EasyMock.createMock(AmazonEC2Client.class);
describeInstancesResult = EasyMock.createMock(DescribeInstancesResult.class);
reservation = EasyMock.createMock(Reservation.class);
instance = new Instance().withInstanceId(INSTANCE_ID).withLaunchTime(new Date()).withImageId(AMI_ID).withPrivateIpAddress(IP);
managementConfig = new SimpleWorkerResourceManagementConfig().setWorkerPort(8080).setWorkerVersion("");
}
use of software.amazon.awssdk.services.ec2.model.Instance in project elasticsearch by elastic.
the class AmazonEC2Mock method describeInstances.
@Override
public DescribeInstancesResult describeInstances(DescribeInstancesRequest describeInstancesRequest) throws AmazonServiceException, AmazonClientException {
Collection<Instance> filteredInstances = new ArrayList<>();
logger.debug("--> mocking describeInstances");
for (Instance instance : instances) {
boolean tagFiltered = false;
boolean instanceFound = false;
Map<String, List<String>> expectedTags = new HashMap<>();
Map<String, List<String>> instanceTags = new HashMap<>();
for (Tag tag : instance.getTags()) {
List<String> tags = instanceTags.get(tag.getKey());
if (tags == null) {
tags = new ArrayList<>();
instanceTags.put(tag.getKey(), tags);
}
tags.add(tag.getValue());
}
for (Filter filter : describeInstancesRequest.getFilters()) {
// If we have the same tag name and one of the values, we add the instance
if (filter.getName().startsWith("tag:")) {
tagFiltered = true;
String tagName = filter.getName().substring(4);
// if we have more than one value for the same key, then the key is appended with .x
Pattern p = Pattern.compile("\\.\\d+", Pattern.DOTALL);
Matcher m = p.matcher(tagName);
if (m.find()) {
int i = tagName.lastIndexOf(".");
tagName = tagName.substring(0, i);
}
List<String> tags = expectedTags.get(tagName);
if (tags == null) {
tags = new ArrayList<>();
expectedTags.put(tagName, tags);
}
tags.addAll(filter.getValues());
}
}
if (tagFiltered) {
logger.debug("--> expected tags: [{}]", expectedTags);
logger.debug("--> instance tags: [{}]", instanceTags);
instanceFound = true;
for (Map.Entry<String, List<String>> expectedTagsEntry : expectedTags.entrySet()) {
List<String> instanceTagValues = instanceTags.get(expectedTagsEntry.getKey());
if (instanceTagValues == null) {
instanceFound = false;
break;
}
for (String expectedValue : expectedTagsEntry.getValue()) {
boolean valueFound = false;
for (String instanceTagValue : instanceTagValues) {
if (instanceTagValue.equals(expectedValue)) {
valueFound = true;
}
}
if (valueFound == false) {
instanceFound = false;
}
}
}
}
if (tagFiltered == false || instanceFound) {
logger.debug("--> instance added");
filteredInstances.add(instance);
} else {
logger.debug("--> instance filtered");
}
}
return new DescribeInstancesResult().withReservations(new Reservation().withInstances(filteredInstances));
}
use of software.amazon.awssdk.services.ec2.model.Instance in project druid by druid-io.
the class EC2AutoScaler method provision.
@Override
public AutoScalingData provision() {
try {
final EC2NodeData workerConfig = envConfig.getNodeData();
final String userDataBase64;
if (envConfig.getUserData() == null) {
userDataBase64 = null;
} else {
if (config.getWorkerVersion() == null) {
userDataBase64 = envConfig.getUserData().getUserDataBase64();
} else {
userDataBase64 = envConfig.getUserData().withVersion(config.getWorkerVersion()).getUserDataBase64();
}
}
RunInstancesRequest request = new RunInstancesRequest(workerConfig.getAmiId(), workerConfig.getMinInstances(), workerConfig.getMaxInstances()).withInstanceType(workerConfig.getInstanceType()).withPlacement(new Placement(envConfig.getAvailabilityZone())).withKeyName(workerConfig.getKeyName()).withIamInstanceProfile(workerConfig.getIamProfile() == null ? null : workerConfig.getIamProfile().toIamInstanceProfileSpecification()).withUserData(userDataBase64);
// leaving it null uses the EC2 default.
if (workerConfig.getAssociatePublicIpAddress() != null) {
request.withNetworkInterfaces(new InstanceNetworkInterfaceSpecification().withAssociatePublicIpAddress(workerConfig.getAssociatePublicIpAddress()).withSubnetId(workerConfig.getSubnetId()).withGroups(workerConfig.getSecurityGroupIds()).withDeviceIndex(0));
} else {
request.withSecurityGroupIds(workerConfig.getSecurityGroupIds()).withSubnetId(workerConfig.getSubnetId());
}
final RunInstancesResult result = amazonEC2Client.runInstances(request);
final List<String> instanceIds = Lists.transform(result.getReservation().getInstances(), new Function<Instance, String>() {
@Override
public String apply(Instance input) {
return input.getInstanceId();
}
});
log.info("Created instances: %s", instanceIds);
return new AutoScalingData(Lists.transform(result.getReservation().getInstances(), new Function<Instance, String>() {
@Override
public String apply(Instance input) {
return input.getInstanceId();
}
}));
} catch (Exception e) {
log.error(e, "Unable to provision any EC2 instances.");
}
return null;
}
use of software.amazon.awssdk.services.ec2.model.Instance in project druid by druid-io.
the class EC2AutoScaler method idToIpLookup.
@Override
public List<String> idToIpLookup(List<String> nodeIds) {
final List<String> retVal = FluentIterable.from(Lists.partition(nodeIds, MAX_AWS_FILTER_VALUES)).transformAndConcat(new Function<List<String>, Iterable<Reservation>>() {
@Override
public Iterable<Reservation> apply(List<String> input) {
return amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("instance-id", input))).getReservations();
}
}).transformAndConcat(new Function<Reservation, Iterable<Instance>>() {
@Override
public Iterable<Instance> apply(Reservation reservation) {
return reservation.getInstances();
}
}).transform(new Function<Instance, String>() {
@Override
public String apply(Instance instance) {
return instance.getPrivateIpAddress();
}
}).toList();
log.debug("Performing lookup: %s --> %s", nodeIds, retVal);
return retVal;
}
use of software.amazon.awssdk.services.ec2.model.Instance in project druid by druid-io.
the class EC2AutoScaler method ipToIdLookup.
@Override
public List<String> ipToIdLookup(List<String> ips) {
final List<String> retVal = FluentIterable.from(Lists.partition(ips, MAX_AWS_FILTER_VALUES)).transformAndConcat(new Function<List<String>, Iterable<Reservation>>() {
@Override
public Iterable<Reservation> apply(List<String> input) {
return amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("private-ip-address", input))).getReservations();
}
}).transformAndConcat(new Function<Reservation, Iterable<Instance>>() {
@Override
public Iterable<Instance> apply(Reservation reservation) {
return reservation.getInstances();
}
}).transform(new Function<Instance, String>() {
@Override
public String apply(Instance instance) {
return instance.getInstanceId();
}
}).toList();
log.debug("Performing lookup: %s --> %s", ips, retVal);
return retVal;
}
Aggregations