Class: Chef::Provider::AwsCloudwatchAlarm

Inherits:
Chef::Provisioning::AWSDriver::AWSProvider show all
Defined in:
lib/chef/provider/aws_cloudwatch_alarm.rb

Constant Summary

Constants inherited from Chef::Provisioning::AWSDriver::AWSProvider

Chef::Provisioning::AWSDriver::AWSProvider::AWSResource

Instance Attribute Summary

Attributes inherited from Chef::Provisioning::AWSDriver::AWSProvider

#purging

Instance Method Summary collapse

Methods inherited from Chef::Provisioning::AWSDriver::AWSProvider

#action_handler, #converge_by, #region, #whyrun_supported?

Instance Method Details

#create_aws_objectObject



6
7
8
9
10
# File 'lib/chef/provider/aws_cloudwatch_alarm.rb', line 6

def create_aws_object
  converge_by "creating cloudwatch alarm #{new_resource.name} in #{region}" do
    new_resource.driver.cloudwatch_client.put_metric_alarm(desired_options)
  end
end

#desired_optionsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chef/provider/aws_cloudwatch_alarm.rb', line 26

def desired_options
  @desired_options ||= begin
    # Because an update is a PUT, we must ensure that any properties not specified
    # on the resource that are already present on the object stay the same
    aws_object = new_resource.aws_object
    opts = { alarm_name: new_resource.name }
    %i{namespace metric_name comparison_operator
       evaluation_periods period statistic threshold
       actions_enabled alarm_description unit}.each do |opt|
      if !new_resource.public_send(opt).nil?
        opts[opt] = new_resource.public_send(opt)
      elsif aws_object && !aws_object.public_send(opt).nil?
        opts[opt] = aws_object.public_send(opt)
      end
    end
    if !new_resource.dimensions.nil?
      opts[:dimensions] = new_resource.dimensions
    elsif aws_object && !aws_object.dimensions.nil?
      opts[:dimensions] = aws_object.dimensions.map!(&:to_h)
    end
    # Normally we would just use `lookup_options` here but because these parameters
    # don't necessarily sound like sns topics we manually do it
    %i{insufficient_data_actions ok_actions alarm_actions}.each do |opt|
      if !new_resource.public_send(opt).nil?
        opts[opt] = new_resource.public_send(opt)
        opts[opt].map! do |action|
          if action.is_a?(String) && action !~ /^arn:/
            aws_object = Chef::Resource::AwsSnsTopic.get_aws_object(action, resource: new_resource)
            action = aws_object.attributes["TopicArn"] if aws_object
          end
          action
        end
      elsif aws_object && !aws_object.public_send(opt).nil?
        opts[opt] = aws_object.public_send(opt)
      end
    end
    opts
  end
end

#destroy_aws_object(alarm) ⇒ Object



20
21
22
23
24
# File 'lib/chef/provider/aws_cloudwatch_alarm.rb', line 20

def destroy_aws_object(alarm)
  converge_by "destroying cloudwatch alarm #{new_resource.name} in #{region}" do
    alarm.delete
  end
end

#update_aws_object(alarm) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/chef/provider/aws_cloudwatch_alarm.rb', line 12

def update_aws_object(alarm)
  if update_required?(alarm)
    converge_by "updating cloudwatch alarm #{new_resource.name} in #{region}" do
      new_resource.driver.cloudwatch_client.put_metric_alarm(desired_options)
    end
  end
end

#update_required?(alarm) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/chef/provider/aws_cloudwatch_alarm.rb', line 66

def update_required?(alarm)
  %i{namespace metric_name comparison_operator
     evaluation_periods period statistic threshold
     actions_enabled alarm_description unit}.each do |opt|
    return true if alarm.public_send(opt) != desired_options[opt]
  end
  unless (Set.new(alarm.dimensions.map(&:to_h)) ^ Set.new(desired_options[:dimensions])).empty?
    return true
  end
  %i{insufficient_data_actions ok_actions alarm_actions}.each do |opt|
    unless (Set.new(alarm.public_send(opt)) ^ Set.new(desired_options[opt])).empty?
      return true
    end
  end
  false
end