Commit d8b3075a authored by Daisuke Fujita's avatar Daisuke Fujita

Merge pull request #121 from dtan4/s3-region

Export buckets only in the same region
parents 7eeaf041 da50db17
......@@ -42,6 +42,10 @@ module Terraforming
private
def bucket_location_of(bucket)
@client.get_bucket_location(bucket: bucket.name).location_constraint
end
def bucket_policy_of(bucket)
@client.get_bucket_policy(bucket: bucket.name)
rescue Aws::S3::Errors::NoSuchBucketPolicy
......@@ -49,12 +53,17 @@ module Terraforming
end
def buckets
@client.list_buckets.buckets
@client.list_buckets.buckets.select { |bucket| same_region?(bucket) }
end
def module_name_of(bucket)
normalize_module_name(bucket.name)
end
def same_region?(bucket)
bucket_location = bucket_location_of(bucket)
(bucket_location == @client.config.region) || (bucket_location == "" && @client.config.region == "us-east-1")
end
end
end
end
......@@ -12,12 +12,16 @@ module Terraforming
{
creation_date: Time.parse("2015-01-01T00:00:00.000Z"),
name: "fuga"
},
{
creation_date: Time.parse("2015-01-01T00:00:00.000Z"),
name: "piyo"
}
]
end
let(:client) do
Aws::S3::Client.new(stub_responses: true)
Aws::S3::Client.new(region: "ap-northeast-1", stub_responses: true)
end
let(:owner) do
......@@ -31,17 +35,35 @@ module Terraforming
"{\"Version\":\"2012-10-17\",\"Id\":\"Policy123456789012\",\"Statement\":[{\"Sid\":\"Stmt123456789012\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::123456789012:user/hoge\"},\"Action\":\"s3:*\",\"Resource\":\"arn:aws:s3:::hoge/*\"}]}"
end
before do
client.stub_responses(:list_buckets, buckets: buckets, owner: owner)
client.stub_responses(:get_bucket_policy, [
{ policy: hoge_policy },
"NoSuchBucketPolicy",
])
let(:hoge_location) do
{ location_constraint: "ap-northeast-1" }
end
let(:fuga_location) do
{ location_constraint: "ap-northeast-1" }
end
let(:piyo_location) do
{ location_constraint: "" }
end
describe ".tf" do
it "should generate tf" do
expect(described_class.tf(client: client)).to eq <<-EOS
context "from ap-northeast-1" do
let(:client) do
Aws::S3::Client.new(region: "ap-northeast-1", stub_responses: true)
end
before do
client.stub_responses(:list_buckets, buckets: buckets, owner: owner)
client.stub_responses(:get_bucket_policy, [
{ policy: hoge_policy },
"NoSuchBucketPolicy",
])
client.stub_responses(:get_bucket_location, [hoge_location, fuga_location, piyo_location])
end
describe ".tf" do
it "should generate tf" do
expect(described_class.tf(client: client)).to eq <<-EOS
resource "aws_s3_bucket" "hoge" {
bucket = "hoge"
acl = "private"
......@@ -70,39 +92,86 @@ resource "aws_s3_bucket" "fuga" {
}
EOS
end
end
end
describe ".tfstate" do
it "should generate tfstate" do
expect(described_class.tfstate(client: client)).to eq({
"aws_s3_bucket.hoge" => {
"type" => "aws_s3_bucket",
"primary" => {
"id" => "hoge",
"attributes" => {
"acl" => "private",
"bucket" => "hoge",
"force_destroy" => "false",
describe ".tfstate" do
it "should generate tfstate" do
expect(described_class.tfstate(client: client)).to eq({
"aws_s3_bucket.hoge" => {
"type" => "aws_s3_bucket",
"primary" => {
"id" => "hoge",
"policy" => "{\"Version\":\"2012-10-17\",\"Id\":\"Policy123456789012\",\"Statement\":[{\"Sid\":\"Stmt123456789012\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::123456789012:user/hoge\"},\"Action\":\"s3:*\",\"Resource\":\"arn:aws:s3:::hoge/*\"}]}",
"attributes" => {
"acl" => "private",
"bucket" => "hoge",
"force_destroy" => "false",
"id" => "hoge",
"policy" => "{\"Version\":\"2012-10-17\",\"Id\":\"Policy123456789012\",\"Statement\":[{\"Sid\":\"Stmt123456789012\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::123456789012:user/hoge\"},\"Action\":\"s3:*\",\"Resource\":\"arn:aws:s3:::hoge/*\"}]}",
}
}
}
},
"aws_s3_bucket.fuga" => {
"type" => "aws_s3_bucket",
"primary" => {
"id" => "fuga",
"attributes" => {
"acl" => "private",
"bucket" => "fuga",
"force_destroy" => "false",
},
"aws_s3_bucket.fuga" => {
"type" => "aws_s3_bucket",
"primary" => {
"id" => "fuga",
"policy" => "",
"attributes" => {
"acl" => "private",
"bucket" => "fuga",
"force_destroy" => "false",
"id" => "fuga",
"policy" => "",
}
}
},
})
end
end
end
context "from us-east-1" do
let(:client) do
Aws::S3::Client.new(region: "us-east-1", stub_responses: true)
end
before do
client.stub_responses(:list_buckets, buckets: buckets, owner: owner)
client.stub_responses(:get_bucket_policy, [
"NoSuchBucketPolicy",
])
client.stub_responses(:get_bucket_location, [hoge_location, fuga_location, piyo_location])
end
describe ".tf" do
it "should generate tf" do
expect(described_class.tf(client: client)).to eq <<-EOS
resource "aws_s3_bucket" "piyo" {
bucket = "piyo"
acl = "private"
}
EOS
end
end
describe ".tfstate" do
it "should generate tfstate" do
expect(described_class.tfstate(client: client)).to eq({
"aws_s3_bucket.piyo" => {
"type" => "aws_s3_bucket",
"primary" => {
"id" => "piyo",
"attributes" => {
"acl" => "private",
"bucket" => "piyo",
"force_destroy" => "false",
"id" => "piyo",
"policy" => "",
}
}
}
},
})
},
})
end
end
end
end
......
......@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_dependency "aws-sdk", "~> 2.1.0"
spec.add_dependency "aws-sdk", "~> 2.1.15"
spec.add_dependency "oj"
spec.add_dependency "ox"
spec.add_dependency "thor"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment