class PracticeTerraforming::Resource::S3

Public Class Methods

new(client) click to toggle source
# File lib/practice_terraforming/resource/s3.rb, line 16
def initialize(client)
  @client = client
end
tf(client: Aws::S3::Client.new) click to toggle source
# File lib/practice_terraforming/resource/s3.rb, line 8
def self.tf(client: Aws::S3::Client.new)
  self.new(client).tf
end
tfstate(client: Aws::S3::Client.new) click to toggle source
# File lib/practice_terraforming/resource/s3.rb, line 12
def self.tfstate(client: Aws::S3::Client.new)
  self.new(client).tfstate
end

Public Instance Methods

tf() click to toggle source
# File lib/practice_terraforming/resource/s3.rb, line 20
def tf
  apply_template(@client, "tf/s3")
end
tfstate() click to toggle source
# File lib/practice_terraforming/resource/s3.rb, line 24
def tfstate
  buckets.inject({}) do |resources, bucket|
    bucket_policy = bucket_policy_of(bucket)
    resources["aws_s3_bucket.#{module_name_of(bucket)}"] = {
      "type" => "aws_s3_bucket",
      "primary" => {
        "id" => bucket.name,
        "attributes" => {
          "acl" => "private",
          "bucket" => bucket.name,
          "force_destroy" => "false",
          "id" => bucket.name,
          "policy" => bucket_policy ? bucket_policy.policy.read : ""
        }
      }
    }

    resources
  end
end

Private Instance Methods

bucket_location_of(bucket) click to toggle source
# File lib/practice_terraforming/resource/s3.rb, line 47
def bucket_location_of(bucket)
  @client.get_bucket_location(bucket: bucket.name).location_constraint
end
bucket_policy_of(bucket) click to toggle source
# File lib/practice_terraforming/resource/s3.rb, line 51
def bucket_policy_of(bucket)
  @client.get_bucket_policy(bucket: bucket.name)
rescue Aws::S3::Errors::NoSuchBucketPolicy
  nil
end
buckets() click to toggle source
# File lib/practice_terraforming/resource/s3.rb, line 57
def buckets
  @client.list_buckets.map(&:buckets).flatten.select { |bucket| same_region?(bucket) }
end
module_name_of(bucket) click to toggle source
# File lib/practice_terraforming/resource/s3.rb, line 61
def module_name_of(bucket)
  normalize_module_name(bucket.name)
end
same_region?(bucket) click to toggle source
# File lib/practice_terraforming/resource/s3.rb, line 65
def same_region?(bucket)
  bucket_location = bucket_location_of(bucket)
  (bucket_location == @client.config.region) || (bucket_location == "" && @client.config.region == "us-east-1")
end