From 94286a9ea9729972de96c9b481d36f96f0b0e605 Mon Sep 17 00:00:00 2001 From: Henry Addison Date: Thu, 19 May 2011 17:12:56 +0100 Subject: [PATCH] added model and collection methods and tests to allow putting of metric data --- .../models/cloud_watch/metric_statistic.rb | 27 +++++++++++++++++ .../models/cloud_watch/metric_statistics.rb | 5 +++- .../cloud_watch/metric_statistics_tests.rb | 30 ++++++++++++++++++- .../cloud_watch/put_metric_data_tests.rb | 10 +++---- 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/lib/fog/aws/models/cloud_watch/metric_statistic.rb b/lib/fog/aws/models/cloud_watch/metric_statistic.rb index c322f9f46..bc23ce30d 100644 --- a/lib/fog/aws/models/cloud_watch/metric_statistic.rb +++ b/lib/fog/aws/models/cloud_watch/metric_statistic.rb @@ -13,6 +13,33 @@ module Fog attribute :sample_count, :aliases => 'SampleCount' attribute :timestamp, :aliases => 'Timestamp' attribute :unit, :aliases => 'Unit' + attribute :metric_name, :aliases => 'MetricName' + attribute :namespace, :aliases => 'Namespace' + attribute :dimensions, :aliases => 'Dimensions' + attribute :value + + def save + requires :metric_name + requires :namespace + requires :unit + + put_opts = {'MetricName' => metric_name, 'Unit' => unit} + put_opts.merge!('Dimensions' => dimensions) if dimensions + put_opts.merge!('Timestamp' => dimensions) if timestamp + if value + put_opts.merge!('Value' => value) + else + put_opts.merge!('StatisticValues' => { + 'Minimum' => minimum, + 'Maximum' => maximum, + 'Sum' => sum, + 'Average' => average, + 'SampleCount' => sample_count + }) + end + connection.put_metric_data(namespace, [put_opts]) + true + end end end end diff --git a/lib/fog/aws/models/cloud_watch/metric_statistics.rb b/lib/fog/aws/models/cloud_watch/metric_statistics.rb index dd1395ef7..369d5f3f6 100644 --- a/lib/fog/aws/models/cloud_watch/metric_statistics.rb +++ b/lib/fog/aws/models/cloud_watch/metric_statistics.rb @@ -4,12 +4,15 @@ require 'fog/aws/models/cloud_watch/metric_statistic' module Fog module AWS class CloudWatch - class MetricStatistics < Fog::Collection model Fog::AWS::CloudWatch::MetricStatistic def all(filters) + metricName = filters['MetricName'] + namespace = filters['Namespace'] + dimensions = filters['Dimensions'] data = connection.get_metric_statistics(filters).body['GetMetricStatisticsResult']['Datapoints'] + data.collect! { |datum| datum.merge('MetricName' => metricName, 'Namespace' => namespace, 'Dimensions' => dimensions) } load(data) # data is an array of attribute hashes end diff --git a/tests/aws/models/cloud_watch/metric_statistics_tests.rb b/tests/aws/models/cloud_watch/metric_statistics_tests.rb index f5127feed..ff3d7a32e 100644 --- a/tests/aws/models/cloud_watch/metric_statistics_tests.rb +++ b/tests/aws/models/cloud_watch/metric_statistics_tests.rb @@ -12,12 +12,40 @@ Shindo.tests("AWS::CloudWatch | metric_statistics", ['aws', 'cloudwatch']) do statisticTypes = ['Minimum','Maximum','Sum','SampleCount','Average'] tests("#all").succeeds do - @statistics = AWS[:cloud_watch].metric_statistics.all({'Statistics' => statisticTypes, 'StartTime' => startTime, 'EndTime' => endTime, 'Period' => period, 'MetricName' => metricName, 'Namespace' => namespace, 'Dimensions' => [{'Name' => 'InstanceId', 'Value' => instanceId}]})) end + @statistics = AWS[:cloud_watch].metric_statistics.all({'Statistics' => statisticTypes, 'StartTime' => startTime, 'EndTime' => endTime, 'Period' => period, 'MetricName' => metricName, 'Namespace' => namespace, 'Dimensions' => [{'Name' => 'InstanceId', 'Value' => instanceId}]}) + end tests("#all_not_empty").returns(true) do @statistics.length > 0 end + new_attributes = { + :namespace => 'Custom/Test', + :metric_name => 'ModelTest', + :value => 9, + :unit => 'None' + } + tests('#new').returns(new_attributes) do + AWS[:cloud_watch].metric_statistics.new(new_attributes).attributes + end + + tests('#create').returns(new_attributes) do + AWS[:cloud_watch].metric_statistics.create(new_attributes).attributes + end + + stats_set_attributes = { + :namespace => 'Custom/Test', + :metric_name => 'ModelTest', + :minimum => 0, + :maximum => 4, + :sum => 10, + :sample_count => 5, + :average => 2, + :unit => 'None' + } + tests('#create_stats_set').returns(stats_set_attributes) do + AWS[:cloud_watch].metric_statistics.create(stats_set_attributes).attributes + end end end \ No newline at end of file diff --git a/tests/aws/requests/cloud_watch/put_metric_data_tests.rb b/tests/aws/requests/cloud_watch/put_metric_data_tests.rb index 0ca7fdfe8..04e1e0630 100644 --- a/tests/aws/requests/cloud_watch/put_metric_data_tests.rb +++ b/tests/aws/requests/cloud_watch/put_metric_data_tests.rb @@ -5,22 +5,22 @@ Shindo.tests('AWS::CloudWatch | metric requests', ['aws', 'cloudwatch']) do @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 + AWS[:cloud_watch].put_metric_data(namespace, [{'MetricName' => 'RequestTest', '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 + AWS[:cloud_watch].put_metric_data(namespace, [{'MetricName' => 'RequestTest', '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 + AWS[:cloud_watch].put_metric_data(namespace, [{'MetricName' => 'RequestTest', '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} + datapoints = (0...3).collect do |i| + dp = {'MetricName' => "#{ith}RequestTest", 'Unit' => 'None', 'Value' => i} if i%2==0 dp['Dimensions'] = [{'Name' => 'Ruler', 'Value' => "measurement_#{i}"}] end