11package windmill
22
33import (
4- "context"
5- "errors"
6- "fmt"
7- "math"
8- "math/rand"
9- "net/http"
104 "os"
11- "github.com/google/uuid"
12- api "github.com/windmill-labs/windmill-go-client/api"
135)
146
15- type ClientWithWorkspace struct {
16- Client * api.ClientWithResponses
17- Workspace string
18- }
19-
207func GetClient () (ClientWithWorkspace , error ) {
218 base_url := os .Getenv ("BASE_INTERNAL_URL" ) + "/api"
229 workspace := os .Getenv ("WM_WORKSPACE" )
2310 token := os .Getenv ("WM_TOKEN" )
2411
25- client , _ := api .NewClientWithResponses (base_url , func (c * api.Client ) error {
26- c .RequestEditors = append (c .RequestEditors , func (ctx context.Context , req * http.Request ) error {
27- req .Header .Add ("Authorization" , fmt .Sprintf ("Bearer %s" , token ))
28- return nil
29- })
30- return nil
31- })
32- return ClientWithWorkspace {
33- Client : client ,
34- Workspace : workspace ,
35- }, nil
12+ return NewClient (base_url , token , workspace )
3613}
3714func newBool (b bool ) * bool {
3815 return & b
@@ -43,98 +20,31 @@ func GetVariable(path string) (string, error) {
4320 if err != nil {
4421 return "" , err
4522 }
46- res , err := client .Client .GetVariableValueWithResponse (context .Background (), client .Workspace , path )
47- if err != nil {
48- return "" , err
49- }
50- if res .StatusCode ()/ 100 != 2 {
51- return "" , errors .New (string (res .Body ))
52- }
53- return * res .JSON200 , nil
23+ return client .GetVariable (path )
5424}
5525
5626func GetResource (path string ) (interface {}, error ) {
5727 client , err := GetClient ()
5828 if err != nil {
5929 return nil , err
6030 }
61- params := api.GetResourceValueInterpolatedParams {}
62- res , err := client .Client .GetResourceValueInterpolatedWithResponse (context .Background (), client .Workspace , path , & params )
63- if err != nil {
64- return nil , err
65- }
66- if res .StatusCode ()/ 100 != 2 {
67- return nil , errors .New (string (res .Body ))
68- }
69- return * res .JSON200 , nil
31+ return client .GetResource (path )
7032}
7133
7234func SetResource (path string , value interface {}, resourceTypeOpt ... string ) error {
7335 client , err := GetClient ()
7436 if err != nil {
7537 return err
7638 }
77- params := api.GetResourceValueInterpolatedParams {}
78- getRes , getErr := client .Client .GetResourceValueInterpolatedWithResponse (context .Background (), client .Workspace , path , & params )
79- if getErr != nil {
80- return getErr
81- }
82- if getRes .StatusCode () == 404 {
83- resourceType := "any"
84- if len (resourceTypeOpt ) > 0 {
85- resourceType = resourceTypeOpt [0 ]
86- }
87- res , err := client .Client .CreateResourceWithResponse (context .Background (), client .Workspace , & api.CreateResourceParams {
88- UpdateIfExists : newBool (true ),
89- }, api.CreateResource {Value : & value , Path : path , ResourceType : resourceType })
90- if err != nil {
91- return err
92- }
93- if res .StatusCode ()/ 100 != 2 {
94- return errors .New (string (res .Body ))
95- }
96- } else {
97- res , err := client .Client .UpdateResourceValueWithResponse (context .Background (), client .Workspace , path , api.UpdateResourceValueJSONRequestBody {
98- Value : & value ,
99- })
100- if err != nil {
101- return err
102- }
103- if res .StatusCode ()/ 100 != 2 {
104- return errors .New (string (res .Body ))
105- }
106- }
107- return nil
39+ return client .SetResource (path , value , resourceTypeOpt ... )
10840}
10941
11042func SetVariable (path string , value string ) error {
11143 client , err := GetClient ()
11244 if err != nil {
11345 return err
11446 }
115- f := false
116- res , err := client .Client .UpdateVariableWithResponse (context .Background (), client .Workspace , path , & api.UpdateVariableParams {AlreadyEncrypted : & f }, api.EditVariable {Value : & value })
117- if err != nil {
118- f = true
119- }
120- if res .StatusCode ()/ 100 != 2 {
121- f = true
122- }
123- if f {
124- res , err := client .Client .CreateVariableWithResponse (context .Background (), client .Workspace , & api.CreateVariableParams {},
125- api.CreateVariableJSONRequestBody {
126- Path : path ,
127- Value : value ,
128- })
129-
130- if err != nil {
131- return err
132- }
133- if res .StatusCode ()/ 100 != 2 {
134- return errors .New (string (res .Body ))
135- }
136- }
137- return nil
47+ return client .SetVariable (path , value )
13848}
13949
14050func GetStatePath () string {
@@ -164,29 +74,9 @@ type ResumeUrls struct {
16474}
16575
16676func GetResumeUrls (approver string ) (ResumeUrls , error ) {
167- var urls ResumeUrls
16877 client , err := GetClient ()
16978 if err != nil {
170- return urls , err
171- }
172- jobId , err := uuid .Parse (os .Getenv ("WM_JOB_ID" ))
173- if err != nil {
174- return urls , err
175- }
176- params := api.GetResumeUrlsParams {Approver : & approver }
177- nonce := rand .Intn (int (math .MaxUint32 ))
178- res , err := client .Client .GetResumeUrlsWithResponse (context .Background (),
179- client .Workspace ,
180- jobId ,
181- nonce ,
182- & params ,
183- )
184- if err != nil {
185- return urls , err
186- }
187- if res .StatusCode ()/ 100 != 2 {
188- return urls , errors .New (string (res .Body ))
79+ return ResumeUrls {}, err
18980 }
190- urls = * res .JSON200
191- return urls , nil
81+ return client .GetResumeUrls (approver )
19282}
0 commit comments