mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #1334 from mikehale/asg-next-token
[AWS|cloud_watch] Add Metrics#each, which follows NextToken
This commit is contained in:
commit
bdbe9db916
3 changed files with 69 additions and 7 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -14,4 +14,19 @@ Shindo.tests("AWS::CloudWatch | metrics", ['aws', 'cloudwatch']) do
|
|||
|
||||
end
|
||||
|
||||
end
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue