Class: Bosh::AwsCloud::TagManager

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud/aws/tag_manager.rb

Constant Summary collapse

MAX_TAG_KEY_LENGTH =
127
MAX_TAG_VALUE_LENGTH =
255

Class Method Summary collapse

Class Method Details

.loggerObject



26
27
28
# File 'lib/cloud/aws/tag_manager.rb', line 26

def self.logger
  Bosh::Clouds::Config.logger
end

.tag(taggable, key, value) ⇒ Object

Add a tag to something, make sure that the tag conforms to the AWS limitation of 127 character key and 255 character value



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cloud/aws/tag_manager.rb', line 9

def self.tag(taggable, key, value)
  return if key.nil? || value.nil?
  trimmed_key = key.to_s.slice(0, MAX_TAG_KEY_LENGTH)
  trimmed_value = value.to_s.slice(0, MAX_TAG_VALUE_LENGTH)
  taggable.add_tag(trimmed_key, :value => trimmed_value)
rescue AWS::EC2::Errors::InvalidParameterValue => e
  logger.error("could not tag #{taggable.id}: #{e.message}")
rescue AWS::EC2::Errors::InvalidAMIID::NotFound,
    AWS::EC2::Errors::InvalidInstanceID::NotFound=> e
  # Due to the AWS eventual consistency, the taggable might not
  # be there, even though we previous have waited until it is,
  # so we wait again...
  logger.warn("tagged object doesn't exist: #{taggable.id}")
  sleep(1)
  retry
end