Merge pull request #571 from aplowe/nova-ec2

Issue 895: adjusting openstack nova ec2 response parsing
This commit is contained in:
Adrian Cole 2012-04-18 07:33:42 -07:00
commit 67dfac1b7a
12 changed files with 386 additions and 19 deletions

View File

@ -50,7 +50,7 @@ import com.google.common.collect.Sets;
* @author Adrian Cole
*/
public class CreateVolumeResponseHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Volume> {
private StringBuilder currentText = new StringBuilder();
protected StringBuilder currentText = new StringBuilder();
@Resource
protected Logger logger = Logger.NULL;
@ -58,7 +58,7 @@ public class CreateVolumeResponseHandler extends ParseSax.HandlerForGeneratedReq
protected DateService dateService;
@Inject
@Region
Supplier<String> defaultRegion;
protected Supplier<String> defaultRegion;
@Inject
@Zone
protected Supplier<Map<String, Supplier<Set<String>>>> regionToZonesSupplier;
@ -66,23 +66,23 @@ public class CreateVolumeResponseHandler extends ParseSax.HandlerForGeneratedReq
@Zone
protected Supplier<Set<String>> zonesSupplier;
private String id;
private int size;
private String snapshotId;
private String availabilityZone;
private Volume.Status volumeStatus;
private Date createTime;
private Set<Attachment> attachments = Sets.newLinkedHashSet();
protected String id;
protected int size;
protected String snapshotId;
protected String availabilityZone;
protected Volume.Status volumeStatus;
protected Date createTime;
protected Set<Attachment> attachments = Sets.newLinkedHashSet();
private String volumeId;
private String instanceId;
private String device;
private Attachment.Status attachmentStatus;
private Date attachTime;
protected String volumeId;
protected String instanceId;
protected String device;
protected Attachment.Status attachmentStatus;
protected Date attachTime;
private boolean inAttachmentSet;
protected boolean inAttachmentSet;
private String region;
protected String region;
public Volume getResult() {
return newVolume();

View File

@ -61,7 +61,7 @@ public class DescribeImagesResponseHandler extends ParseSax.HandlerForGeneratedR
@Resource
protected Logger logger = Logger.NULL;
private Set<Image> contents = Sets.newLinkedHashSet();
protected Set<Image> contents = Sets.newLinkedHashSet();
private StringBuilder currentText = new StringBuilder();
private final Supplier<String> defaultRegion;

View File

@ -29,8 +29,8 @@
</parent>
<groupId>org.jclouds.api</groupId>
<artifactId>openstack-nova-ec2</artifactId>
<name>jclouds Eucalyptus api</name>
<description>EC2 implementation based on Eucalyptus</description>
<name>jclouds openstack-nova-ec2 api</name>
<description>EC2 interface to Openstack Nova</description>
<packaging>bundle</packaging>
<properties>

View File

@ -22,10 +22,14 @@ import org.jclouds.ec2.EC2AsyncClient;
import org.jclouds.ec2.EC2Client;
import org.jclouds.ec2.config.EC2RestClientModule;
import org.jclouds.ec2.suppliers.DescribeAvailabilityZonesInRegion;
import org.jclouds.ec2.xml.CreateVolumeResponseHandler;
import org.jclouds.ec2.xml.DescribeImagesResponseHandler;
import org.jclouds.location.config.LocationModule;
import org.jclouds.location.suppliers.RegionIdToZoneIdsSupplier;
import org.jclouds.location.suppliers.ZoneIdsSupplier;
import org.jclouds.location.suppliers.derived.ZoneIdsFromRegionIdToZoneIdsValues;
import org.jclouds.openstack.nova.ec2.xml.NovaCreateVolumeResponseHandler;
import org.jclouds.openstack.nova.ec2.xml.NovaDescribeImagesResponseHandler;
import org.jclouds.rest.ConfiguresRestClient;
import com.google.inject.Scopes;
@ -41,6 +45,13 @@ public class NovaEC2RestClientModule extends EC2RestClientModule<EC2Client, EC2A
super(EC2Client.class, EC2AsyncClient.class, DELEGATE_MAP);
}
@Override
protected void configure() {
super.configure();
bind(CreateVolumeResponseHandler.class).to(NovaCreateVolumeResponseHandler.class).in(Scopes.SINGLETON);
bind(DescribeImagesResponseHandler.class).to(NovaDescribeImagesResponseHandler.class);
}
@Override
protected void installLocations() {
install(new LocationModule());

View File

@ -0,0 +1,48 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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.openstack.nova.ec2.xml;
import org.jclouds.ec2.domain.Attachment;
import org.jclouds.ec2.domain.Volume;
import org.jclouds.ec2.xml.CreateVolumeResponseHandler;
/**
*
* @author Adam lowe
*/
public class NovaCreateVolumeResponseHandler extends CreateVolumeResponseHandler {
public void endElement(String uri, String name, String qName) {
if (qName.equals("status")) {
String statusString = currentText.toString().trim();
if (statusString.contains(" ")) {
statusString = statusString.substring(0, statusString.indexOf(' '));
}
if (inAttachmentSet) {
attachmentStatus = Attachment.Status.fromValue(statusString);
} else {
volumeStatus = Volume.Status.fromValue(statusString);
}
currentText = new StringBuilder();
} else {
super.endElement(uri, name, qName);
}
}
}

View File

@ -0,0 +1,54 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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.openstack.nova.ec2.xml;
import java.util.Set;
import javax.inject.Inject;
import org.jclouds.ec2.domain.Image;
import org.jclouds.ec2.domain.Image.ImageType;
import org.jclouds.ec2.xml.DescribeImagesResponseHandler;
import org.jclouds.location.Region;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
/**
* Adjusted to filter out non-MACHINE images
*
* @author Adam Lowe
*/
public class NovaDescribeImagesResponseHandler extends DescribeImagesResponseHandler {
@Inject
public NovaDescribeImagesResponseHandler(@Region Supplier<String> defaultRegion) {
super(defaultRegion);
}
public Set<Image> getResult() {
return ImmutableSet.copyOf(Iterables.filter(contents, new Predicate<Image>() {
@Override
public boolean apply(Image image) {
return image.getImageType() == ImageType.MACHINE;
}
}));
}
}

View File

@ -0,0 +1,54 @@
package org.jclouds.openstack.nova.ec2.internal;
import java.net.URI;
import javax.inject.Named;
import javax.ws.rs.core.MediaType;
import org.jclouds.Constants;
import org.jclouds.date.DateService;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.ec2.EC2Client;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.nova.ec2.config.NovaEC2RestClientModule;
import org.jclouds.rest.ConfiguresRestClient;
import org.jclouds.rest.internal.BaseRestClientExpectTest;
import com.google.common.collect.ImmutableMultimap;
import com.google.inject.Module;
import com.google.inject.Provides;
public abstract class BaseNovaEC2RestClientExpectTest extends BaseRestClientExpectTest<EC2Client> {
protected static final String CONSTANT_DATE = "2012-04-16T15:54:08.897Z";
protected DateService dateService = new SimpleDateFormatDateService();
protected URI endpoint = URI.create("http://localhost:8773/services/Cloud/");
protected HttpRequest describeAvailabilityZonesRequest = HttpRequest.builder().method("POST")
.endpoint(endpoint)
.headers(ImmutableMultimap.of("Host", "localhost:8773"))
.payload(payloadFromStringWithContentType("Action=DescribeAvailabilityZones&Signature=S3fa5fybw4KAq4o11IpKHlqwx3cVJdKfeAKw3FIJYvM%3D&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2012-04-16T15%3A54%3A08.897Z&Version=2009-04-04&AWSAccessKeyId=identity", MediaType.APPLICATION_FORM_URLENCODED))
.build();
protected HttpResponse describeAvailabilityZonesResponse = HttpResponse.builder()
.statusCode(200).payload(payloadFromResourceWithContentType("/nova_ec2_availabilityZones.xml", MediaType.APPLICATION_XML)).build();
public BaseNovaEC2RestClientExpectTest() {
provider = "openstack-nova-ec2";
}
@ConfiguresRestClient
private static final class TestNovaEC2RestClientModule extends NovaEC2RestClientModule {
@Override
@Provides
protected String provideTimeStamp(final DateService dateService,
@Named(Constants.PROPERTY_SESSION_INTERVAL) final int expiration) {
return CONSTANT_DATE;
}
}
@Override
protected Module createModule() {
return new TestNovaEC2RestClientModule();
}
}

View File

@ -0,0 +1,58 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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.openstack.nova.ec2.services;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.Set;
import org.jclouds.ec2.domain.Image;
import org.jclouds.ec2.services.AMIClient;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.nova.ec2.internal.BaseNovaEC2RestClientExpectTest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMultimap;
/**
* @author Adam Lowe
*/
@Test(groups = "unit", testName = "NovaEC2ElasticBlockStoreClientTest")
public class NovaEC2AMIClientTest extends BaseNovaEC2RestClientExpectTest {
public void testDescribeImagesWithNonMachineTypes() {
AMIClient client = requestsSendResponses(
describeAvailabilityZonesRequest,
describeAvailabilityZonesResponse,
HttpRequest.builder().method("POST")
.endpoint(URI.create("http://localhost:8773/services/Cloud/"))
.headers(ImmutableMultimap.of("Host", "localhost:8773"))
.payload(payloadFromStringWithContentType("Action=DescribeImages&Signature=Z3q3jSutwlfgvbcINT0Ed3AjrjxM4WMvQloXu%2F1kd40%3D&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2012-04-16T15%3A54%3A08.897Z&Version=2009-04-04&AWSAccessKeyId=identity", "application/x-www-form-urlencoded")).build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/nova_ec2_images_with_ramdisk.xml")).build()
).getAMIServices();
Set<? extends Image> images = client.describeImagesInRegion("nova");
assertEquals(images.size(), 1);
}
}

View File

@ -0,0 +1,68 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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.openstack.nova.ec2.services;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.Set;
import org.jclouds.ec2.domain.Attachment;
import org.jclouds.ec2.domain.Volume;
import org.jclouds.ec2.services.ElasticBlockStoreClient;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.nova.ec2.internal.BaseNovaEC2RestClientExpectTest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
/**
* @author Adam Lowe
*/
@Test(groups = "unit", testName = "NovaEC2ElasticBlockStoreClientTest")
public class NovaEC2ElasticBlockStoreClientTest extends BaseNovaEC2RestClientExpectTest {
public void testDescribeVolumesWithNovaEC2Status() {
ElasticBlockStoreClient client = requestsSendResponses(
describeAvailabilityZonesRequest,
describeAvailabilityZonesResponse,
HttpRequest.builder().method("POST")
.endpoint(URI.create("http://localhost:8773/services/Cloud/"))
.headers(ImmutableMultimap.of("Host", "localhost:8773"))
.payload(payloadFromStringWithContentType("Action=DescribeVolumes&Signature=AvRznSzGExM%2Buaj2JJj66wq4v4f%2BakicyLooRDtC0t0%3D&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2012-04-16T15%3A54%3A08.897Z&Version=2009-04-04&AWSAccessKeyId=identity", "application/x-www-form-urlencoded")).build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/nova_ec2_describe_volumes.xml")).build()
).getElasticBlockStoreServices();
Set<Volume> expected = ImmutableSet.of(Volume
.builder()
.status(Volume.Status.AVAILABLE)
.availabilityZone("nova")
.region("nova")
.id("vol-00000007")
.size(1)
.attachments(Attachment.builder().region("nova").build())
.createTime(dateService.iso8601SecondsDateParse("2012-04-10T10:39:52Z"))
.build());
assertEquals(client.describeVolumesInRegion("nova"), expected);
}
}

View File

@ -0,0 +1,10 @@
<?xml version="1.0" ?>
<DescribeAvailabilityZonesResponse xmlns="http://ec2.amazonaws.com/doc/2009-04-04/">
<requestId>req-a6cd42f8-b5e5-4c94-a1e0-41d21ea0a032</requestId>
<availabilityZoneInfo>
<item>
<zoneState>available</zoneState>
<zoneName>nova</zoneName>
</item>
</availabilityZoneInfo>
</DescribeAvailabilityZonesResponse>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" ?>
<DescribeVolumesResponse xmlns="http://ec2.amazonaws.com/doc/2009-04-04/">
<requestId>req-9e45299b-980d-4893-a475-a574b1a94ed5</requestId>
<volumeSet>
<item>
<status>available (f06de98af01446b2ae6bd79f5fbf3b2a, att-openstack1, None, None)</status>
<availabilityZone>nova</availabilityZone>
<volumeId>vol-00000007</volumeId>
<attachmentSet>
<item/>
</attachmentSet>
<snapshotId/>
<createTime>2012-04-10T10:39:52.000Z</createTime>
<size>1</size>
</item>
</volumeSet>
</DescribeVolumesResponse>

View File

@ -0,0 +1,47 @@
<?xml version="1.0" ?>
<DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2009-04-04/">
<requestId>req-bfdac708-c6c4-48fb-9cba-be5bb0a05b49</requestId>
<imagesSet>
<item>
<description/>
<imageOwnerId/>
<isPublic>true</isPublic>
<imageId>aki-00000002</imageId>
<imageState>available</imageState>
<architecture/>
<imageLocation>None (cirros-0.3.0-x86_64-blank-kernel)</imageLocation>
<rootDeviceType>instance-store</rootDeviceType>
<rootDeviceName>/dev/sda1</rootDeviceName>
<imageType>kernel</imageType>
<name>cirros-0.3.0-x86_64-blank-kernel</name>
</item>
<item>
<description/>
<imageOwnerId/>
<isPublic>true</isPublic>
<imageId>ari-00000003</imageId>
<imageState>available</imageState>
<architecture/>
<imageLocation>None (cirros-0.3.0-x86_64-blank-ramdisk)</imageLocation>
<rootDeviceType>instance-store</rootDeviceType>
<rootDeviceName>/dev/sda1</rootDeviceName>
<imageType>ramdisk</imageType>
<name>cirros-0.3.0-x86_64-blank-ramdisk</name>
</item>
<item>
<name>cirros-0.3.0-x86_64-blank</name>
<imageOwnerId/>
<isPublic>true</isPublic>
<imageId>ami-00000001</imageId>
<imageState>available</imageState>
<rootDeviceType>instance-store</rootDeviceType>
<architecture/>
<imageLocation>None (cirros-0.3.0-x86_64-blank)</imageLocation>
<kernelId>aki-00000002</kernelId>
<ramdiskId>ari-00000003</ramdiskId>
<rootDeviceName>/dev/sda1</rootDeviceName>
<imageType>machine</imageType>
<description/>
</item>
</imagesSet>
</DescribeImagesResponse>