-
Notifications
You must be signed in to change notification settings - Fork 300
/
functions.go
236 lines (205 loc) · 7.59 KB
/
functions.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
package godo
import (
"context"
"fmt"
"net/http"
"time"
)
const (
functionsBasePath = "/v2/functions/namespaces"
functionsNamespacePath = functionsBasePath + "/%s"
functionsTriggerBasePath = functionsNamespacePath + "/triggers"
)
type FunctionsService interface {
ListNamespaces(context.Context) ([]FunctionsNamespace, *Response, error)
GetNamespace(context.Context, string) (*FunctionsNamespace, *Response, error)
CreateNamespace(context.Context, *FunctionsNamespaceCreateRequest) (*FunctionsNamespace, *Response, error)
DeleteNamespace(context.Context, string) (*Response, error)
ListTriggers(context.Context, string) ([]FunctionsTrigger, *Response, error)
GetTrigger(context.Context, string, string) (*FunctionsTrigger, *Response, error)
CreateTrigger(context.Context, string, *FunctionsTriggerCreateRequest) (*FunctionsTrigger, *Response, error)
UpdateTrigger(context.Context, string, string, *FunctionsTriggerUpdateRequest) (*FunctionsTrigger, *Response, error)
DeleteTrigger(context.Context, string, string) (*Response, error)
}
type FunctionsServiceOp struct {
client *Client
}
var _ FunctionsService = &FunctionsServiceOp{}
type namespacesRoot struct {
Namespaces []FunctionsNamespace `json:"namespaces,omitempty"`
}
type namespaceRoot struct {
Namespace *FunctionsNamespace `json:"namespace,omitempty"`
}
type FunctionsNamespace struct {
ApiHost string `json:"api_host,omitempty"`
Namespace string `json:"namespace,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
Label string `json:"label,omitempty"`
Region string `json:"region,omitempty"`
UUID string `json:"uuid,omitempty"`
Key string `json:"key,omitempty"`
}
type FunctionsNamespaceCreateRequest struct {
Label string `json:"label"`
Region string `json:"region"`
}
type triggersRoot struct {
Triggers []FunctionsTrigger `json:"triggers,omitempty"`
}
type triggerRoot struct {
Trigger *FunctionsTrigger `json:"trigger,omitempty"`
}
type FunctionsTrigger struct {
Namespace string `json:"namespace,omitempty"`
Function string `json:"function,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
IsEnabled bool `json:"is_enabled"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
ScheduledDetails *TriggerScheduledDetails `json:"scheduled_details,omitempty"`
ScheduledRuns *TriggerScheduledRuns `json:"scheduled_runs,omitempty"`
}
type TriggerScheduledDetails struct {
Cron string `json:"cron,omitempty"`
Body map[string]interface{} `json:"body,omitempty"`
}
type TriggerScheduledRuns struct {
LastRunAt time.Time `json:"last_run_at,omitempty"`
NextRunAt time.Time `json:"next_run_at,omitempty"`
}
type FunctionsTriggerCreateRequest struct {
Name string `json:"name"`
Type string `json:"type"`
Function string `json:"function"`
IsEnabled bool `json:"is_enabled"`
ScheduledDetails *TriggerScheduledDetails `json:"scheduled_details,omitempty"`
}
type FunctionsTriggerUpdateRequest struct {
IsEnabled *bool `json:"is_enabled,omitempty"`
ScheduledDetails *TriggerScheduledDetails `json:"scheduled_details,omitempty"`
}
// Gets a list of namespaces
func (s *FunctionsServiceOp) ListNamespaces(ctx context.Context) ([]FunctionsNamespace, *Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, functionsBasePath, nil)
if err != nil {
return nil, nil, err
}
nsRoot := new(namespacesRoot)
resp, err := s.client.Do(ctx, req, nsRoot)
if err != nil {
return nil, resp, err
}
return nsRoot.Namespaces, resp, nil
}
// Gets a single namespace
func (s *FunctionsServiceOp) GetNamespace(ctx context.Context, namespace string) (*FunctionsNamespace, *Response, error) {
path := fmt.Sprintf(functionsNamespacePath, namespace)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
nsRoot := new(namespaceRoot)
resp, err := s.client.Do(ctx, req, nsRoot)
if err != nil {
return nil, resp, err
}
return nsRoot.Namespace, resp, nil
}
// Creates a namespace
func (s *FunctionsServiceOp) CreateNamespace(ctx context.Context, opts *FunctionsNamespaceCreateRequest) (*FunctionsNamespace, *Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodPost, functionsBasePath, opts)
if err != nil {
return nil, nil, err
}
nsRoot := new(namespaceRoot)
resp, err := s.client.Do(ctx, req, nsRoot)
if err != nil {
return nil, resp, err
}
return nsRoot.Namespace, resp, nil
}
// Delete a namespace
func (s *FunctionsServiceOp) DeleteNamespace(ctx context.Context, namespace string) (*Response, error) {
path := fmt.Sprintf(functionsNamespacePath, namespace)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// ListTriggers gets a list of triggers
func (s *FunctionsServiceOp) ListTriggers(ctx context.Context, namespace string) ([]FunctionsTrigger, *Response, error) {
path := fmt.Sprintf(functionsTriggerBasePath, namespace)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(triggersRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Triggers, resp, nil
}
// GetTrigger gets a single trigger
func (s *FunctionsServiceOp) GetTrigger(ctx context.Context, namespace string, trigger string) (*FunctionsTrigger, *Response, error) {
path := fmt.Sprintf(functionsTriggerBasePath+"/%s", namespace, trigger)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(triggerRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Trigger, resp, nil
}
// CreateTrigger creates a trigger
func (s *FunctionsServiceOp) CreateTrigger(ctx context.Context, namespace string, opts *FunctionsTriggerCreateRequest) (*FunctionsTrigger, *Response, error) {
path := fmt.Sprintf(functionsTriggerBasePath, namespace)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, opts)
if err != nil {
return nil, nil, err
}
root := new(triggerRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Trigger, resp, nil
}
// UpdateTrigger updates a trigger
func (s *FunctionsServiceOp) UpdateTrigger(ctx context.Context, namespace string, trigger string, opts *FunctionsTriggerUpdateRequest) (*FunctionsTrigger, *Response, error) {
path := fmt.Sprintf(functionsTriggerBasePath+"/%s", namespace, trigger)
req, err := s.client.NewRequest(ctx, http.MethodPut, path, opts)
if err != nil {
return nil, nil, err
}
root := new(triggerRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Trigger, resp, nil
}
// DeleteTrigger deletes a trigger
func (s *FunctionsServiceOp) DeleteTrigger(ctx context.Context, namespace string, trigger string) (*Response, error) {
path := fmt.Sprintf(functionsTriggerBasePath+"/%s", namespace, trigger)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}