8
8
"github.com/aws/aws-sdk-go/service/s3"
9
9
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
10
10
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
11
+ "github.com/scaleway/scaleway-sdk-go/scw"
11
12
)
12
13
13
14
func resourceScalewayObjectBucket () * schema.Resource {
@@ -38,6 +39,14 @@ func resourceScalewayObjectBucket() *schema.Resource {
38
39
s3 .ObjectCannedACLAuthenticatedRead ,
39
40
}, false ),
40
41
},
42
+ "tags" : {
43
+ Type : schema .TypeMap ,
44
+ Elem : & schema.Schema {
45
+ Type : schema .TypeString ,
46
+ },
47
+ Optional : true ,
48
+ Description : "The tags associated with this bucket" ,
49
+ },
41
50
"region" : regionSchema (),
42
51
},
43
52
}
@@ -60,6 +69,27 @@ func resourceScalewayObjectBucketCreate(d *schema.ResourceData, m interface{}) e
60
69
return err
61
70
}
62
71
72
+ tagsSet := make ([]* s3.Tag , 0 )
73
+
74
+ for key , value := range d .Get ("tags" ).(map [string ]interface {}) {
75
+ tagsSet = append (tagsSet , & s3.Tag {
76
+ Key : & key ,
77
+ Value : scw .StringPtr (value .(string )),
78
+ })
79
+ }
80
+
81
+ if len (tagsSet ) > 0 {
82
+ _ , err = s3Client .PutBucketTagging (& s3.PutBucketTaggingInput {
83
+ Bucket : aws .String (bucketName ),
84
+ Tagging : & s3.Tagging {
85
+ TagSet : tagsSet ,
86
+ },
87
+ })
88
+ if err != nil {
89
+ return err
90
+ }
91
+ }
92
+
63
93
d .SetId (newRegionalId (region , bucketName ))
64
94
65
95
return resourceScalewayObjectBucketRead (d , m )
@@ -94,6 +124,35 @@ func resourceScalewayObjectBucketRead(d *schema.ResourceData, m interface{}) err
94
124
return fmt .Errorf ("couldn't read bucket: %s" , err )
95
125
}
96
126
127
+ var tagsSet []* s3.Tag
128
+
129
+ tagsResponse , err := s3Client .GetBucketTagging (& s3.GetBucketTaggingInput {
130
+ Bucket : aws .String (bucketName ),
131
+ })
132
+ if err != nil {
133
+ if serr , ok := err .(awserr.Error ); ! ok || serr .Code () != "NoSuchTagSet" {
134
+ return fmt .Errorf ("couldn't read tags from bucket: %s" , err )
135
+ }
136
+ } else {
137
+ tagsSet = tagsResponse .TagSet
138
+ }
139
+
140
+ tags := map [string ]interface {}{}
141
+
142
+ for _ , tagSet := range tagsSet {
143
+ var key string
144
+ var value string
145
+ if tagSet .Key != nil {
146
+ key = * tagSet .Key
147
+ }
148
+ if tagSet .Value != nil {
149
+ value = * tagSet .Value
150
+ }
151
+ tags [key ] = value
152
+ }
153
+
154
+ _ = d .Set ("tags" , tags )
155
+
97
156
return nil
98
157
}
99
158
@@ -116,6 +175,24 @@ func resourceScalewayObjectBucketUpdate(d *schema.ResourceData, m interface{}) e
116
175
}
117
176
}
118
177
178
+ if d .HasChange ("tags" ) {
179
+ tagsSet := make ([]* s3.Tag , 0 )
180
+
181
+ for key , value := range d .Get ("tags" ).(map [string ]interface {}) {
182
+ tagsSet = append (tagsSet , & s3.Tag {
183
+ Key : & key ,
184
+ Value : scw .StringPtr (value .(string )),
185
+ })
186
+ }
187
+
188
+ _ , err = s3Client .PutBucketTagging (& s3.PutBucketTaggingInput {
189
+ Bucket : aws .String (bucketName ),
190
+ Tagging : & s3.Tagging {
191
+ TagSet : tagsSet ,
192
+ },
193
+ })
194
+ }
195
+
119
196
return resourceScalewayObjectBucketRead (d , m )
120
197
}
121
198
0 commit comments