Class: TerraspacePluginAws::Interfaces::Backend::Bucket::Tagging

Inherits:
TerraspacePluginAws::Interfaces::Backend::Base show all
Defined in:
lib/terraspace_plugin_aws/interfaces/backend/bucket/tagging.rb

Instance Method Summary collapse

Methods included from Logging

#logger

Methods included from Clients

#dynamodb, #s3, #secretsmanager, #ssm, #sts

Constructor Details

#initialize(info) ⇒ Tagging

Returns a new instance of Tagging.



3
4
5
6
# File 'lib/terraspace_plugin_aws/interfaces/backend/bucket/tagging.rb', line 3

def initialize(info)
  super
  @bucket = info['bucket']
end

Instance Method Details

#existing_taggingObject



36
37
38
39
40
# File 'lib/terraspace_plugin_aws/interfaces/backend/bucket/tagging.rb', line 36

def existing_tagging
  s3.get_bucket_tagging(bucket: @bucket).to_h
rescue Aws::S3::Errors::NoSuchTagSet
  {tag_set: []} # normalize return structure
end

#tagObject



8
9
10
11
# File 'lib/terraspace_plugin_aws/interfaces/backend/bucket/tagging.rb', line 8

def tag
  return if tagging.nil? || tagging[:tag_set].empty? # safeguard: dont overwrite current tags
  s3.put_bucket_tagging(bucket: @bucket, tagging: tagging)
end

#taggingObject

Merges existing tag_set structure so always appends tags, wont remove tags. This behavior is consistent with the dynamodb tagging.

Example return:

{
  tag_set: [
    { key: "Key1", value: "Value1" },
    { key: "Key2", value: "Value2" },
  ],
}


25
26
27
28
29
30
31
32
33
34
# File 'lib/terraspace_plugin_aws/interfaces/backend/bucket/tagging.rb', line 25

def tagging
  c = TerraspacePluginAws::Interfaces::Config.instance.config
  tags = !c.s3.tags.empty? ? c.s3.tags : c.tags
  tag_set = tags.map do |k,v|
    {key: k.to_s, value: v}
  end
  return if tag_set == existing_tagging[:tag_set] # return nil so we can avoid the put_bucket_tagging call
  tag_set += existing_tagging[:tag_set]
  { tag_set: tag_set }
end