Class: Chef::Provisioning::AWSDriver::TaggingStrategy::S3

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s3_client, bucket_name, desired_tags) ⇒ S3

Returns a new instance of S3.



6
7
8
9
10
# File 'lib/chef/provisioning/aws_driver/tagging_strategy/s3.rb', line 6

def initialize(s3_client, bucket_name, desired_tags)
  @s3_client = s3_client
  @bucket_name = bucket_name
  @desired_tags = desired_tags
end

Instance Attribute Details

#bucket_nameObject (readonly)

Returns the value of attribute bucket_name.



4
5
6
# File 'lib/chef/provisioning/aws_driver/tagging_strategy/s3.rb', line 4

def bucket_name
  @bucket_name
end

#desired_tagsObject (readonly)

Returns the value of attribute desired_tags.



4
5
6
# File 'lib/chef/provisioning/aws_driver/tagging_strategy/s3.rb', line 4

def desired_tags
  @desired_tags
end

#s3_clientObject (readonly)

Returns the value of attribute s3_client.



4
5
6
# File 'lib/chef/provisioning/aws_driver/tagging_strategy/s3.rb', line 4

def s3_client
  @s3_client
end

Instance Method Details

#current_tagsObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/chef/provisioning/aws_driver/tagging_strategy/s3.rb', line 12

def current_tags
  # http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Client.html#get_bucket_tagging-instance_method
  resp = s3_client.get_bucket_tagging(
    bucket: bucket_name
  )
  Hash[resp.tag_set.map { |t| [t.key, t.value] }]
rescue ::Aws::S3::Errors::NoSuchTagSet => e
  # Instead of returning nil or empty, AWS raises an error :)
  {}
end

#delete_tags(_tag_keys) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chef/provisioning/aws_driver/tagging_strategy/s3.rb', line 36

def delete_tags(_tag_keys)
  if desired_tags.empty?
    s3_client.delete_bucket_tagging(
      bucket: bucket_name
    )
  else
    set_tags(desired_tags)
  end
  # S3 doesn't have a client action for deleting individual tags, just ALL tags.  But the
  # put_bucket_tagging method will set the tags to what is provided so we don't need to
  # worry about this
end

#set_tags(_tags) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chef/provisioning/aws_driver/tagging_strategy/s3.rb', line 23

def set_tags(_tags)
  return if @is_set_tag
  # It will also run from delete_tags to prevent two times execution of same api class variable is defined
  @is_set_tag = true
  # http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Client.html#put_bucket_tagging-instance_method
  s3_client.put_bucket_tagging(
    bucket: bucket_name,
    tagging: {
      tag_set: desired_tags.map { |k, v| { key: k.to_s, value: v.to_s } }
    }
  )
end