Class: AWSTracker::Tag

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/aws_tracker/models/tag.rb

Constant Summary collapse

@@RIGHT_AWS_CLASS_NAMES =

Maps right_aws Tag resource_types to the corresponding ActiveRecord Class names

{
  :instance => :Instance,
  :volume   => :EBSVolume
}
@@AWS_PK_FIELDS =

Maps ActiveRecord Class names to their Amamzon “primary key” attribute names

{
  :Instance   => :instance_id,
  :EBSVolume  => :volume_id
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.refresh_from_account(acc) ⇒ Object



25
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
# File 'lib/aws_tracker/models/tag.rb', line 25

def self.(acc)
  acc.log.info "Updating tags for account '#{acc.name}'"

  right_aws_array = acc.ec2.describe_tags
  right_aws_array.each do |rs_tag|
    # First, dereference the polymorphic association, and
    # ABORT if the Tag's AWS resource is not found in the current state
    right_aws_type = rs_tag[:resource_type].to_sym
    resource_class_name = @@RIGHT_AWS_CLASS_NAMES[right_aws_type]
    aws_pk_attribute = @@AWS_PK_FIELDS[resource_class_name]
    collection_name = resource_class_name.to_s.pluralize.underscore
    tag_resource = acc.send(collection_name).find(:first,
    :conditions => { aws_pk_attribute => rs_tag[:resource_id]})
    if (tag_resource == nil)
      acc.log.warn "Tag #{rs_tag[:key]} references nonexistent "+
      "#{resource_class_name} #{rs_tag[:resource_id]}. Ignoring this Tag."
    else
      # See if this Tag already exists, otherwise create it
      tag_to_update = tag_resource.tags.find(:first,
      :conditions => {:key => rs_tag[:key]})
      tag_to_update ||= Tag.new(:account => acc)

      # Asssociations - link this Tag to its account
      tag_to_update. = acc
      acc.tags << tag_to_update
      # Asssociations - link this tag to its AWS resource
      tag_to_update.resource = tag_resource
      tag_resource.tags << tag_to_update
      tag_to_update.update_from_right_hash(rs_tag)
      tag_to_update.save
    end
  end
  acc.log.info "Account '#{acc.name}' reports #{right_aws_array.count} tags"
end

Instance Method Details

#update_from_right_hash(right_hash) ⇒ Object



60
61
62
63
# File 'lib/aws_tracker/models/tag.rb', line 60

def update_from_right_hash(right_hash)
  self.key            = right_hash[:key]
  self.value          = right_hash[:value]
end