Adjusting openstack-nova-ec2 to handle extended volume status fields (by discarding the extra information)

This commit is contained in:
Adam Lowe 2012-04-17 12:52:24 +01:00
parent 8350e453ca
commit 7c31f51ab7
6 changed files with 205 additions and 0 deletions

View File

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