use of com.amazonaws.services.ec2.model.Reservation in project camel by apache.
the class AmazonEC2ClientMock method runInstances.
@Override
public RunInstancesResult runInstances(RunInstancesRequest runInstancesRequest) {
RunInstancesResult result = new RunInstancesResult();
if (runInstancesRequest.getImageId().equals("test-1")) {
Reservation res = new Reservation();
res.setOwnerId("1");
res.setRequesterId("user-test");
res.setReservationId("res-1");
Collection<Instance> instances = new ArrayList();
Instance ins = new Instance();
ins.setImageId(runInstancesRequest.getImageId());
ins.setInstanceType(runInstancesRequest.getInstanceType());
ins.setInstanceId("instance-1");
if (runInstancesRequest.getSecurityGroups() != null) {
if (runInstancesRequest.getSecurityGroups().contains("secgroup-1") && runInstancesRequest.getSecurityGroups().contains("secgroup-2")) {
GroupIdentifier id1 = new GroupIdentifier();
id1.setGroupId("id-1");
id1.setGroupName("secgroup-1");
GroupIdentifier id2 = new GroupIdentifier();
id2.setGroupId("id-2");
id2.setGroupName("secgroup-2");
Collection secGroups = new ArrayList<GroupIdentifier>();
secGroups.add(id1);
secGroups.add(id2);
ins.setSecurityGroups(secGroups);
} else if (ObjectHelper.isNotEmpty(runInstancesRequest.getKeyName())) {
if (ObjectHelper.isNotEmpty(runInstancesRequest.getKeyName().contains("keypair-1"))) {
GroupIdentifier id1 = new GroupIdentifier();
id1.setGroupId("id-3");
id1.setGroupName("secgroup-3");
GroupIdentifier id2 = new GroupIdentifier();
id2.setGroupId("id-4");
id2.setGroupName("secgroup-4");
Collection secGroups = new ArrayList<GroupIdentifier>();
secGroups.add(id1);
secGroups.add(id2);
ins.setSecurityGroups(secGroups);
}
}
}
instances.add(ins);
res.setInstances(instances);
result.setReservation(res);
} else {
throw new AmazonServiceException("The image-id doesn't exists");
}
return result;
}
use of com.amazonaws.services.ec2.model.Reservation in project ice by Netflix.
the class ReservationCapacityPoller method poll.
@Override
protected void poll() throws Exception {
ProcessorConfig config = ProcessorConfig.getInstance();
// read from s3 if not exists
File file = new File(config.localDir, "reservation_capacity.txt");
if (!file.exists()) {
logger.info("downloading " + file + "...");
AwsUtils.downloadFileIfNotExist(config.workS3BucketName, config.workS3BucketPrefix, file);
logger.info("downloaded " + file);
}
// read from file
Map<String, ReservedInstances> reservations = Maps.newTreeMap();
if (file.exists()) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
String[] tokens = line.split(",");
String accountId = tokens[0];
String region = tokens[1];
String reservationId = tokens[2];
String zone = tokens[3];
Long start = Long.parseLong(tokens[4]);
long duration = Long.parseLong(tokens[5]);
String instanceType = tokens[6];
String productDescription = tokens[7];
int instanceCount = Integer.parseInt(tokens[8]);
String offeringType = tokens[9];
String state = tokens[10];
Long end = tokens.length > 11 ? Long.parseLong(tokens[11]) : null;
float fixedPrice = tokens.length > 12 ? Float.parseFloat(tokens[12]) : 0;
float usagePrice = tokens.length > 13 ? Float.parseFloat(tokens[13]) : 0;
ReservedInstances reservation = new ReservedInstances().withAvailabilityZone(zone).withStart(new Date(start)).withDuration(duration).withInstanceType(instanceType).withProductDescription(productDescription).withInstanceCount(instanceCount).withOfferingType(offeringType).withState(state).withFixedPrice(fixedPrice).withUsagePrice(usagePrice);
if (end != null)
reservation.setEnd(new Date(end));
else
reservation.setEnd(new Date(start + duration * 1000));
reservations.put(accountId + "," + region + "," + reservationId, reservation);
}
} catch (Exception e) {
logger.error("error in reading " + file, e);
} finally {
if (reader != null)
try {
reader.close();
} catch (Exception e) {
}
}
}
logger.info("read " + reservations.size() + " reservations.");
for (Account account : config.accountService.getReservationAccounts().keySet()) {
try {
AmazonEC2Client ec2Client;
String assumeRole = config.accountService.getReservationAccessRoles().get(account);
if (assumeRole != null) {
String externalId = config.accountService.getReservationAccessExternalIds().get(account);
final Credentials credentials = AwsUtils.getAssumedCredentials(account.id, assumeRole, externalId);
ec2Client = new AmazonEC2Client(new AWSSessionCredentials() {
public String getAWSAccessKeyId() {
return credentials.getAccessKeyId();
}
public String getAWSSecretKey() {
return credentials.getSecretAccessKey();
}
public String getSessionToken() {
return credentials.getSessionToken();
}
});
} else
ec2Client = new AmazonEC2Client(AwsUtils.awsCredentialsProvider.getCredentials(), AwsUtils.clientConfig);
for (Region region : Region.getAllRegions()) {
ec2Client.setEndpoint("ec2." + region.name + ".amazonaws.com");
try {
DescribeReservedInstancesResult result = ec2Client.describeReservedInstances();
for (ReservedInstances reservation : result.getReservedInstances()) {
String key = account.id + "," + region.name + "," + reservation.getReservedInstancesId();
reservations.put(key, reservation);
if (reservation.getEnd() == null)
reservation.setEnd(new Date(reservation.getStart().getTime() + reservation.getDuration() * 1000L));
if (reservation.getFixedPrice() == null)
reservation.setFixedPrice(0f);
if (reservation.getUsagePrice() == null)
reservation.setUsagePrice(0f);
}
} catch (Exception e) {
logger.error("error in describeReservedInstances for " + region.name + " " + account.name, e);
}
}
ec2Client.shutdown();
} catch (Exception e) {
logger.error("Error in describeReservedInstances for " + account.name, e);
}
}
config.reservationService.updateEc2Reservations(reservations);
updatedConfig = true;
// archive to disk
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
for (String key : reservations.keySet()) {
ReservedInstances reservation = reservations.get(key);
String[] line = new String[] { key, reservation.getAvailabilityZone(), reservation.getStart().getTime() + "", reservation.getDuration().toString(), reservation.getInstanceType(), reservation.getProductDescription(), reservation.getInstanceCount().toString(), reservation.getOfferingType(), reservation.getState(), reservation.getEnd().getTime() + "", reservation.getFixedPrice() + "", reservation.getUsagePrice() + "" };
writer.write(StringUtils.join(line, ","));
writer.newLine();
}
} catch (Exception e) {
logger.error("", e);
} finally {
if (writer != null)
try {
writer.close();
} catch (Exception e) {
}
}
logger.info("archived " + reservations.size() + " reservations.");
// archive to s3
logger.info("uploading " + file + "...");
AwsUtils.upload(config.workS3BucketName, config.workS3BucketPrefix, config.localDir, file.getName());
logger.info("uploaded " + file);
}
use of com.amazonaws.services.ec2.model.Reservation in project GNS by MobilityFirst.
the class AWSStatusCheck method main.
/**
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
init();
/*
* Amazon EC2
*/
for (String endpoint : endpoints) {
try {
ec2.setEndpoint(endpoint);
System.out.println("**** Endpoint: " + endpoint);
DescribeAvailabilityZonesResult availabilityZonesResult = ec2.describeAvailabilityZones();
System.out.println("You have access to " + availabilityZonesResult.getAvailabilityZones().size() + " Availability Zones.");
for (AvailabilityZone zone : availabilityZonesResult.getAvailabilityZones()) {
System.out.println(zone.getZoneName());
}
DescribeInstancesResult describeInstancesRequest = ec2.describeInstances();
List<Reservation> reservations = describeInstancesRequest.getReservations();
Set<Instance> instances = new HashSet<Instance>();
System.out.println("Instances: ");
for (Reservation reservation : reservations) {
for (Instance instance : reservation.getInstances()) {
instances.add(instance);
System.out.println(instance.getPublicDnsName() + " is " + instance.getState().getName());
}
}
System.out.println("Security groups: ");
DescribeSecurityGroupsResult describeSecurityGroupsResult = ec2.describeSecurityGroups();
for (SecurityGroup securityGroup : describeSecurityGroupsResult.getSecurityGroups()) {
System.out.println(securityGroup.getGroupName());
}
//System.out.println("You have " + instances.size() + " Amazon EC2 instance(s) running.");
} catch (AmazonServiceException ase) {
System.out.println("Caught Exception: " + ase.getMessage());
System.out.println("Reponse Status Code: " + ase.getStatusCode());
System.out.println("Error Code: " + ase.getErrorCode());
System.out.println("Request ID: " + ase.getRequestId());
}
/*
* Amazon SimpleDB
*
*/
try {
ListDomainsRequest sdbRequest = new ListDomainsRequest().withMaxNumberOfDomains(100);
ListDomainsResult sdbResult = sdb.listDomains(sdbRequest);
int totalItems = 0;
for (String domainName : sdbResult.getDomainNames()) {
DomainMetadataRequest metadataRequest = new DomainMetadataRequest().withDomainName(domainName);
DomainMetadataResult domainMetadata = sdb.domainMetadata(metadataRequest);
totalItems += domainMetadata.getItemCount();
}
System.out.println("You have " + sdbResult.getDomainNames().size() + " Amazon SimpleDB domain(s)" + "containing a total of " + totalItems + " items.");
} catch (AmazonServiceException ase) {
System.out.println("Caught Exception: " + ase.getMessage());
System.out.println("Reponse Status Code: " + ase.getStatusCode());
System.out.println("Error Code: " + ase.getErrorCode());
System.out.println("Request ID: " + ase.getRequestId());
}
/*
* Amazon S3
*.
*/
try {
List<Bucket> buckets = s3.listBuckets();
long totalSize = 0;
int totalItems = 0;
for (Bucket bucket : buckets) {
/*
* In order to save bandwidth, an S3 object listing does not
* contain every object in the bucket; after a certain point the
* S3ObjectListing is truncated, and further pages must be
* obtained with the AmazonS3Client.listNextBatchOfObjects()
* method.
*/
ObjectListing objects = s3.listObjects(bucket.getName());
do {
for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
totalSize += objectSummary.getSize();
totalItems++;
}
objects = s3.listNextBatchOfObjects(objects);
} while (objects.isTruncated());
}
System.out.println("You have " + buckets.size() + " Amazon S3 bucket(s), " + "containing " + totalItems + " objects with a total size of " + totalSize + " bytes.");
} catch (AmazonServiceException ase) {
/*
* AmazonServiceExceptions represent an error response from an AWS
* services, i.e. your request made it to AWS, but the AWS service
* either found it invalid or encountered an error trying to execute
* it.
*/
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
/*
* AmazonClientExceptions represent an error that occurred inside
* the client on the local host, either while trying to send the
* request to AWS or interpret the response. For example, if no
* network connection is available, the client won't be able to
* connect to AWS to execute a request and will throw an
* AmazonClientException.
*/
System.out.println("Error Message: " + ace.getMessage());
}
}
}
use of com.amazonaws.services.ec2.model.Reservation in project GNS by MobilityFirst.
the class AWSEC2 method getInstances.
/**
* Returns all the instances in this region.
*
* @param ec2
* @return a set of instance instances
*/
public static Set<Instance> getInstances(AmazonEC2 ec2) {
Set<Instance> instances = new HashSet<>();
DescribeInstancesResult describeInstancesResult = ec2.describeInstances();
List<Reservation> reservations = describeInstancesResult.getReservations();
// add all instances to a Set.
for (Reservation reservation : reservations) {
instances.addAll(reservation.getInstances());
}
return instances;
}
Aggregations