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

included more response elements, request parameters should now be complete. Included model and collection classes

This commit is contained in:
Michael Zeng 2011-09-30 17:10:40 -04:00
parent d9ef5b840f
commit 4c524be0f0
9 changed files with 213 additions and 23 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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
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

View file

@ -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
when 'RequestId'
@response['ResponseMetadata'][name] = value
when 'NextToken'
@response['ResponseMetadata'][name] = value
when 'member'
if !@in_dimensions
unless @metric_alarms == {}

View file

@ -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