Skip to content

Commit f63b1ae

Browse files
committed
fix(bucket): create bucket basic is working
1 parent df7b223 commit f63b1ae

21 files changed

+9404
-8086
lines changed

internal/provider/provider.go

Lines changed: 140 additions & 140 deletions
Large diffs are not rendered by default.

internal/services/object/bucket.go

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"log"
8-
97
"github.com/aws/aws-sdk-go-v2/aws"
108
"github.com/aws/aws-sdk-go-v2/service/s3"
119
s3Types "github.com/aws/aws-sdk-go-v2/service/s3/types"
@@ -17,6 +15,7 @@ import (
1715
"github.com/scaleway/scaleway-sdk-go/scw"
1816
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1917
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
18+
"log"
2019
)
2120

2221
func ResourceBucket() *schema.Resource {
@@ -79,6 +78,7 @@ func ResourceBucket() *schema.Resource {
7978
"cors_rule": {
8079
Type: schema.TypeList,
8180
Optional: true,
81+
Computed: true,
8282
Elem: &schema.Resource{
8383
Schema: map[string]*schema.Schema{
8484
"allowed_headers": {
@@ -225,27 +225,39 @@ func ResourceBucket() *schema.Resource {
225225

226226
func resourceObjectBucketCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
227227
bucketName := d.Get("name").(string)
228-
objectLockEnabled := d.Get("object_lock_enabled").(bool)
229-
acl := d.Get("acl").(string)
228+
//objectLockEnabled := d.Get("object_lock_enabled").(bool)
229+
//acl := d.Get("acl").(string)
230230

231231
s3Client, region, err := s3ClientWithRegion(ctx, d, m)
232232
if err != nil {
233233
return diag.FromErr(err)
234234
}
235235

236236
req := &s3.CreateBucketInput{
237-
Bucket: scw.StringPtr(bucketName),
238-
ObjectLockEnabledForBucket: scw.BoolPtr(objectLockEnabled),
239-
ACL: s3Types.BucketCannedACL(acl),
237+
Bucket: scw.StringPtr(bucketName),
238+
//ObjectLockEnabledForBucket: scw.BoolPtr(objectLockEnabled),
239+
//ACL: s3Types.BucketCannedACL(acl),
240240
}
241+
241242
_, err = s3Client.CreateBucket(ctx, req)
243+
244+
if v, ok := d.GetOk("acl"); ok {
245+
req.ACL = s3Types.BucketCannedACL(v.(string))
246+
}
247+
248+
if v, ok := d.GetOk("object_lock_enabled"); ok {
249+
req.ObjectLockEnabledForBucket = scw.BoolPtr(v.(bool))
250+
}
251+
242252
if TimedOut(err) {
243253
_, err = s3Client.CreateBucket(ctx, req)
244254
}
245255
if err != nil {
246256
return diag.FromErr(err)
247257
}
248258

259+
d.SetId(regional.NewIDString(region, bucketName))
260+
249261
tagsSet := ExpandObjectBucketTags(d.Get("tags"))
250262

251263
if len(tagsSet) > 0 {
@@ -260,8 +272,6 @@ func resourceObjectBucketCreate(ctx context.Context, d *schema.ResourceData, m i
260272
}
261273
}
262274

263-
d.SetId(regional.NewIDString(region, bucketName))
264-
265275
return resourceObjectBucketUpdate(ctx, d, m)
266276
}
267277

@@ -392,9 +402,10 @@ func resourceBucketLifecycleUpdate(ctx context.Context, conn *s3.Client, d *sche
392402
}
393403

394404
// AbortIncompleteMultipartUpload
395-
if val, ok := r["abort_incomplete_multipart_upload_days"].(int32); ok && val > 0 {
405+
if val, ok := r["abort_incomplete_multipart_upload_days"].(int); ok && val > 0 {
406+
days := int32(val)
396407
rule.AbortIncompleteMultipartUpload = &s3Types.AbortIncompleteMultipartUpload{
397-
DaysAfterInitiation: aws.Int32(val),
408+
DaysAfterInitiation: aws.Int32(days),
398409
}
399410
}
400411

@@ -403,8 +414,9 @@ func resourceBucketLifecycleUpdate(ctx context.Context, conn *s3.Client, d *sche
403414
if len(expiration) > 0 && expiration[0] != nil {
404415
e := expiration[0].(map[string]interface{})
405416
i := &s3Types.LifecycleExpiration{}
406-
if val, ok := e["days"].(int32); ok && val > 0 {
407-
i.Days = aws.Int32(val)
417+
if val, ok := e["days"].(int); ok && val > 0 {
418+
days := int32(val)
419+
i.Days = aws.Int32(days)
408420
}
409421
rule.Expiration = i
410422
}
@@ -416,8 +428,9 @@ func resourceBucketLifecycleUpdate(ctx context.Context, conn *s3.Client, d *sche
416428
for _, transition := range transitions {
417429
transition := transition.(map[string]interface{})
418430
i := s3Types.Transition{}
419-
if val, ok := transition["days"].(int32); ok && val >= 0 {
420-
i.Days = aws.Int32(val)
431+
if val, ok := transition["days"].(int); ok && val >= 0 {
432+
days := int32(val)
433+
i.Days = aws.Int32(days)
421434
}
422435
if val, ok := transition["storage_class"].(string); ok && val != "" {
423436
i.StorageClass = s3Types.TransitionStorageClass(val)
@@ -433,7 +446,8 @@ func resourceBucketLifecycleUpdate(ctx context.Context, conn *s3.Client, d *sche
433446
if rule.Expiration == nil && rule.NoncurrentVersionExpiration == nil &&
434447
rule.Transitions == nil && rule.NoncurrentVersionTransitions == nil &&
435448
rule.AbortIncompleteMultipartUpload == nil {
436-
rule.Expiration = &s3Types.LifecycleExpiration{ExpiredObjectDeleteMarker: aws.Bool(false)}
449+
//rule.Expiration = &s3Types.LifecycleExpiration{}
450+
rule.Transitions = []s3Types.Transition{}
437451
}
438452

439453
rules = append(rules, rule)
@@ -535,11 +549,9 @@ func resourceObjectBucketRead(ctx context.Context, d *schema.ResourceData, m int
535549
corsResponse, err := s3Client.GetBucketCors(ctx, &s3.GetBucketCorsInput{
536550
Bucket: scw.StringPtr(bucketName),
537551
})
538-
if err != nil {
539-
if bucketFound, _ := addReadBucketErrorDiagnostic(&diags, err, "CORS configuration", ErrCodeNoSuchCORSConfiguration); !bucketFound {
540-
d.SetId("")
541-
return diags
542-
}
552+
553+
if err != nil && !IsS3Err(err, ErrCodeNoSuchCORSConfiguration, "The CORS configuration does not exist") {
554+
return diag.FromErr(err)
543555
}
544556

545557
_ = d.Set("cors_rule", flattenBucketCORS(corsResponse))
@@ -568,7 +580,7 @@ func resourceObjectBucketRead(ctx context.Context, d *schema.ResourceData, m int
568580
}
569581

570582
lifecycleRules := make([]map[string]interface{}, 0)
571-
if len(lifecycle.Rules) > 0 {
583+
if lifecycle != nil && len(lifecycle.Rules) > 0 {
572584
lifecycleRules = make([]map[string]interface{}, 0, len(lifecycle.Rules))
573585

574586
for _, lifecycleRule := range lifecycle.Rules {
@@ -659,6 +671,7 @@ func resourceObjectBucketRead(ctx context.Context, d *schema.ResourceData, m int
659671

660672
func resourceObjectBucketDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
661673
s3Client, _, bucketName, err := s3ClientWithRegionAndName(ctx, d, m, d.Id())
674+
var nObjectDeleted int64
662675
if err != nil {
663676
return diag.FromErr(err)
664677
}
@@ -675,9 +688,11 @@ func resourceObjectBucketDelete(ctx context.Context, d *schema.ResourceData, m i
675688

676689
if IsS3Err(err, ErrCodeBucketNotEmpty, "") {
677690
if d.Get("force_destroy").(bool) {
678-
err = deleteS3ObjectVersions(ctx, s3Client, bucketName, true)
691+
nObjectDeleted, err = emptyBucket(ctx, s3Client, bucketName, true)
679692
if err != nil {
680693
return diag.FromErr(fmt.Errorf("error S3 bucket force_destroy: %s", err))
694+
} else {
695+
log.Printf("[DEBUG] Deleted %d S3 objects", nObjectDeleted)
681696
}
682697
// Try to delete bucket again after deleting objects
683698
return resourceObjectBucketDelete(ctx, d, m)
@@ -730,13 +745,13 @@ func resourceS3BucketCorsUpdate(ctx context.Context, s3conn *s3.Client, d *schem
730745
rules := expandBucketCORS(ctx, rawCors, bucketName)
731746
corsInput := &s3.PutBucketCorsInput{
732747
Bucket: scw.StringPtr(bucketName),
733-
CORSConfiguration: &s3.CORSConfiguration{
748+
CORSConfiguration: &s3Types.CORSConfiguration{
734749
CORSRules: rules,
735750
},
736751
}
737752
tflog.Debug(ctx, fmt.Sprintf("S3 bucket: %s, put CORS: %#v", bucketName, corsInput))
738753

739-
_, err := s3conn.PutBucketCorsWithContext(ctx, corsInput)
754+
_, err := s3conn.PutBucketCors(ctx, corsInput)
740755
if err != nil {
741756
return fmt.Errorf("error putting S3 CORS: %s", err)
742757
}

0 commit comments

Comments
 (0)