Class: Chef::Provisioning::AWSDriver::TaggingStrategy::EC2

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/provisioning/aws_driver/tagging_strategy/ec2.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ec2_client, aws_object_id, desired_tags) ⇒ EC2

Returns a new instance of EC2.



26
27
28
29
30
# File 'lib/chef/provisioning/aws_driver/tagging_strategy/ec2.rb', line 26

def initialize(ec2_client, aws_object_id, desired_tags)
  @ec2_client = ec2_client
  @aws_object_id = aws_object_id
  @desired_tags = desired_tags
end

Instance Attribute Details

#aws_object_idObject (readonly)

Returns the value of attribute aws_object_id.



24
25
26
# File 'lib/chef/provisioning/aws_driver/tagging_strategy/ec2.rb', line 24

def aws_object_id
  @aws_object_id
end

#desired_tagsObject (readonly)

Returns the value of attribute desired_tags.



24
25
26
# File 'lib/chef/provisioning/aws_driver/tagging_strategy/ec2.rb', line 24

def desired_tags
  @desired_tags
end

#ec2_clientObject (readonly)

Returns the value of attribute ec2_client.



24
25
26
# File 'lib/chef/provisioning/aws_driver/tagging_strategy/ec2.rb', line 24

def ec2_client
  @ec2_client
end

Instance Method Details

#current_tagsObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/chef/provisioning/aws_driver/tagging_strategy/ec2.rb', line 32

def current_tags
  # http://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Client.html#describe_tags-instance_method
  resp = ec2_client.describe_tags({
    filters: [
      {
        name: "resource-id",
        values: [aws_object_id]
      }
    ]
  })
  Hash[resp.tags.map {|t| [t.key, t.value]}]
end

#delete_tags(tag_keys) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/chef/provisioning/aws_driver/tagging_strategy/ec2.rb', line 55

def delete_tags(tag_keys)
  # http://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Client.html#delete_tags-instance_method
  ec2_client.delete_tags({
    resources: [aws_object_id],
    tags: tag_keys.map {|k| {key: k} }
  })
end

#set_tags(tags) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/chef/provisioning/aws_driver/tagging_strategy/ec2.rb', line 45

def set_tags(tags)
  # http://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Client.html#create_tags-instance_method
  # "The value parameter is required, but if you don't want the tag to have a value, specify
  #   the parameter with no value, and we set the value to an empty string."
  ec2_client.create_tags({
    resources: [aws_object_id],
    tags: tags.map {|k,v| {key: k, value: v} }
  })
end