forked from hashicorp/terraform-provider-google
/
data_source_google_billing_account.go
131 lines (111 loc) · 3.28 KB
/
data_source_google_billing_account.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
package google
import (
"fmt"
"net/http"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/cloudbilling/v1"
"google.golang.org/api/googleapi"
)
func dataSourceGoogleBillingAccount() *schema.Resource {
return &schema.Resource{
Read: dataSourceBillingAccountRead,
Schema: map[string]*schema.Schema{
"billing_account": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"display_name"},
},
"display_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"billing_account"},
},
"open": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"project_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func dataSourceBillingAccountRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
open, openOk := d.GetOkExists("open")
var billingAccount *cloudbilling.BillingAccount
if v, ok := d.GetOk("billing_account"); ok {
resp, err := config.clientBilling.BillingAccounts.Get(canonicalBillingAccountName(v.(string))).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusNotFound {
return fmt.Errorf("Billing account not found: %s", v)
}
return fmt.Errorf("Error reading billing account: %s", err)
}
if openOk && resp.Open != open.(bool) {
return fmt.Errorf("Billing account not found: %s", v)
}
billingAccount = resp
} else if v, ok := d.GetOk("display_name"); ok {
token := ""
for paginate := true; paginate; {
resp, err := config.clientBilling.BillingAccounts.List().PageToken(token).Do()
if err != nil {
return fmt.Errorf("Error reading billing accounts: %s", err)
}
for _, ba := range resp.BillingAccounts {
if ba.DisplayName == v.(string) {
if openOk && ba.Open != open.(bool) {
continue
}
if billingAccount != nil {
return fmt.Errorf("More than one matching billing account found")
}
billingAccount = ba
}
}
token = resp.NextPageToken
paginate = token != ""
}
if billingAccount == nil {
return fmt.Errorf("Billing account not found: %s", v)
}
} else {
return fmt.Errorf("one of billing_account or display_name must be set")
}
resp, err := config.clientBilling.BillingAccounts.Projects.List(billingAccount.Name).Do()
if err != nil {
return fmt.Errorf("Error reading billing account projects: %s", err)
}
projectIds := flattenBillingProjects(resp.ProjectBillingInfo)
d.SetId(GetResourceNameFromSelfLink(billingAccount.Name))
d.Set("name", billingAccount.Name)
d.Set("display_name", billingAccount.DisplayName)
d.Set("open", billingAccount.Open)
d.Set("project_ids", projectIds)
return nil
}
func canonicalBillingAccountName(ba string) string {
if strings.HasPrefix(ba, "billingAccounts/") {
return ba
}
return "billingAccounts/" + ba
}
func flattenBillingProjects(billingProjects []*cloudbilling.ProjectBillingInfo) []string {
projectIds := make([]string, len(billingProjects))
for i, billingProject := range billingProjects {
projectIds[i] = billingProject.ProjectId
}
return projectIds
}