ec2: started work on reserved instances

This commit is contained in:
Adrian Cole 2010-07-17 13:08:22 -05:00
parent f7df4af65b
commit 7d27e70d59
6 changed files with 402 additions and 4 deletions

View File

@ -0,0 +1,177 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.aws.ec2.domain;
/**
*
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-CreateVolume.html"
* />
* @author Adrian Cole
*/
public class ReservedInstancesOffering implements Comparable<ReservedInstancesOffering> {
private final String region;
private final String availabilityZone;
private final long duration;
private final float fixedPrice;
private final String instanceType;
private final String productDescription;
private final String id;
private final float usagePrice;
public ReservedInstancesOffering(String region, String availabilityZone, long duration, float fixedPrice, String instanceType,
String productDescription, String reservedInstancesOfferingId, float usagePrice) {
this.region=region;
this.availabilityZone = availabilityZone;
this.duration = duration;
this.fixedPrice = fixedPrice;
this.instanceType = instanceType;
this.productDescription = productDescription;
this.id = reservedInstancesOfferingId;
this.usagePrice = usagePrice;
}
public String getRegion() {
return region;
}
/**
* @return The Availability Zone in which the Reserved Instance can be used.
*/
public String getAvailabilityZone() {
return availabilityZone;
}
/**
*
* @return The duration of the Reserved Instance, in seconds
*/
public long getDuration() {
return duration;
}
/**
*
* @return The purchase price of the Reserved Instance.
*/
public float getFixedPrice() {
return fixedPrice;
}
/**
*
* @return The instance type on which the Reserved Instance can be used.
*/
public String getInstanceType() {
return instanceType;
}
/**
*
* @return The Reserved Instance description.
*/
public String getProductDescription() {
return productDescription;
}
/**
* @return The ID of the Reserved Instance offering.
*/
public String getId() {
return id;
}
/**
*
* @return The usage price of the Reserved Instance, per hour.
*/
public float getUsagePrice() {
return usagePrice;
}
@Override
public int compareTo(ReservedInstancesOffering o) {
return id.compareTo(o.id);
}
@Override
public String toString() {
return "[availabilityZone=" + availabilityZone + ", duration=" + duration
+ ", fixedPrice=" + fixedPrice + ", id=" + id + ", instanceType=" + instanceType + ", productDescription="
+ productDescription + ", region=" + region + ", usagePrice=" + usagePrice + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((availabilityZone == null) ? 0 : availabilityZone.hashCode());
result = prime * result + (int) (duration ^ (duration >>> 32));
result = prime * result + Float.floatToIntBits(fixedPrice);
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((instanceType == null) ? 0 : instanceType.hashCode());
result = prime * result + ((productDescription == null) ? 0 : productDescription.hashCode());
result = prime * result + ((region == null) ? 0 : region.hashCode());
result = prime * result + Float.floatToIntBits(usagePrice);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ReservedInstancesOffering other = (ReservedInstancesOffering) obj;
if (availabilityZone == null) {
if (other.availabilityZone != null)
return false;
} else if (!availabilityZone.equals(other.availabilityZone))
return false;
if (duration != other.duration)
return false;
if (Float.floatToIntBits(fixedPrice) != Float.floatToIntBits(other.fixedPrice))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (instanceType == null) {
if (other.instanceType != null)
return false;
} else if (!instanceType.equals(other.instanceType))
return false;
if (productDescription == null) {
if (other.productDescription != null)
return false;
} else if (!productDescription.equals(other.productDescription))
return false;
if (region == null) {
if (other.region != null)
return false;
} else if (!region.equals(other.region))
return false;
if (Float.floatToIntBits(usagePrice) != Float.floatToIntBits(other.usagePrice))
return false;
return true;
}
}

View File

@ -20,7 +20,6 @@ package org.jclouds.aws.ec2.xml;
import java.util.Date;
import javax.annotation.Resource;
import javax.inject.Inject;
import org.jclouds.aws.Region;
@ -28,7 +27,6 @@ import org.jclouds.aws.ec2.domain.BundleTask;
import org.jclouds.aws.ec2.util.EC2Utils;
import org.jclouds.date.DateService;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.logging.Logger;
import org.jclouds.rest.internal.GeneratedHttpRequest;
/**
@ -38,8 +36,6 @@ import org.jclouds.rest.internal.GeneratedHttpRequest;
public class BundleTaskHandler extends ParseSax.HandlerForGeneratedRequestWithResult<BundleTask> {
private StringBuilder currentText = new StringBuilder();
@Resource
protected Logger logger = Logger.NULL;
@Inject
protected DateService dateService;
@Inject

View File

@ -0,0 +1,58 @@
package org.jclouds.aws.ec2.xml;
import java.util.Set;
import javax.inject.Inject;
import org.jclouds.aws.ec2.domain.ReservedInstancesOffering;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.http.functions.ParseSax.HandlerWithResult;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import com.google.common.collect.Sets;
/**
* @author Adrian Cole
*/
public class DescribeReservedInstancesOfferingResponseHandler extends
ParseSax.HandlerWithResult<Set<ReservedInstancesOffering>> {
private Set<ReservedInstancesOffering> reservedInstancesOfferings = Sets.newLinkedHashSet();
private final ReservedInstancesOfferingHandler reservedInstancesOffering;
@Inject
public DescribeReservedInstancesOfferingResponseHandler(ReservedInstancesOfferingHandler reservedInstancesOffering) {
this.reservedInstancesOffering = reservedInstancesOffering;
}
public Set<ReservedInstancesOffering> getResult() {
return reservedInstancesOfferings;
}
@Override
public HandlerWithResult<Set<ReservedInstancesOffering>> setContext(HttpRequest request) {
reservedInstancesOffering.setContext(request);
return super.setContext(request);
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (!qName.equals("item"))
reservedInstancesOffering.startElement(uri, localName, qName, attributes);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("item")) {
reservedInstancesOfferings.add(reservedInstancesOffering.getResult());
}
reservedInstancesOffering.endElement(uri, localName, qName);
}
public void characters(char ch[], int start, int length) {
reservedInstancesOffering.characters(ch, start, length);
}
}

View File

@ -0,0 +1,90 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.aws.ec2.xml;
import javax.inject.Inject;
import org.jclouds.aws.Region;
import org.jclouds.aws.ec2.domain.ReservedInstancesOffering;
import org.jclouds.aws.ec2.util.EC2Utils;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.rest.internal.GeneratedHttpRequest;
/**
*
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/index.html?ApiReference-query-DescribeReservedInstancesOfferingsResponseSetItemType.html"
* />
* @author Adrian Cole
*/
public class ReservedInstancesOfferingHandler extends ParseSax.HandlerWithResult<ReservedInstancesOffering> {
@Inject
@Region
String defaultRegion;
private StringBuilder currentText = new StringBuilder();
private String availabilityZone;
private long duration = 0;
private float fixedPrice = 0;
private String instanceType;
private String productDescription;
private String id;
private float usagePrice = 0;
public ReservedInstancesOffering getResult() {
String region = EC2Utils.findRegionInArgsOrNull((GeneratedHttpRequest<?>) request);
if (region == null)
region = defaultRegion;
ReservedInstancesOffering returnVal = new ReservedInstancesOffering(region, availabilityZone, duration,
fixedPrice, instanceType, productDescription, id, usagePrice);
this.availabilityZone = null;
this.duration = 0;
this.fixedPrice = 0;
this.instanceType = null;
this.productDescription = null;
this.id = null;
this.usagePrice = 0;
return returnVal;
}
public void endElement(String uri, String name, String qName) {
if (qName.equalsIgnoreCase("reservedInstancesOfferingId")) {
this.id = currentText.toString().trim();
} else if (qName.equalsIgnoreCase("availabilityZone")) {
this.availabilityZone = currentText.toString().trim();
} else if (qName.equalsIgnoreCase("duration")) {
this.duration = Long.parseLong(currentText.toString().trim());
} else if (qName.equalsIgnoreCase("fixedPrice")) {
this.fixedPrice = Float.parseFloat(currentText.toString().trim());
} else if (qName.equalsIgnoreCase("instanceType")) {
this.instanceType = currentText.toString().trim();
} else if (qName.equalsIgnoreCase("productDescription")) {
this.productDescription = currentText.toString().trim();
} else if (qName.equalsIgnoreCase("usagePrice")) {
this.usagePrice = Float.parseFloat(currentText.toString().trim());
}
currentText = new StringBuilder();
}
public void characters(char ch[], int start, int length) {
currentText.append(ch, start, length);
}
}

View File

@ -0,0 +1,62 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.aws.ec2.xml;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import org.jclouds.aws.ec2.domain.ReservedInstancesOffering;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.testng.annotations.Test;
import com.google.inject.internal.Iterables;
/**
* Tests behavior of {@code DescribeReservedInstancesOfferingResponseHandler}
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "ec2.DescribeReservedInstancesOfferingResponseHandlerTest")
public class DescribeReservedInstancesOfferingResponseHandlerTest extends BaseEC2HandlerTest {
public void testApplyInputStream() {
InputStream is = getClass().getResourceAsStream("/ec2/describe_reserved_instances_offerings.xml");
ReservedInstancesOffering expected = new ReservedInstancesOffering("us-east-1", "us-east-1a", 12, 0.0f, "m1.small",
"m1.small offering in us-east-1a", "4b2293b4-5813-4cc8-9ce3-1957fc1dcfc8", 0.0f);
DescribeReservedInstancesOfferingResponseHandler handler = injector
.getInstance(DescribeReservedInstancesOfferingResponseHandler.class);
addDefaultRegionToHandler(handler);
ReservedInstancesOffering result = Iterables.getOnlyElement(factory.create(handler).parse(is));
assertEquals(result, expected);
}
private void addDefaultRegionToHandler(ParseSax.HandlerWithResult<?> handler) {
GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class);
expect(request.getArgs()).andReturn(new Object[] { null });
replay(request);
handler.setContext(request);
}
}

View File

@ -0,0 +1,15 @@
<DescribeReservedInstancesOfferingsResponse
xmlns="http://ec2.amazonaws.com/doc/2009-04-04/">
<reservedInstancesOfferingsSet>
<item>
<reservedInstancesOfferingId>4b2293b4-5813-4cc8-9ce3-1957fc1dcfc8
</reservedInstancesOfferingId>
<instanceType>m1.small</instanceType>
<availabilityZone>us-east-1a</availabilityZone>
<duration>12</duration>
<fixedPrice>0.00</fixedPrice>
<usagePrice>0.00</usagePrice>
<productDescription>m1.small offering in us-east-1a</productDescription>
</item>
</reservedInstancesOfferingsSet>
</DescribeReservedInstancesOfferingsResponse>