Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Improve s3 #477

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/.bundle/
/.yardoc
.idea
/Gemfile.lock
/_yardoc/
/coverage/
Expand Down
67 changes: 54 additions & 13 deletions lib/terraforming/resource/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ def initialize(client)
end

def tf
apply_template(@client, "tf/s3")
apply_template(@client, 'tf/s3').gsub(/^[\s]*$\n/, '')
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 : "",
"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 : '',
}
}
}
}

resources
Expand All @@ -47,19 +47,60 @@ def bucket_location_of(bucket)
end

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

def buckets
@client.list_buckets.map(&:buckets).flatten.select { |bucket| same_region?(bucket) }
return @buckets unless @buckets.nil?
@buckets = []
@client.list_buckets.map(&:buckets).flatten.each do |bucket|
@buckets << Aws::S3::Bucket.new(bucket.name, client: @client) if same_region?(bucket)
end
@buckets
end

def region(bucket)
bucket_location_of(bucket)
end

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

def tagging?(bucket)
return false if bucket.tagging.tag_set.nil?
true
rescue Aws::S3::Errors::NoSuchTagSet
false
end

def cors?(bucket)
return false if bucket.cors.cors_rules.nil?
true
rescue Aws::S3::Errors::NoSuchCORSConfiguration
false
end

def lifecycle?(bucket)
return false if bucket.lifecycle_configuration.rules.nil?
true
rescue Aws::S3::Errors::NoSuchLifecycleConfiguration
false
end

def website_configuation?(bucket)
return false if bucket.website.index_document.nil?
true
rescue Aws::S3::Errors::NoSuchWebsiteConfiguration
false
end

def prettify_website_routing_rules(bucket)
prettify_policy(bucket.website.routing_rules.map { |t| t.to_h.to_json }.to_json.gsub('"{', '{').gsub('\"', '"').gsub('}"', '}'))
end

def same_region?(bucket)
bucket_location = bucket_location_of(bucket)
(bucket_location == @client.config.region) || (bucket_location == "" && @client.config.region == "us-east-1")
Expand Down
114 changes: 109 additions & 5 deletions lib/terraforming/template/tf/s3.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,115 @@
resource "aws_s3_bucket" "<%= module_name_of(bucket) %>" {
bucket = "<%= bucket.name %>"
acl = "private"
<%- unless (policy = bucket_policy_of(bucket)).nil? -%>
region = "<%=region(bucket)%>"
<%- unless (policy = bucket_policy_of(bucket)).nil? -%>
policy = <<POLICY
<%= prettify_policy(policy.policy.read) %>
<%= prettify_policy(policy) %>
POLICY
<%- end -%>
}
<%- end -%>

<%- if tagging?(bucket) -%>
tags {
<%- bucket.tagging.tag_set.each do |tag| -%>
<%= tag[:key] %> = "<%= tag[:value] %>"
<%- end -%>
}
<%- end -%>

<%- if cors?(bucket) -%>
<%- bucket.cors.cors_rules.each do |rule| -%>
cors_rule {
allowed_methods = <%= rule.allowed_methods %>
allowed_origins = <%= rule.allowed_origins %>
<%- unless rule.allowed_headers.nil? -%>
allowed_headers = <%= rule.allowed_headers %>
<%- end -%>
<%- unless rule.expose_headers.nil? -%>
expose_headers = <%= rule.expose_headers %>
<%- end -%>
<%- unless rule.max_age_seconds.nil? -%>
max_age_seconds = <%= rule.max_age_seconds %>
<%- end -%>
}
<%- end -%>
<%- end -%>

<%- unless bucket.versioning.status.nil? -%>
versioning {
enabled = <%= bucket.versioning.status == 'Enabled' %>
}
<%- end -%>

<%- unless bucket.logging.logging_enabled.nil? -%>
logging {
target_bucket = "<%= bucket.logging.logging_enabled.target_bucket %>"
target_prefix = "<%= bucket.logging.logging_enabled.target_prefix %>"
}
<%- end -%>

<%- if lifecycle?(bucket) -%>
<%- bucket.lifecycle_configuration.rules.each do |rule| -%>
lifecycle_rule {
id = "<%= rule.id %>"
prefix = "<%= rule.prefix %>"
enabled = <%= rule.status == 'Enabled' %>
<%- rule.transitions.each do |transition| -%>
transition {
storage_class = "<%= transition.storage_class %>"
<%- unless transition.days.nil? %>
days = <%= transition.days %>
<%- end -%>
<%- unless transition.date.nil? -%>
date = "<%= transition.date %>"
<%- end -%>
}
<%- end -%>

<% end -%>
<%- unless rule.expiration.nil? -%>
expiration {
<%- unless rule.expiration.days.nil? -%>
days = <%= rule.expiration.days %>
<%- end -%>
<%- unless rule.expiration.date.nil? -%>
date = "<%= rule.expiration.date %>"
<%- end -%>
<%- unless rule.expiration.expired_object_delete_marker.nil? -%>
expired_object_delete_marker = "<%= rule.expiration.expired_object_delete_marker %>"
<%- end -%>
}
<%- end -%>

<%- rule.noncurrent_version_transitions.each do |transition| -%>
noncurrent_version_transition {
days = <%= transition.noncurrent_days %>
storage_class = "<%= transition.storage_class %>"
}
<%- end-%>

<%- unless rule.noncurrent_version_expiration.nil? -%>
noncurrent_version_expiration {
days = <%= rule.noncurrent_version_expiration.noncurrent_days %>
}
<%- end -%>

<%- unless rule.abort_incomplete_multipart_upload.nil? -%>
abort_incomplete_multipart_upload_days = <%= rule.abort_incomplete_multipart_upload.days_after_initiation %>
<%- end -%>
}
<%- end %>
<%- end -%>

<%- if website_configuation?(bucket) %>
website {
index_document = "<%= bucket.website.index_document.suffix %>"
<%- unless bucket.website.error_document.nil? %>
error_document = "<%= bucket.website.error_document.key %>"
<%- end -%>
<%- unless bucket.website.routing_rules.nil? %>
routing_rules = <%= prettify_website_routing_rules(bucket) %>
<%- end -%>
}
<%- end -%>

}
<%- end -%>
2 changes: 1 addition & 1 deletion lib/terraforming/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Terraforming
VERSION = "0.18.0"
VERSION = "0.18.1"
end
Loading