Follow up to f06d273 that addresses pull request comments.

* Adds region to the listMetrics and getMetricStatisticsV2
* Fixed hashCode implementations
* Fixed typo in Datapoint.equals()
This commit is contained in:
Jeremy Whitlock 2012-04-30 23:46:30 -06:00
parent f06d273764
commit 13d80f7d3a
12 changed files with 60 additions and 52 deletions

View File

@ -36,18 +36,19 @@ public class CloudWatch {
* List metrics based on the criteria in the {@link ListMetricsOptions} passed in.
*
* @param cloudWatchClient the CloudWatch client
* @param region the region to list metrics in
* @param options the options describing the ListMetrics request
*
* @return iterable of metrics fitting the criteria
*/
public static Iterable<Metric> listMetrics(final CloudWatchClient cloudWatchClient,
final ListMetricsOptions options) {
final String region, final ListMetricsOptions options) {
return new Iterable<Metric>() {
public Iterator<Metric> iterator() {
return new AbstractIterator<Metric>() {
private ListMetricsOptions lastOptions = options;
private ListMetricsResponse response = cloudWatchClient.listMetrics(lastOptions);
private ListMetricsResponse response = cloudWatchClient.listMetrics(region, lastOptions);
private Iterator<Metric> iterator = response.getMetrics().iterator();
/**
@ -63,7 +64,7 @@ public class CloudWatch {
.namespace(lastOptions.getNamespace())
.nextToken(response.getNextToken())
.build();
response = cloudWatchClient.listMetrics(lastOptions);
response = cloudWatchClient.listMetrics(region, lastOptions);
iterator = response.getMetrics().iterator();
}
if (iterator.hasNext()) {

View File

@ -79,21 +79,25 @@ public interface CloudWatchAsyncClient {
GetMetricStatisticsOptions... options);
/**
* @see CloudWatchClient#listMetrics(org.jclouds.cloudwatch.options.ListMetricsOptions)
* @see CloudWatchClient#listMetrics(String, org.jclouds.cloudwatch.options.ListMetricsOptions)
*/
@POST
@Path("/")
@XMLResponseParser(ListMetricsResponseHandler.class)
@FormParams(keys = "Action", values = "ListMetrics")
ListenableFuture<? extends ListMetricsResponse> listMetrics(ListMetricsOptions options);
ListenableFuture<? extends ListMetricsResponse> listMetrics(
@EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region,
ListMetricsOptions options);
/**
* @see CloudWatchClient#getMetricStatistics(org.jclouds.cloudwatch.options.GetMetricStatisticsOptionsV2)
* @see CloudWatchClient#getMetricStatistics(String, org.jclouds.cloudwatch.options.GetMetricStatisticsOptionsV2)
*/
@POST
@Path("/")
@XMLResponseParser(GetMetricStatisticsResponseHandlerV2.class)
@FormParams(keys = "Action", values = "GetMetricStatistics")
ListenableFuture<? extends GetMetricStatisticsResponse> getMetricStatistics(GetMetricStatisticsOptionsV2 options);
ListenableFuture<? extends GetMetricStatisticsResponse> getMetricStatistics(
@EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region,
GetMetricStatisticsOptionsV2 options);
}

View File

@ -92,21 +92,23 @@ public interface CloudWatchClient {
* <h3>Note</h3> Up to 500 results are returned for any one call. To retrieve further results, use returned
* NextToken ({@link org.jclouds.cloudwatch.domain.ListMetricsResponse#getNextToken()})
* value with subsequent calls .To retrieve all available metrics with one call, use
* {@link CloudWatch#listMetrics(CloudWatchClient, org.jclouds.cloudwatch.options.ListMetricsOptions)}.
* {@link CloudWatch#listMetrics(CloudWatchClient, String, org.jclouds.cloudwatch.options.ListMetricsOptions)}.
*
* @param region the region to query metrics in
* @param options the options describing the metrics query
*
* @return the response object
*/
ListMetricsResponse listMetrics(ListMetricsOptions options);
ListMetricsResponse listMetrics(@Nullable String region, ListMetricsOptions options);
/**
* Gets statistics for the specified metric.
*
* @param region the region to gather metrics in
* @param options the options describing the metric statistics query
*
* @return the response object
*/
GetMetricStatisticsResponse getMetricStatistics(GetMetricStatisticsOptionsV2 options);
GetMetricStatisticsResponse getMetricStatistics(@Nullable String region, GetMetricStatisticsOptionsV2 options);
}

View File

@ -119,7 +119,7 @@ public class Datapoint {
@Override
public int hashCode() {
return Objects.hashCode(super.hashCode(), average, customUnit, maximum, minimum, samples, sum, timestamp, unit);
return Objects.hashCode(average, customUnit, maximum, minimum, samples, sum, timestamp, unit);
}
@Override
@ -132,7 +132,7 @@ public class Datapoint {
return false;
Datapoint other = (Datapoint) obj;
return Objects.equal(this.average, other.average) &&
Objects.equal(this.customUnit, other.maximum) &&
Objects.equal(this.customUnit, other.customUnit) &&
Objects.equal(this.maximum, other.maximum) &&
Objects.equal(this.minimum, other.minimum) &&
Objects.equal(this.samples, other.samples) &&

View File

@ -54,7 +54,7 @@ public class Dimension {
*/
@Override
public int hashCode() {
return Objects.hashCode(super.hashCode(), name, value);
return Objects.hashCode(name, value);
}
/**

View File

@ -62,7 +62,7 @@ public class GetMetricStatisticsResponse {
*/
@Override
public int hashCode() {
return Objects.hashCode(super.hashCode(), datapoints, label);
return Objects.hashCode(datapoints, label);
}
/**

View File

@ -64,7 +64,7 @@ public class ListMetricsResponse {
*/
@Override
public int hashCode() {
return Objects.hashCode(super.hashCode(), metrics, nextToken);
return Objects.hashCode(metrics, nextToken);
}
/**

View File

@ -74,7 +74,7 @@ public class Metric {
*/
@Override
public int hashCode() {
return Objects.hashCode(super.hashCode(), dimensions, metricName, namespace);
return Objects.hashCode(dimensions, metricName, namespace);
}
/**

View File

@ -69,7 +69,7 @@ import static org.testng.Assert.assertEquals;
public class CloudWatchAsyncClientTest extends BaseAsyncClientTest<CloudWatchAsyncClient> {
/**
* Tests that {@link CloudWatchAsyncClient#getMetricStatistics(org.jclouds.cloudwatch.options.GetMetricStatisticsOptionsV2)}
* Tests that {@link CloudWatchAsyncClient#getMetricStatistics(String, org.jclouds.cloudwatch.options.GetMetricStatisticsOptionsV2)}
* works as expected.
*
* @throws Exception if anything goes wrong
@ -97,8 +97,9 @@ public class CloudWatchAsyncClientTest extends BaseAsyncClientTest<CloudWatchAsy
.statistic(statistic1)
.statistic(statistic2)
.unit(unit).build();
Method method = CloudWatchAsyncClient.class.getMethod("getMetricStatistics", GetMetricStatisticsOptionsV2.class);
HttpRequest request = processor.createRequest(method, goodOptions);
Method method = CloudWatchAsyncClient.class.getMethod("getMetricStatistics", String.class,
GetMetricStatisticsOptionsV2.class);
HttpRequest request = processor.createRequest(method, null, goodOptions);
assertRequestLineEquals(request, "POST https://monitoring.us-east-1.amazonaws.com/ HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Host: monitoring.us-east-1.amazonaws.com\n");
@ -122,17 +123,17 @@ public class CloudWatchAsyncClientTest extends BaseAsyncClientTest<CloudWatchAsy
}
/**
* Tests that {@link CloudWatchAsyncClient#listMetrics(org.jclouds.cloudwatch.options.ListMetricsOptions)} works
* Tests that {@link CloudWatchAsyncClient#listMetrics(String, org.jclouds.cloudwatch.options.ListMetricsOptions)} works
* as expected.
*
* @throws Exception if anything goes wrong
*/
public void testListMetrics() throws Exception {
Method method = CloudWatchAsyncClient.class.getMethod("listMetrics", ListMetricsOptions.class);
Method method = CloudWatchAsyncClient.class.getMethod("listMetrics", String.class, ListMetricsOptions.class);
HttpRequest request;
// Test an empty request
request = processor.createRequest(method, ListMetricsOptions.builder().build());
request = processor.createRequest(method, null, ListMetricsOptions.builder().build());
assertRequestLineEquals(request, "POST https://monitoring.us-east-1.amazonaws.com/ HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Host: monitoring.us-east-1.amazonaws.com\n");
@ -147,12 +148,12 @@ public class CloudWatchAsyncClientTest extends BaseAsyncClientTest<CloudWatchAsy
String metricName = EC2Constants.MetricName.CPU_UTILIZATION;
String nextToken = "SOMENEXTTOKEN";
String namespace = Namespaces.EC2;
request = processor.createRequest(method, ListMetricsOptions.builder()
.dimension(dimension1)
.metricName(metricName)
.namespace(namespace)
.nextToken(nextToken)
.build());
request = processor.createRequest(method, null, ListMetricsOptions.builder()
.dimension(dimension1)
.metricName(metricName)
.namespace(namespace)
.nextToken(nextToken)
.build());
assertRequestLineEquals(request, "POST https://monitoring.us-east-1.amazonaws.com/ HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Host: monitoring.us-east-1.amazonaws.com\n");
@ -167,12 +168,12 @@ public class CloudWatchAsyncClientTest extends BaseAsyncClientTest<CloudWatchAsy
// Test a request with multiple dimensions and no NextToken
Dimension dimension2 = new Dimension(EC2Constants.Dimension.INSTANCE_TYPE, "t1.micro");
request = processor.createRequest(method, ListMetricsOptions.builder()
.dimension(dimension1)
.dimension(dimension2)
.metricName(metricName)
.namespace(namespace)
.build());
request = processor.createRequest(method, null, ListMetricsOptions.builder()
.dimension(dimension1)
.dimension(dimension2)
.metricName(metricName)
.namespace(namespace)
.build());
assertRequestLineEquals(request, "POST https://monitoring.us-east-1.amazonaws.com/ HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Host: monitoring.us-east-1.amazonaws.com\n");

View File

@ -66,7 +66,7 @@ public class CloudWatchClientLiveTest extends BaseContextLiveTest<RestContext<Cl
@Test
protected void testGetMetricStatisticsV2() {
ListMetricsResponse metricsResponse = client.listMetrics(ListMetricsOptions.builder().build());
ListMetricsResponse metricsResponse = client.listMetrics(null, ListMetricsOptions.builder().build());
// Walk through all datapoints in all metrics until we find a metric datapoint that returns statistics
if (metricsResponse.getMetrics().size() > 0) {
@ -91,7 +91,7 @@ public class CloudWatchClientLiveTest extends BaseContextLiveTest<RestContext<Cl
.statistics(ImmutableSet.of(Statistics.MAXIMUM,
Statistics.MINIMUM))
.unit(Unit.PERCENT).build();
GetMetricStatisticsResponse response = client.getMetricStatistics(options);
GetMetricStatisticsResponse response = client.getMetricStatistics(null, options);
if (response.getDatapoints().size() > 0) {
checkNotNull(response.getLabel());
@ -123,7 +123,7 @@ public class CloudWatchClientLiveTest extends BaseContextLiveTest<RestContext<Cl
String testDimensionValue = "t1.micro";
// Test an empty request (pulls all stored metric options across all products)
response = client.listMetrics(ListMetricsOptions.builder().build());
response = client.listMetrics(null, ListMetricsOptions.builder().build());
performDefaultMetricsTests(response);
@ -157,12 +157,12 @@ public class CloudWatchClientLiveTest extends BaseContextLiveTest<RestContext<Cl
}
// Test with a NextToken, even if it's null
response = client.listMetrics(ListMetricsOptions.builder().nextToken(response.getNextToken()).build());
response = client.listMetrics(null, ListMetricsOptions.builder().nextToken(response.getNextToken()).build());
performDefaultMetricsTests(response);
// Test with a Namespace
response = client.listMetrics(ListMetricsOptions.builder().namespace(testNamespace).build());
response = client.listMetrics(null, ListMetricsOptions.builder().namespace(testNamespace).build());
performDefaultMetricsTests(response);
@ -172,7 +172,7 @@ public class CloudWatchClientLiveTest extends BaseContextLiveTest<RestContext<Cl
}
// Test with a MetricName
response = client.listMetrics(ListMetricsOptions.builder().metricName(testMetricName).build());
response = client.listMetrics(null, ListMetricsOptions.builder().metricName(testMetricName).build());
performDefaultMetricsTests(response);
@ -185,8 +185,7 @@ public class CloudWatchClientLiveTest extends BaseContextLiveTest<RestContext<Cl
if (testDimensionName != null) {
Dimension testDimension = new Dimension(testDimensionName, testDimensionValue);
response = client.listMetrics(ListMetricsOptions.builder()
.dimension(testDimension).build());
response = client.listMetrics(null, ListMetricsOptions.builder().dimension(testDimension).build());
performDefaultMetricsTests(response);

View File

@ -55,7 +55,7 @@ public class CloudWatchLiveTest extends BaseContextLiveTest<RestContext<CloudWat
@Test
protected void testCloudWatchListMetrics() {
// Just make sure there is at least one metric returned (Much better if the account you use has more than 500)
checkArgument(CloudWatch.listMetrics(client, ListMetricsOptions.builder().build()).iterator().hasNext());
checkArgument(CloudWatch.listMetrics(client, null, ListMetricsOptions.builder().build()).iterator().hasNext());
}
}

View File

@ -39,8 +39,8 @@ import static org.easymock.EasyMock.expect;
public class CloudWatchTest {
/**
* Tests {@link CloudWatch#listMetrics(CloudWatchClient, org.jclouds.cloudwatch.options.ListMetricsOptions)} where a
* single response returns all results.
* Tests {@link CloudWatch#listMetrics(CloudWatchClient, String, org.jclouds.cloudwatch.options.ListMetricsOptions)}
* where a single response returns all results.
*
* @throws Exception if anything goes wrong
*/
@ -50,18 +50,18 @@ public class CloudWatchTest {
ListMetricsOptions options = ListMetricsOptions.builder().build();
ListMetricsResponse response = new ListMetricsResponse(ImmutableSet.of(createMock(Metric.class)), null);
expect(client.listMetrics(options))
expect(client.listMetrics(null, options))
.andReturn(response)
.once();
EasyMock.replay(client);
Assert.assertEquals(1, Iterables.size(CloudWatch.listMetrics(client, options)));
Assert.assertEquals(1, Iterables.size(CloudWatch.listMetrics(client, null, options)));
}
/**
* Tests {@link CloudWatch#listMetrics(CloudWatchClient, org.jclouds.cloudwatch.options.ListMetricsOptions)} where
* retrieving all results requires multiple requests.
* Tests {@link CloudWatch#listMetrics(CloudWatchClient, String, org.jclouds.cloudwatch.options.ListMetricsOptions)}
* where retrieving all results requires multiple requests.
*
* @throws Exception if anything goes wrong
*/
@ -72,16 +72,17 @@ public class CloudWatchTest {
ListMetricsResponse response1 = new ListMetricsResponse(ImmutableSet.of(createMock(Metric.class)), "NEXTTOKEN");
ListMetricsResponse response2 = new ListMetricsResponse(ImmutableSet.of(createMock(Metric.class)), null);
expect(client.listMetrics(anyObject(ListMetricsOptions.class)))
// Using EasyMock.eq("") because EasyMock makes it impossible to pass null as a String value here
expect(client.listMetrics(EasyMock.eq(""), anyObject(ListMetricsOptions.class)))
.andReturn(response1)
.once();
expect(client.listMetrics(anyObject(ListMetricsOptions.class)))
expect(client.listMetrics(EasyMock.eq(""), anyObject(ListMetricsOptions.class)))
.andReturn(response2)
.once();
EasyMock.replay(client);
Assert.assertEquals(2, Iterables.size(CloudWatch.listMetrics(client, options)));
Assert.assertEquals(2, Iterables.size(CloudWatch.listMetrics(client, "", options)));
}
}