forked from aiven/aiven-go-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
billing_group.go
156 lines (127 loc) · 4.5 KB
/
billing_group.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
package aiven
type (
// BillingGroup represents an billing group
BillingGroup struct {
BillingGroupRequest
Id string `json:"billing_group_id"`
}
// BillingGroupRequest is the request from Aiven for the billing group endpoints.
BillingGroupRequest struct {
BillingGroupName string `json:"billing_group_name,omitempty"`
AccountId *string `json:"account_id,omitempty"`
CardId *string `json:"card_id,omitempty"`
VatId *string `json:"vat_id,omitempty"`
BillingCurrency *string `json:"billing_currency,omitempty"`
BillingExtraText *string `json:"billing_extra_text,omitempty"`
BillingEmails []*ContactEmail `json:"billing_emails,omitempty"`
Company *string `json:"company,omitempty"`
AddressLines []string `json:"address_lines,omitempty"`
CountryCode *string `json:"country_code,omitempty"`
City *string `json:"city,omitempty"`
State *string `json:"state,omitempty"`
ZipCode *string `json:"zip_code,omitempty"`
}
// BillingGroupHandler is the client that interacts with billing groups on Aiven
BillingGroupHandler struct {
client *Client
}
// BillingGroupResponse is the response from Aiven for the billing group endpoints.
BillingGroupResponse struct {
APIResponse
BillingGroup *BillingGroup `json:"billing_group"`
}
// BillingGroupListResponse is the response from Aiven from a list of billing groups.
BillingGroupListResponse struct {
APIResponse
BillingGroupList []BillingGroup `json:"billing_groups"`
}
// BillingGroupProjectsResponse is the response from Aiven for the billing group projects
BillingGroupProjectsResponse struct {
APIResponse
Projects []BillingGroupProject `json:"projects,omitempty"`
}
// BillingGroupProject is assigned billing group project response
BillingGroupProject struct {
AvailableCredits string `json:"available_credits"`
EstimatedBalance string `json:"estimated_balance"`
ProjectName string `json:"project_name"`
}
)
// ListAll retrieves a list of all billing groups
func (h *BillingGroupHandler) ListAll() ([]BillingGroup, error) {
bts, err := h.client.doGetRequest(buildPath("billing-group"), nil)
if err != nil {
return nil, err
}
var r BillingGroupListResponse
errR := checkAPIResponse(bts, &r)
return r.BillingGroupList, errR
}
// Create creates a new project.
func (h *BillingGroupHandler) Create(req BillingGroupRequest) (*BillingGroup, error) {
bts, err := h.client.doPostRequest(buildPath("billing-group"), req)
if err != nil {
return nil, err
}
var r BillingGroupResponse
errR := checkAPIResponse(bts, &r)
return r.BillingGroup, errR
}
// Get returns gets the specified billing group.
func (h *BillingGroupHandler) Get(id string) (*BillingGroup, error) {
bts, err := h.client.doGetRequest(buildPath("billing-group", id), nil)
if err != nil {
return nil, err
}
var r BillingGroupResponse
errR := checkAPIResponse(bts, &r)
return r.BillingGroup, errR
}
// Update modifies the specified billing group with the given parameters.
func (h *BillingGroupHandler) Update(id string, req BillingGroupRequest) (*BillingGroup, error) {
bts, err := h.client.doPutRequest(buildPath("billing-group", id), req)
if err != nil {
return nil, err
}
var r BillingGroupResponse
errR := checkAPIResponse(bts, &r)
return r.BillingGroup, errR
}
// Delete removes the given billing group.
func (h *BillingGroupHandler) Delete(id string) error {
bts, err := h.client.doDeleteRequest(buildPath("billing-group", id), nil)
if err != nil {
return err
}
return checkAPIResponse(bts, nil)
}
// AssignProjects assigns projects to the billing group
func (h *BillingGroupHandler) AssignProjects(id string, projects []string) error {
req := struct {
ProjectsNames []string `json:"projects_names"`
}{
ProjectsNames: projects,
}
bts, err := h.client.doPostRequest(buildPath("billing-group", id, "projects-assign"), req)
if err != nil {
return err
}
return checkAPIResponse(bts, nil)
}
// GetProjects retrieves a list of assigned projects
func (h *BillingGroupHandler) GetProjects(id string) ([]string, error) {
r := new(BillingGroupProjectsResponse)
bts, err := h.client.doGetRequest(buildPath("billing-group", id, "projects"), nil)
if err != nil {
return nil, err
}
errR := checkAPIResponse(bts, r)
if errR != nil {
return nil, errR
}
var projects []string
for _, p := range r.Projects {
projects = append(projects, p.ProjectName)
}
return projects, nil
}