diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalUsageAsyncClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalUsageAsyncClient.java index 1148a5482d..1e2a27b820 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalUsageAsyncClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalUsageAsyncClient.java @@ -19,18 +19,18 @@ package org.jclouds.cloudstack.features; import com.google.common.util.concurrent.ListenableFuture; -import org.jclouds.cloudstack.binders.BindEndDateAsYyyyMmDdToQueryParams; -import org.jclouds.cloudstack.binders.BindStartDateAsYyyyMmDdToQueryParams; import org.jclouds.cloudstack.domain.JobResult; import org.jclouds.cloudstack.filters.QuerySigner; +import org.jclouds.cloudstack.functions.DateToYyyyMmDd; import org.jclouds.cloudstack.options.GenerateUsageRecordsOptions; -import org.jclouds.rest.annotations.BinderParam; +import org.jclouds.rest.annotations.ParamParser; import org.jclouds.rest.annotations.QueryParams; 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.QueryParam; import javax.ws.rs.core.MediaType; import java.util.Date; @@ -46,4 +46,10 @@ import java.util.Date; @QueryParams(keys = "response", values = "json") public interface GlobalUsageAsyncClient { + @GET + @QueryParams(keys = "command", values = "generateUsageRecords") + @SelectJson("generateusagerecordsresponse") + @Consumes(MediaType.APPLICATION_JSON) + ListenableFuture generateUsageRecords(@QueryParam("startdate") @ParamParser(DateToYyyyMmDd.class) Date start, @QueryParam("enddate") @ParamParser(DateToYyyyMmDd.class) Date end, GenerateUsageRecordsOptions... options); + } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalUsageClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalUsageClient.java index ccb9e34158..bf79959721 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalUsageClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalUsageClient.java @@ -39,4 +39,6 @@ import java.util.concurrent.TimeUnit; @Timeout(duration = 60, timeUnit = TimeUnit.SECONDS) public interface GlobalUsageClient { + JobResult generateUsageRecords(Date start, Date end, GenerateUsageRecordsOptions... options); + } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/functions/DateToYyyyMmDd.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/functions/DateToYyyyMmDd.java new file mode 100644 index 0000000000..a92d2b02ec --- /dev/null +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/functions/DateToYyyyMmDd.java @@ -0,0 +1,43 @@ +/** + * 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.functions; + +import com.google.common.base.Function; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Convert a Date object into a "yyyy-MM-dd" String + * + * @author Richard Downer + */ +public class DateToYyyyMmDd implements Function { + + public String apply(Object input) { + checkNotNull(input, "input cannot be null"); + checkArgument(input instanceof Date, "input must be a Date"); + + return new SimpleDateFormat("yyyy-MM-dd").format((Date)input); + } + +} diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalUsageAsyncClientTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalUsageAsyncClientTest.java index 4d11519c63..da69020c4e 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalUsageAsyncClientTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalUsageAsyncClientTest.java @@ -39,6 +39,56 @@ import java.util.TimeZone; @Test(groups = "unit", testName = "GlobalUsageAsyncClientTest") public class GlobalUsageAsyncClientTest extends BaseCloudStackAsyncClientTest { + public void testGenerateUsageRecords() 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("generateUsageRecords", + Date.class, Date.class, GenerateUsageRecordsOptions[].class); + HttpRequest httpRequest = processor.createRequest(method, start, end); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&command=generateUsageRecords&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 testGenerateUsageRecordsOptions() 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("generateUsageRecords", + Date.class, Date.class, GenerateUsageRecordsOptions[].class); + HttpRequest httpRequest = processor.createRequest(method, start, end, GenerateUsageRecordsOptions.Builder.domainId(42)); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&command=generateUsageRecords&startdate=2012-01-01&enddate=2012-01-31&domainid=42 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> createTypeLiteral() { return new TypeLiteral>() {