1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

got put_metric_data request working and tested

This commit is contained in:
Henry Addison 2011-05-19 15:11:50 +01:00
parent f3fb4626f1
commit 7b219b9b5b
2 changed files with 67 additions and 9 deletions

View file

@ -27,21 +27,46 @@ module Fog
# ==== See Also
# http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_PutMetricData.html
#
def put_metric_data(namespace, metric_data)
statistics = options.delete 'Statistics'
options.merge!(AWS.indexed_param('Statistics.member.%d', [*statistics]))
if dimensions = options.delete('Dimensions')
options.merge!(AWS.indexed_param('Dimensions.member.%d.Name', dimensions.collect {|dimension| dimension['Name']}))
options.merge!(AWS.indexed_param('Dimensions.member.%d.Value', dimensions.collect {|dimension| dimension['Value']}))
def put_metric_data(namespace, metric_data)
options = {'Namespace' => namespace}
#first index the dimensions for any of the datums that have dimensions
metric_data.collect! do |metric_datum|
if dimensions = metric_datum.delete('Dimensions')
metric_datum.merge!(AWS.indexed_param('Dimensions.member.%d.Name', dimensions.collect {|dimension| dimension['Name']}))
metric_datum.merge!(AWS.indexed_param('Dimensions.member.%d.Value', dimensions.collect {|dimension| dimension['Value']}))
end
metric_datum
end
#then flatten out an hashes in the metric_data array
metric_data.collect! { |metric_datum| flatten_hash(metric_datum) }
#then index the metric_data array
options.merge!(AWS.indexed_param('MetricData.member.%d', [*metric_data]))
#then finally flatten out an hashes in the overall options array
options = flatten_hash(options)
request({
'Action' => 'GetMetricStatistics',
:parser => Fog::Parsers::AWS::CloudWatch::GetMetricStatistics.new
'Action' => 'PutMetricData',
:parser => Fog::Parsers::AWS::CloudWatch::PutMetricData.new
}.merge(options))
end
private
def flatten_hash(starting)
finishing = {}
starting.each do |top_level_key, top_level_value|
if top_level_value.is_a?(Hash)
nested_hash = top_level_value
nested_hash.each do |nested_key, nested_value|
finishing["#{top_level_key}.#{nested_key}"] = nested_value
end
else
finishing[top_level_key] = top_level_value
end
end
return finishing
end
end
end
end

View file

@ -0,0 +1,33 @@
Shindo.tests('AWS::CloudWatch | metric requests', ['aws', 'cloudwatch']) do
tests('success') do
namespace = 'Custom/Test'
@puts_format = {'ResponseMetadata' => {'RequestId' => String}}
tests('#puts_value').formats(@puts_format) do
AWS[:cloud_watch].put_metric_data(namespace, [{'MetricName' => 'PutsValue', 'Unit' => 'None', 'Value' => 1}]).body
end
tests('#puts_statistics_set').succeeds do
AWS[:cloud_watch].put_metric_data(namespace, [{'MetricName' => 'PutsStatisticsSet', 'Unit' => 'None', 'StatisticValues' => {'Minimum' => 0, 'Maximum' => 9, 'Sum' => 45, 'SampleCount' => 10, 'Average' => 4.5}}]).body
end
tests('#puts with dimensions').succeeds do
dimensions = [{}]
AWS[:cloud_watch].put_metric_data(namespace, [{'MetricName' => 'PutsWithDimensions', 'Unit' => 'None', 'Value' => 1, 'Dimensions' => dimensions}]).body
end
tests('#puts more than one').succeeds do
datapoints = (0..4).collect do |i|
dp = {'MetricName' => "Puts#{i}thValue", 'Unit' => 'None', 'Value' => i}
if i%2==0
dp['Dimensions'] = [{'Name' => 'Ruler', 'Value' => "measurement_#{i}"}]
end
dp
end
AWS[:cloud_watch].put_metric_data(namespace, datapoints).body
end
end
end