-
Notifications
You must be signed in to change notification settings - Fork 649
/
resource_ibm_cos_bucket_website_configuration.go
507 lines (485 loc) · 18.6 KB
/
resource_ibm_cos_bucket_website_configuration.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package cos
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/ibm-cos-sdk-go/aws"
"github.com/IBM/ibm-cos-sdk-go/service/s3"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
validation "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func ResourceIBMCOSBucketWebsiteConfiguration() *schema.Resource {
return &schema.Resource{
Create: resourceIBMCOSBucketWebsiteConfigurationCreate,
Read: resourceIBMCOSBucketWebsiteConfigurationRead,
Update: resourceIBMCOSBucketWebsiteConfigurationUpdate,
Delete: resourceIBMCOSBucketWebsiteConfigurationDelete,
Importer: &schema.ResourceImporter{},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(60 * time.Minute),
Update: schema.DefaultTimeout(20 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"bucket_crn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "COS bucket CRN",
},
"bucket_location": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "COS bucket location",
},
"endpoint_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.ValidateAllowedStringValues([]string{"public", "private", "direct"}),
Description: "COS endpoint type: public, private, direct",
Default: "public",
},
"website_configuration": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Description: "Configuration for Hosting a static website on COS with public access.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"error_document": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "This is returned when an error occurs.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"index_document": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Home or the default page of the website.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"suffix": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"redirect_all_requests_to": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Redirect requests can be set to specific page documents, individual routing rules, or redirect all requests globally to one bucket or domain.",
ConflictsWith: []string{"website_configuration.0.error_document", "website_configuration.0.index_document", "website_configuration.0.routing_rule", "website_configuration.0.routing_rules"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"host_name": {
Type: schema.TypeString,
Required: true,
},
"protocol": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(s3.Protocol_Values(), false),
},
},
},
},
"routing_rule": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: "Rules that define when a redirect is applied and the redirect behavior.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"condition": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "A condition that must be met for the specified redirect to be applie.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"http_error_code_returned_equals": {
Type: schema.TypeString,
Optional: true,
Description: "The HTTP error code when the redirect is applied. Valid codes are 4XX or 5XX..",
},
"key_prefix_equals": {
Type: schema.TypeString,
Optional: true,
Description: "The object key name prefix when the redirect is applied..",
},
},
},
},
"redirect": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Description: ".",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"host_name": {
Type: schema.TypeString,
Optional: true,
Description: "The host name the request should be redirected to.",
},
"http_redirect_code": {
Type: schema.TypeString,
Optional: true,
Description: "The HTTP redirect code to use on the response. Valid codes are 3XX except 300..",
},
"protocol": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(s3.Protocol_Values(), false),
Description: "Protocol to be used in the Location header that is returned in the response.",
},
"replace_key_prefix_with": {
Type: schema.TypeString,
Optional: true,
Description: "The prefix of the object key name that replaces the value of KeyPrefixEquals in the redirect request.",
},
"replace_key_with": {
Type: schema.TypeString,
Optional: true,
Description: "The object key to be used in the Location header that is returned in the response.",
},
},
},
},
},
},
},
"routing_rules": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Rules that define when a redirect is applied and the redirect behavior.",
ConflictsWith: []string{"website_configuration.0.routing_rule"},
ValidateFunc: validation.StringIsJSON,
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
},
},
},
},
},
"website_endpoint": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func errorDocumentSetFunction(errorDocument []interface{}) *s3.ErrorDocument {
var error_document *s3.ErrorDocument
errorDocumentValue := s3.ErrorDocument{}
if len(errorDocument) != 0 {
errorDocumentMap := errorDocument[0].(map[string]interface{})
if keyValue, ok := errorDocumentMap["key"].(string); ok && keyValue != "" {
errorDocumentValue.Key = aws.String(keyValue)
}
error_document = &errorDocumentValue
return error_document
} else {
return nil
}
}
func indexDocumentSetFunction(indexDocument []interface{}) *s3.IndexDocument {
var index_document *s3.IndexDocument
indexDocumentValue := s3.IndexDocument{}
if len(indexDocument) != 0 {
indexDocumentMap, _ := indexDocument[0].(map[string]interface{})
if suffixValue, ok := indexDocumentMap["suffix"].(string); ok && suffixValue != "" {
indexDocumentValue.Suffix = aws.String(suffixValue)
}
index_document = &indexDocumentValue
return index_document
} else {
return nil
}
}
func redirectAllRequestsSetFunction(redirectAllRequestsSet []interface{}) *s3.RedirectAllRequestsTo {
var redirect_all_requests *s3.RedirectAllRequestsTo
redirectAllRequestsValue := s3.RedirectAllRequestsTo{}
if len(redirectAllRequestsSet) != 0 {
redirectAllRequestsMap, _ := redirectAllRequestsSet[0].(map[string]interface{})
if hostName, ok := redirectAllRequestsMap["host_name"].(string); ok && hostName != "" {
redirectAllRequestsValue.HostName = aws.String(hostName)
}
if protocol, ok := redirectAllRequestsMap["protocol"].(string); ok && protocol != "" {
redirectAllRequestsValue.Protocol = aws.String(protocol)
}
redirect_all_requests = &redirectAllRequestsValue
return redirect_all_requests
} else {
return nil
}
}
func routingRouteConditionSet(routingRuleConditionSet []interface{}) *s3.Condition {
var routingRuleCondition *s3.Condition
conditionValue := s3.Condition{}
if len(routingRuleConditionSet) != 0 {
routingRuleConditionMap, _ := routingRuleConditionSet[0].(map[string]interface{})
if httpErrorCodeReturnedEquals, ok := routingRuleConditionMap["http_error_code_returned_equals"].(string); ok && httpErrorCodeReturnedEquals != "" {
conditionValue.HttpErrorCodeReturnedEquals = aws.String(httpErrorCodeReturnedEquals)
}
if keyPrefixEquals, ok := routingRuleConditionMap["key_prefix_equals"].(string); ok && keyPrefixEquals != "" {
conditionValue.KeyPrefixEquals = aws.String(keyPrefixEquals)
}
routingRuleCondition = &conditionValue
return routingRuleCondition
} else {
return nil
}
}
func routingRouteRedirectSet(routingRuleRedirectSet []interface{}) *s3.Redirect {
var routingRuleRedirect *s3.Redirect
redirectValue := s3.Redirect{}
if len(routingRuleRedirectSet) != 0 {
routingRuleRedirectMap, _ := routingRuleRedirectSet[0].(map[string]interface{})
if hostName, ok := routingRuleRedirectMap["host_name"].(string); ok && hostName != "" {
redirectValue.HostName = aws.String(hostName)
}
if httpRedirectCode, ok := routingRuleRedirectMap["http_redirect_code"].(string); ok && httpRedirectCode != "" {
redirectValue.HttpRedirectCode = aws.String(httpRedirectCode)
}
if protocol, ok := routingRuleRedirectMap["protocol"].(string); ok && protocol != "" {
redirectValue.Protocol = aws.String(protocol)
}
if replaceKeyPrefixWith, ok := routingRuleRedirectMap["replace_key_prefix_with"].(string); ok && replaceKeyPrefixWith != "" {
redirectValue.ReplaceKeyPrefixWith = aws.String(replaceKeyPrefixWith)
}
if replaceKeyWith, ok := routingRuleRedirectMap["replace_key_with"].(string); ok && replaceKeyWith != "" {
redirectValue.ReplaceKeyWith = aws.String(replaceKeyWith)
}
routingRuleRedirect = &redirectValue
return routingRuleRedirect
} else {
return nil
}
}
func routingRuleSetFunction(routingRuleSet []interface{}) []*s3.RoutingRule {
var rules []*s3.RoutingRule
for _, l := range routingRuleSet {
ruleMap, ok := l.(map[string]interface{})
if !ok {
continue
}
routing_rule := s3.RoutingRule{}
if condition, ok := ruleMap["condition"].([]interface{}); ok && len(condition) > 0 && condition[0] != nil {
routing_rule.Condition = routingRouteConditionSet(condition)
}
if redirect, ok := ruleMap["redirect"].([]interface{}); ok && len(redirect) > 0 && redirect[0] != nil {
routing_rule.Redirect = routingRouteRedirectSet(redirect)
}
rules = append(rules, &routing_rule)
}
return rules
}
func websiteConfigurationSet(websiteConfigurationList []interface{}) (*s3.WebsiteConfiguration, error) {
var websiteConfig *s3.WebsiteConfiguration
website_configuration := s3.WebsiteConfiguration{}
configurationMap, _ := websiteConfigurationList[0].(map[string]interface{})
// error document is provided
if errorDocumentSet, exist := configurationMap["error_document"]; exist {
website_configuration.ErrorDocument = errorDocumentSetFunction(errorDocumentSet.([]interface{}))
}
//index document is provided
if indexDocumentSet, exist := configurationMap["index_document"]; exist {
website_configuration.IndexDocument = indexDocumentSetFunction(indexDocumentSet.([]interface{}))
}
//redirect_all_requests_to is provided
if redirectAllRequestsSet, exist := configurationMap["redirect_all_requests_to"]; exist {
website_configuration.RedirectAllRequestsTo = redirectAllRequestsSetFunction(redirectAllRequestsSet.([]interface{}))
}
// routing_rules provided
if routingRulesSet, exist := configurationMap["routing_rule"]; exist {
website_configuration.RoutingRules = routingRuleSetFunction(routingRulesSet.([]interface{}))
}
// if json routing routes are provided
if routingRulesJsonSet, exist := configurationMap["routing_rules"]; exist {
jsonValue := fmt.Sprint(configurationMap["routing_rules"])
if jsonValue != "" {
var unmarshalledRules []*s3.RoutingRule
if err := json.Unmarshal([]byte(routingRulesJsonSet.(string)), &unmarshalledRules); err != nil {
return nil, fmt.Errorf("failed to update the json routing rules in the website configuration : %v", err)
}
fmt.Println("unmarshal rules : ", unmarshalledRules)
website_configuration.RoutingRules = unmarshalledRules
}
}
websiteConfig = &website_configuration
return websiteConfig, nil
}
func resourceIBMCOSBucketWebsiteConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
bucketCRN := d.Get("bucket_crn").(string)
bucketName := strings.Split(bucketCRN, ":bucket:")[1]
instanceCRN := fmt.Sprintf("%s::", strings.Split(bucketCRN, ":bucket:")[0])
bucketLocation := d.Get("bucket_location").(string)
endpointType := d.Get("endpoint_type").(string)
bxSession, err := meta.(conns.ClientSession).BluemixSession()
if err != nil {
return err
}
s3Client, err := getS3ClientSession(bxSession, bucketLocation, endpointType, instanceCRN)
var websiteConfiguration *s3.WebsiteConfiguration
configuration, ok := d.GetOk("website_configuration")
if ok {
websiteConfiguration, err = websiteConfigurationSet(configuration.([]interface{}))
}
if err != nil {
return fmt.Errorf("failed to read website configuration for COS bucket %s, %v", bucketName, err)
}
putBucketWebsiteConfigurationInput := s3.PutBucketWebsiteInput{
Bucket: aws.String(bucketName),
WebsiteConfiguration: websiteConfiguration,
}
_, err = s3Client.PutBucketWebsite(&putBucketWebsiteConfigurationInput)
if err != nil {
return fmt.Errorf("failed to put website configuration on the COS bucket %s, %v", bucketName, err)
}
bktID := fmt.Sprintf("%s:%s:%s:meta:%s:%s", strings.Replace(instanceCRN, "::", "", -1), "bucket", bucketName, bucketLocation, endpointType)
d.SetId(bktID)
return resourceIBMCOSBucketWebsiteConfigurationUpdate(d, meta)
}
func resourceIBMCOSBucketWebsiteConfigurationUpdate(d *schema.ResourceData, meta interface{}) error {
bucketCRN := d.Get("bucket_crn").(string)
bucketName := strings.Split(bucketCRN, ":bucket:")[1]
instanceCRN := fmt.Sprintf("%s::", strings.Split(bucketCRN, ":bucket:")[0])
bucketLocation := d.Get("bucket_location").(string)
endpointType := d.Get("endpoint_type").(string)
bxSession, err := meta.(conns.ClientSession).BluemixSession()
if err != nil {
return err
}
s3Client, err := getS3ClientSession(bxSession, bucketLocation, endpointType, instanceCRN)
if err != nil {
return err
}
if d.HasChange("website_configuration") {
var websiteConfiguration *s3.WebsiteConfiguration
configuration, ok := d.GetOk("website_configuration")
if ok {
websiteConfiguration, err = websiteConfigurationSet(configuration.([]interface{}))
}
if err != nil {
return fmt.Errorf("failed to read website configuration for COS bucket %s, %v", bucketName, err)
}
putBucketWebsiteConfigurationInput := s3.PutBucketWebsiteInput{
Bucket: aws.String(bucketName),
WebsiteConfiguration: websiteConfiguration,
}
_, err = s3Client.PutBucketWebsite(&putBucketWebsiteConfigurationInput)
if err != nil {
return fmt.Errorf("failed to update website configuration on the COS bucket %s, %v", bucketName, err)
}
}
return resourceIBMCOSBucketWebsiteConfigurationRead(d, meta)
}
func resourceIBMCOSBucketWebsiteConfigurationRead(d *schema.ResourceData, meta interface{}) error {
bucketCRN := parseWebsiteId(d.Id(), "bucketCRN")
bucketName := parseWebsiteId(d.Id(), "bucketName")
bucketLocation := parseWebsiteId(d.Id(), "bucketLocation")
instanceCRN := parseWebsiteId(d.Id(), "instanceCRN")
endpointType := parseWebsiteId(d.Id(), "endpointType")
d.Set("bucket_crn", bucketCRN)
d.Set("bucket_location", bucketLocation)
if endpointType != "" {
d.Set("endpoint_type", endpointType)
}
bxSession, err := meta.(conns.ClientSession).BluemixSession()
if err != nil {
return err
}
s3Client, err := getS3ClientSession(bxSession, bucketLocation, endpointType, instanceCRN)
if err != nil {
return err
}
//getBucketConfiguration
getBucketWebsiteConfigurationInput := &s3.GetBucketWebsiteInput{
Bucket: aws.String(bucketName),
}
output, err := s3Client.GetBucketWebsite(getBucketWebsiteConfigurationInput)
var outputptr *s3.WebsiteConfiguration
outputptr = (*s3.WebsiteConfiguration)(output)
if err != nil && !strings.Contains(err.Error(), "AccessDenied: Access Denied") {
return err
}
if output != nil {
websiteConfiguration := flex.WebsiteConfigurationGet(outputptr)
if len(websiteConfiguration) > 0 {
d.Set("website_configuration", websiteConfiguration)
}
websiteEndpoint := getWebsiteEndpoint(bucketName, bucketLocation)
if websiteEndpoint != "" {
d.Set("website_endpoint", websiteEndpoint)
}
}
return nil
}
func resourceIBMCOSBucketWebsiteConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
bucketName := parseWebsiteId(d.Id(), "bucketName")
bucketLocation := parseWebsiteId(d.Id(), "bucketLocation")
instanceCRN := parseWebsiteId(d.Id(), "instanceCRN")
endpointType := parseWebsiteId(d.Id(), "endpointType")
bxSession, err := meta.(conns.ClientSession).BluemixSession()
if err != nil {
return err
}
s3Client, err := getS3ClientSession(bxSession, bucketLocation, endpointType, instanceCRN)
if err != nil {
return err
}
deleteBucketWebsiteInput := &s3.DeleteBucketWebsiteInput{
Bucket: aws.String(bucketName),
}
_, err = s3Client.DeleteBucketWebsite(deleteBucketWebsiteInput)
if err != nil {
return fmt.Errorf("failed to delete the objectlock configuration on the COS bucket %s, %v", bucketName, err)
}
return nil
}
func parseWebsiteId(id string, info string) string {
bucketCRN := strings.Split(id, ":meta:")[0]
meta := strings.Split(id, ":meta:")[1]
if info == "bucketName" {
return strings.Split(bucketCRN, ":bucket:")[1]
}
if info == "instanceCRN" {
return fmt.Sprintf("%s::", strings.Split(bucketCRN, ":bucket:")[0])
}
if info == "bucketCRN" {
return bucketCRN
}
if info == "bucketLocation" {
return strings.Split(meta, ":")[0]
}
if info == "endpointType" {
return strings.Split(meta, ":")[1]
}
if info == "keyName" {
return strings.Split(meta, ":key:")[1]
}
return parseBucketId(bucketCRN, info)
}
func getWebsiteEndpoint(bucketName string, bucketLocation string) string {
return fmt.Sprintf("https://%s.s3-web.%s.cloud-object-storage.appdomain.cloud", bucketName, bucketLocation)
}