/
function.go
134 lines (109 loc) · 3.27 KB
/
function.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
package meroxa
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
const functionsBasePath = "/v1/functions"
type FunctionState string
// Enum values for FunctionState
const (
FunctionStatePending FunctionState = "pending"
FunctionStateRunning FunctionState = "running"
FunctionStateError FunctionState = "error"
)
type Function struct {
UUID string `json:"uuid"`
Name string `json:"name"`
InputStream string `json:"input_stream"`
OutputStream string `json:"output_stream"`
Image string `json:"image"`
Command []string `json:"command"`
Args []string `json:"args"`
EnvVars map[string]string `json:"env_vars"`
Status FunctionStatus `json:"status"`
Pipeline PipelineIdentifier `json:"pipeline"`
Logs string `json:"logs"` // CLI includes what's returned by GetFunctionLogs
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type FunctionStatus struct {
State FunctionState `json:"state"`
Details string `json:"details"`
}
type CreateFunctionInput struct {
Name string `json:"name"`
InputStream string `json:"input_stream"`
OutputStream string `json:"output_stream"`
Pipeline PipelineIdentifier `json:"pipeline"`
Image string `json:"image"`
Command []string `json:"command"`
Args []string `json:"args"`
EnvVars map[string]string `json:"env_vars"`
}
func (c *client) CreateFunction(ctx context.Context, input *CreateFunctionInput) (*Function, error) {
resp, err := c.MakeRequest(ctx, http.MethodPost, functionsBasePath, input, nil, nil)
if err != nil {
return nil, err
}
if err := handleAPIErrors(resp); err != nil {
return nil, err
}
var fun Function
if err := json.NewDecoder(resp.Body).Decode(&fun); err != nil {
return nil, err
}
return &fun, nil
}
func (c *client) GetFunction(ctx context.Context, nameOrUUID string) (*Function, error) {
path := fmt.Sprintf("%s/%s", functionsBasePath, nameOrUUID)
resp, err := c.MakeRequest(ctx, http.MethodGet, path, nil, nil, nil)
if err != nil {
return nil, err
}
err = handleAPIErrors(resp)
if err != nil {
return nil, err
}
var fun Function
err = json.NewDecoder(resp.Body).Decode(&fun)
if err != nil {
return nil, err
}
return &fun, nil
}
func (c *client) ListFunctions(ctx context.Context) ([]*Function, error) {
resp, err := c.MakeRequest(ctx, http.MethodGet, functionsBasePath, nil, nil, nil)
if err != nil {
return nil, err
}
err = handleAPIErrors(resp)
if err != nil {
return nil, err
}
var funs []*Function
err = json.NewDecoder(resp.Body).Decode(&funs)
if err != nil {
return nil, err
}
return funs, nil
}
func (c *client) DeleteFunction(ctx context.Context, nameOrUUID string) (*Function, error) {
path := fmt.Sprintf("%s/%s", functionsBasePath, nameOrUUID)
resp, err := c.MakeRequest(ctx, http.MethodDelete, path, nil, nil, nil)
if err != nil {
return nil, err
}
err = handleAPIErrors(resp)
if err != nil {
return nil, err
}
var fun Function
err = json.NewDecoder(resp.Body).Decode(&fun)
if err != nil {
return nil, err
}
return &fun, nil
}