@@ -3,65 +3,60 @@ package resources
33import (
44 "context"
55 "fmt"
6- "strings"
76
87 "github.com/aws/aws-sdk-go-v2/aws"
9- "github.com/aws/aws-sdk-go-v2/service/s3"
108
119 "terraform-provider-iterative/task/common"
10+ "terraform-provider-iterative/task/common/machine"
1211)
1312
1413// NewExistingS3Bucket returns a new data source refering to a pre-allocated
1514// S3 bucket.
16- func NewExistingS3Bucket (client S3Client , credentials aws.Credentials , storageParams common.RemoteStorage ) * ExistingS3Bucket {
15+ func NewExistingS3Bucket (credentials aws.Credentials , storageParams common.RemoteStorage ) * ExistingS3Bucket {
1716 return & ExistingS3Bucket {
18- client : client ,
1917 credentials : credentials ,
2018 params : storageParams ,
2119 }
2220}
2321
2422// ExistingS3Bucket identifies an existing S3 bucket.
2523type ExistingS3Bucket struct {
26- client S3Client
2724 credentials aws.Credentials
2825
2926 params common.RemoteStorage
3027}
3128
3229// Read verifies the specified S3 bucket is accessible.
3330func (b * ExistingS3Bucket ) Read (ctx context.Context ) error {
34- input := s3.HeadBucketInput {
35- Bucket : aws .String (b .params .Container ),
36- }
37- if _ , err := b .client .HeadBucket (ctx , & input ); err != nil {
38- if errorCodeIs (err , errNotFound ) {
39- return common .NotFoundError
40- }
41- return err
31+ err := machine .CheckStorage (ctx , b .connection ())
32+ if err != nil {
33+ return fmt .Errorf ("failed to verify existing s3 bucket: %w" , err )
4234 }
4335 return nil
4436}
4537
38+ func (b * ExistingS3Bucket ) connection () machine.RcloneConnection {
39+ region := b .params .Config ["region" ]
40+ return machine.RcloneConnection {
41+ Backend : machine .RcloneBackendS3 ,
42+ Container : b .params .Container ,
43+ Path : b .params .Path ,
44+ Config : map [string ]string {
45+ "provider" : "AWS" ,
46+ "region" : region ,
47+ "access_key_id" : b .credentials .AccessKeyID ,
48+ "secret_access_key" : b .credentials .SecretAccessKey ,
49+ "session_token" : b .credentials .SessionToken ,
50+ },
51+ }
52+ }
53+
4654// ConnectionString implements common.StorageCredentials.
4755// The method returns the rclone connection string for the specific bucket.
4856func (b * ExistingS3Bucket ) ConnectionString (ctx context.Context ) (string , error ) {
49- region := b .params .Config ["region" ]
50- connectionString := fmt .Sprintf (
51- ":s3,provider=AWS,region=%s,access_key_id=%s,secret_access_key=%s,session_token=%s:%s/%s" ,
52- region ,
53- b .credentials .AccessKeyID ,
54- b .credentials .SecretAccessKey ,
55- b .credentials .SessionToken ,
56- b .params .Container ,
57- strings .TrimPrefix (b .params .Path , "/" ))
58- return connectionString , nil
57+ connection := b .connection ()
58+ return connection .String (), nil
5959}
6060
6161// build-time check to ensure Bucket implements BucketCredentials.
6262var _ common.StorageCredentials = (* ExistingS3Bucket )(nil )
63-
64- // S3Client defines the functions of the AWS S3 API used.
65- type S3Client interface {
66- HeadBucket (context.Context , * s3.HeadBucketInput , ... func (* s3.Options )) (* s3.HeadBucketOutput , error )
67- }
0 commit comments