Split availability Zone API to return Basic Info and Detailed info

This commit is contained in:
Arvind Nadendla 2015-07-15 11:02:01 -07:00 committed by Zack Shoylev
parent 2e1aa3c055
commit a38ee21207
5 changed files with 238 additions and 60 deletions

View File

@ -17,58 +17,23 @@
package org.jclouds.openstack.nova.v2_0.domain.regionscoped; package org.jclouds.openstack.nova.v2_0.domain.regionscoped;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.collect.ImmutableMap;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.beans.ConstructorProperties; import java.beans.ConstructorProperties;
import java.util.Date;
import java.util.Map;
/**
* Availability Zone which shows name and state
*/
public class AvailabilityZone { public class AvailabilityZone {
public static final class HostService {
private final boolean available;
private final boolean active;
@SerializedName("updated_at")
private final Date updated;
@ConstructorProperties({"available", "active", "updated_at"})
protected HostService(boolean available, boolean active, Date updated) {
this.available = available;
this.active = active;
this.updated = updated;
}
public boolean isAvailable() { return available; }
public boolean isActive() { return active; }
public Date getUpdated() { return updated; }
protected Objects.ToStringHelper string() {
return Objects.toStringHelper(this)
.add("available", available)
.add("active", active)
.add("updated", updated);
}
@Override
public String toString() {
return string().toString();
}
}
@SerializedName("zoneName") @SerializedName("zoneName")
private final String name; private final String name;
private final ZoneState state; private final ZoneState state;
private final Map<String, Map<String, HostService>> hosts;
@ConstructorProperties({"zoneName" , "zoneState", "hosts"}) @ConstructorProperties({"zoneName" , "zoneState"})
protected AvailabilityZone(String name, ZoneState state, Map<String, Map<String, HostService>> hosts) { protected AvailabilityZone(String name, ZoneState state) {
this.name = name; this.name = name;
this.state = state; this.state = state;
this.hosts = hosts == null ? ImmutableMap.<String, Map<String, HostService>>of() : ImmutableMap.copyOf(hosts);
} }
public String getName() { public String getName() {
@ -79,16 +44,9 @@ public class AvailabilityZone {
return state; return state;
} }
/**
* @return returns a map of host name and Host service objects
*/
public Map<String, Map<String, HostService>> getHosts() {
return this.hosts;
}
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hashCode(name, state, hosts); return Objects.hashCode(name, state);
} }
@Override @Override
@ -97,15 +55,13 @@ public class AvailabilityZone {
return false; return false;
if (obj == null || getClass() != obj.getClass()) return false; if (obj == null || getClass() != obj.getClass()) return false;
AvailabilityZone that = AvailabilityZone.class.cast(obj); AvailabilityZone that = AvailabilityZone.class.cast(obj);
return Objects.equal(this.name, that.name) && Objects.equal(this.state, that.state) && Objects.equal(this.hosts, return Objects.equal(this.name, that.name) && Objects.equal(this.state, that.state);
that.hosts);
} }
protected Objects.ToStringHelper string() { protected Objects.ToStringHelper string() {
return Objects.toStringHelper(this) return Objects.toStringHelper(this)
.add("name", name) .add("name", name)
.add("state", state) .add("state", state);
.add("Hosts", hosts);
} }
@Override @Override

View File

@ -0,0 +1,121 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.regionscoped;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableMap;
import com.google.gson.annotations.SerializedName;
import java.beans.ConstructorProperties;
import java.util.Date;
import java.util.Map;
/**
* Availability Zone which show name, state and host information.
*
* Host information includes the host name and the services running on the hosts.
*/
public class AvailabilityZoneDetails {
public static final class HostService {
private final boolean available;
private final boolean active;
@SerializedName("updated_at")
private final Date updated;
@ConstructorProperties({"available", "active", "updated_at"})
protected HostService(boolean available, boolean active, Date updated) {
this.available = available;
this.active = active;
this.updated = updated;
}
public boolean isAvailable() { return available; }
public boolean isActive() { return active; }
public Date getUpdated() { return updated; }
protected Objects.ToStringHelper string() {
return Objects.toStringHelper(this)
.add("available", available)
.add("active", active)
.add("updated", updated);
}
@Override
public String toString() {
return string().toString();
}
}
@SerializedName("zoneName")
private final String name;
private final ZoneState state;
private final Map<String, Map<String, HostService>> hosts;
@ConstructorProperties({"zoneName" , "zoneState", "hosts"})
protected AvailabilityZoneDetails(String name, ZoneState state, Map<String, Map<String, HostService>> hosts) {
this.name = name;
this.state = state;
this.hosts = hosts == null ? ImmutableMap.<String, Map<String, HostService>>of() : ImmutableMap.copyOf(hosts);
}
public String getName() {
return name;
}
public ZoneState getState() {
return state;
}
/**
* @return returns a map of host name and Host service objects
*/
public Map<String, Map<String, HostService>> getHosts() {
return this.hosts;
}
@Override
public int hashCode() {
return Objects.hashCode(name, state, hosts);
}
@Override
public boolean equals(Object obj) {
if (this != obj)
return false;
if (obj == null || getClass() != obj.getClass()) return false;
AvailabilityZoneDetails that = AvailabilityZoneDetails.class.cast(obj);
return Objects.equal(this.name, that.name) && Objects.equal(this.state, that.state) && Objects.equal(this.hosts,
that.hosts);
}
protected Objects.ToStringHelper string() {
return Objects.toStringHelper(this)
.add("name", name)
.add("state", state)
.add("Hosts", hosts);
}
@Override
public String toString() {
return string().toString();
}
}

View File

@ -21,6 +21,7 @@ import com.google.common.collect.FluentIterable;
import org.jclouds.Fallbacks.EmptyFluentIterableOnNotFoundOr404; import org.jclouds.Fallbacks.EmptyFluentIterableOnNotFoundOr404;
import org.jclouds.openstack.keystone.v2_0.filters.AuthenticateRequest; import org.jclouds.openstack.keystone.v2_0.filters.AuthenticateRequest;
import org.jclouds.openstack.nova.v2_0.domain.regionscoped.AvailabilityZone; import org.jclouds.openstack.nova.v2_0.domain.regionscoped.AvailabilityZone;
import org.jclouds.openstack.nova.v2_0.domain.regionscoped.AvailabilityZoneDetails;
import org.jclouds.openstack.v2_0.ServiceType; import org.jclouds.openstack.v2_0.ServiceType;
import org.jclouds.openstack.v2_0.services.Extension; import org.jclouds.openstack.v2_0.services.Extension;
import org.jclouds.rest.annotations.Fallback; import org.jclouds.rest.annotations.Fallback;
@ -54,12 +55,16 @@ public interface AvailabilityZoneApi {
@Fallback(EmptyFluentIterableOnNotFoundOr404.class) @Fallback(EmptyFluentIterableOnNotFoundOr404.class)
FluentIterable<org.jclouds.openstack.nova.v2_0.domain.zonescoped.AvailabilityZone> list(); FluentIterable<org.jclouds.openstack.nova.v2_0.domain.zonescoped.AvailabilityZone> list();
/**
* @return all availability zones
*/
@Named("availabilityZone:list") @Named("availabilityZone:list")
@GET @GET
@SelectJson("availabilityZoneInfo") @SelectJson("availabilityZoneInfo")
@Fallback(EmptyFluentIterableOnNotFoundOr404.class) @Fallback(EmptyFluentIterableOnNotFoundOr404.class)
FluentIterable<AvailabilityZone> listAvailabilityZones(); FluentIterable<AvailabilityZone> listAvailabilityZones();
@Named("availabilityZone:list")
@GET
@Path("/detail")
@SelectJson("availabilityZoneInfo")
@Fallback(EmptyFluentIterableOnNotFoundOr404.class)
FluentIterable<AvailabilityZoneDetails> listInDetail();
} }

View File

@ -23,6 +23,8 @@ import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.nova.v2_0.NovaApi; import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.domain.regionscoped.AvailabilityZone; import org.jclouds.openstack.nova.v2_0.domain.regionscoped.AvailabilityZone;
import org.jclouds.openstack.nova.v2_0.domain.regionscoped.AvailabilityZoneDetails;
import org.jclouds.openstack.nova.v2_0.domain.regionscoped.AvailabilityZoneDetails.HostService;
import org.jclouds.openstack.nova.v2_0.internal.BaseNovaApiExpectTest; import org.jclouds.openstack.nova.v2_0.internal.BaseNovaApiExpectTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -30,11 +32,34 @@ import java.util.Date;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
@Test(groups = "unit", testName = "AvailabilityZoneApiExpectTest") @Test(groups = "unit", testName = "AvailabilityZoneApiExpectTest")
public class AvailabilityZoneApiExpectTest extends BaseNovaApiExpectTest { public class AvailabilityZoneApiExpectTest extends BaseNovaApiExpectTest {
public void testAvailabilityZonesList() throws Exception { public void testWhenNamespaceInExtensionsListAvailabilityZonesPresent() throws Exception {
NovaApi apiWhenExtensionInList = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName,
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse);
assertEquals(apiWhenExtensionInList.getConfiguredRegions(), ImmutableSet.of("az-1.region-a.geo-1", "az-2.region-a.geo-1", "az-3.region-a.geo-1"));
assertTrue(apiWhenExtensionInList.getFloatingIPApi("az-1.region-a.geo-1").isPresent());
}
public void testWhenNamespaceNotInExtensionsListAvailabilityZonesPresent() throws Exception {
NovaApi apiWhenExtensionNotInList = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName,
responseWithKeystoneAccess, extensionsOfNovaRequest, unmatchedExtensionsOfNovaResponse);
assertEquals(apiWhenExtensionNotInList.getConfiguredRegions(), ImmutableSet.of("az-1.region-a.geo-1", "az-2.region-a.geo-1", "az-3.region-a.geo-1"));
assertFalse(apiWhenExtensionNotInList.getFloatingIPApi("az-1.region-a.geo-1").isPresent());
}
public void testListAvailabilityZones() throws Exception {
HttpRequest list = HttpRequest HttpRequest list = HttpRequest
.builder() .builder()
.method("GET") .method("GET")
@ -45,14 +70,54 @@ public class AvailabilityZoneApiExpectTest extends BaseNovaApiExpectTest {
HttpResponse listResponse = HttpResponse.builder().statusCode(200) HttpResponse listResponse = HttpResponse.builder().statusCode(200)
.payload(payloadFromResource("/listAvailabilityZones.json")).build(); .payload(payloadFromResource("/listAvailabilityZones.json")).build();
NovaApi availabilityZonesApi = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, responseWithKeystoneAccess,
extensionsOfNovaRequest, extensionsOfNovaResponse, list, listResponse);
assertEquals(availabilityZonesApi.getConfiguredRegions(), 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").get().listAvailabilityZones();
Optional<? extends AvailabilityZone> zone = zones.first();
assertTrue(zone.isPresent(), "Couldn't find zone");
assertEquals(zone.get().getName(), "internal", "Expected zone name to be internal but it was: " + zone.get().getName());
assertTrue(zone.get().getState().isAvailable(), "Zone: " + zone.get().getName() + " is not available.");
}
public void testListAvailabilityZonesWhenResponseIs404() throws Exception {
HttpRequest list = HttpRequest.builder()
.method("GET")
.endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v2/3456/os-availability-zone")
.addHeader("Accept", "application/json")
.addHeader("X-Auth-Token", authToken).build();
HttpResponse listResponse = HttpResponse.builder().statusCode(404).build();
NovaApi apiWhenNoServersExist = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, responseWithKeystoneAccess,
extensionsOfNovaRequest, extensionsOfNovaResponse, list, listResponse);
assertTrue(apiWhenNoServersExist.getAvailabilityZoneApi("az-1.region-a.geo-1").get().listAvailabilityZones().isEmpty());
}
public void testListInDetail() throws Exception {
HttpRequest list = HttpRequest
.builder()
.method("GET")
.endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v2/3456/os-availability-zone/detail")
.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, NovaApi availabilityZonesApi = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName,
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, list, listResponse); responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, list, listResponse);
assertEquals(availabilityZonesApi.getConfiguredRegions(), ImmutableSet.of("az-1.region-a.geo-1", "az-2.region-a.geo-1", "az-3.region-a.geo-1")); assertEquals(availabilityZonesApi.getConfiguredRegions(), 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").get().listAvailabilityZones(); FluentIterable<? extends AvailabilityZoneDetails> zones = availabilityZonesApi.getAvailabilityZoneApi("az-1.region-a.geo-1").get().listInDetail();
Optional<? extends AvailabilityZone> zone = zones.first(); Optional<? extends AvailabilityZoneDetails> zone = zones.first();
assertTrue(zone.isPresent(), "Couldn't find zone"); assertTrue(zone.isPresent(), "Couldn't find zone");
assertEquals(zone.get() assertEquals(zone.get()
@ -67,10 +132,25 @@ public class AvailabilityZoneApiExpectTest extends BaseNovaApiExpectTest {
String hostServiceName = zone.get().getHosts().get(hostName).keySet().iterator().next(); String hostServiceName = zone.get().getHosts().get(hostName).keySet().iterator().next();
assertEquals(hostServiceName, "nova-conductor", assertEquals(hostServiceName, "nova-conductor",
"Expected host service name to be nova-conductor but it was: " + hostServiceName); "Expected host service name to be nova-conductor but it was: " + hostServiceName);
AvailabilityZone.HostService hostService = zone.get().getHosts().get(hostName).get(hostServiceName); HostService hostService = zone.get().getHosts().get(hostName).get(hostServiceName);
assertTrue(hostService.isAvailable(), "Couldn't find host service availability"); assertTrue(hostService.isAvailable(), "Couldn't find host service availability");
assertTrue(hostService.isActive(), "Couldn't find host service state"); assertTrue(hostService.isActive(), "Couldn't find host service state");
assertEquals(hostService.getUpdated(), new Date(1436509815000L), assertEquals(hostService.getUpdated(), new Date(1436509815000L),
"Expected Updated time: " + new Date(1436509815000L) + " does match Updated time : " + hostService.getUpdated()); "Expected Updated time: " + new Date(1436509815000L) + " does match Updated time : " + hostService.getUpdated());
} }
public void testListInDetailWhenResponseIs404() throws Exception {
HttpRequest list = HttpRequest.builder()
.method("GET")
.endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v2/3456/os-availability-zone/detail")
.addHeader("Accept", "application/json")
.addHeader("X-Auth-Token", authToken).build();
HttpResponse listResponse = HttpResponse.builder().statusCode(404).build();
NovaApi apiWhenNoServersExist = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, responseWithKeystoneAccess,
extensionsOfNovaRequest, extensionsOfNovaResponse, list, listResponse);
assertTrue(apiWhenNoServersExist.getAvailabilityZoneApi("az-1.region-a.geo-1").get().listInDetail().isEmpty());
}
} }

View File

@ -19,6 +19,8 @@ package org.jclouds.openstack.nova.v2_0.extensions;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable; import com.google.common.collect.FluentIterable;
import org.jclouds.openstack.nova.v2_0.domain.regionscoped.AvailabilityZone; import org.jclouds.openstack.nova.v2_0.domain.regionscoped.AvailabilityZone;
import org.jclouds.openstack.nova.v2_0.domain.regionscoped.AvailabilityZoneDetails;
import org.jclouds.openstack.nova.v2_0.domain.regionscoped.AvailabilityZoneDetails.HostService;
import org.jclouds.openstack.nova.v2_0.internal.BaseNovaApiLiveTest; import org.jclouds.openstack.nova.v2_0.internal.BaseNovaApiLiveTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -36,6 +38,20 @@ public class AvailabilityZonesApiLiveTest extends BaseNovaApiLiveTest {
FluentIterable<? extends AvailabilityZone> zones = availabilityZoneApi.get().listAvailabilityZones(); FluentIterable<? extends AvailabilityZone> zones = availabilityZoneApi.get().listAvailabilityZones();
for (AvailabilityZone zone : zones) { for (AvailabilityZone zone : zones) {
assertNotNull(zone.getName());
assertTrue(zone.getState().isAvailable(), "zone: " + zone.getName() + " is not available.");
}
}
}
@Test
public void testListInDetail() throws Exception {
Optional<? extends AvailabilityZoneApi> availabilityZoneApi = api.getAvailabilityZoneApi("RegionOne");
if (availabilityZoneApi.isPresent()) {
FluentIterable<? extends AvailabilityZoneDetails> zones = availabilityZoneApi.get().listInDetail();
for (AvailabilityZoneDetails zone : zones) {
assertNotNull(zone.getName()); assertNotNull(zone.getName());
assertTrue(zone.getState() assertTrue(zone.getState()
.isAvailable(), "zone: " + zone.getName() + " is not available."); .isAvailable(), "zone: " + zone.getName() + " is not available.");
@ -43,7 +59,7 @@ public class AvailabilityZonesApiLiveTest extends BaseNovaApiLiveTest {
assertNotNull(hostName, "Expected host name to be not null"); assertNotNull(hostName, "Expected host name to be not null");
String hostServiceName = zone.getHosts().get(hostName).keySet().iterator().next(); String hostServiceName = zone.getHosts().get(hostName).keySet().iterator().next();
assertNotNull(hostServiceName, "Expected host service name to be not null"); assertNotNull(hostServiceName, "Expected host service name to be not null");
AvailabilityZone.HostService hostService = zone.getHosts().get(hostName).get(hostServiceName); HostService hostService = zone.getHosts().get(hostName).get(hostServiceName);
assertTrue(hostService.isAvailable(), "Couldn't find host service availability"); assertTrue(hostService.isAvailable(), "Couldn't find host service availability");
assertTrue(hostService.isActive(), "Couldn't find host service state"); assertTrue(hostService.isActive(), "Couldn't find host service state");
assertNotNull(hostService.getUpdated(), "Expected Updated time, but none received "); assertNotNull(hostService.getUpdated(), "Expected Updated time, but none received ");