Sha256: cd80d186d941bb1452d6c9099120e0c29ca8f131624b0ea2f55d5f595ac5b3e0

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

module Terraforming
  module Resource
    class S3
      include Terraforming::Util

      def self.tf(client: Aws::S3::Client.new)
        self.new(client).tf
      end

      def self.tfstate(client: Aws::S3::Client.new)
        self.new(client).tfstate
      end

      def initialize(client)
        @client = client
      end

      def tf
        apply_template(@client, "tf/s3")
      end

      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

      def bucket_policy_of(bucket)
        @client.get_bucket_policy(bucket: bucket.name)
      rescue Aws::S3::Errors::NoSuchBucketPolicy
        nil
      end

      def buckets
        @client.list_buckets.buckets
      end

      def module_name_of(bucket)
        normalize_module_name(bucket.name)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
terraforming-0.1.6 lib/terraforming/resource/s3.rb
terraforming-0.1.5 lib/terraforming/resource/s3.rb