Add the listUsageRecords API operation

This commit is contained in:
Richard Downer 2011-12-13 15:46:01 +00:00
parent 1fbc029bc8
commit 35e034a29a
5 changed files with 211 additions and 0 deletions

View File

@ -20,9 +20,11 @@ package org.jclouds.cloudstack.features;
import com.google.common.util.concurrent.ListenableFuture;
import org.jclouds.cloudstack.domain.JobResult;
import org.jclouds.cloudstack.domain.UsageRecord;
import org.jclouds.cloudstack.filters.QuerySigner;
import org.jclouds.cloudstack.functions.DateToYyyyMmDd;
import org.jclouds.cloudstack.options.GenerateUsageRecordsOptions;
import org.jclouds.cloudstack.options.ListUsageRecordsOptions;
import org.jclouds.rest.annotations.ParamParser;
import org.jclouds.rest.annotations.QueryParams;
import org.jclouds.rest.annotations.RequestFilters;
@ -33,6 +35,7 @@ import javax.ws.rs.GET;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.Date;
import java.util.Set;
/**
* Provides asynchronous access to CloudStack usage features.
@ -52,4 +55,10 @@ public interface GlobalUsageAsyncClient {
@Consumes(MediaType.APPLICATION_JSON)
ListenableFuture<JobResult> generateUsageRecords(@QueryParam("startdate") @ParamParser(DateToYyyyMmDd.class) Date start, @QueryParam("enddate") @ParamParser(DateToYyyyMmDd.class) Date end, GenerateUsageRecordsOptions... options);
@GET
@QueryParams(keys = "command", values = "listUsageRecords")
@SelectJson("usagerecord")
@Consumes(MediaType.APPLICATION_JSON)
ListenableFuture<Set<UsageRecord>> listUsageRecords(@QueryParam("startdate") @ParamParser(DateToYyyyMmDd.class) Date start, @QueryParam("enddate") @ParamParser(DateToYyyyMmDd.class) Date end, ListUsageRecordsOptions... options);
}

View File

@ -41,4 +41,6 @@ public interface GlobalUsageClient {
JobResult generateUsageRecords(Date start, Date end, GenerateUsageRecordsOptions... options);
Set<UsageRecord> listUsageRecords(Date start, Date end, ListUsageRecordsOptions... options);
}

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.cloudstack.options;
import com.google.common.collect.ImmutableSet;
/**
* Options to the GlobalUsageClient.listUsageOptions() API call
*
* @author Richard Downer
*/
public class ListUsageRecordsOptions extends AccountInDomainOptions {
public static final ListUsageRecordsOptions NONE = new ListUsageRecordsOptions();
public static class Builder {
public static ListUsageRecordsOptions accountInDomain(String account, long domainId) {
ListUsageRecordsOptions options = new ListUsageRecordsOptions();
return options.accountInDomain(account, domainId);
}
public static ListUsageRecordsOptions domainId(long domainId) {
ListUsageRecordsOptions options = new ListUsageRecordsOptions();
return options.domainId(domainId);
}
public static ListUsageRecordsOptions accountId(long accountId) {
ListUsageRecordsOptions options = new ListUsageRecordsOptions();
return options.accountId(accountId);
}
public static ListUsageRecordsOptions keyword(String keyword) {
ListUsageRecordsOptions options = new ListUsageRecordsOptions();
return options.keyword(keyword);
}
}
@Override
public ListUsageRecordsOptions accountInDomain(String account, long domain) {
return (ListUsageRecordsOptions) super.accountInDomain(account, domain);
}
@Override
public ListUsageRecordsOptions domainId(long domainId) {
return (ListUsageRecordsOptions) super.domainId(domainId);
}
public ListUsageRecordsOptions accountId(long accountId) {
this.queryParameters.replaceValues("accountid", ImmutableSet.of(accountId + ""));
return this;
}
public ListUsageRecordsOptions keyword(String keyword) {
this.queryParameters.replaceValues("keyword", ImmutableSet.of(keyword));
return this;
}
}

View File

@ -20,6 +20,7 @@ package org.jclouds.cloudstack.features;
import com.google.inject.TypeLiteral;
import org.jclouds.cloudstack.options.GenerateUsageRecordsOptions;
import org.jclouds.cloudstack.options.ListUsageRecordsOptions;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.functions.ParseFirstJsonValueNamed;
import org.jclouds.rest.functions.MapHttp4xxCodesToExceptions;
@ -89,6 +90,56 @@ public class GlobalUsageAsyncClientTest extends BaseCloudStackAsyncClientTest<Gl
checkFilters(httpRequest);
}
public void testListUsageRecords() throws Exception {
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
c.set(Calendar.YEAR, 2012);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DAY_OF_MONTH, 1);
Date start = c.getTime();
c.set(Calendar.DAY_OF_MONTH, 31);
Date end = c.getTime();
Method method = GlobalUsageAsyncClient.class.getMethod("listUsageRecords",
Date.class, Date.class, ListUsageRecordsOptions[].class);
HttpRequest httpRequest = processor.createRequest(method, start, end);
assertRequestLineEquals(httpRequest,
"GET http://localhost:8080/client/api?response=json&command=listUsageRecords&startdate=2012-01-01&enddate=2012-01-31 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, MapHttp4xxCodesToExceptions.class);
checkFilters(httpRequest);
}
public void testListUsageRecordsOptions() throws Exception {
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
c.set(Calendar.YEAR, 2012);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DAY_OF_MONTH, 1);
Date start = c.getTime();
c.set(Calendar.DAY_OF_MONTH, 31);
Date end = c.getTime();
Method method = GlobalUsageAsyncClient.class.getMethod("listUsageRecords",
Date.class, Date.class, ListUsageRecordsOptions[].class);
HttpRequest httpRequest = processor.createRequest(method, start, end, ListUsageRecordsOptions.Builder.accountInDomain("fred", 42).accountId(41).keyword("bob"));
assertRequestLineEquals(httpRequest,
"GET http://localhost:8080/client/api?response=json&command=listUsageRecords&startdate=2012-01-01&enddate=2012-01-31&account=fred&domainid=42&accountid=41&keyword=bob HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, MapHttp4xxCodesToExceptions.class);
checkFilters(httpRequest);
}
@Override
protected TypeLiteral<RestAnnotationProcessor<GlobalUsageAsyncClient>> createTypeLiteral() {
return new TypeLiteral<RestAnnotationProcessor<GlobalUsageAsyncClient>>() {

View File

@ -0,0 +1,76 @@
/**
* 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.cloudstack.options;
import com.google.common.collect.ImmutableSet;
import org.testng.annotations.Test;
import static org.jclouds.cloudstack.options.ListUsageRecordsOptions.Builder.*;
import static org.testng.Assert.assertEquals;
/**
* Options to the GlobalUsageClient.listUsageOptions() API call
*
* @author Richard Downer
*/
@Test(groups = "unit")
public class ListUsageRecordsOptionsTest {
public void testAccountInDomain() {
ListUsageRecordsOptions options = new ListUsageRecordsOptions().accountInDomain("fred", 42);
assertEquals(ImmutableSet.of("fred"), options.buildQueryParameters().get("account"));
assertEquals(ImmutableSet.of("42"), options.buildQueryParameters().get("domainid"));
}
public void testAccountInDomainStatic() {
ListUsageRecordsOptions options = accountInDomain("fred", 42);
assertEquals(ImmutableSet.of("fred"), options.buildQueryParameters().get("account"));
assertEquals(ImmutableSet.of("42"), options.buildQueryParameters().get("domainid"));
}
public void testDomainId() {
ListUsageRecordsOptions options = new ListUsageRecordsOptions().domainId(42);
assertEquals(ImmutableSet.of("42"), options.buildQueryParameters().get("domainid"));
}
public void testDomainIdStatic() {
ListUsageRecordsOptions options = domainId(42);
assertEquals(ImmutableSet.of("42"), options.buildQueryParameters().get("domainid"));
}
public void testAccountId() {
ListUsageRecordsOptions options = new ListUsageRecordsOptions().accountId(41);
assertEquals(ImmutableSet.of("41"), options.buildQueryParameters().get("accountid"));
}
public void testAccountIdStatic() {
ListUsageRecordsOptions options = accountId(41);
assertEquals(ImmutableSet.of("41"), options.buildQueryParameters().get("accountid"));
}
public void testKeyword() {
ListUsageRecordsOptions options = new ListUsageRecordsOptions().keyword("bob");
assertEquals(ImmutableSet.of("bob"), options.buildQueryParameters().get("keyword"));
}
public void testKeywordStatic() {
ListUsageRecordsOptions options = keyword("bob");
assertEquals(ImmutableSet.of("bob"), options.buildQueryParameters().get("keyword"));
}
}