Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data source for listing S3 objects #6968

Merged
merged 5 commits into from
Aug 1, 2019

Conversation

YakDriver
Copy link
Member

Fixes #2634

Changes proposed in this pull request:

  • Add new data source called aws_s3_bucket_objects to list objects in an S3 bucket
  • Add documentation / tests for new data source

Example usage:

data "aws_s3_bucket_objects" "yesh" {
  bucket    = "my_bucket"
  prefix    = "prefix/of/the/objects/"
}

The result is a list of object keys: data.aws_s3_bucket_objects.yesh.keys

Output from acceptance testing:

$ make testacc TESTARGS='-run=TestAccDataSourceAWSS3BucketObjects_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./... -v -parallel 20 -run=TestAccDataSourceAWSS3BucketObjects_ -timeout 120m
?   	github.com/terraform-providers/terraform-provider-aws	[no test files]
=== RUN   TestAccDataSourceAWSS3BucketObjects_basic
=== PAUSE TestAccDataSourceAWSS3BucketObjects_basic
=== RUN   TestAccDataSourceAWSS3BucketObjects_all
=== PAUSE TestAccDataSourceAWSS3BucketObjects_all
=== RUN   TestAccDataSourceAWSS3BucketObjects_prefixes
=== PAUSE TestAccDataSourceAWSS3BucketObjects_prefixes
=== RUN   TestAccDataSourceAWSS3BucketObjects_encoded
=== PAUSE TestAccDataSourceAWSS3BucketObjects_encoded
=== RUN   TestAccDataSourceAWSS3BucketObjects_maxKeys
=== PAUSE TestAccDataSourceAWSS3BucketObjects_maxKeys
=== RUN   TestAccDataSourceAWSS3BucketObjects_startAfter
=== PAUSE TestAccDataSourceAWSS3BucketObjects_startAfter
=== RUN   TestAccDataSourceAWSS3BucketObjects_fetchOwner
=== PAUSE TestAccDataSourceAWSS3BucketObjects_fetchOwner
=== CONT  TestAccDataSourceAWSS3BucketObjects_basic
=== CONT  TestAccDataSourceAWSS3BucketObjects_maxKeys
=== CONT  TestAccDataSourceAWSS3BucketObjects_all
=== CONT  TestAccDataSourceAWSS3BucketObjects_prefixes
=== CONT  TestAccDataSourceAWSS3BucketObjects_encoded
=== CONT  TestAccDataSourceAWSS3BucketObjects_startAfter
=== CONT  TestAccDataSourceAWSS3BucketObjects_fetchOwner
--- PASS: TestAccDataSourceAWSS3BucketObjects_startAfter (10.91s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_all (11.15s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_basic (11.22s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_maxKeys (11.23s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_encoded (11.27s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_fetchOwner (11.31s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_prefixes (11.37s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	11.442s

@ghost ghost added size/XL Managed by automation to categorize the size of a PR. documentation Introduces or discusses updates to documentation. provider Pertains to the provider itself, rather than any interaction with AWS. service/s3 Issues and PRs that pertain to the s3 service. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. labels Dec 22, 2018
@YakDriver
Copy link
Member Author

@bflad Is there any adjust you'd like to see on this?

@bflad bflad added the new-data-source Introduces a new data source. label Jan 14, 2019
@AndresCidoncha
Copy link
Contributor

Any chance this will be merged soon? That will allow us to improve our code a lot.

@aeschright aeschright requested a review from a team June 25, 2019 21:47
@bflad bflad self-assigned this Jun 26, 2019
@YakDriver
Copy link
Member Author

@bflad I've done the following on this PR:

  • Rebase
  • Add note about performance if you retrieve too many keys
  • Add a default value for max_keys of 1000
  • Re-run acceptance tests
$ make testacc TESTARGS='-run=TestAccDataSourceAWSS3BucketObjects_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./... -v -parallel 20 -run=TestAccDataSourceAWSS3BucketObjects_ -timeout 120m
?   	github.com/terraform-providers/terraform-provider-aws	[no test files]
=== RUN   TestAccDataSourceAWSS3BucketObjects_basic
=== PAUSE TestAccDataSourceAWSS3BucketObjects_basic
=== RUN   TestAccDataSourceAWSS3BucketObjects_all
=== PAUSE TestAccDataSourceAWSS3BucketObjects_all
=== RUN   TestAccDataSourceAWSS3BucketObjects_prefixes
=== PAUSE TestAccDataSourceAWSS3BucketObjects_prefixes
=== RUN   TestAccDataSourceAWSS3BucketObjects_encoded
=== PAUSE TestAccDataSourceAWSS3BucketObjects_encoded
=== RUN   TestAccDataSourceAWSS3BucketObjects_maxKeys
=== PAUSE TestAccDataSourceAWSS3BucketObjects_maxKeys
=== RUN   TestAccDataSourceAWSS3BucketObjects_startAfter
=== PAUSE TestAccDataSourceAWSS3BucketObjects_startAfter
=== RUN   TestAccDataSourceAWSS3BucketObjects_fetchOwner
=== PAUSE TestAccDataSourceAWSS3BucketObjects_fetchOwner
=== CONT  TestAccDataSourceAWSS3BucketObjects_basic
=== CONT  TestAccDataSourceAWSS3BucketObjects_fetchOwner
=== CONT  TestAccDataSourceAWSS3BucketObjects_startAfter
=== CONT  TestAccDataSourceAWSS3BucketObjects_encoded
=== CONT  TestAccDataSourceAWSS3BucketObjects_all
=== CONT  TestAccDataSourceAWSS3BucketObjects_maxKeys
=== CONT  TestAccDataSourceAWSS3BucketObjects_prefixes
--- PASS: TestAccDataSourceAWSS3BucketObjects_startAfter (33.67s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_encoded (33.79s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_fetchOwner (33.92s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_all (34.07s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_prefixes (34.20s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_basic (34.38s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_maxKeys (34.39s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	34.677s

Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey again @YakDriver 👋 Excited to see this making progress! Left some feedback items below -- please reach out with any questions or if you do not have time to implement them.

aws/data_source_aws_s3_bucket_objects.go Outdated Show resolved Hide resolved
aws/data_source_aws_s3_bucket_objects.go Outdated Show resolved Hide resolved
aws/data_source_aws_s3_bucket_objects.go Outdated Show resolved Hide resolved
aws/data_source_aws_s3_bucket_objects.go Outdated Show resolved Hide resolved
aws/data_source_aws_s3_bucket_objects.go Outdated Show resolved Hide resolved
aws/data_source_aws_s3_bucket_objects.go Outdated Show resolved Hide resolved
aws/data_source_aws_s3_bucket_objects_test.go Show resolved Hide resolved
website/docs/d/s3_bucket_objects.html.markdown Outdated Show resolved Hide resolved
website/docs/d/s3_bucket_objects.html.markdown Outdated Show resolved Hide resolved
@bflad bflad added the waiting-response Maintainers are waiting on response from community or contributor. label Aug 1, 2019
@YakDriver
Copy link
Member Author

YakDriver commented Aug 1, 2019

@bflad Eventual consistency is killing the acceptance tests. The S3 service doesn't return the correct responses until a while after s3 resource creation. The s3 object should have a wait state but that's a separate issue. 😞

@ghost ghost removed the waiting-response Maintainers are waiting on response from community or contributor. label Aug 1, 2019
@bflad
Copy link
Contributor

bflad commented Aug 1, 2019

@YakDriver This could be related to https://github.com/terraform-providers/terraform-provider-aws/pull/6968/files#r309787429 as well -- a workaround pattern you can use is a "two step" apply in the testing, which ensures all the resources are applied first and introduces a slight delay in the testing before trying to call the data source, e.g.

Steps: []resource.TestStep{
  {
    Config: testAccAWSDataSourceS3ObjectsConfigResources(rInt), // NOTE: contains no data source
    // Does not need Check
  },
  {
    Config: testAccAWSDataSourceS3ObjectsConfigBasic(rInt), // NOTE: contains data source
    Check: /* ... */,
  },
}

@YakDriver
Copy link
Member Author

@bflad Genius!

$ make testacc TESTARGS='-run=TestAccDataSourceAWSS3BucketObjects_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./... -v -parallel 20 -run=TestAccDataSourceAWSS3BucketObjects_ -timeout 120m
?   	github.com/terraform-providers/terraform-provider-aws	[no test files]
=== RUN   TestAccDataSourceAWSS3BucketObjects_basic
=== PAUSE TestAccDataSourceAWSS3BucketObjects_basic
=== RUN   TestAccDataSourceAWSS3BucketObjects_all
=== PAUSE TestAccDataSourceAWSS3BucketObjects_all
=== RUN   TestAccDataSourceAWSS3BucketObjects_prefixes
=== PAUSE TestAccDataSourceAWSS3BucketObjects_prefixes
=== RUN   TestAccDataSourceAWSS3BucketObjects_encoded
=== PAUSE TestAccDataSourceAWSS3BucketObjects_encoded
=== RUN   TestAccDataSourceAWSS3BucketObjects_maxKeys
=== PAUSE TestAccDataSourceAWSS3BucketObjects_maxKeys
=== RUN   TestAccDataSourceAWSS3BucketObjects_startAfter
=== PAUSE TestAccDataSourceAWSS3BucketObjects_startAfter
=== RUN   TestAccDataSourceAWSS3BucketObjects_fetchOwner
=== PAUSE TestAccDataSourceAWSS3BucketObjects_fetchOwner
=== CONT  TestAccDataSourceAWSS3BucketObjects_basic
=== CONT  TestAccDataSourceAWSS3BucketObjects_maxKeys
=== CONT  TestAccDataSourceAWSS3BucketObjects_all
=== CONT  TestAccDataSourceAWSS3BucketObjects_encoded
=== CONT  TestAccDataSourceAWSS3BucketObjects_prefixes
=== CONT  TestAccDataSourceAWSS3BucketObjects_fetchOwner
=== CONT  TestAccDataSourceAWSS3BucketObjects_startAfter
--- PASS: TestAccDataSourceAWSS3BucketObjects_basic (51.04s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_prefixes (51.05s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_startAfter (51.30s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_maxKeys (51.31s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_encoded (51.39s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_fetchOwner (51.52s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_all (51.70s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	51.768s

@bflad bflad self-requested a review August 1, 2019 20:29
@bflad bflad added this to the v2.23.0 milestone Aug 1, 2019
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, @YakDriver, thanks! 🚀

--- PASS: TestAccDataSourceAWSS3BucketObjects_basic (18.48s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_maxKeys (18.69s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_fetchOwner (18.80s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_encoded (18.81s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_prefixes (18.85s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_startAfter (18.97s)
--- PASS: TestAccDataSourceAWSS3BucketObjects_all (19.02s)

@bflad bflad merged commit 1194fe8 into hashicorp:master Aug 1, 2019
bflad added a commit that referenced this pull request Aug 1, 2019
@YakDriver YakDriver deleted the s3-buck-objs branch August 1, 2019 21:44
@ghost
Copy link

ghost commented Aug 7, 2019

This has been released in version 2.23.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks!

@ghost
Copy link

ghost commented Nov 2, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Nov 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Introduces or discusses updates to documentation. new-data-source Introduces a new data source. provider Pertains to the provider itself, rather than any interaction with AWS. service/s3 Issues and PRs that pertain to the s3 service. size/XL Managed by automation to categorize the size of a PR. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: aws_s3_bucket_objects data source
3 participants