From 70e8739c0fb9f0743f44a7a6bca3c472c7c57fc7 Mon Sep 17 00:00:00 2001 From: Michael Hale Date: Wed, 17 Oct 2012 21:42:14 -0400 Subject: [PATCH] [AWS|cloud_watch] Add Metrics#each, which follows NextToken --- lib/fog/aws/models/cloud_watch/metrics.rb | 32 +++++++++++++++---- .../aws/requests/cloud_watch/list_metrics.rb | 27 ++++++++++++++++ tests/aws/models/cloud_watch/metrics_tests.rb | 17 +++++++++- 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/lib/fog/aws/models/cloud_watch/metrics.rb b/lib/fog/aws/models/cloud_watch/metrics.rb index c97aedbe2..6dc907960 100644 --- a/lib/fog/aws/models/cloud_watch/metrics.rb +++ b/lib/fog/aws/models/cloud_watch/metrics.rb @@ -6,26 +6,46 @@ module Fog class CloudWatch class Metrics < Fog::Collection + attribute :next_token, :aliases => 'NextToken' + model Fog::AWS::CloudWatch::Metric - + def all(conditions={}) - data = connection.list_metrics(conditions).body['ListMetricsResult']['Metrics'] - load(data) # data is an array of attribute hashes + result = connection.list_metrics(conditions).body['ListMetricsResult'] + merge_attributes("NextToken" => result["NextToken"]) + load(result['Metrics']) # an array of attribute hashes end - + + alias :each_metric_this_page :each + def each + if !block_given? + self + else + subset = dup.all + subset.each_metric_this_page {|m| yield m } + + while next_token = subset.next_token + subset = subset.all("NextToken" => next_token) + subset.each_metric_this_page {|m| yield m } + end + + self + end + end + def get(namespace, metric_name, dimensions=nil) list_opts = {'Namespace' => namespace, 'MetricName' => metric_name} if dimensions dimensions_array = dimensions.collect do |name, value| {'Name' => name, 'Value' => value} end - # list_opts.merge!('Dimensions' => dimensions_array) + # list_opts.merge!('Dimensions' => dimensions_array) end if data = connection.list_metrics(list_opts).body['ListMetricsResult']['Metrics'].first new(data) end end - + end end end diff --git a/lib/fog/aws/requests/cloud_watch/list_metrics.rb b/lib/fog/aws/requests/cloud_watch/list_metrics.rb index 8db8ea4b3..cf8456a35 100644 --- a/lib/fog/aws/requests/cloud_watch/list_metrics.rb +++ b/lib/fog/aws/requests/cloud_watch/list_metrics.rb @@ -33,6 +33,33 @@ module Fog end end + + class Mock + def list_metrics(options={}) + body = case options["NextToken"] + when nil + { "ListMetricsResult" => { + "Metrics" => (0...500).map{ {} }, + "NextToken" => '1' + }} + when "1" + { "ListMetricsResult" => { + "Metrics" => (0...500).map{ {} }, + "NextToken" => '2' + }} + when "2" + { "ListMetricsResult" => { + "Metrics" => (0...1).map{ {} } + }} + end + + Excon::Response.new.tap do |response| + response.body = body + response.status = 200 + end + end + end + end end end diff --git a/tests/aws/models/cloud_watch/metrics_tests.rb b/tests/aws/models/cloud_watch/metrics_tests.rb index 1285caf1e..59609eeed 100644 --- a/tests/aws/models/cloud_watch/metrics_tests.rb +++ b/tests/aws/models/cloud_watch/metrics_tests.rb @@ -14,4 +14,19 @@ Shindo.tests("AWS::CloudWatch | metrics", ['aws', 'cloudwatch']) do end -end \ No newline at end of file + tests('#each') do + Fog.mock! + tests("handle NextToken").returns(1001) do + count = 0 + Fog::AWS[:cloud_watch].metrics.each {|e| count += 1 } + count + end + + tests("yields Metrics instances").succeeds do + all = [] + Fog::AWS[:cloud_watch].metrics.each {|e| all << e } + all.all? {|e| e.is_a?(Fog::AWS::CloudWatch::Metric) } + end + end + +end