This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
forked from scaleway/terraform-provider-scaleway
-
Notifications
You must be signed in to change notification settings - Fork 3
/
resource_object_bucket.go
132 lines (113 loc) · 3.55 KB
/
resource_object_bucket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package scaleway
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
func resourceScalewayObjectBucket() *schema.Resource {
return &schema.Resource{
Create: resourceScalewayObjectBucketCreate,
Read: resourceScalewayObjectBucketRead,
Update: resourceScalewayObjectBucketUpdate,
Delete: resourceScalewayObjectBucketDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the bucket",
},
"acl": {
Type: schema.TypeString,
Optional: true,
Default: "private",
Description: "ACL of the bucket: either 'public-read' or 'private'.",
ValidateFunc: validation.StringInSlice([]string{
s3.ObjectCannedACLPrivate,
s3.ObjectCannedACLPublicRead,
s3.ObjectCannedACLPublicReadWrite,
s3.ObjectCannedACLAuthenticatedRead,
}, false),
},
"region": regionSchema(),
},
}
}
func resourceScalewayObjectBucketCreate(d *schema.ResourceData, m interface{}) error {
bucketName := d.Get("name").(string)
acl := d.Get("acl").(string)
s3Client, region, err := s3ClientWithRegion(d, m)
if err != nil {
return err
}
_, err = s3Client.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String(bucketName),
ACL: aws.String(acl),
})
if err != nil {
return err
}
d.SetId(newRegionalId(region, bucketName))
return resourceScalewayObjectBucketRead(d, m)
}
func resourceScalewayObjectBucketRead(d *schema.ResourceData, m interface{}) error {
s3Client, _, bucketName, err := s3ClientWithRegionAndName(m, d.Id())
if err != nil {
return err
}
_ = d.Set("name", bucketName)
// We do not read `acl` attribute because it could be impossible to find
// the right canned ACL from a complex ACL object.
//
// Known issue:
// Import a bucket (eg. terraform import scaleway_object_bucket.x fr-par/x)
// will always trigger a diff (eg. terraform plan) on acl attribute because
// we do not read it and it has a "private" default value.
// AWS has the same issue: https://github.com/terraform-providers/terraform-provider-aws/issues/6193
_, err = s3Client.ListObjects(&s3.ListObjectsInput{
Bucket: aws.String(bucketName),
})
if err != nil {
if serr, ok := err.(awserr.Error); ok && serr.Code() == s3.ErrCodeNoSuchBucket {
l.Errorf("Bucket %q was not found - removing from state!", bucketName)
d.SetId("")
return nil
}
return fmt.Errorf("couldn't read bucket: %s", err)
}
return nil
}
func resourceScalewayObjectBucketUpdate(d *schema.ResourceData, m interface{}) error {
s3Client, _, bucketName, err := s3ClientWithRegionAndName(m, d.Id())
if err != nil {
return err
}
if d.HasChange("acl") {
acl := d.Get("acl").(string)
_, err := s3Client.PutBucketAcl(&s3.PutBucketAclInput{
Bucket: aws.String(bucketName),
ACL: aws.String(acl),
})
if err != nil {
l.Errorf("Couldn't update bucket ACL: %s", err)
return fmt.Errorf("couldn't update bucket ACL: %s", err)
}
}
return resourceScalewayObjectBucketRead(d, m)
}
func resourceScalewayObjectBucketDelete(d *schema.ResourceData, m interface{}) error {
s3Client, _, bucketName, err := s3ClientWithRegionAndName(m, d.Id())
if err != nil {
return err
}
_, err = s3Client.DeleteBucket(&s3.DeleteBucketInput{
Bucket: aws.String(bucketName),
})
return err
}