forked from negronjl/terraform-provider-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.go
93 lines (83 loc) · 2.52 KB
/
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
package main
import (
"errors"
"log"
"github.com/hashicorp/terraform/helper/schema"
"fmt"
)
func resourceS3Bucket() *schema.Resource {
return &schema.Resource{
Create: resourceS3BucketCreate,
Read: resourceS3BucketRead,
Update: resourceS3BucketUpdate,
Delete: resourceS3BucketDelete,
Schema: map[string]*schema.Schema{
"bucket": {
Type: schema.TypeString,
Required: true,
},
"debug": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
func resourceS3BucketCreate(d *schema.ResourceData, meta interface{}) error {
debug := d.Get("debug").(bool)
bucket := d.Get("bucket").(string)
region := meta.(*s3Client).region
s3_client := meta.(*s3Client).s3Client
if debug {
log.Printf("[DEBUG] Creating bucket: [%s] in region: [%s]", bucket, region)
}
err := s3_client.MakeBucket(bucket, region)
if err != nil {
log.Printf("[FATAL] Unable to create bucket [%s] in region [%s]. Failed with error: %v", bucket, region, err)
return errors.New(fmt.Sprintf("Unable to create bucket [%s] in region [%s]. Failed with error: %v", bucket, region, err))
}
if debug {
log.Printf("[DEBUG] Created bucket: [%s] in region: [%s]", bucket, region)
}
return nil
}
func resourceS3BucketRead(d *schema.ResourceData, meta interface{}) error {
debug := d.Get("debug").(bool)
bucket := d.Get("bucket").(string)
region := meta.(*s3Client).region
s3_client := meta.(*s3Client).s3Client
if debug {
log.Printf("[DEBUG] Reading bucket [%s] in region [%s]", bucket, region)
}
found, err := s3_client.BucketExists(bucket)
if !found {
return errors.New(fmt.Sprintf("[FATAL] Unable to find bucket [%s] in region [%s]. Error: %v",
bucket, region, err))
}
return nil
}
func resourceS3BucketUpdate(d *schema.ResourceData, meta interface{}) error {
debug := d.Get("debug").(bool)
bucket := d.Get("bucket").(string)
region := meta.(*s3Client).region
if debug {
log.Printf("[DEBUG] Bucket update operation not implemented. Bucket: [%s], Region: [%s]",
bucket, region)
}
return nil
}
func resourceS3BucketDelete(d *schema.ResourceData, meta interface{}) error {
debug := d.Get("debug").(bool)
bucket := d.Get("bucket").(string)
region := meta.(*s3Client).region
s3_client := meta.(*s3Client).s3Client
if debug {
log.Printf("[DEBUG] Deleting bucket [%s] from region [%s]", bucket, region)
}
if s3_client.RemoveBucket(bucket) != nil {
log.Printf("[FATAL] Unable to remove bucket [%s].")
return errors.New(fmt.Sprintf("[FATAL] Unable to remove bucket [%s]", bucket))
}
return nil
}