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

Add more AWS Alarm functionality

This commit is contained in:
Trotter Cashion 2012-06-17 09:47:07 -07:00
parent eab5d489ed
commit 382381d2c1
6 changed files with 127 additions and 8 deletions

View file

@ -36,9 +36,37 @@ module Fog
class Mock
def initialize(options={})
def self.data
@data ||= Hash.new do |hash, region|
hash[region] = Hash.new do |region_hash, key|
region_hash[key] = {
:metric_alarms => {}
}
end
end
end
def self.reset
@data = nil
end
def initialize(options={})
@aws_access_key_id = options[:aws_access_key_id]
@region = options[:region] || 'us-east-1'
unless ['ap-northeast-1', 'ap-southeast-1', 'eu-west-1', 'sa-east-1', 'us-east-1', 'us-west-1', 'us-west-2'].include?(@region)
raise ArgumentError, "Unknown region: #{@region.inspect}"
end
end
def data
self.class.data[@region][@aws_access_key_id]
end
def reset_data
self.class.data[@region].delete(@aws_access_key_id)
end
end
class Real

View file

@ -14,8 +14,8 @@ module Fog
attribute :scaling_adjustment, :aliases => 'ScalingAdjustment'
def initialize(attributes)
attributes['AdjustmentType'] = 'ChangeInCapacity'
attributes['ScalingAdjustment'] = 1
attributes['AdjustmentType'] ||= 'ChangeInCapacity'
attributes['ScalingAdjustment'] ||= 1
super
end

View file

@ -3,9 +3,56 @@ require 'fog/core/model'
module Fog
module AWS
class CloudWatch
class Alarm < Fog::Model
attribute :alarm_names, :aliases => 'AlarmNames'
identity :id, :aliases => 'AlarmName'
attribute :actions_enabled, :aliases => 'ActionsEnabled'
attribute :alarm_actions, :aliases => 'AlarmActions'
attribute :arn, :aliases => 'AlarmArn'
attribute :alarm_configuration_updated_timestamp, :aliases => 'AlarmConfigurationUpdatedTimestamp'
attribute :alarm_description, :aliases => 'AlarmDescription'
attribute :comparison_operator, :aliases => 'ComparisonOperator'
attribute :dimensions, :aliases => 'Dimensions'
attribute :evaluation_periods, :aliases => 'EvaluationPeriods'
attribute :insufficient_data_actions, :aliases => 'InsufficientDataActions'
attribute :metric_name, :aliases => 'MetricName'
attribute :namespace, :aliases => 'Namespace'
attribute :ok_actions, :aliases => 'OKActions'
attribute :state_reason, :aliases => 'StateReason'
attribute :state_reason_data, :aliases => 'StateReasonData'
attribute :state_updated_timestamp, :aliases => 'StateUpdatedTimestamp'
attribute :state_value, :aliases => 'StateValue'
attribute :statistic, :aliases => 'Statistic'
attribute :threshold, :aliases => 'Threshold'
attribute :unit, :aliases => 'Unit'
end
def initialize(attributes)
attributes['EvaluationPeriods'] ||= 1
attributes['Namespace'] ||= 'AWS/EC2'
super
end
def save
requires :id
requires :comparison_operator
requires :evaluation_periods
requires :metric_name
requires :namespace
requires :period
requires :statistic
requires :threshold
options = Hash[self.class.aliases.map { |key, value| [key, send(value)] }]
options.delete_if { |key, value| value.nil? }
connection.put_metric_alarm(options)
reload
end
def destroy
requires :id
connection.delete_alarms(id)
end
end
end

View file

@ -7,7 +7,24 @@ module Fog
class Alarms < Fog::Collection
model Fog::AWS::CloudWatch::Alarm
def all
data = []
next_token = nil
loop do
result = connection.describe_alarms('NextToken' => next_token).body['DescribeAlarmsResult']
data += result['MetricAlarms']
next_token = result['NextToken']
break if next_token.nil?
end
load(data)
end
def get(identity)
data = connections.describe_alarms('AlarmNames' => identity).body['DescribeAlarmsResult']['MetricAlarms'].first
new(data) unless data.nil?
end
#alarm_names is an array of alarm names
def delete(alarm_names)
connection.delete_alarms(alarm_names)

View file

@ -1,7 +1,7 @@
module Fog
module AWS
class CloudWatch
class Real
class Real
require 'fog/aws/parsers/cloud_watch/describe_alarms'
@ -32,7 +32,25 @@ module Fog
:parser => Fog::Parsers::AWS::CloudWatch::DescribeAlarms.new
}.merge(options))
end
end
end
class Mock
def describe_alarms(options={})
results = { 'MetricAlarms' => [] }
data[:metric_alarms].each do |alarm_name, alarm_data|
results['MetricAlarms'] << {
'AlarmName' => alarm_name
}.merge!(alarm_data)
end
response = Excon::Response.new
response.status = 200
response.body = {
'DescribeAlarmsResult' => results,
'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id }
}
response
end
end
end
end
end

View file

@ -71,6 +71,15 @@ module Fog
end
end
data[:metric_alarms][options['AlarmName']] = {
'AlarmARN' => "arn:aws:cloudwatch:eu-west-1:000000000000:metricAlarm:00000000-0000-0000-0000-000000000000:alarmName/#{options['AlarmName']}",
'ActionsEnabled' => false,
'AlarmActions' => [],
'AlarmConfigurationUpdatedTimestamp' => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
'Dimensions' => [],
'OKActions' => [],
}.merge!(options)
response = Excon::Response.new
response.status = 200
response.body = {