forked from aiven/aiven-go-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.go
98 lines (81 loc) · 2.71 KB
/
database.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
// Copyright (c) 2017 jelmersnoeck
// Copyright (c) 2018 Aiven, Helsinki, Finland. https://aiven.io/
package aiven
import (
"fmt"
)
type (
// Database represents a database type on Aiven.
Database struct {
DatabaseName string `json:"database_name"`
LcCollate string `json:"lc_collate,omitempty"`
LcType string `json:"lc_ctype,omitempty"`
}
// DatabasesHandler is the client which interacts with the Aiven database
// endpoints.
DatabasesHandler struct {
client *Client
}
// CreateDatabaseRequest contains the parameters used to create a database.
CreateDatabaseRequest struct {
Database string `json:"database"`
LcCollate string `json:"lc_collate,omitempty"`
LcType string `json:"lc_ctype,omitempty"`
}
// DatabaseListResponse represents the response from Aiven for listing
// databases.
DatabaseListResponse struct {
APIResponse
Databases []*Database `json:"databases"`
}
)
// Create creates a database with the given parameters.
func (h *DatabasesHandler) Create(project, service string, req CreateDatabaseRequest) (*Database, error) {
path := buildPath("project", project, "service", service, "db")
bts, err := h.client.doPostRequest(path, req)
if err != nil {
return nil, err
}
errR := checkAPIResponse(bts, nil)
if errR != nil {
return nil, err
}
db := Database{DatabaseName: req.Database, LcCollate: req.LcCollate, LcType: req.LcType}
return &db, nil
}
// Get returns a specific database from Aiven.
func (h *DatabasesHandler) Get(projectName, serviceName, databaseName string) (*Database, error) {
// There's no API for getting database by name. List all databases and pick the correct one
// instead. (There typically aren't that many databases, 100 is already very large number)
databases, err := h.List(projectName, serviceName)
if err != nil {
return nil, err
}
for _, database := range databases {
if database.DatabaseName == databaseName {
return database, nil
}
}
err = Error{Message: fmt.Sprintf("Database with name %v not found", databaseName), Status: 404}
return nil, err
}
// Delete removes the specified database.
func (h *DatabasesHandler) Delete(project, service, database string) error {
path := buildPath("project", project, "service", service, "db", database)
bts, err := h.client.doDeleteRequest(path, nil)
if err != nil {
return err
}
return checkAPIResponse(bts, nil)
}
// List will return all the databases for a given service.
func (h *DatabasesHandler) List(project, service string) ([]*Database, error) {
path := buildPath("project", project, "service", service, "db")
rsp, err := h.client.doGetRequest(path, nil)
if err != nil {
return nil, err
}
var r DatabaseListResponse
errR := checkAPIResponse(rsp, &r)
return r.Databases, errR
}