-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.go
122 lines (102 loc) · 3.42 KB
/
extension.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
package dbaas
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// Extension is the API response for the extension.
type Extension struct {
ID string `json:"id"`
ProjectID string `json:"project_id"`
AvailableExtensionID string `json:"available_extension_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
DatastoreID string `json:"datastore_id"`
DatabaseID string `json:"database_id"`
Status Status `json:"status"`
}
// ExtensionCreateOpts represents options for the extension Create request.
type ExtensionCreateOpts struct {
AvailableExtensionID string `json:"available_extension_id"`
DatastoreID string `json:"datastore_id"`
DatabaseID string `json:"database_id"`
}
// ExtensionQueryParams represents available query parameters for extension.
type ExtensionQueryParams struct {
ID string `json:"id,omitempty"`
ProjectID string `json:"project_id,omitempty"`
AvailableExtensionID string `json:"available_extension_id,omitempty"`
DatastoreID string `json:"datastore_id,omitempty"`
DatabaseID string `json:"database_id,omitempty"`
Status Status `json:"status,omitempty"`
}
const ExtensionsURI = "/extensions"
// Extensions returns all extensions.
func (api *API) Extensions(ctx context.Context, params *ExtensionQueryParams) ([]Extension, error) {
uri, err := setQueryParams(ExtensionsURI, params)
if err != nil {
return []Extension{}, err
}
resp, err := api.makeRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return []Extension{}, err
}
var result struct {
Extensions []Extension `json:"extensions"`
}
err = json.Unmarshal(resp, &result)
if err != nil {
return []Extension{}, fmt.Errorf("Error during Unmarshal, %w", err)
}
return result.Extensions, nil
}
// Extension returns a extension based on the ID.
func (api *API) Extension(ctx context.Context, extensionID string) (Extension, error) {
uri := fmt.Sprintf("%s/%s", ExtensionsURI, extensionID)
resp, err := api.makeRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return Extension{}, err
}
var result struct {
Extension Extension `json:"extension"`
}
err = json.Unmarshal(resp, &result)
if err != nil {
return Extension{}, fmt.Errorf("Error during Unmarshal, %w", err)
}
return result.Extension, nil
}
// CreateExtension creates a new extension.
func (api *API) CreateExtension(ctx context.Context, opts ExtensionCreateOpts) (Extension, error) {
createExtensionOpts := struct {
Extension ExtensionCreateOpts `json:"extension"`
}{
Extension: opts,
}
requestBody, err := json.Marshal(createExtensionOpts)
if err != nil {
return Extension{}, fmt.Errorf("Error marshalling params to JSON, %w", err)
}
resp, err := api.makeRequest(ctx, http.MethodPost, ExtensionsURI, requestBody)
if err != nil {
return Extension{}, err
}
var result struct {
Extension Extension `json:"extension"`
}
err = json.Unmarshal(resp, &result)
if err != nil {
return Extension{}, fmt.Errorf("Error during Unmarshal, %w", err)
}
return result.Extension, nil
}
// DeleteExtension deletes an existing extension.
func (api *API) DeleteExtension(ctx context.Context, extensionID string) error {
uri := fmt.Sprintf("%s/%s", ExtensionsURI, extensionID)
_, err := api.makeRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}