Adding Simple Tenant Usage extension

This commit is contained in:
Adam Lowe 2012-04-19 10:42:30 +01:00
parent f45b9ca849
commit adc6e2aa93
11 changed files with 856 additions and 8 deletions

View File

@ -27,6 +27,7 @@ import org.jclouds.openstack.nova.v1_1.extensions.FloatingIPAsyncClient;
import org.jclouds.openstack.nova.v1_1.extensions.HostAdministrationAsyncClient;
import org.jclouds.openstack.nova.v1_1.extensions.KeyPairAsyncClient;
import org.jclouds.openstack.nova.v1_1.extensions.SecurityGroupAsyncClient;
import org.jclouds.openstack.nova.v1_1.extensions.SimpleTenantUsageAsyncClient;
import org.jclouds.openstack.nova.v1_1.features.ExtensionAsyncClient;
import org.jclouds.openstack.nova.v1_1.features.FlavorAsyncClient;
import org.jclouds.openstack.nova.v1_1.features.ImageAsyncClient;
@ -112,4 +113,11 @@ public interface NovaAsyncClient {
Optional<HostAdministrationAsyncClient> getHostAdministrationExtensionForZone(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone);
/**
* Provides asynchronous access to Simple Tenant Usage features.
*/
@Delegate
Optional<SimpleTenantUsageAsyncClient> getSimpleTenantUsageExtensionForZone(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone);
}

View File

@ -29,6 +29,7 @@ import org.jclouds.openstack.nova.v1_1.extensions.FloatingIPClient;
import org.jclouds.openstack.nova.v1_1.extensions.HostAdministrationClient;
import org.jclouds.openstack.nova.v1_1.extensions.KeyPairClient;
import org.jclouds.openstack.nova.v1_1.extensions.SecurityGroupClient;
import org.jclouds.openstack.nova.v1_1.extensions.SimpleTenantUsageClient;
import org.jclouds.openstack.nova.v1_1.features.ExtensionClient;
import org.jclouds.openstack.nova.v1_1.features.FlavorClient;
import org.jclouds.openstack.nova.v1_1.features.ImageClient;
@ -114,4 +115,11 @@ public interface NovaClient {
Optional<HostAdministrationClient> getHostAdministrationExtensionForZone(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone);
/**
* Provides synchronous access to Simple Tenant Usage features.
*/
@Delegate
Optional<SimpleTenantUsageClient> getSimpleTenantUsageExtensionForZone(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone);
}

View File

@ -35,14 +35,7 @@ import org.jclouds.openstack.keystone.v2_0.config.KeystoneAuthenticationModule;
import org.jclouds.openstack.nova.v1_1.NovaAsyncClient;
import org.jclouds.openstack.nova.v1_1.NovaClient;
import org.jclouds.openstack.nova.v1_1.domain.Extension;
import org.jclouds.openstack.nova.v1_1.extensions.FloatingIPAsyncClient;
import org.jclouds.openstack.nova.v1_1.extensions.FloatingIPClient;
import org.jclouds.openstack.nova.v1_1.extensions.HostAdministrationAsyncClient;
import org.jclouds.openstack.nova.v1_1.extensions.HostAdministrationClient;
import org.jclouds.openstack.nova.v1_1.extensions.KeyPairAsyncClient;
import org.jclouds.openstack.nova.v1_1.extensions.KeyPairClient;
import org.jclouds.openstack.nova.v1_1.extensions.SecurityGroupAsyncClient;
import org.jclouds.openstack.nova.v1_1.extensions.SecurityGroupClient;
import org.jclouds.openstack.nova.v1_1.extensions.*;
import org.jclouds.openstack.nova.v1_1.features.ExtensionAsyncClient;
import org.jclouds.openstack.nova.v1_1.features.ExtensionClient;
import org.jclouds.openstack.nova.v1_1.features.FlavorAsyncClient;
@ -79,6 +72,7 @@ public class NovaRestClientModule extends RestClientModule<NovaClient, NovaAsync
.put(SecurityGroupClient.class, SecurityGroupAsyncClient.class)
.put(KeyPairClient.class, KeyPairAsyncClient.class)
.put(HostAdministrationClient.class, HostAdministrationAsyncClient.class)
.put(SimpleTenantUsageClient.class, SimpleTenantUsageAsyncClient.class)
.build();
public NovaRestClientModule() {

View File

@ -0,0 +1,302 @@
/**
* 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.v1_1.domain;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Date;
import org.jclouds.javax.annotation.Nullable;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import com.google.gson.annotations.SerializedName;
/**
* Information the SimpleTenantUsage extension return data about each Server
*
* @author Adam Lowe
*/
public class SimpleServerUsage {
public static enum Status {
UNRECOGNIZED, ACTIVE;
public String value() {
return name();
}
public static Status fromValue(String v) {
try {
return valueOf(v.toUpperCase());
} catch (IllegalArgumentException e) {
return UNRECOGNIZED;
}
}
}
public static Builder<?> builder() {
return new ConcreteBuilder();
}
public Builder<?> toBuilder() {
return new ConcreteBuilder().fromSimpleServerUsage(this);
}
public static abstract class Builder<T extends Builder<T>> {
private String instanceName;
private double hours;
private double flavorMemoryMb;
private double flavorLocalGb;
private double flavorVcpus;
private String tenantId;
private String flavorName;
private Date instanceCreated;
private Date instanceTerminiated;
private Status instanceStatus;
private long uptime;
protected abstract T self();
public T instanceName(String instanceName) {
this.instanceName = instanceName;
return self();
}
public T hours(double hours) {
this.hours = hours;
return self();
}
public T flavorMemoryMb(double flavorMemoryMb) {
this.flavorMemoryMb = flavorMemoryMb;
return self();
}
public T flavorLocalGb(double flavorLocalGb) {
this.flavorLocalGb = flavorLocalGb;
return self();
}
public T flavorVcpus(double flavorVcpus) {
this.flavorVcpus = flavorVcpus;
return self();
}
public T tenantId(String tenantId) {
this.tenantId = tenantId;
return self();
}
public T flavorName(String flavorName) {
this.flavorName = flavorName;
return self();
}
public T instanceCreated(Date instanceCreated) {
this.instanceCreated = instanceCreated;
return self();
}
public T instanceTerminiated(Date instanceTerminiated) {
this.instanceTerminiated = instanceTerminiated;
return self();
}
public T instanceStatus(Status instanceStatus) {
this.instanceStatus = instanceStatus;
return self();
}
public T uptime(long uptime) {
this.uptime = uptime;
return self();
}
public SimpleServerUsage build() {
return new SimpleServerUsage(this);
}
public T fromSimpleServerUsage(SimpleServerUsage in) {
return this
.instanceName(in.getInstanceName())
.flavorMemoryMb(in.getFlavorMemoryMb())
.flavorLocalGb(in.getFlavorLocalGb())
.flavorVcpus(in.getFlavorVcpus())
.tenantId(in.getTenantId())
.flavorName(in.getFlavorName())
.instanceCreated(in.getInstanceCreated())
.instanceTerminiated(in.getInstanceTerminiated())
.instanceStatus(in.getInstanceStatus())
.uptime(in.getUptime())
;
}
}
private static class ConcreteBuilder extends Builder<ConcreteBuilder> {
@Override
protected ConcreteBuilder self() {
return this;
}
}
@SerializedName("name")
private final String instanceName;
private final double hours;
@SerializedName("memory_mb")
private final double flavorMemoryMb;
@SerializedName("local_gb")
private final double flavorLocalGb;
@SerializedName("vcpus")
private final double flavorVcpus;
@SerializedName("tenant_id")
private final String tenantId;
@SerializedName("flavor")
private final String flavorName;
@SerializedName("started_at")
private final Date instanceCreated;
@SerializedName("ended_at")
private final Date instanceTerminiated;
@SerializedName("state")
private final Status instanceStatus;
private final long uptime;
private SimpleServerUsage(Builder<?> builder) {
this.instanceName = checkNotNull(builder.instanceName, "instanceName");
this.hours = builder.hours;
this.flavorMemoryMb = builder.flavorMemoryMb;
this.flavorLocalGb = builder.flavorLocalGb;
this.flavorVcpus = builder.flavorVcpus;
this.tenantId = checkNotNull(builder.tenantId, "tenantId");
this.flavorName = checkNotNull(builder.flavorName, "flavorName");
this.instanceCreated = builder.instanceCreated; //checkNotNull(builder.instanceCreated, "instanceCreated");
this.instanceTerminiated = builder.instanceTerminiated;
this.instanceStatus = checkNotNull(builder.instanceStatus, "instanceStatus");
this.uptime = checkNotNull(builder.uptime, "uptime");
}
/**
*/
public String getInstanceName() {
return this.instanceName;
}
/**
*/
public double getFlavorMemoryMb() {
return this.flavorMemoryMb;
}
/**
*/
public double getFlavorLocalGb() {
return this.flavorLocalGb;
}
/**
*/
public double getFlavorVcpus() {
return this.flavorVcpus;
}
/**
*/
public String getTenantId() {
return this.tenantId;
}
/**
*/
public String getFlavorName() {
return this.flavorName;
}
/**
*/
public Date getInstanceCreated() {
return this.instanceCreated;
}
/**
*/
@Nullable
public Date getInstanceTerminiated() {
return this.instanceTerminiated;
}
/**
*/
public Status getInstanceStatus() {
return this.instanceStatus;
}
/**
*/
public long getUptime() {
return this.uptime;
}
@Override
public int hashCode() {
return Objects.hashCode(instanceName, flavorMemoryMb, flavorLocalGb, flavorVcpus, tenantId, flavorName, instanceCreated, instanceTerminiated, instanceStatus, uptime);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
SimpleServerUsage that = SimpleServerUsage.class.cast(obj);
return Objects.equal(this.instanceName, that.instanceName)
&& Objects.equal(this.flavorMemoryMb, that.flavorMemoryMb)
&& Objects.equal(this.flavorLocalGb, that.flavorLocalGb)
&& Objects.equal(this.flavorVcpus, that.flavorVcpus)
&& Objects.equal(this.tenantId, that.tenantId)
&& Objects.equal(this.flavorName, that.flavorName)
&& Objects.equal(this.instanceCreated, that.instanceCreated)
&& Objects.equal(this.instanceTerminiated, that.instanceTerminiated)
&& Objects.equal(this.instanceStatus, that.instanceStatus)
&& Objects.equal(this.uptime, that.uptime)
;
}
protected ToStringHelper string() {
return Objects.toStringHelper("")
.add("instanceName", instanceName)
.add("flavorMemoryMb", flavorMemoryMb)
.add("flavorLocalGb", flavorLocalGb)
.add("flavorVcpus", flavorVcpus)
.add("tenantId", tenantId)
.add("flavorName", flavorName)
.add("instanceCreated", instanceCreated)
.add("instanceTerminiated", instanceTerminiated)
.add("instanceStatus", instanceStatus)
.add("uptime", uptime)
;
}
@Override
public String toString() {
return string().toString();
}
}

View File

@ -0,0 +1,239 @@
/**
* 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.v1_1.domain;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Collections;
import java.util.Date;
import java.util.Set;
import org.jclouds.javax.annotation.Nullable;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.gson.annotations.SerializedName;
/**
* Information the SimpleTenantUsage extension returns data about each tenant
*
* @author Adam Lowe
*/
public class SimpleTenantUsage {
public static Builder<?> builder() {
return new ConcreteBuilder();
}
public Builder<?> toBuilder() {
return new ConcreteBuilder().fromSimpleTenantUsage(this);
}
public static abstract class Builder<T extends Builder<T>> {
private String tenantId;
private double totalLocalGbUsage;
private double totalVcpusUsage;
private double totalMemoryMbUsage;
private double totalHours;
private Date start;
private Date stop;
private Set<SimpleServerUsage> serverUsages = Sets.newLinkedHashSet();
protected abstract T self();
public T tenantId(String tenantId) {
this.tenantId = tenantId;
return self();
}
public T totalLocalGbUsage(double total_local_gb_usage) {
this.totalLocalGbUsage = total_local_gb_usage;
return self();
}
public T totalVcpusUsage(double total_vcpus_usage) {
this.totalVcpusUsage = total_vcpus_usage;
return self();
}
public T totalMemoryMbUsage(double total_memory_mb_usage) {
this.totalMemoryMbUsage = total_memory_mb_usage;
return self();
}
public T totalHours(double total_hours) {
this.totalHours = total_hours;
return self();
}
public T start(Date start) {
this.start = start;
return self();
}
public T stop(Date stop) {
this.stop = stop;
return self();
}
public T serverUsages(Set<SimpleServerUsage> serverUsages) {
this.serverUsages = serverUsages;
return self();
}
public SimpleTenantUsage build() {
return new SimpleTenantUsage(this);
}
public T fromSimpleTenantUsage(SimpleTenantUsage in) {
return this
.totalLocalGbUsage(in.getTotalLocalGbUsage())
.totalVcpusUsage(in.getTotalVcpusUsage())
.totalMemoryMbUsage(in.getTotalMemoryMbUsage())
.totalHours(in.getTotalHours())
.start(in.getStart())
.stop(in.getStop())
.serverUsages(in.getServerUsages())
;
}
}
private static class ConcreteBuilder extends Builder<ConcreteBuilder> {
@Override
protected ConcreteBuilder self() {
return this;
}
}
@SerializedName("tenant_id")
private final String tenantId;
@SerializedName("total_local_gb_usage")
private final double totalLocalGbUsage;
@SerializedName("total_vcpus_usage")
private final double totalVcpusUsage;
@SerializedName("total_memory_mb_usage")
private final double totalMemoryMbUsage;
@SerializedName("total_hours")
private final double totalHours;
private final Date start;
private final Date stop;
@SerializedName("server_usages")
private final Set<SimpleServerUsage> serverUsages;
private SimpleTenantUsage(Builder<?> builder) {
this.tenantId = builder.tenantId;
this.totalLocalGbUsage = builder.totalLocalGbUsage;
this.totalVcpusUsage = builder.totalVcpusUsage;
this.totalMemoryMbUsage = builder.totalMemoryMbUsage;
this.totalHours = builder.totalHours;
this.start = builder.start;
this.stop = builder.stop;
this.serverUsages = ImmutableSet.copyOf(checkNotNull(builder.serverUsages, "serverUsages"));
}
public String getTenantId() {
return tenantId;
}
/**
*/
public double getTotalLocalGbUsage() {
return this.totalLocalGbUsage;
}
/**
*/
public double getTotalVcpusUsage() {
return this.totalVcpusUsage;
}
/**
*/
public double getTotalMemoryMbUsage() {
return this.totalMemoryMbUsage;
}
/**
*/
public double getTotalHours() {
return this.totalHours;
}
/**
*/
@Nullable
public Date getStart() {
return this.start;
}
/**
*/
@Nullable
public Date getStop() {
return this.stop;
}
/**
*/
@Nullable
public Set<SimpleServerUsage> getServerUsages() {
return serverUsages == null ? ImmutableSet.<SimpleServerUsage>of() : Collections.unmodifiableSet(this.serverUsages);
}
@Override
public int hashCode() {
return Objects.hashCode(totalLocalGbUsage, totalVcpusUsage, totalMemoryMbUsage, totalHours, start, stop, serverUsages);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
SimpleTenantUsage that = SimpleTenantUsage.class.cast(obj);
return Objects.equal(this.totalLocalGbUsage, that.totalLocalGbUsage)
&& Objects.equal(this.totalVcpusUsage, that.totalVcpusUsage)
&& Objects.equal(this.totalMemoryMbUsage, that.totalMemoryMbUsage)
&& Objects.equal(this.totalHours, that.totalHours)
&& Objects.equal(this.start, that.start)
&& Objects.equal(this.stop, that.stop)
&& Objects.equal(this.serverUsages, that.serverUsages)
;
}
protected ToStringHelper string() {
return Objects.toStringHelper("")
.add("totalLocalGbUsage", totalLocalGbUsage)
.add("totalVcpusUsage", totalVcpusUsage)
.add("totalMemoryMbUsage", totalMemoryMbUsage)
.add("totalHours", totalHours)
.add("start", start)
.add("stop", stop)
.add("serverUsages", serverUsages)
;
}
@Override
public String toString() {
return string().toString();
}
}

View File

@ -0,0 +1,77 @@
/**
* 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.v1_1.extensions;
import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import org.jclouds.openstack.filters.AuthenticateRequest;
import org.jclouds.openstack.nova.v1_1.domain.SimpleTenantUsage;
import org.jclouds.openstack.services.Extension;
import org.jclouds.openstack.services.ServiceType;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SelectJson;
import org.jclouds.rest.annotations.SkipEncoding;
import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import com.google.common.util.concurrent.ListenableFuture;
/**
* Provides asynchronous access to Simple Tenant Usage via the REST API.
* <p/>
*
* @author Adam Lowe
* @see SimpleTenantUsageClient
* @see <a href= "http://docs.openstack.org/api/openstack-compute/2/content/Extensions-d1e1444.html" />
* @see <a href="http://nova.openstack.org/api_ext" />
* @see <a href="http://nova.openstack.org/api/nova.api.openstack.compute.contrib.simple_tenant_usage.html" />
*/
@Extension(of = ServiceType.COMPUTE, namespace = ExtensionNamespaces.SIMPLE_TENANT_USAGE)
@SkipEncoding({'/', '='})
@RequestFilters(AuthenticateRequest.class)
public interface SimpleTenantUsageAsyncClient {
/**
* @see SimpleTenantUsageClient#listTenantUsages()
*/
@GET
@Path("/os-simple-tenant-usage")
@SelectJson("tenant_usages")
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
ListenableFuture<Set<SimpleTenantUsage>> listTenantUsages();
/**
* @see SimpleTenantUsageClient#getTenantUsage(String)
*/
@GET
@Path("/os-simple-tenant-usage/{id}")
@SelectJson("tenant_usage")
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<SimpleTenantUsage> getTenantUsage(@PathParam("id") String tenantId);
}

View File

@ -0,0 +1,53 @@
/**
* 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.v1_1.extensions;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.jclouds.concurrent.Timeout;
import org.jclouds.openstack.nova.v1_1.domain.SimpleTenantUsage;
import org.jclouds.openstack.services.Extension;
import org.jclouds.openstack.services.ServiceType;
/**
* Provides asynchronous access to Simple Tenant Usage via the REST API.
* <p/>
*
* @author Adam Lowe
* @see SimpleTenantUsageAsyncClient
*/
@Extension(of = ServiceType.COMPUTE, namespace = ExtensionNamespaces.SIMPLE_TENANT_USAGE)
@Timeout(duration = 180, timeUnit = TimeUnit.SECONDS)
public interface SimpleTenantUsageClient {
/**
* Retrive tenant_usage for all tenants
*
* @return the set of TenantUsage reports
*/
Set<SimpleTenantUsage> listTenantUsages();
/**
* Retrive tenant_usage for a specified tenant
*
* @return the requested tenant usage
*/
SimpleTenantUsage getTenantUsage(String tenantId);
}

View File

@ -0,0 +1,110 @@
/**
* 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.v1_1.extensions;
import static org.testng.Assert.*;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.Set;
import javax.ws.rs.core.MediaType;
import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.date.DateService;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.nova.v1_1.NovaClient;
import org.jclouds.openstack.nova.v1_1.domain.Ingress;
import org.jclouds.openstack.nova.v1_1.domain.IpProtocol;
import org.jclouds.openstack.nova.v1_1.domain.SecurityGroup;
import org.jclouds.openstack.nova.v1_1.domain.SecurityGroupRule;
import org.jclouds.openstack.nova.v1_1.domain.Server;
import org.jclouds.openstack.nova.v1_1.domain.SimpleServerUsage;
import org.jclouds.openstack.nova.v1_1.domain.SimpleTenantUsage;
import org.jclouds.openstack.nova.v1_1.internal.BaseNovaClientExpectTest;
import org.jclouds.openstack.nova.v1_1.parse.ParseSecurityGroupListTest;
import org.jclouds.openstack.nova.v1_1.parse.ParseSecurityGroupTest;
import org.jclouds.rest.internal.RestContextImpl;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
/**
* Tests SimpleTenantUsageClient guice wiring and parsing
*
* @author Adam Lowe
*/
@Test(groups = "unit", testName = "SimpleTenantUsageClientExpectTest")
public class SimpleTenantUsageClientExpectTest extends BaseNovaClientExpectTest {
private DateService dateService = new SimpleDateFormatDateService();
public void testList() throws Exception {
URI endpoint = URI.create("https://compute.north.host/v1.1/3456/os-simple-tenant-usage");
SimpleTenantUsageClient client = requestsSendResponses(keystoneAuthWithUsernameAndPassword,
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse,
HttpRequest.builder().method("GET").headers(ImmutableMultimap.of("Accept", MediaType.APPLICATION_JSON, "X-Auth-Token", authToken))
.endpoint(endpoint).build(),
HttpResponse.builder().statusCode(200)
.payload(payloadFromResource("/simple_tenant_usages.json")).build())
.getSimpleTenantUsageExtensionForZone("az-1.region-a.geo-1").get();
Set<SimpleTenantUsage> results = client.listTenantUsages();
SimpleTenantUsage usage = Iterables.getOnlyElement(results);
assertEquals(usage.getTenantId(), "f8535069c3fb404cb61c873b1a0b4921");
assertEquals(usage.getTotalHours(), 4.888888888888889e-07);
assertEquals(usage.getTotalLocalGbUsage(), 1.9555555555555557e-05);
assertEquals(usage.getTotalMemoryMbUsage(), 0.0015018666666666667);
assertEquals(usage.getTotalVcpusUsage(), 7.333333333333333e-07);
assertEquals(usage.getStart(), dateService.iso8601DateParse("2012-04-18 12:18:39.702411"));
assertEquals(usage.getStop(), dateService.iso8601DateParse("2012-04-18 12:18:39.702499"));
assertNotNull(usage.getServerUsages());
assertTrue(usage.getServerUsages().isEmpty());
}
public void testGet() throws Exception {
URI endpoint = URI.create("https://compute.north.host/v1.1/3456/os-simple-tenant-usage/test-1234");
SimpleTenantUsageClient client = requestsSendResponses(keystoneAuthWithUsernameAndPassword,
responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse,
HttpRequest.builder().method("GET").headers(ImmutableMultimap.of("Accept", MediaType.APPLICATION_JSON, "X-Auth-Token", authToken))
.endpoint(endpoint).build(),
HttpResponse.builder().statusCode(200)
.payload(payloadFromResource("/simple_tenant_usage.json")).build())
.getSimpleTenantUsageExtensionForZone("az-1.region-a.geo-1").get();
SimpleTenantUsage usage = client.getTenantUsage("test-1234");
assertEquals(usage.getTenantId(), "f8535069c3fb404cb61c873b1a0b4921");
SimpleTenantUsage expected = SimpleTenantUsage.builder().totalHours(4.833333333333333E-7).totalLocalGbUsage(1.933333333333333E-05)
.start(dateService.iso8601DateParse("2012-04-18 13:32:07.255743")).stop(dateService.iso8601DateParse("2012-04-18 13:32:07.255743"))
.totalMemoryMbUsage(0.0014847999999999999).totalVcpusUsage(7.249999999999999E-07).serverUsages(
ImmutableSet.of(
SimpleServerUsage.builder().hours(2.4166666666666665e-07).uptime(91149).flavorLocalGb(50).instanceName("test1").tenantId("f8535069c3fb404cb61c873b1a0b4921").flavorVcpus(2).flavorMemoryMb(4096).instanceStatus(SimpleServerUsage.Status.ACTIVE).flavorName("m1.medium").instanceCreated(this.dateService.iso8601SecondsDateParse("2012-04-17T12:12:58")).build(),
SimpleServerUsage.builder().hours(2.4166666666666665e-07).uptime(84710).flavorLocalGb(30).instanceName("mish_test").tenantId("f8535069c3fb404cb61c873b1a0b4921").flavorVcpus(1).flavorMemoryMb(2048).instanceStatus(SimpleServerUsage.Status.ACTIVE).flavorName("m1.small").instanceCreated(this.dateService.iso8601SecondsDateParse("2012-04-17T14:00:17")).build()
)).build();
assertEquals(usage, expected);
}
}

View File

@ -0,0 +1,55 @@
/**
* 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.v1_1.extensions;
import static org.testng.Assert.assertNotNull;
import java.util.Set;
import org.jclouds.openstack.nova.v1_1.domain.SecurityGroup;
import org.jclouds.openstack.nova.v1_1.domain.SimpleTenantUsage;
import org.jclouds.openstack.nova.v1_1.internal.BaseNovaClientExpectTest;
import org.jclouds.openstack.nova.v1_1.internal.BaseNovaClientLiveTest;
import org.testng.annotations.Test;
import com.google.common.base.Optional;
/**
* Tests behavior of SimpleTenantUsageClient
*
* @author Adam Lowe
*/
@Test(groups = "live", testName = "SimpleTenantUsageClientLiveTest")
public class SimpleTenantUsageClientLiveTest extends BaseNovaClientLiveTest {
public void testList() throws Exception {
for (String zoneId : novaContext.getApi().getConfiguredZones()) {
Optional<SimpleTenantUsageClient> optClient = novaContext.getApi().getSimpleTenantUsageExtensionForZone(zoneId);
if (optClient.isPresent() && identity.endsWith(":admin")) {
SimpleTenantUsageClient client = optClient.get();
Set<SimpleTenantUsage> usages = client.listTenantUsages();
assertNotNull(usages);
for (SimpleTenantUsage usage : usages) {
SimpleTenantUsage details = client.getTenantUsage(usage.getTenantId());
assertNotNull(details);
}
}
}
}
}

View File

@ -0,0 +1 @@
{"tenant_usage": {"total_memory_mb_usage": 0.0014847999999999999, "total_vcpus_usage": 7.249999999999999e-07, "total_hours": 4.833333333333333e-07, "tenant_id": "f8535069c3fb404cb61c873b1a0b4921", "stop": "2012-04-18 13:32:07.255830", "server_usages": [{"hours": 2.4166666666666665e-07, "uptime": 91149, "local_gb": 50, "ended_at": null, "name": "test1", "tenant_id": "f8535069c3fb404cb61c873b1a0b4921", "vcpus": 2, "memory_mb": 4096, "state": "active", "flavor": "m1.medium", "started_at": "2012-04-17 12:12:58"}, {"hours": 2.4166666666666665e-07, "uptime": 84710, "local_gb": 30, "ended_at": null, "name": "mish_test", "tenant_id": "f8535069c3fb404cb61c873b1a0b4921", "vcpus": 1, "memory_mb": 2048, "state": "active", "flavor": "m1.small", "started_at": "2012-04-17 14:00:17"}], "start": "2012-04-18 13:32:07.255743", "total_local_gb_usage": 1.933333333333333e-05}}

View File

@ -0,0 +1 @@
{"tenant_usages": [{"total_memory_mb_usage": 0.0015018666666666667, "total_vcpus_usage": 7.333333333333333e-07, "start": "2012-04-18 12:18:39.702411", "tenant_id": "f8535069c3fb404cb61c873b1a0b4921", "stop": "2012-04-18 12:18:39.702499", "total_hours": 4.888888888888889e-07, "total_local_gb_usage": 1.9555555555555557e-05}]