From 4c524be0f07db59953d302361ae1cc67fe0c78f5 Mon Sep 17 00:00:00 2001 From: Michael Zeng Date: Fri, 30 Sep 2011 17:10:40 -0400 Subject: [PATCH] included more response elements, request parameters should now be complete. Included model and collection classes --- lib/fog/aws/models/cloud_watch/alarm.rb | 12 ++++ lib/fog/aws/models/cloud_watch/alarm_data.rb | 39 +++++++++++ lib/fog/aws/models/cloud_watch/alarm_datum.rb | 66 +++++++++++++++++++ .../aws/models/cloud_watch/alarm_histories.rb | 18 +++++ .../aws/models/cloud_watch/alarm_history.rb | 17 +++++ lib/fog/aws/models/cloud_watch/alarms.rb | 30 +++++++++ .../cloud_watch/describe_alarm_history.rb | 18 ++--- .../parsers/cloud_watch/describe_alarms.rb | 10 ++- .../cloud_watch/describe_alarms_for_metric.rb | 26 ++++---- 9 files changed, 213 insertions(+), 23 deletions(-) create mode 100644 lib/fog/aws/models/cloud_watch/alarm.rb create mode 100644 lib/fog/aws/models/cloud_watch/alarm_data.rb create mode 100644 lib/fog/aws/models/cloud_watch/alarm_datum.rb create mode 100644 lib/fog/aws/models/cloud_watch/alarm_histories.rb create mode 100644 lib/fog/aws/models/cloud_watch/alarm_history.rb create mode 100644 lib/fog/aws/models/cloud_watch/alarms.rb diff --git a/lib/fog/aws/models/cloud_watch/alarm.rb b/lib/fog/aws/models/cloud_watch/alarm.rb new file mode 100644 index 000000000..a36baecac --- /dev/null +++ b/lib/fog/aws/models/cloud_watch/alarm.rb @@ -0,0 +1,12 @@ +require 'fog/core/model' + +module Fog + module AWS + class CloudWatch + + class Alarm < Fog::Model + attribute :alarm_names, :aliases => 'AlarmNames' + end + end + end +end \ No newline at end of file diff --git a/lib/fog/aws/models/cloud_watch/alarm_data.rb b/lib/fog/aws/models/cloud_watch/alarm_data.rb new file mode 100644 index 000000000..7bc48704e --- /dev/null +++ b/lib/fog/aws/models/cloud_watch/alarm_data.rb @@ -0,0 +1,39 @@ +require 'fog/core/collection' +require 'fog/aws/models/cloud_watch/alarm_datum' + +module Fog + module AWS + class CloudWatch + class AlarmData < Fog::Collection + model Fog::AWS::CloudWatch::AlarmDatum + + def all(conditions={}) + data = connection.describe_alarms(conditions).body['DescribeAlarmsResult']['MetricAlarms'] + load(data) # data is an array of attribute hashes + end + + def get(namespace, metric_name, dimensions=nil, period=nil, statistic=nil, unit=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) + end + if period + list_opts.merge!('Period' => period) + end + if statistic + list_opts.merge!('Statistic' => statistic) + end + if unit + list_opts.merge!('Unit' => unit) + end + data = connection.describe_alarms_for_metric(list_opts).body['DescribeAlarmsForMetricResult']['MetricAlarms'] + load(data) + end + + end + end + end +end \ No newline at end of file diff --git a/lib/fog/aws/models/cloud_watch/alarm_datum.rb b/lib/fog/aws/models/cloud_watch/alarm_datum.rb new file mode 100644 index 000000000..e60a48b01 --- /dev/null +++ b/lib/fog/aws/models/cloud_watch/alarm_datum.rb @@ -0,0 +1,66 @@ +require 'fog/core/model' + +module Fog + module AWS + class CloudWatch + + class AlarmDatum < Fog::Model + attribute :alarm_name, :aliases => 'AlarmName' + attribute :metric_name, :aliases => 'MetricName' + attribute :namespace, :aliases => 'Namespace' + attribute :dimensions, :aliases => 'Dimensions' + attribute :alarm_description, :aliases => 'AlarmDescription' + attribute :alarm_arn, :aliases => 'AlarmArn' + attribute :state_value, :aliases => 'StateValue' + attribute :statistic, :aliases => 'Statistic' + attribute :comparison_operator, :aliases => 'ComparisonOperator' + attribute :state_reason, :aliases => 'StateReason' + attribute :action_enabled. :aliases => 'ActionsEnabled' + attribute :period, :aliases => 'Period' + attribute :evaluation_periods, :aliases => 'EvaluationPeriods' + attribute :threshold, :aliases => 'Threshold' + attribute :alarm_actions, :aliases => 'AlarmActions' + attribute :ok_actions, :aliases => 'OKActions' + attribute :insufficient_actions, :aliases => 'InsufficientDataActions' + attribute :unit, :aliases => 'Unit' + attribute :state_updated_timestamp, :aliases => 'StateUpdatedTimestamp' + attribute :alarm_configuration_updated_timestamp, :aliases => 'AlarmConfigurationUpdatedTimestamp' + + def save + requires :alarm_name + requires :comparison_operator + requires :evaluation_periods + requires :metric_name + requires :namespace + requires :period + requires :statistic + requires :threshold + + alarm_definition = { + 'AlarnName' => alarm_name, + 'ComparisonOperator' => comparison_operator, + 'EvaluationPeriods' => evaluation_periods, + 'MetricName' => metric_name, + 'Namespace' => namespace, + 'Period' => period, + 'Statistic' => statistic, + 'Threshold' => threshold + } + + alarm_definition.merge!('ActionsEnabled' => action_enabled) if action_enabled + alarm_definition.merge!('AlarmActions' => alarm_actions) if alarm_actions + alarm_definition.merge!('AlarmDescription' => alarm_description) if alarm_description + + #dimension is an array of Name/Value pairs, ex. ['host'=>'fog-host', 'version'=>'0.11.0'] + alarm_definition.merge!('Dimensions' => dimensions) if dimensions + alarm_definition.merge!('InsufficientDataActions' => insufficient_actions) if insufficient_actions + alarm_definition.merge!('OKActions' => ok_actions) if ok_actions + alarm_definition.merge!('Unit' => unit) if unit + + connection.put_metric_alarm(alarm_definition) + true + end + end + end + end +end \ No newline at end of file diff --git a/lib/fog/aws/models/cloud_watch/alarm_histories.rb b/lib/fog/aws/models/cloud_watch/alarm_histories.rb new file mode 100644 index 000000000..27558c21f --- /dev/null +++ b/lib/fog/aws/models/cloud_watch/alarm_histories.rb @@ -0,0 +1,18 @@ +require 'fog/core/collection' +require 'fog/aws/models/cloud_watch/alarm_history' + +module Fog + module AWS + class CloudWatch + class AlarmHistories < Fog::Collection + model Fog::AWS::CloudWatch::AlarmHistory + + def all(conditions={}) + data = connection.describe_alarm_history(conditions).body['DescribeAlarmHistoryResult']['AlarmHistoryItems'] + load(data) # data is an array of attribute hashes + end + + end + end + end +end \ No newline at end of file diff --git a/lib/fog/aws/models/cloud_watch/alarm_history.rb b/lib/fog/aws/models/cloud_watch/alarm_history.rb new file mode 100644 index 000000000..9b665a47a --- /dev/null +++ b/lib/fog/aws/models/cloud_watch/alarm_history.rb @@ -0,0 +1,17 @@ +require 'fog/core/model' + +module Fog + module AWS + class CloudWatch + + class AlarmHistory < Fog::Model + attribute :alarm_name, :aliases => 'AlarmName' + attribute :end_date, :aliases => 'EndDate' + attribute :history_item_type, :aliases => 'HistoryItemType' + attribute :max_records, :aliases => 'MaxRecords' + attribute :start_date, :aliases => 'StartDate' + + end + end + end +end \ No newline at end of file diff --git a/lib/fog/aws/models/cloud_watch/alarms.rb b/lib/fog/aws/models/cloud_watch/alarms.rb new file mode 100644 index 000000000..9cc82fbe0 --- /dev/null +++ b/lib/fog/aws/models/cloud_watch/alarms.rb @@ -0,0 +1,30 @@ +require 'fog/core/collection' +require 'fog/aws/models/cloud_watch/alarm' + +module Fog + module AWS + class CloudWatch + + class Alarms < Fog::Collection + model Fog::AWS::CloudWatch::Alarm + + #alarm_names is an array of alarm names + def delete(alarm_names) + connection.list_metrics(alarm_names) + true + end + + def disable(alarm_names) + connection.disable_alarm_actions(alarm_names) + true + end + + def enable(alarm_names) + connection.enable_alarm_actions(alarm_names) + true + end + + end + end + end +end diff --git a/lib/fog/aws/parsers/cloud_watch/describe_alarm_history.rb b/lib/fog/aws/parsers/cloud_watch/describe_alarm_history.rb index 1a6637315..410ab3bfd 100644 --- a/lib/fog/aws/parsers/cloud_watch/describe_alarm_history.rb +++ b/lib/fog/aws/parsers/cloud_watch/describe_alarm_history.rb @@ -6,12 +6,12 @@ module Fog class DescribeAlarmHistory < Fog::Parsers::Base def reset - @response = { 'DescribeAlarmHistoryResult' => {'AlarmHistory' => []}, 'ResponseMetadata' => {} } - reset_alarm_history + @response = { 'DescribeAlarmHistoryResult' => {'AlarmHistoryItems' => []}, 'ResponseMetadata' => {} } + reset_alarm_history_item end def reset_alarm_history - @alarm_history = {} + @alarm_history_item = {} end def start_element(name, attrs = []) @@ -21,14 +21,16 @@ module Fog def end_element(name) case name when 'AlarmName', 'HistoryItemType', 'HistorySummary' - @alarm_history[name] = value + @alarm_history_item[name] = value when 'Timestamp' - @alarm_history[name] = Time.parse value + @alarm_history_item[name] = Time.parse value when 'RequestId' - @response['ResponseMetadata'][name] = value + @response['ResponseMetadata'][name] = value + when 'NextToken' + @response['ResponseMetadata'][name] = value when 'member' - @response['DescribeAlarmHistoryResult']['AlarmHistory'] << @alarm_history - reset_alarm_history + @response['DescribeAlarmHistoryResult']['AlarmHistoryItems'] << @alarm_history_item + reset_alarm_history_item end end end diff --git a/lib/fog/aws/parsers/cloud_watch/describe_alarms.rb b/lib/fog/aws/parsers/cloud_watch/describe_alarms.rb index 62bb18e8b..92c037c34 100644 --- a/lib/fog/aws/parsers/cloud_watch/describe_alarms.rb +++ b/lib/fog/aws/parsers/cloud_watch/describe_alarms.rb @@ -42,13 +42,17 @@ module Fog @metric_alarms[name] = value.to_f when 'AlarmActions', 'OKActions', 'InsufficientDataActions' @metric_alarms[name] = value.to_s.strip - when 'AlarmName', 'Namespace', 'MetricName', 'AlarmDescription', 'AlarmArn', + when 'AlarmName', 'Namespace', 'MetricName', 'AlarmDescription', 'AlarmArn', 'Unit' 'StateValue', 'Statistic', 'ComparisonOperator', 'StateReason', 'ActionsEnabled' @metric_alarms[name] = value + when 'StateUpdatedTimestamp', 'AlarmConfigurationUpdatedTimestamp' + @metric_alarms[name] = Time.parse value when 'Dimensions' - @in_dimensions = false + @in_dimensions = false when 'RequestId' - @response['ResponseMetadata'][name] = value + @response['ResponseMetadata'][name] = value + when 'NextToken' + @response['ResponseMetadata'][name] = value when 'member' if !@in_dimensions unless @metric_alarms == {} diff --git a/lib/fog/aws/parsers/cloud_watch/describe_alarms_for_metric.rb b/lib/fog/aws/parsers/cloud_watch/describe_alarms_for_metric.rb index 3c30dd1bd..d170ab73a 100644 --- a/lib/fog/aws/parsers/cloud_watch/describe_alarms_for_metric.rb +++ b/lib/fog/aws/parsers/cloud_watch/describe_alarms_for_metric.rb @@ -6,12 +6,12 @@ module Fog class DescribeAlarmsForMetric < Fog::Parsers::Base def reset - @response = { 'DescribeAlarmsForMetricResult' => {'AlarmsForMetric' => []}, 'ResponseMetadata' => {} } - reset_alarms_for_metric + @response = { 'DescribeAlarmsForMetricResult' => {'MetricAlarms' => []}, 'ResponseMetadata' => {} } + reset_metric_alarms end - def reset_alarms_for_metric - @alarms_for_metric = {'Dimensions' => []} + def reset_metric_alarms + @metric_alarms = {'Dimensions' => []} end def reset_dimension @@ -35,24 +35,26 @@ module Fog when 'Name', 'Value' @dimension[name] = value when 'Period', 'EvaluationPeriods' - @alarms_for_metric[name] = value.to_i + @metric_alarms[name] = value.to_i when 'Threshold' - @alarms_for_metric[name] = value.to_f + @metric_alarms[name] = value.to_f when 'AlarmActions', 'OKActions', 'InsufficientDataActions' - @alarms_for_metric[name] = value.to_s.strip - when 'AlarmName', 'Namespace', 'MetricName', 'AlarmDescription', 'AlarmArn', + @metric_alarms[name] = value.to_s.strip + when 'AlarmName', 'Namespace', 'MetricName', 'AlarmDescription', 'AlarmArn', 'Unit' 'StateValue', 'Statistic', 'ComparisonOperator', 'StateReason', 'ActionsEnabled' - @alarms_for_metric[name] = value + @metric_alarms[name] = value + when 'StateUpdatedTimestamp', 'AlarmConfigurationUpdatedTimestamp' + @metric_alarms[name] = Time.parse value when 'Dimensions' @in_dimensions = false when 'RequestId' @response['ResponseMetadata'][name] = value when 'member' if !@in_dimensions - @response['DescribeAlarmsForMetricResult']['AlarmsForMetric'] << @alarms_for_metric - reset_alarms_for_metric + @response['DescribeAlarmsForMetricResult']['MetricAlarms'] << @metric_alarms + reset_metric_alarms else - @alarms_for_metric['Dimensions'] << @dimension + @metric_alarms['Dimensions'] << @dimension end end end