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

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



50
51
52
53
54
55
56
# File 'lib/fog/aws/cloud_watch.rb', line 50

def initialize(options={})
  @aws_access_key_id = options[:aws_access_key_id]

  @region = options[:region] || 'us-east-1'

  Fog::AWS.validate_region!(@region)
end

Class Method Details

.dataObject



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

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

.resetObject



46
47
48
# File 'lib/fog/aws/cloud_watch.rb', line 46

def self.reset
  @data = nil
end

Instance Method Details

#dataObject



58
59
60
# File 'lib/fog/aws/cloud_watch.rb', line 58

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

#delete_alarms(alarm_names) ⇒ Object



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

#describe_alarms(options = {}) ⇒ Object



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/describe_alarms.rb', line 37

def describe_alarms(options={})

  records = if alarm_names = options.delete('AlarmNames')
              [*alarm_names].inject({}) do |r, name|
                (record = data[:metric_alarms][name]) ? r.merge(name => record) : r
              end
            else
              self.data[:metric_alarms]
            end

  results = records.inject([]) do |r, (name, data)|
    r << {'AlarmName' => name}.merge(data)
  end

  response = Excon::Response.new
  response.status = 200
  response.body = {
    'DescribeAlarmsResult' => { 'MetricAlarms' => results },
    'ResponseMetadata'     => { 'RequestId'    => Fog::AWS::Mock.request_id }
  }
  response
end

#list_metrics(options = {}) ⇒ Object



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

#put_metric_alarm(options) ⇒ Object

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::AWS::Compute::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::AWS::Compute::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

#reset_dataObject



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

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