From 7b219b9b5bf760fd19122d44553f0ab6ed2978b8 Mon Sep 17 00:00:00 2001 From: Henry Addison Date: Thu, 19 May 2011 15:11:50 +0100 Subject: [PATCH] got put_metric_data request working and tested --- .../requests/cloud_watch/put_metric_data.rb | 43 +++++++++++++++---- .../cloud_watch/put_metric_data_tests.rb | 33 ++++++++++++++ 2 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 tests/aws/requests/cloud_watch/put_metric_data_tests.rb diff --git a/lib/fog/aws/requests/cloud_watch/put_metric_data.rb b/lib/fog/aws/requests/cloud_watch/put_metric_data.rb index 077a61789..436724329 100644 --- a/lib/fog/aws/requests/cloud_watch/put_metric_data.rb +++ b/lib/fog/aws/requests/cloud_watch/put_metric_data.rb @@ -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])) + options = {'Namespace' => namespace} - 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']})) + #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 diff --git a/tests/aws/requests/cloud_watch/put_metric_data_tests.rb b/tests/aws/requests/cloud_watch/put_metric_data_tests.rb new file mode 100644 index 000000000..0ca7fdfe8 --- /dev/null +++ b/tests/aws/requests/cloud_watch/put_metric_data_tests.rb @@ -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 \ No newline at end of file