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.



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

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

.dataObject



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

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



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

def self.reset
  @data = nil
end

Instance Method Details

#dataObject



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

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

#delete_alarms(alarm_names) ⇒ Object



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

def delete_alarms(alarm_names)
  [*alarm_names].each do |alarm_name|
    unless data[:metric_alarms].has_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



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

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

#list_metrics(options = {}) ⇒ Object



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

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::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.has_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

#reset_dataObject



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

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