forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_usage_export_bucket.go
103 lines (88 loc) · 2.47 KB
/
resource_usage_export_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
package google
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
)
func resourceProjectUsageBucket() *schema.Resource {
return &schema.Resource{
Create: resourceProjectUsageBucketCreate,
Read: resourceProjectUsageBucketRead,
Delete: resourceProjectUsageBucketDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"bucket_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"prefix": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
func resourceProjectUsageBucketRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project := d.Id()
p, err := config.clientCompute.Projects.Get(project).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Project data for project %s", project))
}
if p.UsageExportLocation == nil {
log.Printf("[WARN] Removing usage export location resource %s because it's not enabled server-side.", project)
d.SetId("")
}
d.Set("project", project)
d.Set("prefix", p.UsageExportLocation.ReportNamePrefix)
d.Set("bucket_name", p.UsageExportLocation.BucketName)
return nil
}
func resourceProjectUsageBucketCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
op, err := config.clientCompute.Projects.SetUsageExportBucket(project, &compute.UsageExportLocation{
ReportNamePrefix: d.Get("prefix").(string),
BucketName: d.Get("bucket_name").(string),
}).Do()
if err != nil {
return err
}
d.SetId(project)
err = computeOperationWait(config.clientCompute, op, project, "Setting usage export bucket.")
if err != nil {
d.SetId("")
return err
}
d.Set("project", project)
return resourceProjectUsageBucketRead(d, meta)
}
func resourceProjectUsageBucketDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project := d.Id()
op, err := config.clientCompute.Projects.SetUsageExportBucket(project, nil).Do()
if err != nil {
return err
}
d.SetId(project)
err = computeOperationWait(config.clientCompute, op, project, "Setting usage export bucket.")
if err != nil {
return err
}
d.SetId("")
return nil
}