Added ComputePoolCpuUsage objects+service call+tests

This commit is contained in:
Jason King 2011-12-06 13:38:53 +00:00
parent 2849284777
commit e4d4d7fa61
9 changed files with 706 additions and 0 deletions

View File

@ -0,0 +1,224 @@
/**
* 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.tmrk.enterprisecloud.domain.resource;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.tmrk.enterprisecloud.domain.Action;
import org.jclouds.tmrk.enterprisecloud.domain.Link;
import org.jclouds.tmrk.enterprisecloud.domain.internal.BaseResource;
import org.jclouds.tmrk.enterprisecloud.domain.internal.Resource;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.net.URI;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* ComputePoolCpuUsage is more than a simple wrapper as it extends Resource.
* <xs:complexType name="ComputePoolCpuUsage">
* @author Jason King
*
*/
@XmlRootElement(name = "ComputePoolCpuUsage")
public class ComputePoolCpuUsage extends Resource<ComputePoolCpuUsage> {
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
/**
* {@inheritDoc}
*/
@Override
public Builder toBuilder() {
return new Builder().fromComputePoolCpuUsage(this);
}
public static class Builder extends Resource.Builder<ComputePoolCpuUsage> {
private Date startTime;
private Date endTime;
private CpuUsageDetails details;
/**
* @see ComputePoolCpuUsage#getStartTime
*/
public Builder startTime(Date startTime) {
this.startTime =(checkNotNull(startTime,"startTime"));
return this;
}
/**
* @see ComputePoolCpuUsage#getEndTime
*/
public Builder endTime(Date endTime) {
this.endTime =(checkNotNull(endTime,"endTime"));
return this;
}
/**
* @see ComputePoolCpuUsage#getDetails
*/
public Builder details(CpuUsageDetails details) {
this.details =(checkNotNull(details,"details"));
return this;
}
@Override
public ComputePoolCpuUsage build() {
return new ComputePoolCpuUsage(href, type, name, links, actions, startTime, endTime, details);
}
public Builder fromComputePoolCpuUsage(ComputePoolCpuUsage in) {
return fromResource(in).startTime(in.getStartTime()).endTime(in.getEndTime()).details(in.getDetails());
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromBaseResource(BaseResource<ComputePoolCpuUsage> in) {
return Builder.class.cast(super.fromBaseResource(in));
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromResource(Resource<ComputePoolCpuUsage> in) {
return Builder.class.cast(super.fromResource(in));
}
/**
* {@inheritDoc}
*/
@Override
public Builder type(String type) {
return Builder.class.cast(super.type(type));
}
/**
* {@inheritDoc}
*/
@Override
public Builder href(URI href) {
return Builder.class.cast(super.href(href));
}
/**
* {@inheritDoc}
*/
@Override
public Builder name(String name) {
return Builder.class.cast(super.name(name));
}
/**
* {@inheritDoc}
*/
@Override
public Builder links(Set<Link> links) {
return Builder.class.cast(super.links(links));
}
/**
* {@inheritDoc}
*/
@Override
public Builder actions(Set<Action> actions) {
return Builder.class.cast(super.actions(actions));
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromAttributes(Map<String, String> attributes) {
return Builder.class.cast(super.fromAttributes(attributes));
}
}
@XmlElement(name = "StartTime", required = true)
private Date startTime;
@XmlElement(name = "EndTime", required = true)
private Date endTime;
@XmlElement(name = "CpuUsageDetailSummary", required = false)
private CpuUsageDetails details;
private ComputePoolCpuUsage(URI href, String type, String name, Set<Link> links, Set<Action> actions, Date startTime, Date endTime, @Nullable CpuUsageDetails details) {
super(href, type, name, links, actions);
this.startTime = checkNotNull(startTime, "startTime");
this.endTime = checkNotNull(endTime, "endTime");
this.details = details;
}
private ComputePoolCpuUsage() {
//For JAXB
}
public Date getStartTime() {
return startTime;
}
public Date getEndTime() {
return endTime;
}
public CpuUsageDetails getDetails() {
return details;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
ComputePoolCpuUsage that = (ComputePoolCpuUsage) o;
if (details != null ? !details.equals(that.details) : that.details != null)
return false;
if (!endTime.equals(that.endTime)) return false;
if (!startTime.equals(that.startTime)) return false;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + startTime.hashCode();
result = 31 * result + endTime.hashCode();
result = 31 * result + (details != null ? details.hashCode() : 0);
return result;
}
@Override
public String string() {
return super.string()+", startTime="+startTime+", endTime="+endTime+", details="+details;
}
}

View File

@ -0,0 +1,127 @@
/**
* 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.tmrk.enterprisecloud.domain.resource;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.tmrk.enterprisecloud.domain.internal.ResourceCapacity;
import javax.xml.bind.annotation.XmlElement;
import java.util.Date;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* <xs:complexType name="ComputePoolCpuUsageDetailSummaryEntry">
* @author Jason King
*
*/
public class ComputePoolCpuUsageDetailSummaryEntry {
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
/**
* {@inheritDoc}
*/
public Builder toBuilder() {
return new Builder().ComputePoolCpuUsageDetailSummaryEntry(this);
}
public static class Builder {
private Date time;
private ResourceCapacity value;
/**
* @see org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolCpuUsageDetailSummaryEntry#getTime
*/
public Builder time(Date time) {
this.time = time;
return this;
}
/**
* @see org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolCpuUsageDetailSummaryEntry#getValue
*/
public Builder value(ResourceCapacity value) {
this.value = value;
return this;
}
public ComputePoolCpuUsageDetailSummaryEntry build() {
return new ComputePoolCpuUsageDetailSummaryEntry(time,value);
}
public Builder ComputePoolCpuUsageDetailSummaryEntry(ComputePoolCpuUsageDetailSummaryEntry in) {
return time(in.getTime()).value(in.getValue());
}
}
@XmlElement(name = "Time", required = true)
private Date time;
@XmlElement(name = "Value", required = false)
private ResourceCapacity value;
private ComputePoolCpuUsageDetailSummaryEntry(Date time,@Nullable ResourceCapacity value) {
this.time = checkNotNull(time, "time");
this.value = value;
}
private ComputePoolCpuUsageDetailSummaryEntry() {
//For JAXB
}
public Date getTime() {
return time;
}
public ResourceCapacity getValue() {
return value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ComputePoolCpuUsageDetailSummaryEntry that = (ComputePoolCpuUsageDetailSummaryEntry) o;
if (!time.equals(that.time)) return false;
if (value != null ? !value.equals(that.value) : that.value != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = time.hashCode();
result = 31 * result + (value != null ? value.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "[time="+ time +", value="+value+"]";
}
}

View File

@ -0,0 +1,106 @@
/**
* 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.tmrk.enterprisecloud.domain.resource;
import com.google.common.collect.Sets;
import javax.xml.bind.annotation.XmlElement;
import java.util.Collections;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Wraps individual ComputePoolCpuUsageDetailSummaryEntry elements.
* <xs:complexType name="CpuUsageDetails">
* @author Jason King
*/
public class CpuUsageDetails {
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
public Builder toBuilder() {
return new Builder().fromCpuUsageDetails(this);
}
public static class Builder {
private Set<ComputePoolCpuUsageDetailSummaryEntry> entries = Sets.newLinkedHashSet();
/**
* @see CpuUsageDetails#getEntries
*/
public Builder entries(Set<ComputePoolCpuUsageDetailSummaryEntry> entries) {
this.entries = Sets.newLinkedHashSet(checkNotNull(entries, "entries"));
return this;
}
public Builder addEntry(ComputePoolCpuUsageDetailSummaryEntry entry) {
entries.add(checkNotNull(entry,"entry"));
return this;
}
public CpuUsageDetails build() {
return new CpuUsageDetails(entries);
}
public Builder fromCpuUsageDetails(CpuUsageDetails in) {
return entries(in.getEntries());
}
}
private CpuUsageDetails() {
//For JAXB and builder use
}
private CpuUsageDetails(Set<ComputePoolCpuUsageDetailSummaryEntry> entries) {
this.entries = Sets.newLinkedHashSet(entries);
}
@XmlElement(name = "CpuUsageDetail", required=false)
private Set<ComputePoolCpuUsageDetailSummaryEntry> entries = Sets.newLinkedHashSet();
public Set<ComputePoolCpuUsageDetailSummaryEntry> getEntries() {
return Collections.unmodifiableSet(entries);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CpuUsageDetails tasks1 = (CpuUsageDetails) o;
if (!entries.equals(tasks1.entries)) return false;
return true;
}
@Override
public int hashCode() {
return entries.hashCode();
}
public String toString() {
return "["+ entries.toString()+"]";
}
}

View File

@ -22,6 +22,7 @@ import com.google.common.util.concurrent.ListenableFuture;
import org.jclouds.http.filters.BasicAuthentication;
import org.jclouds.rest.annotations.*;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolCpuUsage;
import org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolResourceSummary;
import org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolResourceSummaryList;
@ -60,4 +61,13 @@ public interface ResourceAsyncClient {
@JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<ComputePoolResourceSummary> getResourceSummary(@EndpointParam URI uri);
/**
* @see ResourceClient#getComputePoolCpuUsage
*/
@GET
@Consumes("application/vnd.tmrk.cloud.computePoolCpuUsage")
@JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<ComputePoolCpuUsage> getComputePoolCpuUsage(@EndpointParam URI uri);
}

View File

@ -19,6 +19,7 @@
package org.jclouds.tmrk.enterprisecloud.features;
import org.jclouds.concurrent.Timeout;
import org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolCpuUsage;
import org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolResourceSummary;
import org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolResourceSummaryList;
@ -56,4 +57,15 @@ public interface ResourceClient {
*/
ComputePoolResourceSummary getResourceSummary(URI uri);
/**
* The call returns information regarding processor usage for a specified compute pool
* defined in an environment at five minute intervals for the 24 hours ending one hour
* prior to the current time, rounded later.
* For example, if current time is 11:22, the end time is 10:25.
* @param uri the uri of the call based upon the compute pool
* e.g. /cloudapi/ecloud/computepools/{id}/usage/cpu
* @return The cpu usage for the compute pool
*/
ComputePoolCpuUsage getComputePoolCpuUsage(URI uri);
}

View File

@ -68,6 +68,21 @@ public class ResourceAsyncClientTest extends BaseTerremarkEnterpriseCloudAsyncCl
checkFilters(httpRequest);
}
public void testGetComputePoolCpuUsage() throws SecurityException, NoSuchMethodException, IOException, URISyntaxException {
Method method = ResourceAsyncClient.class.getMethod("getComputePoolCpuUsage", URI.class);
HttpRequest httpRequest = processor.createRequest(method, URI.create("/cloudapi/ecloud/computepools/89/usage/cpu"));
assertRequestLineEquals(httpRequest, "GET https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/computepools/89/usage/cpu HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest,
"Accept: application/vnd.tmrk.cloud.computePoolCpuUsage\nx-tmrk-version: 2011-07-01\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(httpRequest);
}
@Override
protected TypeLiteral<RestAnnotationProcessor<ResourceAsyncClient>> createTypeLiteral() {
return new TypeLiteral<RestAnnotationProcessor<ResourceAsyncClient>>() {

View File

@ -60,4 +60,13 @@ public class ResourceClientLiveTest extends BaseTerremarkEnterpriseCloudClientLi
public void testMissingResourceSummary() {
assertNull(client.getResourceSummary(URI.create("/cloudapi/ecloud/computepools/-1/resourcesummary")));
}
public void testGetComputePoolCpuUsage() throws Exception {
ComputePoolResourceSummary resourceSummary = client.getResourceSummary(URI.create("/cloudapi/ecloud/computepools/89/usage/cpu"));
assertNotNull(resourceSummary);
}
public void testMissingComputePoolCpuUsage() {
assertNull(client.getResourceSummary(URI.create("/cloudapi/ecloud/computepools/-1/usage/cpu")));
}
}

View File

@ -0,0 +1,138 @@
/**
* 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.tmrk.enterprisecloud.xml;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.Provides;
import org.jclouds.crypto.Crypto;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.http.functions.ParseXMLWithJAXB;
import org.jclouds.logging.config.NullLoggingModule;
import org.jclouds.rest.AuthorizationException;
import org.jclouds.rest.BaseRestClientTest;
import org.jclouds.rest.RestContextSpec;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.tmrk.enterprisecloud.domain.Link;
import org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolCpuUsage;
import org.jclouds.tmrk.enterprisecloud.domain.resource.ComputePoolCpuUsageDetailSummaryEntry;
import org.jclouds.tmrk.enterprisecloud.domain.resource.CpuUsageDetails;
import org.jclouds.tmrk.enterprisecloud.features.ResourceAsyncClient;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import javax.inject.Named;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Set;
import static org.jclouds.io.Payloads.newInputStreamPayload;
import static org.jclouds.rest.RestContextFactory.contextSpec;
import static org.jclouds.rest.RestContextFactory.createContextBuilder;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
/**
* Tests behavior of JAXB parsing for ComputePoolResourceSummaryList
*
* @author Jason King
*/
@Test(groups = "unit", testName = "ComputePoolResourceSummaryListJAXBParsingTest")
public class ComputePoolResourceSummaryListJAXBParsingTest extends BaseRestClientTest {
private SimpleDateFormatDateService dateService;
@BeforeMethod
public void setUp() {
dateService = new SimpleDateFormatDateService();
}
@BeforeClass
void setupFactory() {
RestContextSpec<String, Integer> contextSpec = contextSpec("test", "http://localhost:9999", "1", "", "userfoo",
"credentialFoo", String.class, Integer.class,
ImmutableSet.<Module>of(new MockModule(), new NullLoggingModule(), new AbstractModule() {
@Override
protected void configure() {
}
@SuppressWarnings("unused")
@Provides
@Named("exception")
Set<String> exception() {
throw new AuthorizationException();
}
}));
injector = createContextBuilder(contextSpec).buildInjector();
parserFactory = injector.getInstance(ParseSax.Factory.class);
crypto = injector.getInstance(Crypto.class);
}
public void testParseTemplates() throws Exception {
Method method = ResourceAsyncClient.class.getMethod("getComputePoolCpuUsage", URI.class);
HttpRequest request = factory(ResourceAsyncClient.class).createRequest(method,new URI("/1"));
assertResponseParserClassEquals(method, request, ParseXMLWithJAXB.class);
Function<HttpResponse, ComputePoolCpuUsage> parser = (Function<HttpResponse, ComputePoolCpuUsage>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request);
InputStream is = getClass().getResourceAsStream("/computePoolsCpuUsage.xml");
ComputePoolCpuUsage cpuUsage = parser.apply(new HttpResponse(200, "ok", newInputStreamPayload(is)));
assertLinks(cpuUsage.getLinks());
assertEquals(cpuUsage.getStartTime(), dateService.iso8601DateParse("2011-12-05T09:45:00.0Z"));
assertEquals(cpuUsage.getEndTime(), dateService.iso8601DateParse("2011-12-06T09:45:00.0Z"));
assertDetails(cpuUsage.getDetails());
}
private void assertLinks(Set<Link> links) {
assertEquals(links.size(),2);
Link link = Iterables.get(links, 0);
assertEquals(link.getName(),"Default Compute Pool");
assertEquals(link.getRelationship(), Link.Relationship.UP);
Link link2 = Iterables.get(links, 1);
assertEquals(link2.getHref(), URI.create("/cloudapi/ecloud/computepools/89/usage/cpu/details?time=2011-12-05t09%3a45%3a00z"));
assertEquals(link2.getType(), "application/vnd.tmrk.cloud.computePoolCpuUsageDetail");
assertEquals(link2.getRelationship(), Link.Relationship.DOWN);
}
private void assertDetails(CpuUsageDetails details) {
assertEquals(details.getEntries().size(), 7);
for(ComputePoolCpuUsageDetailSummaryEntry entry: details.getEntries()) {
assertDetail(entry);
}
}
private void assertDetail(ComputePoolCpuUsageDetailSummaryEntry entry) {
assertNotNull(entry.getTime());
assertNotNull(entry.getValue());
}
}

View File

@ -0,0 +1,65 @@
<ComputePoolCpuUsage href="/cloudapi/ecloud/computepools/89/usage/cpu"
type="application/vnd.tmrk.cloud.computePoolCpuUsage"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Links>
<Link href="/cloudapi/ecloud/computepools/89"
name="Default Compute Pool"
type="application/vnd.tmrk.cloud.computePool" rel="up"/>
<Link href="/cloudapi/ecloud/computepools/89/usage/cpu/details?time=2011-12-05t09%3a45%3a00z"
type="application/vnd.tmrk.cloud.computePoolCpuUsageDetail"
rel="down"/>
</Links>
<StartTime>2011-12-05T09:45:00Z</StartTime>
<EndTime>2011-12-06T09:45:00Z</EndTime>
<CpuUsageDetailSummary>
<CpuUsageDetail>
<Time>2011-12-05T09:45:00Z</Time>
<Value>
<Unit>MHz</Unit>
<Value>57</Value>
</Value>
</CpuUsageDetail>
<CpuUsageDetail>
<Time>2011-12-05T09:50:00Z</Time>
<Value>
<Unit>MHz</Unit>
<Value>42</Value>
</Value>
</CpuUsageDetail>
<CpuUsageDetail>
<Time>2011-12-05T09:55:00Z</Time>
<Value>
<Unit>MHz</Unit>
<Value>42</Value>
</Value>
</CpuUsageDetail>
<CpuUsageDetail>
<Time>2011-12-05T10:00:00Z</Time>
<Value>
<Unit>MHz</Unit>
<Value>42</Value>
</Value>
</CpuUsageDetail>
<CpuUsageDetail>
<Time>2011-12-05T10:05:00Z</Time>
<Value>
<Unit>MHz</Unit>
<Value>43</Value>
</Value>
</CpuUsageDetail>
<CpuUsageDetail>
<Time>2011-12-05T10:10:00Z</Time>
<Value>
<Unit>MHz</Unit>
<Value>43</Value>
</Value>
</CpuUsageDetail>
<CpuUsageDetail>
<Time>2011-12-05T10:15:00Z</Time>
<Value>
<Unit>MHz</Unit>
<Value>41</Value>
</Value>
</CpuUsageDetail>
</CpuUsageDetailSummary>
</ComputePoolCpuUsage>