From b5228d2299f18da2a1edc67ac13785d26ae360de Mon Sep 17 00:00:00 2001 From: andreisavu Date: Thu, 22 Dec 2011 17:30:33 +0200 Subject: [PATCH 1/2] Allow user to specify the Unit on CloudWatch queries --- .../cloudwatch/CloudWatchAsyncClient.java | 3 +- .../jclouds/cloudwatch/CloudWatchClient.java | 3 +- .../jclouds/cloudwatch/domain/Datapoint.java | 6 +- .../jclouds/cloudwatch/domain/Statistics.java | 38 ++++++++++++ .../domain/{StandardUnit.java => Unit.java} | 39 +++++++++++-- .../options/GetMetricStatisticsOptions.java | 18 ++++++ .../cloudwatch/xml/DatapointHandler.java | 6 +- .../cloudwatch/CloudWatchAsyncClientTest.java | 7 ++- .../cloudwatch/CloudWatchClientLiveTest.java | 3 +- .../GetMetricStatisticsOptionsTest.java | 58 +++++++++++++++++++ ...etMetricStatisticsResponseHandlerTest.java | 6 +- .../compute/AWSEC2ComputeServiceLiveTest.java | 6 +- 12 files changed, 170 insertions(+), 23 deletions(-) create mode 100644 apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/Statistics.java rename apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/{StandardUnit.java => Unit.java} (64%) create mode 100644 apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/options/GetMetricStatisticsOptionsTest.java diff --git a/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/CloudWatchAsyncClient.java b/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/CloudWatchAsyncClient.java index b614fe98c9..c5d36c8e6d 100644 --- a/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/CloudWatchAsyncClient.java +++ b/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/CloudWatchAsyncClient.java @@ -27,6 +27,7 @@ import javax.ws.rs.Path; import org.jclouds.aws.filters.FormSigner; import org.jclouds.cloudwatch.domain.Datapoint; +import org.jclouds.cloudwatch.domain.Statistics; import org.jclouds.cloudwatch.functions.ISO8601Format; import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions; import org.jclouds.cloudwatch.xml.GetMetricStatisticsResponseHandler; @@ -70,6 +71,6 @@ public interface CloudWatchAsyncClient { @FormParam("StartTime") @ParamParser(ISO8601Format.class) Date startTime, @FormParam("EndTime") @ParamParser(ISO8601Format.class) Date endTime, @FormParam("Period") int period, - @FormParam("Statistics.member.1") String statistics, + @FormParam("Statistics.member.1") Statistics statistics, GetMetricStatisticsOptions... options); } diff --git a/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/CloudWatchClient.java b/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/CloudWatchClient.java index ea08cd66da..341e099711 100644 --- a/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/CloudWatchClient.java +++ b/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/CloudWatchClient.java @@ -23,6 +23,7 @@ import java.util.Set; import java.util.concurrent.TimeUnit; import org.jclouds.cloudwatch.domain.Datapoint; +import org.jclouds.cloudwatch.domain.Statistics; import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions; import org.jclouds.concurrent.Timeout; import org.jclouds.javax.annotation.Nullable; @@ -77,6 +78,6 @@ public interface CloudWatchClient { * more filtering options (e.g. instance ID) */ Set getMetricStatisticsInRegion(@Nullable String region, String metricName, String namespace, - Date startTime, Date endTime, int period, String statistics, GetMetricStatisticsOptions... options); + Date startTime, Date endTime, int period, Statistics statistics, GetMetricStatisticsOptions... options); } diff --git a/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/Datapoint.java b/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/Datapoint.java index 2d78ed4ae5..9548321cb3 100644 --- a/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/Datapoint.java +++ b/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/Datapoint.java @@ -36,11 +36,11 @@ public class Datapoint { private final Date timestamp; private final Double samples; private final Double sum; - private final StandardUnit unit; + private final Unit unit; private final String customUnit; public Datapoint(@Nullable Double average, @Nullable Double maximum, @Nullable Double minimum, - @Nullable Date timestamp, @Nullable Double samples, @Nullable Double sum, @Nullable StandardUnit unit, + @Nullable Date timestamp, @Nullable Double samples, @Nullable Double sum, @Nullable Unit unit, @Nullable String customUnit) { this.average = average; this.maximum = maximum; @@ -104,7 +104,7 @@ public class Datapoint { * return Standard unit used for the datapoint. */ @Nullable - public StandardUnit getUnit() { + public Unit getUnit() { return unit; } diff --git a/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/Statistics.java b/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/Statistics.java new file mode 100644 index 0000000000..6405a05341 --- /dev/null +++ b/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/Statistics.java @@ -0,0 +1,38 @@ +package org.jclouds.cloudwatch.domain; + +import com.google.common.base.CaseFormat; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * @see + * + * @author Andrei Savu + */ +public enum Statistics { + AVERAGE, + SUM, + SAMPLE_COUNT, + MAXIMUM, + MINIMUM, + UNRECOGNIZED; + + public String value() { + return (CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name())); + } + + @Override + public String toString() { + return value(); + } + + public static Statistics fromValue(String value) { + try { + return valueOf(CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, checkNotNull(value, "value"))); + + } catch (IllegalArgumentException e) { + return UNRECOGNIZED; + } + } + +} diff --git a/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/StandardUnit.java b/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/Unit.java similarity index 64% rename from apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/StandardUnit.java rename to apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/Unit.java index 03dc247bc5..3a7b9d567b 100644 --- a/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/StandardUnit.java +++ b/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/Unit.java @@ -23,15 +23,42 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.base.CaseFormat; /** + * @see * - * - * @author Adrian Cole + * @author Adrian Cole, Andrei Savu */ -public enum StandardUnit { - SECONDS, PERCENT, BYTES, BITS, COUNT, BITS_PER_SECOND, COUNT_PER_SECOND, NONE, UNRECOGNIZED; +public enum Unit { + SECONDS, + MICROSECONDS, + MILLISECONDS, + BYTES, + KILOBYTES, + MEGABYTES, + GIGABYTES, + TERABYTES, + BITS, + KILOBITS, + MEGABITS, + GIGABITS, + TERABITS, + PERCENT, + COUNT, + BYTES_PER_SECOND, + KILOBYTES_PER_SECOND, + MEGABYTES_PER_SECOND, + GIGABYTES_PER_SECOND, + TERABYTES_PER_SECOND, + BITS_PER_SECOND, + KILOBITS_PER_SECOND, + MEGABITS_PER_SECOND, + GIGABITS_PER_SECOND, + TERABITS_PER_SECOND, + COUNT_PER_SECOND, + NONE, + UNRECOGNIZED; public String value() { - return (CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name().replace("_PER_", "/"))); + return (CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name()).replace("Per", "/")); } @Override @@ -39,7 +66,7 @@ public enum StandardUnit { return value(); } - public static StandardUnit fromValue(String state) { + public static Unit fromValue(String state) { try { return valueOf(CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, checkNotNull(state, "state").replace( "/", "_PER_"))); diff --git a/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/options/GetMetricStatisticsOptions.java b/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/options/GetMetricStatisticsOptions.java index c78cdcf024..8db20b9031 100644 --- a/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/options/GetMetricStatisticsOptions.java +++ b/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/options/GetMetricStatisticsOptions.java @@ -19,6 +19,7 @@ package org.jclouds.cloudwatch.options; import org.jclouds.aws.util.AWSUtils; +import org.jclouds.cloudwatch.domain.Unit; import org.jclouds.http.options.BaseHttpRequestOptions; import static com.google.common.base.Preconditions.checkNotNull; @@ -46,6 +47,15 @@ public class GetMetricStatisticsOptions extends BaseHttpRequestOptions { return this; } + /** + * @param unit + * the unit of the metric + */ + public GetMetricStatisticsOptions unit(Unit unit) { + this.formParameters.put("Unit", unit.toString()); + return this; + } + public static class Builder { /** @@ -55,5 +65,13 @@ public class GetMetricStatisticsOptions extends BaseHttpRequestOptions { GetMetricStatisticsOptions options = new GetMetricStatisticsOptions(); return options.instanceId(instanceId); } + + /** + * @see GetMetricStatisticsOptions#unit + */ + public static GetMetricStatisticsOptions unit(Unit unit) { + GetMetricStatisticsOptions options = new GetMetricStatisticsOptions(); + return options.unit(unit); + } } } diff --git a/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/xml/DatapointHandler.java b/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/xml/DatapointHandler.java index e7dc504ce0..33ab50729e 100644 --- a/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/xml/DatapointHandler.java +++ b/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/xml/DatapointHandler.java @@ -23,7 +23,7 @@ import java.util.Date; import javax.inject.Inject; import org.jclouds.cloudwatch.domain.Datapoint; -import org.jclouds.cloudwatch.domain.StandardUnit; +import org.jclouds.cloudwatch.domain.Unit; import org.jclouds.date.DateService; import org.jclouds.http.functions.ParseSax; @@ -41,7 +41,7 @@ public class DatapointHandler extends ParseSax.HandlerForGeneratedRequestWithRes private Date timestamp; private Double samples; private Double sum; - private StandardUnit unit; + private Unit unit; private String customUnit; @Inject @@ -76,7 +76,7 @@ public class DatapointHandler extends ParseSax.HandlerForGeneratedRequestWithRes } else if (qName.equals("Sum")) { sum = doubleOrNull(); } else if (qName.equals("Unit")) { - unit = StandardUnit.fromValue(currentText.toString().trim()); + unit = Unit.fromValue(currentText.toString().trim()); } else if (qName.equals("CustomUnit")) { customUnit = currentText.toString().trim(); } diff --git a/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/CloudWatchAsyncClientTest.java b/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/CloudWatchAsyncClientTest.java index 50b4d0d15b..ae815f778e 100644 --- a/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/CloudWatchAsyncClientTest.java +++ b/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/CloudWatchAsyncClientTest.java @@ -33,6 +33,7 @@ import org.jclouds.Constants; import org.jclouds.aws.domain.Region; import org.jclouds.aws.filters.FormSigner; import org.jclouds.cloudwatch.config.CloudWatchRestClientModule; +import org.jclouds.cloudwatch.domain.Statistics; import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions; import org.jclouds.cloudwatch.xml.GetMetricStatisticsResponseHandler; import org.jclouds.date.DateService; @@ -63,9 +64,9 @@ public class CloudWatchAsyncClientTest extends RestClientTest datapoints = client.getMetricStatisticsInRegion( - region, "CPUUtilization", "AWS/EC2", cal.getTime(), new Date(), 180, "Average"); + region, "CPUUtilization", "AWS/EC2", cal.getTime(), new Date(), 180, Statistics.AVERAGE); return checkNotNull(datapoints, "Got null response for EC2 datapoints in region "); } diff --git a/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/options/GetMetricStatisticsOptionsTest.java b/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/options/GetMetricStatisticsOptionsTest.java new file mode 100644 index 0000000000..c0a54e5ddd --- /dev/null +++ b/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/options/GetMetricStatisticsOptionsTest.java @@ -0,0 +1,58 @@ +/** + * 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.cloudwatch.options; + +import com.google.common.collect.ImmutableSet; +import org.jclouds.cloudwatch.domain.Unit; +import org.testng.annotations.Test; + +import static org.jclouds.cloudwatch.options.GetMetricStatisticsOptions.Builder.instanceId; +import static org.jclouds.cloudwatch.options.GetMetricStatisticsOptions.Builder.unit; +import static org.testng.Assert.assertEquals; + +/** + * Tests behavior of {@code GetMetricStatisticsOptions} + * + * @author Andrei Savu + */ +@Test(groups = "unit") +public class GetMetricStatisticsOptionsTest { + + public void testInstanceId() { + GetMetricStatisticsOptions options = new GetMetricStatisticsOptions().instanceId("us-east-1/i-123"); + assertEquals(ImmutableSet.of("InstanceId"), options.buildFormParameters().get("Dimensions.member.1.Name")); + assertEquals(ImmutableSet.of("i-123"), options.buildFormParameters().get("Dimensions.member.1.Value")); + } + + public void testInstanceIdStatic() { + GetMetricStatisticsOptions options = instanceId("us-east-1/i-123"); + assertEquals(ImmutableSet.of("InstanceId"), options.buildFormParameters().get("Dimensions.member.1.Name")); + assertEquals(ImmutableSet.of("i-123"), options.buildFormParameters().get("Dimensions.member.1.Value")); + } + + public void testUnit() { + GetMetricStatisticsOptions options = new GetMetricStatisticsOptions().unit(Unit.GIGABYTES_PER_SECOND); + assertEquals(ImmutableSet.of("Gigabytes/Second"), options.buildFormParameters().get("Unit")); + } + + public void testUnitStatic() { + GetMetricStatisticsOptions options = unit(Unit.GIGABYTES_PER_SECOND); + assertEquals(ImmutableSet.of("Gigabytes/Second"), options.buildFormParameters().get("Unit")); + } +} diff --git a/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/xml/GetMetricStatisticsResponseHandlerTest.java b/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/xml/GetMetricStatisticsResponseHandlerTest.java index cb4c132d49..289b2d1618 100644 --- a/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/xml/GetMetricStatisticsResponseHandlerTest.java +++ b/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/xml/GetMetricStatisticsResponseHandlerTest.java @@ -24,7 +24,7 @@ import java.io.InputStream; import java.util.Set; import org.jclouds.cloudwatch.domain.Datapoint; -import org.jclouds.cloudwatch.domain.StandardUnit; +import org.jclouds.cloudwatch.domain.Unit; import org.jclouds.date.DateService; import org.jclouds.http.functions.BaseHandlerTest; import org.testng.annotations.Test; @@ -44,9 +44,9 @@ public class GetMetricStatisticsResponseHandlerTest extends BaseHandlerTest { InputStream is = getClass().getResourceAsStream("/get_metric_statistics.xml"); Set expected = ImmutableSet.of(new Datapoint(0.17777777777777778, null, null, dateService - .iso8601SecondsDateParse("2009-01-16T00:00:00Z"), 9.0, null, StandardUnit.PERCENT, null), new Datapoint( + .iso8601SecondsDateParse("2009-01-16T00:00:00Z"), 9.0, null, Unit.PERCENT, null), new Datapoint( 0.1, null, null, dateService.iso8601SecondsDateParse("2009-01-16T00:01:00Z"), 8.0, null, - StandardUnit.PERCENT, null)); + Unit.PERCENT, null)); GetMetricStatisticsResponseHandler handler = injector.getInstance(GetMetricStatisticsResponseHandler.class); Set result = factory.create(handler).parse(is); diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/AWSEC2ComputeServiceLiveTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/AWSEC2ComputeServiceLiveTest.java index c624bad766..7038d94e35 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/AWSEC2ComputeServiceLiveTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/AWSEC2ComputeServiceLiveTest.java @@ -30,6 +30,8 @@ import org.jclouds.aws.ec2.services.AWSSecurityGroupClient; import org.jclouds.cloudwatch.CloudWatchAsyncClient; import org.jclouds.cloudwatch.CloudWatchClient; import org.jclouds.cloudwatch.domain.Datapoint; +import org.jclouds.cloudwatch.domain.Statistics; +import org.jclouds.cloudwatch.domain.Unit; import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions; import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.Template; @@ -156,8 +158,8 @@ public class AWSEC2ComputeServiceLiveTest extends EC2ComputeServiceLiveTest { try { Set datapoints = monitoringContext.getApi().getMetricStatisticsInRegion(instance.getRegion(), - "CPUUtilization", "AWS/EC2", before, new Date(), 60, "Average", - GetMetricStatisticsOptions.Builder.instanceId(instance.getId())); + "CPUUtilization", "AWS/EC2", before, new Date(), 60, Statistics.AVERAGE, + GetMetricStatisticsOptions.Builder.instanceId(instance.getId()).unit(Unit.PERCENT)); assert datapoints != null && datapoints.size() > 0; } finally { From 529dc4f687531d23ad957d185b9089eafefdd6f7 Mon Sep 17 00:00:00 2001 From: andreisavu Date: Thu, 22 Dec 2011 17:40:22 +0200 Subject: [PATCH 2/2] Strict string replacement & improved live test --- .../src/main/java/org/jclouds/cloudwatch/domain/Unit.java | 2 +- .../org/jclouds/cloudwatch/CloudWatchClientLiveTest.java | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/Unit.java b/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/Unit.java index 3a7b9d567b..1d51fbd034 100644 --- a/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/Unit.java +++ b/apis/cloudwatch/src/main/java/org/jclouds/cloudwatch/domain/Unit.java @@ -58,7 +58,7 @@ public enum Unit { UNRECOGNIZED; public String value() { - return (CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name()).replace("Per", "/")); + return (CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name()).replace("PerSecond", "/Second")); } @Override diff --git a/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/CloudWatchClientLiveTest.java b/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/CloudWatchClientLiveTest.java index cfd5b3eac5..5b277633d4 100644 --- a/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/CloudWatchClientLiveTest.java +++ b/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/CloudWatchClientLiveTest.java @@ -28,6 +28,8 @@ import java.util.Set; import org.jclouds.Constants; import org.jclouds.cloudwatch.domain.Datapoint; import org.jclouds.cloudwatch.domain.Statistics; +import org.jclouds.cloudwatch.domain.Unit; +import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions; import org.jclouds.logging.log4j.config.Log4JLoggingModule; import org.jclouds.rest.RestContext; import org.jclouds.rest.RestContextFactory; @@ -94,7 +96,8 @@ public class CloudWatchClientLiveTest { cal.add(Calendar.MINUTE, -60 * 24 * 3); // 3 days Set datapoints = client.getMetricStatisticsInRegion( - region, "CPUUtilization", "AWS/EC2", cal.getTime(), new Date(), 180, Statistics.AVERAGE); + region, "CPUUtilization", "AWS/EC2", cal.getTime(), new Date(), 180, Statistics.AVERAGE, + GetMetricStatisticsOptions.Builder.unit(Unit.PERCENT)); return checkNotNull(datapoints, "Got null response for EC2 datapoints in region "); }