Skip to content

Commit

Permalink
Merge pull request #529 from soruma/fix-524-support-s3-location
Browse files Browse the repository at this point in the history
Add support for S3 bucket location
  • Loading branch information
k1LoW authored Apr 14, 2021
2 parents 6042181 + 25c804f commit 0fdae29
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions doc/_resource_types/s3_bucket.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ describe s3_bucket('my-bucket') do
end
```

### have_location

```ruby
describe s3_bucket('my-bucket') do
it { should have_location('us-east-1') }
end
```

### have_logging_enabled

```ruby
Expand Down
9 changes: 9 additions & 0 deletions doc/resource_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -3114,6 +3114,15 @@ end
```


### have_location

```ruby
describe s3_bucket('my-bucket') do
it { should have_location('us-east-1') }
end
```


### have_logging_enabled

```ruby
Expand Down
4 changes: 4 additions & 0 deletions lib/awspec/generator/spec/s3_bucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ def bucket_spec_template
<%= line %>
<% end %>
<%- end -%>
<%- if location -%>
it { should have_location('<%= location %>') }
<%- end -%>
end
EOF
template
Expand All @@ -117,6 +120,7 @@ def content(bucket)
bucket_policy = policy.policy.read if policy
lifecycle_rule = find_bucket_lifecycle_configuration(bucket.name)
lifecycle_specs = generate_lifecycle_rule_specs(lifecycle_rule) if lifecycle_rule
location = find_bucket_location(bucket.name)
ERB.new(bucket_spec_template, nil, '-').result(binding).gsub(/^\n/, '')
end
end
Expand Down
7 changes: 7 additions & 0 deletions lib/awspec/helper/finder/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ def find_bucket_policy(id)
nil
end

def find_bucket_location(id)
bucket_location = s3_client.get_bucket_location(bucket: id)
bucket_location.location_constraint
rescue Aws::S3::Errors::ServiceError
nil
end

def find_bucket_logging(id)
s3_client.get_bucket_logging(bucket: id)
rescue Aws::S3::Errors::ServiceError
Expand Down
3 changes: 3 additions & 0 deletions lib/awspec/stub/s3_bucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
}
]
},
get_bucket_location: {
location_constraint: 'us-east-1'
},
get_bucket_encryption: {
server_side_encryption_configuration: {
rules: [
Expand Down
5 changes: 5 additions & 0 deletions lib/awspec/type/s3_bucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def has_acl_grant?(grantee:, permission:)
end
end

def has_location?(location)
check_existence
find_bucket_location(id) == location
end

def acl_owner
check_existence
@acl = find_bucket_acl(id)
Expand Down
1 change: 1 addition & 0 deletions spec/generator/spec/s3_bucket_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
status: 'Enabled'
)
end
it { should have_location('us-east-1') }
end
EOF
expect(s3_bucket.generate_all.to_s).to eq spec
Expand Down
1 change: 1 addition & 0 deletions spec/type/s3_bucket_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
it { should have_acl_grant(grantee: 'my-bucket-owner', permission: 'FULL_CONTROL') }
it { should have_acl_grant(grantee: 'http://acs.amazonaws.com/groups/s3/LogDelivery', permission: 'WRITE') }
it { should have_acl_grant(grantee: '68f4bb06b094152df53893bfba57760e', permission: 'READ') }
it { should have_location('us-east-1') }

it do
should have_cors_rule(
Expand Down

0 comments on commit 0fdae29

Please sign in to comment.