JCLOUDS-353 openstack list availability zones for NOVA

This commit is contained in:
istolber 2013-10-14 10:03:44 +02:00 committed by Everett Toews
parent f196ee3b6b
commit 725b7c5c2c
10 changed files with 377 additions and 5 deletions

View File

@ -21,6 +21,7 @@ import java.util.Set;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.location.Zone;
import org.jclouds.location.functions.ZoneToEndpoint;
import org.jclouds.openstack.nova.v2_0.extensions.AvailabilityZoneAPI;
import org.jclouds.openstack.nova.v2_0.extensions.FlavorExtraSpecsApi;
import org.jclouds.openstack.nova.v2_0.extensions.FloatingIPApi;
import org.jclouds.openstack.nova.v2_0.extensions.HostAdministrationApi;
@ -49,7 +50,7 @@ import com.google.inject.Provides;
/**
* Provides synchronous access to Nova.
* <p/>
*
*
* @see NovaAsyncApi
* @see <a href="http://docs.openstack.org/api/openstack-compute/1.1/content/"
* />
@ -57,13 +58,20 @@ import com.google.inject.Provides;
*/
public interface NovaApi extends Closeable {
/**
*
*
* @return the Zone codes configured
*/
@Provides
@Zone
Set<String> getConfiguredZones();
/**
* Provides synchronous access to availability zone features
*/
@Delegate
AvailabilityZoneAPI getAvailabilityZoneApi(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone);
/**
* Provides synchronous access to Server features.
*/
@ -147,7 +155,7 @@ public interface NovaApi extends Closeable {
@Delegate
Optional<? extends ServerAdminApi> getServerAdminExtensionForZone(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone);
/**
* Provides synchronous access to Aggregate features.
*/

View File

@ -22,6 +22,7 @@ import java.util.Set;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.location.Zone;
import org.jclouds.location.functions.ZoneToEndpoint;
import org.jclouds.openstack.nova.v2_0.extensions.AvailabilityZoneAPI;
import org.jclouds.openstack.nova.v2_0.extensions.FlavorExtraSpecsAsyncApi;
import org.jclouds.openstack.nova.v2_0.extensions.FloatingIPAsyncApi;
import org.jclouds.openstack.nova.v2_0.extensions.HostAdministrationAsyncApi;
@ -50,7 +51,7 @@ import com.google.inject.Provides;
/**
* Provides asynchronous access to Nova via their REST API.
* <p/>
*
*
* @see NovaApi
* @see <a href="http://docs.openstack.org/api/openstack-compute/1.1/content/"
* />
@ -62,13 +63,20 @@ import com.google.inject.Provides;
public interface NovaAsyncApi extends Closeable {
/**
*
*
* @return the Zone codes configured
*/
@Provides
@Zone
Set<String> getConfiguredZones();
/**
* Provides asynchronous access to availability zone features
*/
@Delegate
AvailabilityZoneAPI getAvailabilityZoneApi(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone);
/**
* Provides asynchronous access to Server features.
*/

View File

@ -31,6 +31,8 @@ import org.jclouds.http.annotation.Redirection;
import org.jclouds.http.annotation.ServerError;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.NovaAsyncApi;
import org.jclouds.openstack.nova.v2_0.extensions.AvailabilityZoneAPI;
import org.jclouds.openstack.nova.v2_0.extensions.AvailabilityZoneAsyncApi;
import org.jclouds.openstack.nova.v2_0.extensions.ExtensionNamespaces;
import org.jclouds.openstack.nova.v2_0.extensions.FlavorExtraSpecsApi;
import org.jclouds.openstack.nova.v2_0.extensions.FlavorExtraSpecsAsyncApi;
@ -100,6 +102,7 @@ public class NovaRestClientModule<S extends NovaApi, A extends NovaAsyncApi> ext
.put(ImageApi.class, ImageAsyncApi.class)
.put(ExtensionApi.class, ExtensionAsyncApi.class)
.put(FloatingIPApi.class, FloatingIPAsyncApi.class)
.put(AvailabilityZoneAPI.class, AvailabilityZoneAsyncApi.class)
.put(SecurityGroupApi.class, SecurityGroupAsyncApi.class)
.put(KeyPairApi.class, KeyPairAsyncApi.class)
.put(HostAdministrationApi.class, HostAdministrationAsyncApi.class)

View File

@ -0,0 +1,73 @@
/**
* 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.v2_0.domain.zonescoped;
import com.google.common.base.Objects;
import com.google.gson.annotations.SerializedName;
import java.beans.ConstructorProperties;
/**
* @author Inbar Stolberg
*/
public class AvailabilityZone {
@SerializedName("zoneName")
private final String name;
private final ZoneState state;
@ConstructorProperties({"zoneName" , "zoneState"})
protected AvailabilityZone(String name, ZoneState state) {
this.name = name;
this.state = state;
}
public String getName() {
return name;
}
public ZoneState getState() {
return state;
}
@Override
public int hashCode() {
return Objects.hashCode(name, state);
}
@Override
public boolean equals(Object obj) {
if (this != obj) return false;
if (obj == null || getClass() != obj.getClass()) return false;
AvailabilityZone that = AvailabilityZone.class.cast(obj);
return Objects.equal(this.name, that.name) && Objects.equal(this.state, that.state);
}
protected Objects.ToStringHelper string() {
return Objects.toStringHelper(this)
.add("name", name)
.add("state", state);
}
@Override
public String toString() {
return string().toString();
}
}

View File

@ -0,0 +1,62 @@
/**
* 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.v2_0.domain.zonescoped;
import com.google.common.base.Objects;
import java.beans.ConstructorProperties;
/**
* @author Inbar Stolberg
*/
public class ZoneState {
private final boolean available;
protected ZoneState(boolean available) {
this.available = available;
}
public boolean available() {
return this.available;
}
@Override
public int hashCode() {
return Objects.hashCode(available);
}
@Override
public boolean equals(Object obj) {
if (this != obj) return false;
if (obj == null || getClass() != obj.getClass()) return false;
ZoneState that = ZoneState.class.cast(obj);
return Objects.equal(this.available, that.available);
}
protected Objects.ToStringHelper string() {
return Objects.toStringHelper(this)
.add("available", available);
}
@Override
public String toString() {
return string().toString();
}
}

View File

@ -0,0 +1,40 @@
/**
* 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.v2_0.extensions;
import com.google.common.annotations.Beta;
import com.google.common.collect.FluentIterable;
import org.jclouds.openstack.nova.v2_0.domain.zonescoped.AvailabilityZone;
import org.jclouds.openstack.v2_0.ServiceType;
import org.jclouds.openstack.v2_0.services.Extension;
/**
* @author Inbar Stolberg
*/
@Beta
@Extension(of = ServiceType.COMPUTE, namespace = ExtensionNamespaces.ADMIN_ACTIONS)
public interface AvailabilityZoneAPI {
/**
* @return all availability zones
*/
FluentIterable<? extends AvailabilityZone> list();
}

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.v2_0.extensions;
import com.google.common.annotations.Beta;
import com.google.common.collect.FluentIterable;
import com.google.common.util.concurrent.ListenableFuture;
import org.jclouds.Fallbacks;
import org.jclouds.openstack.keystone.v2_0.filters.AuthenticateRequest;
import org.jclouds.openstack.nova.v2_0.domain.zonescoped.AvailabilityZone;
import org.jclouds.openstack.v2_0.ServiceType;
import org.jclouds.openstack.v2_0.services.Extension;
import org.jclouds.rest.annotations.Fallback;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SelectJson;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
/**
* @author Inbar Stolberg
*/
@Beta
@Extension(of = ServiceType.COMPUTE, namespace = ExtensionNamespaces.ADMIN_ACTIONS)
@RequestFilters(AuthenticateRequest.class)
public interface AvailabilityZoneAsyncApi {
/**
* @return all availability zones
*/
@GET
@Path("/os-availability-zone")
@SelectJson("availabilityZoneInfo")
@Consumes(MediaType.APPLICATION_JSON)
@Fallback(Fallbacks.EmptyFluentIterableOnNotFoundOr404.class)
ListenableFuture<? extends FluentIterable<? extends AvailabilityZone>> list();
}

View File

@ -0,0 +1,64 @@
/**
* 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.v2_0.extensions;
import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.domain.zonescoped.AvailabilityZone;
import org.jclouds.openstack.nova.v2_0.internal.BaseNovaApiExpectTest;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
/**
* @author Inbar Stolberg
*/
@Test(groups = "unit", testName = "AvailabilityZoneApiExpectTest")
public class AvailabilityZoneApiExpectTest extends BaseNovaApiExpectTest {
public void testLAvailabilityZonesList() throws Exception {
HttpRequest list = HttpRequest
.builder()
.method("GET")
.endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/os-availability-zone")
.addHeader("Accept", "application/json")
.addHeader("X-Auth-Token", authToken).build();
HttpResponse listResponse = HttpResponse.builder().statusCode(200)
.payload(payloadFromResource("/listAvailabilityZones.json")).build();
NovaApi availabilityZonesApi = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName,
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, list, listResponse);
assertEquals(availabilityZonesApi.getConfiguredZones(), ImmutableSet.of("az-1.region-a.geo-1", "az-2.region-a.geo-1", "az-3.region-a.geo-1"));
FluentIterable<? extends AvailabilityZone> zones = availabilityZonesApi.getAvailabilityZoneApi("az-1.region-a.geo-1").list();
Optional<? extends AvailabilityZone> zone = zones.first();
assertTrue(zone.isPresent(), "Couldn't find zone");
assertTrue(zone.get().getName().equals("nova"), "Expected zone name to be nova but it was: " + zone.get().getName());
assertTrue(zone.get().getState().available(), "Zone: "+ zone.get().getName() + " is not available.");
}
}

View File

@ -0,0 +1,45 @@
/**
* 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.v2_0.extensions;
import com.google.common.collect.FluentIterable;
import org.jclouds.openstack.nova.v2_0.domain.zonescoped.AvailabilityZone;
import org.jclouds.openstack.nova.v2_0.internal.BaseNovaApiLiveTest;
import org.testng.annotations.Test;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
/**
* @author inbar stolberg
*/
@Test(groups = "live", testName = "AvailabilityZonesApiLiveTest")
public class AvailabilityZonesApiLiveTest extends BaseNovaApiLiveTest {
@Test
public void testListAvailabilityZones() throws Exception {
AvailabilityZoneAPI availabilityZoneApi = api.getAvailabilityZoneApi("RegionOne");
FluentIterable<? extends AvailabilityZone> zones = availabilityZoneApi.list();
for (AvailabilityZone zone : zones) {
assertNotNull(zone.getName());
assertTrue(zone.getState().available(), "zone: " + zone.getName() + " is not available.");
}
}
}

View File

@ -0,0 +1,11 @@
{
"availabilityZoneInfo": [
{
"zoneState": {
"available": true
},
"hosts": null,
"zoneName": "nova"
}
]
}