Class: CfnGuardian::Tagger

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/cfnguardian/tagger.rb

Instance Method Summary collapse

Methods included from Logging

colors, included, logger, #logger, logger=

Constructor Details

#initializeTagger

Returns a new instance of Tagger.



9
10
11
# File 'lib/cfnguardian/tagger.rb', line 9

def initialize()
  @client = Aws::CloudWatch::Client.new(max_attempts: 5)
end

Instance Method Details

#get_alarm_tags(alarm_arn) ⇒ Object



58
59
60
61
62
63
# File 'lib/cfnguardian/tagger.rb', line 58

def get_alarm_tags(alarm_arn)
  resp = @client.list_tags_for_resource({
    resource_arn: alarm_arn
  })
  return resp.tags
end

#get_tags(alarm, global_tags) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cfnguardian/tagger.rb', line 46

def get_tags(alarm, global_tags)
  defaults = {
    'guardian:resource:id' => alarm.resource_id,
    'guardian:resource:group' => alarm.group,
    'guardian:alarm:name' => alarm.name,
    'guardian:alarm:metric' => alarm.metric_name,
    'guardian:alarm:severity' => alarm.alarm_action
  }
  tags = global_tags.merge(defaults)
  return alarm.tags.merge(tags)
end

#get_tags_to_delete(current_tags, new_tags) ⇒ Object



65
66
67
# File 'lib/cfnguardian/tagger.rb', line 65

def get_tags_to_delete(current_tags, new_tags)
  return current_tags.select {|tag| !new_tags.has_key?(tag.key) && !tag.key.start_with?('aws:') }.map { |tag| tag.key }
end

#tag_alarm(alarm, global_tags = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cfnguardian/tagger.rb', line 13

def tag_alarm(alarm, global_tags={})
  alarm_arn = CfnGuardian::CloudWatch.get_alarm_arn(alarm)
  
  new_tags = get_tags(alarm, global_tags)
  current_tags = get_alarm_tags(alarm_arn)
  tags_to_delete = get_tags_to_delete(current_tags, new_tags)

  if tags_to_delete.any?
    logger.debug "Removing tags #{tags_to_delete} from alarm #{alarm_arn}"
    @client.untag_resource({
      resource_arn: alarm_arn,
      tag_keys: tags_to_delete
    })
  end

  if tags_changed?(current_tags, new_tags)
    logger.debug "Updating tags on alarm #{alarm_arn}"
    new_tags.delete_if {|key, value| value.include?('?')}
    begin
      alarm_severity = new_tags["guardian:alarm:severity"]
      if alarm_severity.is_a?(Array)
        new_tags["guardian:alarm:severity"] = new_tags["guardian:alarm:severity"].join("/")
      end
        @client.tag_resource({
        resource_arn: alarm_arn,
        tags: new_tags.map {|key,value| {key: key, value: value}}
      })
    rescue Aws::CloudWatch::Errors::InvalidParameterValue => e
      logger.debug "Failed due to invalid character in tags for: #{alarm_arn}"
    end
  end
end

#tags_changed?(current_tags, new_tags) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/cfnguardian/tagger.rb', line 69

def tags_changed?(current_tags, new_tags)
  return tags_to_hash(current_tags) != new_tags
end

#tags_to_hash(tags) ⇒ Object



73
74
75
# File 'lib/cfnguardian/tagger.rb', line 73

def tags_to_hash(tags)
  return tags.map {|tag| {tag.key => tag.value} }.reduce(Hash.new, :merge)
end