Class: Fog::AWS::CloudWatch::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/cloud_watch.rb,
lib/fog/aws/requests/cloud_watch/list_metrics.rb,
lib/fog/aws/requests/cloud_watch/delete_alarms.rb,
lib/fog/aws/requests/cloud_watch/describe_alarms.rb,
lib/fog/aws/requests/cloud_watch/put_metric_alarm.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Mock) initialize(options = {})

Returns a new instance of Mock



52
53
54
55
56
57
58
59
60
# File 'lib/fog/aws/cloud_watch.rb', line 52

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', 'ap-southeast-2', '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

Class Method Details

+ (Object) data



38
39
40
41
42
43
44
45
46
# File 'lib/fog/aws/cloud_watch.rb', line 38

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

+ (Object) reset



48
49
50
# File 'lib/fog/aws/cloud_watch.rb', line 48

def self.reset
  @data = nil
end

Instance Method Details

- (Object) data



62
63
64
# File 'lib/fog/aws/cloud_watch.rb', line 62

def data
  self.class.data[@region][@aws_access_key_id]
end

- (Object) delete_alarms(alarm_names)



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fog/aws/requests/cloud_watch/delete_alarms.rb', line 29

def delete_alarms(alarm_names)
  [*alarm_names].each do |alarm_name|
    unless data[:metric_alarms].key?(alarm_name)
      raise Fog::AWS::AutoScaling::NotFound, "The alarm '#{alarm_name}' does not exist."
    end
  end

  [*alarm_names].each { |alarm_name| data[:metric_alarms].delete(alarm_name) }
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id }
  }
  response
end

- (Object) describe_alarms(options = {})



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fog/aws/requests/cloud_watch/describe_alarms.rb', line 37

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

- (Object) list_metrics(options = {})



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fog/aws/requests/cloud_watch/list_metrics.rb', line 36

def list_metrics(options={})
  body = case options["NextToken"]
         when nil
           { "ListMetricsResult" => {
              "Metrics" => (0...500).map{ {} },
              "NextToken" => '1'
           }}
         when "1"
           { "ListMetricsResult" => {
              "Metrics" => (0...500).map{ {} },
              "NextToken" => '2'
           }}
         when "2"
           { "ListMetricsResult" => {
              "Metrics" => (0...1).map{ {} }
           }}
         end

  Excon::Response.new.tap do |response|
    response.body = body
    response.status = 200
  end
end

- (Object) put_metric_alarm(options)

See: Fog::AWS::CloudWatch::Real.put_metric_alarm()



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fog/aws/requests/cloud_watch/put_metric_alarm.rb', line 60

def put_metric_alarm(options)
  supported_actions = [ "InsufficientDataActions", "OKActions", "AlarmActions" ]
  found_actions = options.keys.select {|key| supported_actions.include? key }
  if found_actions.empty?
    raise Fog::Compute::AWS::Error.new("The request must contain at least one of #{supported_actions.join(", ")}'")
  end

  requirements = [ "AlarmName", "ComparisonOperator", "EvaluationPeriods", "Namespace", "Period", "Statistic", "Threshold" ]
  requirements.each do |req|
    unless options.key?(req)
      raise Fog::Compute::AWS::Error.new("The request must contain a the parameter '%s'" % req)
    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 = {
    'requestId' => Fog::AWS::Mock.request_id
  }
  response
end

- (Object) reset_data



66
67
68
# File 'lib/fog/aws/cloud_watch.rb', line 66

def reset_data
  self.class.data[@region].delete(@aws_access_key_id)
end