forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_google_billing_subaccount.go
158 lines (135 loc) · 4.56 KB
/
resource_google_billing_subaccount.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
package google
import (
"fmt"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"google.golang.org/api/cloudbilling/v1"
)
func resourceBillingSubaccount() *schema.Resource {
return &schema.Resource{
Create: resourceBillingSubaccountCreate,
Read: resourceBillingSubaccountRead,
Delete: resourceBillingSubaccountDelete,
Update: resourceBillingSubaccountUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"display_name": {
Type: schema.TypeString,
Required: true,
},
"master_billing_account": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
"deletion_policy": {
Type: schema.TypeString,
Optional: true,
Default: "",
ValidateFunc: validation.StringInSlice([]string{"RENAME_ON_DESTROY", ""}, false),
},
"billing_account_id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"open": {
Type: schema.TypeBool,
Computed: true,
},
},
UseJSONNumber: true,
}
}
func resourceBillingSubaccountCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
displayName := d.Get("display_name").(string)
masterBillingAccount := d.Get("master_billing_account").(string)
billingAccount := &cloudbilling.BillingAccount{
DisplayName: displayName,
MasterBillingAccount: canonicalBillingAccountName(masterBillingAccount),
}
res, err := config.NewBillingClient(userAgent).BillingAccounts.Create(billingAccount).Do()
if err != nil {
return fmt.Errorf("Error creating billing subaccount '%s' in master account '%s': %s", displayName, masterBillingAccount, err)
}
d.SetId(res.Name)
return resourceBillingSubaccountRead(d, meta)
}
func resourceBillingSubaccountRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
id := d.Id()
billingAccount, err := config.NewBillingClient(userAgent).BillingAccounts.Get(d.Id()).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Billing Subaccount Not Found : %s", id))
}
if err := d.Set("name", billingAccount.Name); err != nil {
return fmt.Errorf("Error setting name: %s", err)
}
if err := d.Set("display_name", billingAccount.DisplayName); err != nil {
return fmt.Errorf("Error setting display_na,e: %s", err)
}
if err := d.Set("open", billingAccount.Open); err != nil {
return fmt.Errorf("Error setting open: %s", err)
}
if err := d.Set("master_billing_account", billingAccount.MasterBillingAccount); err != nil {
return fmt.Errorf("Error setting master_billing_account: %s", err)
}
if err := d.Set("billing_account_id", strings.TrimPrefix(d.Get("name").(string), "billingAccounts/")); err != nil {
return fmt.Errorf("Error setting billing_account_id: %s", err)
}
return nil
}
func resourceBillingSubaccountUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
if ok := d.HasChange("display_name"); ok {
billingAccount := &cloudbilling.BillingAccount{
DisplayName: d.Get("display_name").(string),
}
_, err := config.NewBillingClient(userAgent).BillingAccounts.Patch(d.Id(), billingAccount).UpdateMask("display_name").Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Error updating billing account : %s", d.Id()))
}
}
return resourceBillingSubaccountRead(d, meta)
}
func resourceBillingSubaccountDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
deletionPolicy := d.Get("deletion_policy").(string)
if deletionPolicy == "RENAME_ON_DESTROY" {
t := time.Now()
billingAccount := &cloudbilling.BillingAccount{
DisplayName: "Terraform Destroyed " + t.Format("20060102150405"),
}
_, err := config.NewBillingClient(userAgent).BillingAccounts.Patch(d.Id(), billingAccount).UpdateMask("display_name").Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Error updating billing account : %s", d.Id()))
}
}
d.SetId("")
return nil
}