-
Notifications
You must be signed in to change notification settings - Fork 28
Support for existing storage containers in aws and gcp. #651
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0a0fe36
Update config schema.
tasdomas 9431291
AWS support for existing S3 buckets.
tasdomas 120934c
Existing bucket support for gcp.
tasdomas fa13722
Fix subdirectory in rclone remote.
tasdomas cbfd058
Add mocks and tests to existing bucket resource in aws.
tasdomas 34e2586
Update task/task.go
tasdomas 07e4116
Review fix: remove unused interface.
tasdomas 6e584b0
Update docs with new pre-allocated container fields.
tasdomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,5 @@ sweep: | |
|
||
testacc: | ||
TF_ACC=1 go test ./... -v ${TESTARGS} -timeout 120m | ||
|
||
generate: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package resources | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path" | ||
|
||
"terraform-provider-iterative/task/common" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
) | ||
|
||
// NewExistingS3Bucket returns a new data source refering to a pre-allocated | ||
// S3 bucket. | ||
func NewExistingS3Bucket(client S3Client, credentials aws.Credentials, id string, region string, path string) *ExistingS3Bucket { | ||
return &ExistingS3Bucket{ | ||
client: client, | ||
credentials: credentials, | ||
region: region, | ||
|
||
id: id, | ||
path: path, | ||
} | ||
} | ||
|
||
// ExistingS3Bucket identifies an existing S3 bucket. | ||
type ExistingS3Bucket struct { | ||
client S3Client | ||
credentials aws.Credentials | ||
|
||
id string | ||
region string | ||
path string | ||
} | ||
|
||
// Read verifies the specified S3 bucket is accessible. | ||
func (b *ExistingS3Bucket) Read(ctx context.Context) error { | ||
input := s3.HeadBucketInput{ | ||
Bucket: aws.String(b.id), | ||
} | ||
if _, err := b.client.HeadBucket(ctx, &input); err != nil { | ||
if errorCodeIs(err, errNotFound) { | ||
return common.NotFoundError | ||
} | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// ConnectionString implements common.StorageCredentials. | ||
// The method returns the rclone connection string for the specific bucket. | ||
func (b *ExistingS3Bucket) ConnectionString(ctx context.Context) (string, error) { | ||
containerPath := path.Join(b.id, b.path) | ||
connectionString := fmt.Sprintf( | ||
":s3,provider=AWS,region=%s,access_key_id=%s,secret_access_key=%s,session_token=%s:%s", | ||
b.region, | ||
b.credentials.AccessKeyID, | ||
b.credentials.SecretAccessKey, | ||
b.credentials.SessionToken, | ||
containerPath) | ||
return connectionString, nil | ||
} | ||
|
||
// build-time check to ensure Bucket implements BucketCredentials. | ||
var _ common.StorageCredentials = (*ExistingS3Bucket)(nil) | ||
|
||
// S3Client defines the functions of the AWS S3 API used. | ||
type S3Client interface { | ||
HeadBucket(context.Context, *s3.HeadBucketInput, ...func(*s3.Options)) (*s3.HeadBucketOutput, error) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package resources_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"terraform-provider-iterative/task/aws/resources" | ||
"terraform-provider-iterative/task/aws/resources/mocks" | ||
"terraform-provider-iterative/task/common" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
"github.com/aws/smithy-go" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestExistingBucketConnectionString(t *testing.T) { | ||
ctx := context.Background() | ||
creds := aws.Credentials{ | ||
AccessKeyID: "access-key-id", | ||
SecretAccessKey: "secret-access-key", | ||
SessionToken: "session-token", | ||
} | ||
b := resources.NewExistingS3Bucket(nil, creds, "pre-created-bucket", "us-east-1", "subdirectory") | ||
connStr, err := b.ConnectionString(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, connStr, ":s3,provider=AWS,region=us-east-1,access_key_id=access-key-id,secret_access_key=secret-access-key,session_token=session-token:pre-created-bucket/subdirectory") | ||
} | ||
|
||
func TestExistingBucketRead(t *testing.T) { | ||
ctx := context.Background() | ||
ctl := gomock.NewController(t) | ||
defer ctl.Finish() | ||
|
||
s3Cl := mocks.NewMockS3Client(ctl) | ||
s3Cl.EXPECT().HeadBucket(gomock.Any(), &s3.HeadBucketInput{Bucket: aws.String("bucket-id")}).Return(nil, nil) | ||
b := resources.NewExistingS3Bucket(s3Cl, aws.Credentials{}, "bucket-id", "us-east-1", "subdirectory") | ||
err := b.Read(ctx) | ||
require.NoError(t, err) | ||
} | ||
|
||
// TestExistingBucketReadNotFound tests the case where the s3 client indicates that the bucket could not be | ||
// found. | ||
func TestExistingBucketReadNotFound(t *testing.T) { | ||
ctx := context.Background() | ||
ctl := gomock.NewController(t) | ||
defer ctl.Finish() | ||
|
||
s3Cl := mocks.NewMockS3Client(ctl) | ||
|
||
s3Cl.EXPECT(). | ||
HeadBucket(gomock.Any(), &s3.HeadBucketInput{Bucket: aws.String("bucket-id")}). | ||
Return(nil, &smithy.GenericAPIError{Code: "NotFound"}) | ||
b := resources.NewExistingS3Bucket(s3Cl, aws.Credentials{}, "bucket-id", "us-east-1", "subdirectory") | ||
err := b.Read(ctx) | ||
require.ErrorIs(t, err, common.NotFoundError) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package mocks | ||
|
||
// This file includes go:generate statements for regenerating mocks. | ||
|
||
//go:generate mockgen -destination s3client_generated.go -package mocks .. S3Client |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests are nice, although mocking cloud infrastructure is rather risky. Maybe we can rely on
rclone
to use functionality that it's already tested on their side or can be tested locally with the same API as cloud resources?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how mocking cloud apis (not infrastructure) is riskier than having no tests at all.
My current proposal is twofold:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If your proposal implies accounting for a considerable amount of cloud-specific details (e.g. “limited interfaces” for S3 and EC2, others for Cloud Storage and Compute Engine, ...) I wonder if maintaining them will be any easier than testing the code against actual cloud APIs, even if it's noticeably slower.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thing with unit tests is that we can test for various edge cases in a repeatable and deterministic way. And while they are in no way a replacement for integration tests, accounting for edge cases in integration tests will either require developing extensive test suites or exhaustive checklists, completing which will take considerable time.