-
Notifications
You must be signed in to change notification settings - Fork 1
/
windmill.go
192 lines (178 loc) · 4.54 KB
/
windmill.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
package windmill
import (
"context"
"errors"
"fmt"
"math"
"math/rand"
"net/http"
"os"
"github.com/google/uuid"
api "github.com/windmill-labs/windmill-go-client/api"
)
type ClientWithWorkspace struct {
Client *api.ClientWithResponses
Workspace string
}
func GetClient() (ClientWithWorkspace, error) {
base_url := os.Getenv("BASE_INTERNAL_URL") + "/api"
workspace := os.Getenv("WM_WORKSPACE")
token := os.Getenv("WM_TOKEN")
client, _ := api.NewClientWithResponses(base_url, func(c *api.Client) error {
c.RequestEditors = append(c.RequestEditors, func(ctx context.Context, req *http.Request) error {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
return nil
})
return nil
})
return ClientWithWorkspace{
Client: client,
Workspace: workspace,
}, nil
}
func newBool(b bool) *bool {
return &b
}
func GetVariable(path string) (string, error) {
client, err := GetClient()
if err != nil {
return "", err
}
res, err := client.Client.GetVariableValueWithResponse(context.Background(), client.Workspace, path)
if err != nil {
return "", err
}
if res.StatusCode()/100 != 2 {
return "", errors.New(string(res.Body))
}
return *res.JSON200, nil
}
func GetResource(path string) (interface{}, error) {
client, err := GetClient()
if err != nil {
return nil, err
}
params := api.GetResourceValueInterpolatedParams{}
res, err := client.Client.GetResourceValueInterpolatedWithResponse(context.Background(), client.Workspace, path, ¶ms)
if err != nil {
return nil, err
}
if res.StatusCode()/100 != 2 {
return nil, errors.New(string(res.Body))
}
return *res.JSON200, nil
}
func SetResource(path string, value interface{}, resourceTypeOpt ...string) error {
client, err := GetClient()
if err != nil {
return err
}
params := api.GetResourceValueInterpolatedParams{}
getRes, getErr := client.Client.GetResourceValueInterpolatedWithResponse(context.Background(), client.Workspace, path, ¶ms)
if getErr != nil {
return getErr
}
if getRes.StatusCode() == 404 {
resourceType := "any"
if len(resourceTypeOpt) > 0 {
resourceType = resourceTypeOpt[0]
}
res, err := client.Client.CreateResourceWithResponse(context.Background(), client.Workspace, &api.CreateResourceParams{
UpdateIfExists: newBool(true),
}, api.CreateResource{Value: &value, Path: path, ResourceType: resourceType})
if err != nil {
return err
}
if res.StatusCode()/100 != 2 {
return errors.New(string(res.Body))
}
} else {
res, err := client.Client.UpdateResourceValueWithResponse(context.Background(), client.Workspace, path, api.UpdateResourceValueJSONRequestBody{
Value: &value,
})
if err != nil {
return err
}
if res.StatusCode()/100 != 2 {
return errors.New(string(res.Body))
}
}
return nil
}
func SetVariable(path string, value string) error {
client, err := GetClient()
if err != nil {
return err
}
f := false
res, err := client.Client.UpdateVariableWithResponse(context.Background(), client.Workspace, path, &api.UpdateVariableParams{AlreadyEncrypted: &f}, api.EditVariable{Value: &value})
if err != nil {
f = true
}
if res.StatusCode()/100 != 2 {
f = true
}
if f {
res, err := client.Client.CreateVariableWithResponse(context.Background(), client.Workspace, &api.CreateVariableParams{},
api.CreateVariableJSONRequestBody{
Path: path,
Value: value,
})
if err != nil {
return err
}
if res.StatusCode()/100 != 2 {
return errors.New(string(res.Body))
}
}
return nil
}
func GetStatePath() string {
value := os.Getenv("WM_STATE_PATH_NEW")
if len(value) == 0 {
return os.Getenv("WM_STATE_PATH")
}
return value
}
func GetState() (interface{}, error) {
return GetResource(GetStatePath())
}
func SetState(state interface{}) error {
err := SetResource(GetStatePath(), state)
if err != nil {
return err
}
return nil
}
type ResumeUrls struct {
ApprovalPage string `json:"approvalPage"`
Cancel string `json:"cancel"`
Resume string `json:"resume"`
}
func GetResumeUrls(approver string) (ResumeUrls, error) {
var urls ResumeUrls
client, err := GetClient()
if err != nil {
return urls, err
}
jobId, err := uuid.Parse(os.Getenv("WM_JOB_ID"))
if err != nil {
return urls, err
}
params := api.GetResumeUrlsParams{Approver: &approver}
nonce := rand.Intn(int(math.MaxUint32))
res, err := client.Client.GetResumeUrlsWithResponse(context.Background(),
client.Workspace,
jobId,
nonce,
¶ms,
)
if err != nil {
return urls, err
}
if res.StatusCode()/100 != 2 {
return urls, errors.New(string(res.Body))
}
urls = *res.JSON200
return urls, nil
}