Add support to apis/cloudwatch for listing stored metrics.

* Also adds some useful constants for CloudWatch dimensions, metric names and namespaces
This commit is contained in:
Jeremy Whitlock 2012-04-26 19:14:30 -06:00
parent 193506a4c0
commit 82533729da
26 changed files with 1894 additions and 42 deletions

1
.gitignore vendored
View File

@ -14,3 +14,4 @@ bin/
*.DS_STORE *.DS_STORE
TAGS TAGS
.metadata/ .metadata/
atlassian-ide-plugin.xml

View File

@ -0,0 +1,84 @@
/**
* 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;
import com.google.common.collect.AbstractIterator;
import org.jclouds.cloudwatch.domain.ListMetricsResponse;
import org.jclouds.cloudwatch.domain.Metric;
import org.jclouds.cloudwatch.options.ListMetricsOptions;
import java.util.Iterator;
/**
* Utilities for using CloudWatch.
*
* @author Jeremy Whitlock
*/
public class CloudWatch {
/**
* List metrics based on the criteria in the {@link ListMetricsOptions} passed in.
*
* @param cloudWatchClient the CloudWatch client
* @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) {
return new Iterable<Metric>() {
public Iterator<Metric> iterator() {
return new AbstractIterator<Metric>() {
private ListMetricsOptions lastOptions = options;
private ListMetricsResponse response = cloudWatchClient.listMetrics(lastOptions);
private Iterator<Metric> iterator = response.getMetrics().iterator();
/**
* {@inheritDoc}
*/
@Override
protected Metric computeNext() {
while (true) {
if (iterator == null) {
lastOptions = ListMetricsOptions.builder()
.dimensions(lastOptions.getDimensions())
.metricName(lastOptions.getMetricName())
.namespace(lastOptions.getNamespace())
.nextToken(response.getNextToken())
.build();
response = cloudWatchClient.listMetrics(lastOptions);
iterator = response.getMetrics().iterator();
}
if (iterator.hasNext()) {
return iterator.next();
}
if (response.getNextToken() == null) {
return endOfData();
}
iterator = null;
}
}
};
}
};
}
}

View File

@ -18,19 +18,16 @@
*/ */
package org.jclouds.cloudwatch; package org.jclouds.cloudwatch;
import java.util.Date; import com.google.common.util.concurrent.ListenableFuture;
import java.util.Set;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import org.jclouds.aws.filters.FormSigner; import org.jclouds.aws.filters.FormSigner;
import org.jclouds.cloudwatch.domain.Datapoint; import org.jclouds.cloudwatch.domain.Datapoint;
import org.jclouds.cloudwatch.domain.ListMetricsResponse;
import org.jclouds.cloudwatch.domain.Statistics; import org.jclouds.cloudwatch.domain.Statistics;
import org.jclouds.cloudwatch.functions.ISO8601Format; import org.jclouds.cloudwatch.functions.ISO8601Format;
import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions; import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions;
import org.jclouds.cloudwatch.options.ListMetricsOptions;
import org.jclouds.cloudwatch.xml.GetMetricStatisticsResponseHandler; import org.jclouds.cloudwatch.xml.GetMetricStatisticsResponseHandler;
import org.jclouds.cloudwatch.xml.ListMetricsResponseHandler;
import org.jclouds.javax.annotation.Nullable; import org.jclouds.javax.annotation.Nullable;
import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull; import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull;
import org.jclouds.rest.annotations.EndpointParam; import org.jclouds.rest.annotations.EndpointParam;
@ -40,7 +37,11 @@ import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.VirtualHost; import org.jclouds.rest.annotations.VirtualHost;
import org.jclouds.rest.annotations.XMLResponseParser; import org.jclouds.rest.annotations.XMLResponseParser;
import com.google.common.util.concurrent.ListenableFuture; import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.Date;
import java.util.Set;
/** /**
* Provides access to Amazon CloudWatch via the Query API * Provides access to Amazon CloudWatch via the Query API
@ -72,4 +73,14 @@ public interface CloudWatchAsyncClient {
@FormParam("Period") int period, @FormParam("Period") int period,
@FormParam("Statistics.member.1") Statistics statistics, @FormParam("Statistics.member.1") Statistics statistics,
GetMetricStatisticsOptions... options); GetMetricStatisticsOptions... options);
/**
* @see CloudWatchClient#listMetrics(org.jclouds.cloudwatch.options.ListMetricsOptions)
*/
@POST
@Path("/")
@XMLResponseParser(ListMetricsResponseHandler.class)
@FormParams(keys = "Action", values = "ListMetrics")
ListenableFuture<? extends ListMetricsResponse> listMetrics(ListMetricsOptions options);
} }

View File

@ -18,16 +18,18 @@
*/ */
package org.jclouds.cloudwatch; package org.jclouds.cloudwatch;
import org.jclouds.cloudwatch.domain.Datapoint;
import org.jclouds.cloudwatch.domain.ListMetricsResponse;
import org.jclouds.cloudwatch.domain.Statistics;
import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions;
import org.jclouds.cloudwatch.options.ListMetricsOptions;
import org.jclouds.concurrent.Timeout;
import org.jclouds.javax.annotation.Nullable;
import java.util.Date; import java.util.Date;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; 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;
/** /**
* Provides access to Amazon CloudWatch via the Query API * Provides access to Amazon CloudWatch via the Query API
* <p/> * <p/>
@ -80,4 +82,20 @@ public interface CloudWatchClient {
Set<Datapoint> getMetricStatisticsInRegion(@Nullable String region, String metricName, String namespace, Set<Datapoint> getMetricStatisticsInRegion(@Nullable String region, String metricName, String namespace,
Date startTime, Date endTime, int period, Statistics statistics, GetMetricStatisticsOptions... options); Date startTime, Date endTime, int period, Statistics statistics, GetMetricStatisticsOptions... options);
/**
* Returns a list of valid metrics stored for the AWS account owner.
*
* <p/>
* <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)}.
*
* @param options the options describing the metrics query
*
* @return the list of valid metrics based on the AWS account owner and the options passed in
*/
ListMetricsResponse listMetrics(ListMetricsOptions options);
} }

View File

@ -0,0 +1,48 @@
/**
* 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.domain;
/**
* Constants interface for the AWS AutoScaling dimensions and metric names as of 2012-04-24.
*
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/DeveloperGuide/CW_Support_For_AWS.html#as-metricscollected" />
*
* @author Jeremy Whitlock
*/
public interface AutoScalingConstants {
public static class Dimension {
public static final String AUTO_SCALING_GROUP_NAME = "AutoScalingGroupName";
}
public static class MetricName {
public static final String GROUP_DESIRED_CAPACITY = "GroupDesiredCapacity";
public static final String GROUP_IN_SERVICE_INSTANCES = "GroupInServiceInstances";
public static final String GROUP_MAX_SIZE = "GroupMaxSize";
public static final String GROUP_MIN_SIZE = "GroupMinSize";
public static final String GROUP_PENDING_INSTANCES = "GroupPendingInstances";
public static final String GROUP_TERMINATING_INSTANCES = "GroupTerminatingInstances";
public static final String GROUP_TOTAL_INSTANCES = "GroupTotalInstances";
}
}

View File

@ -0,0 +1,83 @@
/**
* 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.domain;
/**
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_Dimension.html" />
*
* @author Jeremy Whitlock
*/
public class Dimension {
private final String name;
private final String value;
public Dimension(String name, String value) {
this.name = name;
this.value = value;
}
/**
* return the dimension name.
*/
public String getName() {
return name;
}
/**
* return the dimension value.
*/
public String getValue() {
return value;
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Dimension other = (Dimension)obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "[name=" + name + ", value=" + value + "]";
}
}

View File

@ -0,0 +1,49 @@
/**
* 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.domain;
/**
* Constants interface for the AWS AutoScaling dimensions and metric names as of 2012-04-24.
*
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/DeveloperGuide/CW_Support_For_AWS.html#dynamo-metricscollected" />
*
* @author Jeremy Whitlock
*/
public interface DynamoDBConstants {
public static class Dimension {
public static final String OPERATION = "Operation";
public static final String TABLE_NAME = "TableName";
}
public static class MetricName {
public static final String CONSUMED_READ_CAPACITY_UNITS = "ConsumedReadCapacityUnits";
public static final String CONSUMED_WRITE_CAPACITY_UNITS = "ConsumedWriteCapacityUnits";
public static final String SUCCESSFUL_REQUEST_LATENCY = "SuccessfulRequestLatency";
public static final String RETURNED_ITEM_COUNT = "ReturnedItemCount";
public static final String SYSTEM_ERRORS = "SystemErrors";
public static final String THROTTLED_REQUESTS = "ThrottledRequests";
public static final String USER_ERRORS = "UserErrors";
}
}

View File

@ -0,0 +1,49 @@
/**
* 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.domain;
/**
* Constants interface for the AWS AutoScaling dimensions and metric names as of 2012-04-24.
*
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/DeveloperGuide/CW_Support_For_AWS.html#ebs-metricscollected" />
*
* @author Jeremy Whitlock
*/
public interface EBSConstants {
public static class Dimension {
public static final String VOLUME_ID = "VolumeId";
}
public static class MetricName {
public static final String VOLUME_IDLE_TIME = "VolumeIdleTime";
public static final String VOLUME_QUEUE_LENGTH = "VolumeQueueLength";
public static final String VOLUME_READ_BYTES = "VolumeReadBytes";
public static final String VOLUME_READ_OPS = "VolumeReadOps";
public static final String VOLUME_TOTAL_READ_TIME = "VolumeTotalReadTime";
public static final String VOLUME_TOTAL_WRITE_TIME = "VolumeTotalWriteTime";
public static final String VOLUME_WRITE_BYTES = "VolumeWriteBytes";
public static final String VOLUME_WRITE_OPS = "VolumeWriteOps";
}
}

View File

@ -0,0 +1,51 @@
/**
* 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.domain;
/**
* Constants interface for the AWS AutoScaling dimensions and metric names as of 2012-04-24.
*
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/DeveloperGuide/CW_Support_For_AWS.html#ec2-metricscollected" />
*
* @author Jeremy Whitlock
*/
public interface EC2Constants {
public static class Dimension {
public static final String AUTO_SCALING_GROUP_NAME = "AutoScalingGroupName";
public static final String IMAGE_ID = "ImageId";
public static final String INSTANCE_ID = "InstanceId";
public static final String INSTANCE_TYPE = "InstanceType";
}
public static class MetricName {
public static final String CPU_UTILIZATION = "CPUUtilization";
public static final String DISK_READ_BYTES = "DiskReadBytes";
public static final String DISK_READ_OPS = "DiskReadOps";
public static final String DISK_WRITE_BYTES = "DiskWriteBytes";
public static final String DISK_WRITE_OPS = "DiskWriteOps";
public static final String NETWORK_IN = "NetworkIn";
public static final String NETWORK_OUT = "NetworkOut";
}
}

View File

@ -0,0 +1,52 @@
/**
* 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.domain;
/**
* Constants interface for the AWS AutoScaling dimensions and metric names as of 2012-04-24.
*
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/DeveloperGuide/CW_Support_For_AWS.html#elb-metricscollected" />
*
* @author Jeremy Whitlock
*/
public interface ELBConstants {
public static class Dimension {
public static final String AVAILABILITY_ZONE = "AvailabilityZone";
public static final String LOAD_BALANCER_NAME = "LoadBalancerName";
}
public static class MetricName {
public static final String HEALTHY_HOST_COUNT = "HealthyHostCount";
public static final String HTTP_CODE_BACKEND_2XX = "HTTPCode_Backend_2XX";
public static final String HTTP_CODE_BACKEND_3XX = "HTTPCode_Backend_3XX";
public static final String HTTP_CODE_BACKEND_4XX = "HTTPCode_Backend_4XX";
public static final String HTTP_CODE_BACKEND_5XX = "HTTPCode_Backend_5XX";
public static final String HTTP_CODE_ELB_4XX = "HTTPCode_ELB_4XX";
public static final String HTTP_CODE_ELB_5XX = "HTTPCode_ELB_5XX";
public static final String LATENCY = "Latency";
public static final String REQUEST_COUNT = "RequestCount";
public static final String UNHEALTHY_HOST_COUNT = "UnHealthyHostCount";
}
}

View File

@ -0,0 +1,65 @@
/**
* 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.domain;
/**
* Constants interface for the AWS AutoScaling dimensions and metric names as of 2012-04-24.
*
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/DeveloperGuide/CW_Support_For_AWS.html#emr-metricscollected" />
*
* @author Jeremy Whitlock
*/
public interface EMRConstants {
public static class Dimension {
public static final String JOB_FLOW_ID = "JobFlowId";
public static final String JOB_ID = "JobId";
}
public static class MetricName {
public static final String CORE_NODES_PENDING = "CoreNodesPending";
public static final String CORE_NODES_RUNNING = "CoreNodesRunning";
public static final String HDFS_BYTES_READ = "HDFSBytesRead";
public static final String HDFS_BYTES_WRITTEN = "HDFSBytesWritten";
public static final String HDFS_UTILIZATION = "HDFSUtilization";
public static final String IS_IDLE = "IsIdle";
public static final String JOBS_FAILED = "JobsFailed";
public static final String JOBS_RUNNING = "JobsRunning";
public static final String LIVE_DATA_NODES = "LiveDataNodes";
public static final String LIVE_TASK_TRACKERS = "LiveTaskTrackers";
public static final String MAP_SLOTS_OPEN = "MapSlotsOpen";
public static final String MISSING_BLOCKS = "MissingBlocks";
public static final String REDUCE_SLOTS_OPEN = "ReduceSlotsOpen";
public static final String REMAINING_MAP_TASKS = "RemainingMapTasks";
public static final String REMAINING_MAP_TASKS_PER_SLOT = "RemainingMapTasksPerSlot";
public static final String REMAINING_REDUCE_TASKS = "RemainingReduceTasks";
public static final String RUNNING_MAP_TASKS = "RunningMapTasks";
public static final String RUNNING_REDUCE_TASKS = "RunningReduceTasks";
public static final String S3_BYTES_READ = "S3BytesRead";
public static final String S3_BYTES_WRITTEN = "S3BytesWritten";
public static final String TASK_NODES_PENDING = "TaskNodesPending";
public static final String TASK_NODES_RUNNING = "TaskNodesRunning";
public static final String TOTAL_LOAD = "TotalLoad";
}
}

View File

@ -0,0 +1,115 @@
/**
* 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.domain;
import com.google.common.collect.Sets;
import org.jclouds.javax.annotation.Nullable;
import java.util.Iterator;
import java.util.Set;
/**
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_ListMetrics.html" />
*
* @author Jeremy Whitlock
*/
public class ListMetricsResponse {
private final Set<Metric> metrics;
private final String nextToken;
public ListMetricsResponse(Set<Metric> metrics, String nextToken) {
// Default to an empty set
if (metrics == null) {
this.metrics = Sets.newLinkedHashSet();
} else {
this.metrics = metrics;
}
this.nextToken = nextToken;
}
/**
* return the list of {@link Metric}
*/
public Set<Metric> getMetrics() {
return metrics;
}
/**
* return the next token or null if there is none.
*/
@Nullable
public String getNextToken() {
return nextToken;
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ListMetricsResponse other = (ListMetricsResponse)obj;
if (metrics == null) {
if (other.metrics != null)
return false;
} else if (!metrics.equals(other.metrics))
return false;
if (nextToken == null) {
if (other.nextToken != null)
return false;
} else if (!nextToken.equals(other.nextToken))
return false;
return true;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("[metrics=[");
Iterator<Metric> iterator = metrics.iterator();
while (iterator.hasNext()) {
builder.append(iterator.next());
if (iterator.hasNext()) {
builder.append(", ");
}
}
builder.append("]");
builder.append(", nextToken=")
.append(nextToken)
.append("]");
return builder.toString();
}
}

View File

@ -0,0 +1,132 @@
/**
* 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.domain;
import com.google.common.collect.Sets;
import org.jclouds.javax.annotation.Nullable;
import java.util.Iterator;
import java.util.Set;
/**
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_Metric.html" />
*
* @author Jeremy Whitlock
*/
public class Metric {
private final Set<Dimension> dimensions;
private final String metricName;
private final String namespace;
public Metric (String metricName, String namespace, @Nullable Set<Dimension> dimensions) {
// Default to an empty set
if (dimensions == null) {
this.dimensions = Sets.newLinkedHashSet();
} else {
this.dimensions = dimensions;
}
this.metricName = metricName;
this.namespace = namespace;
}
/**
* return the metric name for the metric.
*/
public String getMetricName() {
return metricName;
}
/**
* return the namespace for the metric
*/
public String getNamespace() {
return namespace;
}
/**
* return the available dimensions for the metric
*/
@Nullable
public Set<Dimension> getDimensions() {
return dimensions;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("[metricName=")
.append(metricName)
.append(", namespace=")
.append(namespace);
builder.append(", dimensions=[");
Iterator<Dimension> iterator = dimensions.iterator();
while (iterator.hasNext()) {
builder.append(iterator.next());
if (iterator.hasNext()) {
builder.append(", ");
}
}
builder.append("]]");
return builder.toString();
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Metric other = (Metric)obj;
if (metricName == null) {
if (other.metricName != null)
return false;
} else if (!metricName.equals(other.metricName))
return false;
if (namespace == null) {
if (other.namespace != null)
return false;
} else if (!namespace.equals(other.namespace))
return false;
if (dimensions == null) {
if (other.dimensions != null)
return false;
} else if (!dimensions.equals(other.dimensions))
return false;
return true;
}
}

View File

@ -0,0 +1,41 @@
/**
* 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.domain;
/**
* Constants interface for the AWS provided namespaces as of 2012-04-24.
*
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/DeveloperGuide/CW_Support_For_AWS.html#aws-namespaces" />
*
* @author Jeremy Whitlock
*/
public interface Namespaces {
public static final String AUTO_SCALING = "AWS/AutoScaling";
public static final String DYNAMODB = "AWS/DynamoDB";
public static final String EBS = "AWS/EBS";
public static final String EC2 = "AWS/EC2";
public static final String ELB = "AWS/ELB";
public static final String EMR = "AWS/EMR";
public static final String RDS = "AWS/RDS";
public static final String SNS = "AWS/SNS";
public static final String SQS = "AWS/SQS";
public static final String STORAGE_GATEWAY = "AWS/StorageGateway";
}

View File

@ -0,0 +1,56 @@
/**
* 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.domain;
/**
* Constants interface for the AWS AutoScaling dimensions and metric names as of 2012-04-24.
*
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/DeveloperGuide/CW_Support_For_AWS.html#rds-metricscollected" />
*
* @author Jeremy Whitlock
*/
public interface RDSConstants {
public static class Dimension {
public static final String DB_INSTANCE_IDENTIFIER = "DBInstanceIdentifier";
public static final String DATABASE_CLASS = "DatabaseClass";
public static final String ENGINE_NAME = "EngineName";
}
public static class MetricName {
public static final String BIN_LOG_DISK_USAGE = "BinLogDiskUsage";
public static final String CPU_UTILIZATION = "CPUUtilization";
public static final String DATABASE_CONNECTIONS = "DatabaseConnections";
public static final String FREEABLE_MEMORY = "FreeableMemory";
public static final String FREE_STORAGE_SPACE = "FreeStorageSpace";
public static final String REPLICA_LAG = "ReplicaLag";
public static final String READ_IO_OPS = "ReadIOPS";
public static final String READ_LATENCY = "ReadLatency";
public static final String READ_THROUGHPUT = "ReadThroughput";
public static final String SWAP_USAGE = "SwapUsage";
public static final String WRITE_IO_OPS = "WriteIOPS";
public static final String WRITE_LATENCY = "WriteLtency";
public static final String WRITE_THROUGHPUT = "WriteThroughput";
}
}

View File

@ -0,0 +1,45 @@
/**
* 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.domain;
/**
* Constants interface for the AWS AutoScaling dimensions and metric names as of 2012-04-24.
*
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/DeveloperGuide/CW_Support_For_AWS.html#sns-metricscollected" />
*
* @author Jeremy Whitlock
*/
public interface SNSConstants {
public static class Dimension {
public static final String TOPIC_NAME = "TopicName";
}
public static class MetricName {
public static final String NUMBER_OF_MESSAGES_PUBLISHED = "NumberOfMessagesPublished";
public static final String NUMBER_OF_NOTIFICATIONS_DELIVERED = "NumberOfNotificationsDelivered";
public static final String NUMBER_OF_NOTIFICATIONS_FAILED = "NumberOfNotificationsFailed";
public static final String PUBLISH_SIZE = "PublishSize";
}
}

View File

@ -0,0 +1,49 @@
/**
* 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.domain;
/**
* Constants interface for the AWS AutoScaling dimensions and metric names as of 2012-04-24.
*
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/DeveloperGuide/CW_Support_For_AWS.html#sqs-metricscollected" />
*
* @author Jeremy Whitlock
*/
public interface SQSConstants {
public static class Dimension {
public static final String QUEUE_NAME = "QueueName";
}
public static class MetricName {
public static final String APPROXIMATE_NUMBER_OF_MESSAGES_DELAYED = "ApproximateNumberOfMessagesDelayed";
public static final String APPROXIMATE_NUMBER_OF_MESSAGES_NOT_VISIBLE = "ApproximateNumberOfMessagesNotVisible";
public static final String APPROXIMATE_NUMBER_OF_MESSAGES_VISIBLE = "ApproximateNumberOfMessagesVisible";
public static final String NUMBER_OF_EMPTY_RECEIVES = "NumberOfEmptyReceives";
public static final String NUMBER_OF_MESSAGES_DELETED = "NumberOfMessagesDeleted";
public static final String NUMBER_OF_MESSAGES_RECEIVED = "NumberOfMessagesReceived";
public static final String NUMBER_OF_MESSAGES_SENT = "NumberOfMessagesSent";
public static final String SENT_MESSAGES_SIZE = "SentMessageSize";
}
}

View File

@ -0,0 +1,57 @@
/**
* 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.domain;
/**
* Constants interface for the AWS AutoScaling dimensions and metric names as of 2012-04-24.
*
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/DeveloperGuide/CW_Support_For_AWS.html#awssg-metricscollected" />
*
* @author Jeremy Whitlock
*/
public interface StorageGatewayConstants {
public static class Dimension {
public static final String GATEWAY_ID = "GatewayId";
public static final String GATEWAY_NAME = "GatewayName";
public static final String VOLUME_ID = "VolumeId";
}
public static class MetricName {
// Applicable for all Dimensions
public static final String QUEUED_WRITES = "QueuedWrites";
public static final String READ_BYTES = "ReadBytes";
public static final String READ_TIME = "ReadTime";
public static final String WRITE_BYTES = "WriteBytes";
public static final String WRITE_TIME = "WriteTime";
// Applicable for only GatewayId and GatewayName Dimensions
public static final String CLOUD_BYTES_DOWNLOADED = "CloudBytesDownloaded";
public static final String CLOUD_BYTES_UPLOADED = "CloudBytesUploaded";
public static final String CLOUD_DOWNLOAD_LATENCY = "CloudDownloadLatency";
public static final String WORKING_STORAGE_FREE = "WorkingStorageFree";
public static final String WORKING_STORAGE_PERCENT_USED = "WorkingStoragePercentUsed";
public static final String WORKING_STORAGE_USED = "WorkingStorageUsed";
}
}

View File

@ -0,0 +1,217 @@
/**
* 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 org.jclouds.cloudwatch.domain.Dimension;
import org.jclouds.http.options.BaseHttpRequestOptions;
import org.jclouds.javax.annotation.Nullable;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* Options used to list available metrics.
*
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_ListMetrics.html" />
*
* @author Jeremy Whitlock
*/
public class ListMetricsOptions extends BaseHttpRequestOptions {
private final Set<Dimension> dimensions;
private final String metricName;
private final String namespace;
private final String nextToken;
/**
* Private constructor to enforce using {@link Builder}.
*/
private ListMetricsOptions(@Nullable String namespace, @Nullable String metricName,
@Nullable Set<Dimension> dimensions, @Nullable String nextToken) {
this.dimensions = dimensions;
this.metricName = metricName;
this.namespace = namespace;
this.nextToken = nextToken;
}
/**
* return the set of dimensions for this request
*/
@Nullable
public Set<Dimension> getDimensions() {
return dimensions;
}
/**
* return the metric name for this request
*/
@Nullable
public String getMetricName() {
return metricName;
}
/**
* return the namespace for this request
*/
@Nullable
public String getNamespace() {
return namespace;
}
/**
* return the next token for this request
*/
@Nullable
public String getNextToken() {
return nextToken;
}
/**
* Returns a new builder. The generated builder is equivalent to the builder
* created by the {@link Builder} constructor.
*/
public static Builder builder() {
return new Builder();
}
public static class Builder {
private Set<Dimension> dimensions = new LinkedHashSet<Dimension>();
private String metricName;
private String namespace;
private String nextToken;
/**
* Creates a new builder. The returned builder is equivalent to the builder
* generated by {@link ListMetricsOptions#builder}.
*/
public Builder() {}
/**
* Filter available metrics specific to the namespace provided. To get all metrics regardless of the namespace,
* do not use this builder option. Only one namespace can be specified at a time so multiple invocations of
* this will just overwrite what was set prior. Available AWS namespaces are listed in the
* {@link org.jclouds.cloudwatch.domain.Namespaces} class. You can also use custom namespaces if you've
* configured CloudWatch for such things.
*
* @param namespace the namespace to filter the returned metrics by
*
* @return this {@code Builder} object
*/
public Builder namespace(String namespace) {
this.namespace = namespace;
return this;
}
/**
* Filter available metrics specific to the provided metric name. To get all metrics regardless of the metric
* name, do not use this builder option. Only one namespace can be specified at a time so multiple invocations
* of this will just overwrite what was set prior. Available AWS metric names are listed in the
* org.jclouds.cloudwatch.domain.*Constants classes and are AWS product specific. You can also use custom
* metric names if you've configured CloudWatch for such things.
*
* @param metricName the metric name to filter the returned metrics by
*
* @return this {@code Builder} object
*/
public Builder metricName(String metricName) {
this.metricName = metricName;
return this;
}
/**
* Same as {@link #dimension(org.jclouds.cloudwatch.domain.Dimension)} but replaces the current set of
* dimensions with the one passed in.
*
* @see #dimension(org.jclouds.cloudwatch.domain.Dimension)
*/
public Builder dimensions(Set<Dimension> dimensions) {
if (dimensions.size() > 10) {
throw new IllegalArgumentException("The maximum number of dimensions for ListOptions is 10.");
}
this.dimensions = dimensions;
return this;
}
/**
* Filter available metrics based on the {@link Dimension} passed in. To get all metrics regardless of the
* dimension, do not use this builder option. You can specify up to 10 different dimensions per request.
* Available AWS dimensions are listed in the org.jclouds.cloudwatch.domain.*Constants classes and are AWS
* product specific. You can also use custom dimensions if you've configured CloudWatch for such things.
*
* @param dimension the dimension to filter the returned metrics by
*
* @return this {@code Builder} object
*
* @throws IllegalArgumentException if this is invoked more than 10 times
*/
public Builder dimension(Dimension dimension) {
if (dimension != null) {
if (this.dimensions.size() >= 10) {
throw new IllegalArgumentException("You have exceeded the maximum number of dimensions per " +
"request.");
}
this.dimensions.add(dimension);
}
return this;
}
/**
* Specify that this request is a follow-up to retrieve more data from a paginated request.
*
* @param nextToken the next token
*
* @return this {@code Builder} object
*/
public Builder nextToken(String nextToken) {
this.nextToken = nextToken;
return this;
}
/**
* Returns a newly-created {@code ListMetricsOptions} based on the contents of
* the {@code Builder}.
*/
public ListMetricsOptions build() {
ListMetricsOptions lmo = new ListMetricsOptions(namespace, metricName, dimensions, nextToken);
int dimensionIndex = 1;
if (this.namespace != null) {
lmo.formParameters.put("Namespace", this.namespace);
}
if (this.metricName != null) {
lmo.formParameters.put("MetricName", this.metricName);
}
for (Dimension dimension : this.dimensions) {
lmo.formParameters.put("Dimensions.member." + dimensionIndex + ".Name", dimension.getName());
lmo.formParameters.put("Dimensions.member." + dimensionIndex + ".Value", dimension.getValue());
dimensionIndex++;
}
if (this.nextToken != null) {
lmo.formParameters.put("NextToken", this.nextToken);
}
return lmo;
}
}
}

View File

@ -0,0 +1,72 @@
/**
* 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.xml;
import org.jclouds.cloudwatch.domain.Dimension;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.util.SaxUtils;
import org.xml.sax.SAXException;
/**
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_Dimension.html" />
*
* @author Jeremy Whitlock
*/
public class DimensionHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Dimension> {
private StringBuilder currentText = new StringBuilder();
private String name;
private String value;
/**
* {@inheritDoc}
*/
@Override
public Dimension getResult() {
Dimension dimension = new Dimension(name, value);
// Reset since this handler is created once but produces N results
name = null;
value = null;
return dimension;
}
/**
* {@inheritDoc}
*/
@Override
public void endElement(String uri, String name, String qName) throws SAXException {
if (qName.equals("Name")) {
this.name = SaxUtils.currentOrNull(currentText);
} else if (qName.equals("Value")) {
value = SaxUtils.currentOrNull(currentText);
}
currentText = new StringBuilder();
}
/**
* {@inheritDoc}
*/
@Override
public void characters(char ch[], int start, int length) {
currentText.append(ch, start, length);
}
}

View File

@ -0,0 +1,104 @@
/**
* 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.xml;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
import org.jclouds.cloudwatch.domain.ListMetricsResponse;
import org.jclouds.cloudwatch.domain.Metric;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.util.SaxUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import java.util.Set;
/**
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_ListMetrics.html" />
*
* @author Jeremy Whitlock
*/
public class ListMetricsResponseHandler extends ParseSax.HandlerForGeneratedRequestWithResult<ListMetricsResponse> {
private final MetricHandler metricHandler;
private StringBuilder currentText = new StringBuilder();
private Set<Metric> metrics = Sets.newLinkedHashSet();
private boolean inMetrics;
private String nextToken;
@Inject
public ListMetricsResponseHandler(MetricHandler metricHandler) {
this.metricHandler = metricHandler;
}
/**
* {@inheritDoc}
*/
@Override
public ListMetricsResponse getResult() {
return new ListMetricsResponse(metrics, nextToken);
}
/**
* {@inheritDoc}
*/
@Override
public void startElement(String url, String name, String qName, Attributes attributes) throws SAXException {
if (SaxUtils.equalsOrSuffix(qName, "Metrics")) {
inMetrics = true;
}
if (inMetrics) {
metricHandler.startElement(url, name, qName, attributes);
}
}
/**
* {@inheritDoc}
*/
@Override
public void endElement(String uri, String name, String qName) throws SAXException {
if (inMetrics) {
if (qName.equals("Metrics")) {
inMetrics = false;
} else if (qName.equals("member") && !metricHandler.inDimensions()) {
metrics.add(metricHandler.getResult());
} else {
metricHandler.endElement(uri, name, qName);
}
} else if (qName.equals("NextToken")) {
nextToken = SaxUtils.currentOrNull(currentText);
}
currentText = new StringBuilder();
}
/**
* {@inheritDoc}
*/
@Override
public void characters(char ch[], int start, int length) {
if (inMetrics) {
metricHandler.characters(ch, start, length);
} else {
currentText.append(ch, start, length);
}
}
}

View File

@ -0,0 +1,118 @@
/**
* 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.xml;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
import org.jclouds.cloudwatch.domain.Dimension;
import org.jclouds.cloudwatch.domain.Metric;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.util.SaxUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import java.util.Set;
/**
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_Metric.html" />
*
* @author Jeremy Whitlock
*/
public class MetricHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Metric> {
private final DimensionHandler dimensionHandler;
private StringBuilder currentText = new StringBuilder();
private Set<Dimension> dimensions = Sets.newLinkedHashSet();
private boolean inDimensions;
private String metricName;
private String namespace;
@Inject
public MetricHandler(DimensionHandler dimensionHandler) {
this.dimensionHandler = dimensionHandler;
}
public boolean inDimensions() {
return inDimensions;
}
/**
* {@inheritDoc}
*/
@Override
public Metric getResult() {
Metric metric = new Metric(metricName, namespace, dimensions);
// Reset since this handler is created once but produces N results
dimensions = Sets.newLinkedHashSet();
metricName = null;
namespace = null;
return metric;
}
/**
* {@inheritDoc}
*/
@Override
public void startElement(String url, String name, String qName, Attributes attributes) throws SAXException {
if (!inDimensions && SaxUtils.equalsOrSuffix(qName, "member")) {
inDimensions = true;
}
if (inDimensions) {
dimensionHandler.startElement(url, name, qName, attributes);
}
}
/**
* {@inheritDoc}
*/
@Override
public void endElement(String uri, String name, String qName) throws SAXException {
if (inDimensions) {
if (qName.equals("Dimensions")) {
inDimensions = false;
} else if (qName.equals("member")) {
dimensions.add(dimensionHandler.getResult());
} else {
dimensionHandler.endElement(uri, name, qName);
}
} else if (qName.equals("MetricName")) {
metricName = SaxUtils.currentOrNull(currentText);
} else if (qName.equals("Namespace")) {
namespace = SaxUtils.currentOrNull(currentText);
}
currentText = new StringBuilder();
}
/**
* {@inheritDoc}
*/
@Override
public void characters(char ch[], int start, int length) {
if (inDimensions) {
dimensionHandler.characters(ch, start, length);
} else {
currentText.append(ch, start, length);
}
}
}

View File

@ -18,24 +18,21 @@
*/ */
package org.jclouds.cloudwatch; package org.jclouds.cloudwatch;
import static com.google.common.collect.Maps.transformValues; import com.google.common.base.Supplier;
import static org.testng.Assert.assertEquals; import com.google.common.collect.ImmutableMap;
import com.google.inject.Module;
import java.io.IOException; import com.google.inject.TypeLiteral;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Date;
import java.util.Map;
import javax.inject.Named;
import org.jclouds.Constants; import org.jclouds.Constants;
import org.jclouds.apis.ApiMetadata; import org.jclouds.apis.ApiMetadata;
import org.jclouds.aws.domain.Region; import org.jclouds.aws.domain.Region;
import org.jclouds.aws.filters.FormSigner; import org.jclouds.aws.filters.FormSigner;
import org.jclouds.cloudwatch.config.CloudWatchRestClientModule; import org.jclouds.cloudwatch.config.CloudWatchRestClientModule;
import org.jclouds.cloudwatch.domain.Dimension;
import org.jclouds.cloudwatch.domain.EC2Constants;
import org.jclouds.cloudwatch.domain.Namespaces;
import org.jclouds.cloudwatch.domain.Statistics; import org.jclouds.cloudwatch.domain.Statistics;
import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions; import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions;
import org.jclouds.cloudwatch.options.ListMetricsOptions;
import org.jclouds.cloudwatch.xml.GetMetricStatisticsResponseHandler; import org.jclouds.cloudwatch.xml.GetMetricStatisticsResponseHandler;
import org.jclouds.date.DateService; import org.jclouds.date.DateService;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
@ -48,10 +45,16 @@ import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.util.Suppliers2; import org.jclouds.util.Suppliers2;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Supplier; import javax.inject.Named;
import com.google.common.collect.ImmutableMap; import java.io.IOException;
import com.google.inject.Module; import java.lang.reflect.Method;
import com.google.inject.TypeLiteral; import java.net.URI;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Map;
import static com.google.common.collect.Maps.transformValues;
import static org.testng.Assert.assertEquals;
/** /**
* Tests behavior of {@code CloudWatchAsyncClient} * Tests behavior of {@code CloudWatchAsyncClient}
@ -63,6 +66,72 @@ import com.google.inject.TypeLiteral;
@Test(groups = "unit", testName = "CloudWatchAsyncClientTest") @Test(groups = "unit", testName = "CloudWatchAsyncClientTest")
public class CloudWatchAsyncClientTest extends BaseAsyncClientTest<CloudWatchAsyncClient> { public class CloudWatchAsyncClientTest extends BaseAsyncClientTest<CloudWatchAsyncClient> {
/**
* Tests that {@link CloudWatchAsyncClient#listMetrics(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);
HttpRequest request;
// Test an empty request
request = processor.createRequest(method, 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");
assertPayloadEquals(request,
"Action=ListMetrics",
"application/x-www-form-urlencoded", false);
// Note: Order of request params is as follows => Namespace, MetricName, Dimensions, NextToken
// Test a request with all (only one dimension)
Dimension dimension1 = new Dimension(EC2Constants.Dimension.INSTANCE_ID, "SOMEINSTANCEID");
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());
assertRequestLineEquals(request, "POST https://monitoring.us-east-1.amazonaws.com/ HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Host: monitoring.us-east-1.amazonaws.com\n");
assertPayloadEquals(request,
"Action=ListMetrics" +
"&Namespace=" + URLEncoder.encode(namespace, "UTF-8") +
"&MetricName=" + metricName +
"&Dimensions.member.1.Name=" + dimension1.getName() +
"&Dimensions.member.1.Value=" + dimension1.getValue() +
"&NextToken=" + nextToken,
"application/x-www-form-urlencoded", false);
// 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());
assertRequestLineEquals(request, "POST https://monitoring.us-east-1.amazonaws.com/ HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Host: monitoring.us-east-1.amazonaws.com\n");
assertPayloadEquals(request,
"Action=ListMetrics" +
"&Namespace=" + URLEncoder.encode(namespace, "UTF-8") +
"&MetricName=" + metricName +
"&Dimensions.member.1.Name=" + dimension1.getName() +
"&Dimensions.member.1.Value=" + dimension1.getValue() +
"&Dimensions.member.2.Name=" + dimension2.getName() +
"&Dimensions.member.2.Value=" + dimension2.getValue(),
"application/x-www-form-urlencoded", false);
}
public void testRegisterInstancesWithMeasure() throws SecurityException, NoSuchMethodException, IOException { public void testRegisterInstancesWithMeasure() throws SecurityException, NoSuchMethodException, IOException {
Date date = new Date(10000000l); Date date = new Date(10000000l);
Method method = CloudWatchAsyncClient.class.getMethod("getMetricStatisticsInRegion", String.class, String.class, Method method = CloudWatchAsyncClient.class.getMethod("getMetricStatisticsInRegion", String.class, String.class,

View File

@ -18,22 +18,28 @@
*/ */
package org.jclouds.cloudwatch; package org.jclouds.cloudwatch;
import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.reflect.TypeToken;
import org.jclouds.apis.BaseContextLiveTest;
import org.jclouds.cloudwatch.domain.Datapoint;
import org.jclouds.cloudwatch.domain.Dimension;
import org.jclouds.cloudwatch.domain.EC2Constants;
import org.jclouds.cloudwatch.domain.ListMetricsResponse;
import org.jclouds.cloudwatch.domain.Metric;
import org.jclouds.cloudwatch.domain.Namespaces;
import org.jclouds.cloudwatch.domain.Statistics;
import org.jclouds.cloudwatch.domain.Unit;
import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions;
import org.jclouds.cloudwatch.options.ListMetricsOptions;
import org.jclouds.rest.RestContext;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Set; import java.util.Set;
import org.jclouds.apis.BaseContextLiveTest; import static com.google.common.base.Preconditions.checkArgument;
import org.jclouds.cloudwatch.domain.Datapoint; import static com.google.common.base.Preconditions.checkNotNull;
import org.jclouds.cloudwatch.domain.Statistics;
import org.jclouds.cloudwatch.domain.Unit;
import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions;
import org.jclouds.rest.RestContext;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.common.reflect.TypeToken;
/** /**
* Tests behavior of {@code CloudWatchClient} * Tests behavior of {@code CloudWatchClient}
@ -55,6 +61,117 @@ public class CloudWatchClientLiveTest extends BaseContextLiveTest<RestContext<Cl
client = context.getApi(); client = context.getApi();
} }
@Test
protected void testListMetrics() {
ListMetricsResponse response;
String testNamespace = Namespaces.EC2;
String testMetricName = EC2Constants.MetricName.CPU_UTILIZATION;
String testDimensionName = EC2Constants.Dimension.INSTANCE_TYPE;
String testDimensionValue = "t1.micro";
// Test an empty request (pulls all stored metric options across all products)
response = client.listMetrics(ListMetricsOptions.builder().build());
performDefaultMetricsTests(response);
if (response.getMetrics().size() > 0) {
Metric metric = response.getMetrics().iterator().next();
testMetricName = metric.getMetricName();
testNamespace = metric.getNamespace();
if (metric.getDimensions().size() > 0) {
Dimension dimension = metric.getDimensions().iterator().next();
testDimensionName = dimension.getName();
testDimensionValue = dimension.getValue();
}
if (testDimensionName == null) {
for (Metric metric1 : response.getMetrics()) {
Set<Dimension> dimensions = metric1.getDimensions();
if (dimensions.size() > 0) {
Dimension dimension = metric.getDimensions().iterator().next();
testDimensionName = dimension.getName();
testDimensionValue = dimension.getValue();
break;
}
}
}
}
// Test with a NextToken, even if it's null
response = client.listMetrics(ListMetricsOptions.builder().nextToken(response.getNextToken()).build());
performDefaultMetricsTests(response);
// Test with a Namespace
response = client.listMetrics(ListMetricsOptions.builder().namespace(testNamespace).build());
performDefaultMetricsTests(response);
for (Metric metric : response.getMetrics()) {
checkArgument(metric.getNamespace().equals(testNamespace),
"All metrics should have the " + testNamespace + " Namespace.");
}
// Test with a MetricName
response = client.listMetrics(ListMetricsOptions.builder().metricName(testMetricName).build());
performDefaultMetricsTests(response);
for (Metric metric : response.getMetrics()) {
checkArgument(metric.getMetricName().equals(testMetricName),
"All metrics should have the " + testMetricName + " MetricName.");
}
// Test with a Dimension
if (testDimensionName != null) {
Dimension testDimension = new Dimension(testDimensionName, testDimensionValue);
response = client.listMetrics(ListMetricsOptions.builder()
.dimension(testDimension).build());
performDefaultMetricsTests(response);
for (Metric metric : response.getMetrics()) {
Set<Dimension> dimensions = metric.getDimensions();
checkArgument(dimensions.size() == 1, "There should only be one Dimension.");
Dimension dimension = dimensions.iterator().next();
checkArgument(dimension.equals(testDimension),
"The retrieved Dimension and test Dimension should be equal.");
}
}
}
private void performDefaultMetricsTests(ListMetricsResponse response) {
// If there are less than 500 metrics, NextToken should be null
if (response.getMetrics().size() < 500) {
checkArgument(response.getNextToken() == null,
"NextToken should be null for response with fewer than 500 metrics.");
}
for (Metric metric : response.getMetrics()) {
Set<Dimension> dimensions = metric.getDimensions();
checkArgument(dimensions.size() <= 10, "Dimensions set cannot be greater than 10 items.");
for (Dimension dimension : dimensions) {
checkNotNull(dimension.getName(), "Name cannot be null for a Dimension.");
checkNotNull(dimension.getValue(), "Value cannot be null for a Dimension.");
}
checkNotNull(metric.getMetricName(), "MetricName cannot be null for a Metric.");
checkNotNull(metric.getNamespace(), "Namespace cannot be null for a Metric.");
}
}
@Test @Test
protected void testGetMetricStatisticsInRegion() { protected void testGetMetricStatisticsInRegion() {
getEC2MetricStatisticsInRegion(null); getEC2MetricStatisticsInRegion(null);
@ -74,4 +191,5 @@ public class CloudWatchClientLiveTest extends BaseContextLiveTest<RestContext<Cl
protected TypeToken<RestContext<CloudWatchClient, CloudWatchAsyncClient>> contextType() { protected TypeToken<RestContext<CloudWatchClient, CloudWatchAsyncClient>> contextType() {
return CloudWatchApiMetadata.CONTEXT_TOKEN; return CloudWatchApiMetadata.CONTEXT_TOKEN;
} }
} }

View File

@ -0,0 +1,61 @@
/**
* 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;
import com.google.common.reflect.TypeToken;
import org.jclouds.apis.BaseContextLiveTest;
import org.jclouds.cloudwatch.options.ListMetricsOptions;
import org.jclouds.rest.RestContext;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static com.google.common.base.Preconditions.checkArgument;
/**
* Tests behavior of {@code CloudWatch}.
*
* @author Jeremy Whitlock
*/
public class CloudWatchLiveTest extends BaseContextLiveTest<RestContext<CloudWatchClient, CloudWatchAsyncClient>> {
public CloudWatchLiveTest() {
provider = "cloudwatch";
}
private CloudWatchClient client;
@Override
@BeforeClass(groups = { "integration", "live" })
public void setupContext() {
super.setupContext();
client = context.getApi();
}
@Override
protected TypeToken<RestContext<CloudWatchClient, CloudWatchAsyncClient>> contextType() {
return CloudWatchApiMetadata.CONTEXT_TOKEN;
}
@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());
}
}

View File

@ -0,0 +1,87 @@
/**
* 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;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import org.easymock.EasyMock;
import org.jclouds.cloudwatch.domain.ListMetricsResponse;
import org.jclouds.cloudwatch.domain.Metric;
import org.jclouds.cloudwatch.options.ListMetricsOptions;
import org.testng.Assert;
import org.testng.annotations.Test;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
/**
* Tests behavior of {@code CloudWatch}.
*
* @author Jeremy Whitlock
*/
public class CloudWatchTest {
/**
* Tests {@link CloudWatch#listMetrics(CloudWatchClient, org.jclouds.cloudwatch.options.ListMetricsOptions)} where a
* single response returns all results.
*
* @throws Exception if anything goes wrong
*/
@Test
public void testSinglePageResult() throws Exception {
CloudWatchClient client = createMock(CloudWatchClient.class);
ListMetricsOptions options = ListMetricsOptions.builder().build();
ListMetricsResponse response = new ListMetricsResponse(ImmutableSet.of(createMock(Metric.class)), null);
expect(client.listMetrics(options))
.andReturn(response)
.once();
EasyMock.replay(client);
Assert.assertEquals(1, Iterables.size(CloudWatch.listMetrics(client, options)));
}
/**
* Tests {@link CloudWatch#listMetrics(CloudWatchClient, org.jclouds.cloudwatch.options.ListMetricsOptions)} where
* retrieving all results requires multiple requests.
*
* @throws Exception if anything goes wrong
*/
@Test
public void testMultiPageResult() throws Exception {
CloudWatchClient client = createMock(CloudWatchClient.class);
ListMetricsOptions options = ListMetricsOptions.builder().build();
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)))
.andReturn(response1)
.once();
expect(client.listMetrics(anyObject(ListMetricsOptions.class)))
.andReturn(response2)
.once();
EasyMock.replay(client);
Assert.assertEquals(2, Iterables.size(CloudWatch.listMetrics(client, options)));
}
}