-
Notifications
You must be signed in to change notification settings - Fork 28
/
billing_group.go
281 lines (234 loc) · 9.58 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package aiven
import "context"
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"`
CopyFromBillingGroup *string `json:"copy_from_billing_group,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"`
}
// BillingGroupInvoice is the structure of the billing group invoice.
BillingGroupInvoice struct {
// BillingGroupId is the billing group ID.
BillingGroupId string `json:"billing_group_id"`
// BillingGroupName is the billing group name.
BillingGroupName string `json:"billing_group_name"`
// BillingGroupState is the billing group state.
BillingGroupState string `json:"billing_group_state"`
// Currency is the currency of the invoice.
Currency string `json:"currency"`
// DownloadCookie is the authentication cookie for downloading the invoice.
DownloadCookie string `json:"download_cookie"`
// GeneratedAt is the time when the invoice was generated.
GeneratedAt *string `json:"generated_at,omitempty"`
// InvoiceNumber is the invoice number.
InvoiceNumber string `json:"invoice_number"`
// PeriodBegin is the start of the billing period.
PeriodBegin string `json:"period_begin"`
// PeriodEnd is the end of the billing period.
PeriodEnd string `json:"period_end"`
// State is the state of the invoice.
State string `json:"state"`
// TotalIncVAT is the total amount including VAT.
TotalIncVAT string `json:"total_inc_vat"`
// TotalVAT is the total amount excluding VAT.
TotalVATZero string `json:"total_vat_zero"`
}
// BillingGroupListInvoicesResponse is the response from Aiven for the billing group invoices.
BillingGroupListInvoicesResponse struct {
APIResponse
// Invoices is the list of invoices.
Invoices []BillingGroupInvoice `json:"invoices"`
}
// BillingGroupInvoiceResponse is the response from Aiven for the billing group invoice.
BillingGroupInvoiceResponse struct {
APIResponse
// Invoice is the invoice.
Invoice BillingGroupInvoice `json:"invoice"`
}
// BillingGroupInvoiceLine is the structure of the billing group invoice line.
BillingGroupInvoiceLine struct {
// CloudName is the name of the cloud.
CloudName *string `json:"cloud_name,omitempty"`
// CommitmentName is the name of the commitment.
CommitmentName *string `json:"commitment_name,omitempty"`
// Description is the human-readable description of the line.
Description string `json:"description"`
// LinePreDiscountLocal is the line amount before discount in local currency.
LinePreDiscountLocal *string `json:"line_pre_discount_local,omitempty"`
// LineTotalLocal is the line total in local currency.
LineTotalLocal *string `json:"line_total_local,omitempty"`
// LineTotalUSD is the line total in USD.
LineTotalUSD string `json:"line_total_usd"`
// LineType is the type of the line.
LineType string `json:"line_type"`
// LocalCurrency is the local currency.
LocalCurrency *string `json:"local_currency,omitempty"`
// ProjectName is the name of the project.
ProjectName *string `json:"project_name,omitempty"`
// ServiceName is the name of the service.
ServiceName *string `json:"service_name,omitempty"`
// ServicePlan is the name of the service plan.
ServicePlan *string `json:"service_plan,omitempty"`
// ServiceType is the type of the service.
ServiceType *string `json:"service_type,omitempty"`
// Tags is the list of tags.
Tags map[string]string `json:"tags"`
// TimestampBegin is the start of the line.
TimestampBegin *string `json:"timestamp_begin,omitempty"`
// TimestampEnd is the end of the line.
TimestampEnd *string `json:"timestamp_end,omitempty"`
}
// BillingGroupListInvoiceLinesResponse is the response from Aiven for the billing group invoice lines.
BillingGroupListInvoiceLinesResponse struct {
APIResponse
// Lines is the list of invoice lines.
Lines []BillingGroupInvoiceLine `json:"lines"`
}
)
// ListAll retrieves a list of all billing groups
func (h *BillingGroupHandler) ListAll(ctx context.Context) ([]BillingGroup, error) {
bts, err := h.client.doGetRequest(ctx, 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(ctx context.Context, req BillingGroupRequest) (*BillingGroup, error) {
bts, err := h.client.doPostRequest(ctx, 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(ctx context.Context, id string) (*BillingGroup, error) {
bts, err := h.client.doGetRequest(ctx, 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(ctx context.Context, id string, req BillingGroupRequest) (*BillingGroup, error) {
bts, err := h.client.doPutRequest(ctx, 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(ctx context.Context, id string) error {
bts, err := h.client.doDeleteRequest(ctx, 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(ctx context.Context, id string, projects []string) error {
req := struct {
ProjectsNames []string `json:"projects_names"`
}{
ProjectsNames: projects,
}
bts, err := h.client.doPostRequest(ctx, 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(ctx context.Context, id string) ([]string, error) {
r := new(BillingGroupProjectsResponse)
bts, err := h.client.doGetRequest(ctx, 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
}
// ListInvoices lists invoices for the billing group.
func (h *BillingGroupHandler) ListInvoices(ctx context.Context, id string) (*BillingGroupListInvoicesResponse, error) {
bts, err := h.client.doGetRequest(ctx, buildPath("billing-group", id, "invoice"), nil)
if err != nil {
return nil, err
}
var r BillingGroupListInvoicesResponse
return &r, checkAPIResponse(bts, &r)
}
// GetInvoice gets the specified invoice for the billing group.
func (h *BillingGroupHandler) GetInvoice(ctx context.Context, id, invoiceNumber string) (*BillingGroupInvoiceResponse, error) {
bts, err := h.client.doGetRequest(ctx, buildPath("billing-group", id, "invoice", invoiceNumber), nil)
if err != nil {
return nil, err
}
var r BillingGroupInvoiceResponse
return &r, checkAPIResponse(bts, &r)
}
// ListLines lists invoice lines for the billing group's invoice.
func (h *BillingGroupHandler) ListLines(ctx context.Context, id, invoiceNumber string) (*BillingGroupListInvoiceLinesResponse, error) {
bts, err := h.client.doGetRequest(ctx, buildPath("billing-group", id, "invoice", invoiceNumber, "lines"), nil)
if err != nil {
return nil, err
}
var r BillingGroupListInvoiceLinesResponse
return &r, checkAPIResponse(bts, &r)
}