@@ -3,65 +3,60 @@ package resources
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "strings"
7
6
8
7
"github.com/aws/aws-sdk-go-v2/aws"
9
- "github.com/aws/aws-sdk-go-v2/service/s3"
10
8
11
9
"terraform-provider-iterative/task/common"
10
+ "terraform-provider-iterative/task/common/machine"
12
11
)
13
12
14
13
// NewExistingS3Bucket returns a new data source refering to a pre-allocated
15
14
// S3 bucket.
16
- func NewExistingS3Bucket (client S3Client , credentials aws.Credentials , storageParams common.RemoteStorage ) * ExistingS3Bucket {
15
+ func NewExistingS3Bucket (credentials aws.Credentials , storageParams common.RemoteStorage ) * ExistingS3Bucket {
17
16
return & ExistingS3Bucket {
18
- client : client ,
19
17
credentials : credentials ,
20
18
params : storageParams ,
21
19
}
22
20
}
23
21
24
22
// ExistingS3Bucket identifies an existing S3 bucket.
25
23
type ExistingS3Bucket struct {
26
- client S3Client
27
24
credentials aws.Credentials
28
25
29
26
params common.RemoteStorage
30
27
}
31
28
32
29
// Read verifies the specified S3 bucket is accessible.
33
30
func (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 )
42
34
}
43
35
return nil
44
36
}
45
37
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
+
46
54
// ConnectionString implements common.StorageCredentials.
47
55
// The method returns the rclone connection string for the specific bucket.
48
56
func (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
59
59
}
60
60
61
61
// build-time check to ensure Bucket implements BucketCredentials.
62
62
var _ 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