generated from keboola/template
/
workspaces_api.go
88 lines (74 loc) · 2.16 KB
/
workspaces_api.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
package keboola
import (
"context"
"sync"
"github.com/hashicorp/go-multierror"
"github.com/keboola/go-client/pkg/request"
)
type WorkspaceID string
func (v WorkspaceID) String() string {
return string(v)
}
type Workspace struct {
ID WorkspaceID `json:"id"`
Type string `json:"type"`
Size string `json:"size"` // Only exists for container workspaces (Python, R)
Active bool `json:"active"`
Shared bool `json:"shared"`
User string `json:"user"`
Host string `json:"host"`
URL string `json:"url"`
Password string `json:"password"`
Created WorkspacesTime `json:"createdTimestamp"`
Updated WorkspacesTime `json:"updatedTimestamp"`
Start WorkspacesTime `json:"startTimestamp"`
// Workspace details - only exists for Snowflake workspaces
Details *WorkspaceDetails `json:"workspaceDetails"`
}
type WorkspaceDetails struct {
Connection struct {
Database string `json:"database"`
Schema string `json:"schema"`
Warehouse string `json:"warehouse"`
} `json:"connection"`
}
func (a *AuthorizedAPI) GetWorkspaceInstanceRequest(workspaceID WorkspaceID) request.APIRequest[*Workspace] {
result := &Workspace{}
req := a.newRequest(WorkspacesAPI).
WithResult(&result).
WithGet("sandboxes/{sandboxId}").
AndPathParam("sandboxId", workspaceID.String())
return request.NewAPIRequest(result, req)
}
func (a *AuthorizedAPI) ListWorkspaceInstancesRequest() request.APIRequest[*[]*Workspace] {
result := make([]*Workspace, 0)
req := a.newRequest(WorkspacesAPI).
WithResult(&result).
WithGet("sandboxes")
return request.NewAPIRequest(&result, req)
}
func (a *AuthorizedAPI) CleanWorkspaceInstances(ctx context.Context) error {
instances, err := a.ListWorkspaceInstancesRequest().Send(ctx)
if err != nil {
return err
}
wg := &sync.WaitGroup{}
m := &sync.Mutex{}
for _, s := range *instances {
s := s
wg.Add(1)
go func() {
defer wg.Done()
if e := a.DeleteWorkspaceJobRequest(s.ID).SendOrErr(ctx); e != nil {
m.Lock()
defer m.Unlock()
err = multierror.Append(err, e)
}
}()
}
wg.Wait()
if err != nil {
return err
}
return nil
}