-
Notifications
You must be signed in to change notification settings - Fork 29
/
project.go
344 lines (293 loc) · 13.2 KB
/
project.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package aiven
import (
"context"
"time"
)
type (
// Project represents the Project model on Aiven.
Project struct {
AvailableCredits string `json:"available_credits"`
BillingAddress string `json:"billing_address"`
BillingEmails []*ContactEmail `json:"billing_emails"`
BillingExtraText string `json:"billing_extra_text"`
Card Card `json:"card_info"`
Country string `json:"country"`
CountryCode string `json:"country_code"`
DefaultCloud string `json:"default_cloud"`
EstimatedBalance string `json:"estimated_balance"`
PaymentMethod string `json:"payment_method"`
Name string `json:"project_name"`
TechnicalEmails []*ContactEmail `json:"tech_emails"`
VatID string `json:"vat_id"`
AccountId string `json:"account_id"`
BillingCurrency string `json:"billing_currency"`
CopyFromProject string `json:"copy_from_project"`
BillingGroupId string `json:"billing_group_id"`
BillingGroupName string `json:"billing_group_name"`
Tags map[string]string `json:"tags"`
OrganizationId string `json:"organization_id"`
}
// ProjectsHandler is the client which interacts with the Projects endpoints
// on Aiven.
ProjectsHandler struct {
client *Client
}
// CreateProjectRequest are the parameters for creating a project.
CreateProjectRequest struct {
BillingAddress *string `json:"billing_address,omitempty"`
BillingEmails *[]*ContactEmail `json:"billing_emails,omitempty"`
BillingExtraText *string `json:"billing_extra_text,omitempty"`
CardID *string `json:"card_id,omitempty"`
Cloud *string `json:"cloud,omitempty"`
CopyFromProject string `json:"copy_from_project,omitempty"`
CountryCode *string `json:"country_code,omitempty"`
Project string `json:"project"`
AccountId *string `json:"account_id,omitempty"`
TechnicalEmails *[]*ContactEmail `json:"tech_emails,omitempty"`
BillingCurrency string `json:"billing_currency,omitempty"`
VatID *string `json:"vat_id,omitempty"`
UseSourceProjectBillingGroup bool `json:"use_source_project_billing_group,omitempty"`
BillingGroupId string `json:"billing_group_id,omitempty"`
AddAccountOwnersAdminAccess *bool `json:"add_account_owners_admin_access,omitempty"`
Tags map[string]string `json:"tags"`
}
// UpdateProjectRequest are the parameters for updating a project.
UpdateProjectRequest struct {
Name string `json:"project_name,omitempty"`
BillingAddress *string `json:"billing_address,omitempty"`
BillingEmails *[]*ContactEmail `json:"billing_emails,omitempty"`
BillingExtraText *string `json:"billing_extra_text,omitempty"`
CardID *string `json:"card_id,omitempty"`
Cloud *string `json:"cloud,omitempty"`
CountryCode *string `json:"country_code,omitempty"`
AccountId string `json:"account_id"`
TechnicalEmails *[]*ContactEmail `json:"tech_emails,omitempty"`
BillingCurrency string `json:"billing_currency,omitempty"`
VatID *string `json:"vat_id,omitempty"`
Tags map[string]string `json:"tags"`
AddAccountOwnersAdminAccess *bool `json:"add_account_owners_admin_access,omitempty"`
}
// ContactEmail represents either a technical contact or billing contact.
ContactEmail struct {
Email string `json:"email"`
}
// ProjectResponse is the response from Aiven for the project endpoints.
ProjectResponse struct {
APIResponse
Project *Project `json:"project"`
}
// ProjectListResponse is the response from Aiven for listing projects.
ProjectListResponse struct {
APIResponse
Projects []*Project `json:"projects"`
}
// ProjectEventLogEntriesResponse is the response from Aiven for project event log entries
ProjectEventLogEntriesResponse struct {
APIResponse
Events []*ProjectEvent `json:"events"`
}
// ProjectEvent represents a project event log entry
ProjectEvent struct {
Actor string `json:"actor"`
EventDesc string `json:"event_desc"`
EventType string `json:"event_type"`
ID string `json:"id"`
ServiceName string `json:"service_name"`
Time *time.Time `json:"time"`
}
// ProjectServiceTypesResponse is the response from Aiven for listing project service types.
ProjectServiceTypesResponse struct {
APIResponse
ServiceTypes map[string]ServiceType `json:"service_types"`
}
// ServiceType represents a service type.
ServiceType struct {
DefaultVersion string `json:"default_version"`
Description string `json:"description"`
LatestAvailableVersion string `json:"latest_available_version"`
ServicePlans []ServicePlan `json:"service_plans"`
UserConfigSchema UserConfigSchema `json:"user_config_schema"`
}
// ServicePlan represents a service plan.
ServicePlan struct {
BackupConfig *BackupConfig `json:"backup_config"`
MaxMemoryPercent int `json:"max_memory_percent"`
NodeCount int `json:"node_count"`
Regions interface{} `json:"regions"`
ServicePlan string `json:"service_plan"`
ServiceType string `json:"service_type"`
}
// BackupConfig represents a backup config.
BackupConfig struct {
FrequentIntervalMinutes int `json:"frequent_interval_minutes"`
FrequentOldestAgeMinutes int `json:"frequent_oldest_age_minutes"`
InfrequentIntervalMinutes int `json:"infrequent_interval_minutes"`
InfrequentOldestAgeMinutes int `json:"infrequent_oldest_age_minutes"`
Interval int `json:"interval"`
MaxCount int `json:"max_count"`
RecoveryMode string `json:"recovery_mode"`
}
// ProjectIntegrationTypesResponse is the response from Aiven for listing project integration types.
ProjectIntegrationTypesResponse struct {
APIResponse
IntegrationTypes []IntegrationType `json:"integration_types"`
}
// IntegrationType represents an integration type.
IntegrationType struct {
DestDescription string `json:"dest_description"`
DestServiceTypes []string `json:"dest_service_types"`
IntegrationType string `json:"integration_type"`
SourceDescription string `json:"source_description"`
SourceServiceTypes []string `json:"source_service_types"`
UserConfigSchema UserConfigSchema `json:"user_config_schema"`
}
// ProjectIntegrationEndpointTypesResponse is the response from Aiven for listing project integration endpoint types.
ProjectIntegrationEndpointTypesResponse struct {
APIResponse
EndpointTypes []IntegrationEndpointType `json:"endpoint_types"`
}
// IntegrationEndpointType represents an integration endpoint type.
IntegrationEndpointType struct {
EndpointType string `json:"endpoint_type"`
ServiceTypes []string `json:"service_types"`
Title string `json:"title"`
UserConfigSchema UserConfigSchema `json:"user_config_schema"`
}
// UserConfigSchema represents a user config schema.
UserConfigSchema struct {
Title string `json:"title"`
Description string `json:"description"`
Type interface{} `json:"type"`
Default interface{} `json:"default"`
Required []string `json:"required"`
Properties map[string]UserConfigSchema `json:"properties"`
AdditionalProperties interface{} `json:"additionalProperties"`
Items *UserConfigSchema `json:"items"`
OneOf []UserConfigSchema `json:"oneOf"`
Enum []interface{} `json:"enum"`
Minimum *float64 `json:"minimum"`
Maximum *float64 `json:"maximum"`
MinLength *int `json:"minLength"`
MaxLength *int `json:"maxLength"`
MaxItems *int `json:"maxItems"`
CreateOnly bool `json:"createOnly"`
Pattern string `json:"pattern"`
Example interface{} `json:"example"`
UserError string `json:"user_error"`
Secure bool `json:"_secure"`
}
)
// ContactEmailFromStringSlice creates []*ContactEmail from string slice
func ContactEmailFromStringSlice(emails []string) *[]*ContactEmail {
var result []*ContactEmail
for _, e := range emails {
result = append(result, &ContactEmail{
Email: e,
})
}
return &result
}
// emailsToStringSlice converts contact emails to string slice
func emailsToStringSlice(c []*ContactEmail) []string {
var result []string
for _, e := range c {
result = append(result, e.Email)
}
return result
}
// GetBillingEmailsAsStringSlice retrieves BillingEmails converted to string slice
func (p Project) GetBillingEmailsAsStringSlice() []string {
return emailsToStringSlice(p.BillingEmails)
}
// GetTechnicalEmailsAsStringSlice retrieves TechnicalEmails converted to string slice
func (p Project) GetTechnicalEmailsAsStringSlice() []string {
return emailsToStringSlice(p.TechnicalEmails)
}
// Create creates a new project.
func (h *ProjectsHandler) Create(ctx context.Context, req CreateProjectRequest) (*Project, error) {
bts, err := h.client.doPostRequest(ctx, buildPath("project"), req)
if err != nil {
return nil, err
}
var r ProjectResponse
errR := checkAPIResponse(bts, &r)
return r.Project, errR
}
// Get returns gets the specified project.
func (h *ProjectsHandler) Get(ctx context.Context, project string) (*Project, error) {
bts, err := h.client.doGetRequest(ctx, buildPath("project", project), nil)
if err != nil {
return nil, err
}
var r ProjectResponse
errR := checkAPIResponse(bts, &r)
return r.Project, errR
}
// Update modifies the specified project with the given parameters.
func (h *ProjectsHandler) Update(ctx context.Context, project string, req UpdateProjectRequest) (*Project, error) {
bts, err := h.client.doPutRequest(ctx, buildPath("project", project), req)
if err != nil {
return nil, err
}
var r ProjectResponse
errR := checkAPIResponse(bts, &r)
return r.Project, errR
}
// Delete removes the given project.
func (h *ProjectsHandler) Delete(ctx context.Context, project string) error {
bts, err := h.client.doDeleteRequest(ctx, buildPath("project", project), nil)
if err != nil {
return err
}
return checkAPIResponse(bts, nil)
}
// List returns all the available projects linked to the account.
func (h *ProjectsHandler) List(ctx context.Context) ([]*Project, error) {
bts, err := h.client.doGetRequest(ctx, buildPath("project"), nil)
if err != nil {
return nil, err
}
var r ProjectListResponse
errR := checkAPIResponse(bts, &r)
return r.Projects, errR
}
// GetEventLog returns project event log entries
func (h *ProjectsHandler) GetEventLog(ctx context.Context, project string) ([]*ProjectEvent, error) {
bts, err := h.client.doGetRequest(ctx, buildPath("project", project, "events"), nil)
if err != nil {
return nil, err
}
var r ProjectEventLogEntriesResponse
errR := checkAPIResponse(bts, &r)
return r.Events, errR
}
// ServiceTypes returns all the available service types.
func (h *ProjectsHandler) ServiceTypes(ctx context.Context, project string) (map[string]ServiceType, error) {
bts, err := h.client.doGetRequest(ctx, buildPath("project", project, "service_types"), nil)
if err != nil {
return nil, err
}
var r ProjectServiceTypesResponse
err = checkAPIResponse(bts, &r)
return r.ServiceTypes, err
}
// IntegrationTypes returns all the available integration types.
func (h *ProjectsHandler) IntegrationTypes(ctx context.Context, project string) ([]IntegrationType, error) {
bts, err := h.client.doGetRequest(ctx, buildPath("project", project, "integration_types"), nil)
if err != nil {
return nil, err
}
var r ProjectIntegrationTypesResponse
err = checkAPIResponse(bts, &r)
return r.IntegrationTypes, err
}
// IntegrationEndpointTypes returns all the available integration endpoint types.
func (h *ProjectsHandler) IntegrationEndpointTypes(ctx context.Context, project string) ([]IntegrationEndpointType, error) {
bts, err := h.client.doGetRequest(ctx, buildPath("project", project, "integration_endpoint_types"), nil)
if err != nil {
return nil, err
}
var r ProjectIntegrationEndpointTypesResponse
err = checkAPIResponse(bts, &r)
return r.EndpointTypes, err
}