-
Notifications
You must be signed in to change notification settings - Fork 13
/
work_pools.go
190 lines (148 loc) · 5.03 KB
/
work_pools.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
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/google/uuid"
"github.com/prefecthq/terraform-provider-prefect/internal/api"
)
var _ = api.WorkPoolsClient(&WorkPoolsClient{})
// WorkPoolsClient is a client for working with work pools.
type WorkPoolsClient struct {
hc *http.Client
apiKey string
routePrefix string
}
// WorkPools returns a WorkPoolsClient.
//
//nolint:ireturn // required to support PrefectClient mocking
func (c *Client) WorkPools(accountID uuid.UUID, workspaceID uuid.UUID) (api.WorkPoolsClient, error) {
if accountID == uuid.Nil {
accountID = c.defaultAccountID
}
if workspaceID == uuid.Nil {
workspaceID = c.defaultWorkspaceID
}
return &WorkPoolsClient{
hc: c.hc,
apiKey: c.apiKey,
routePrefix: getWorkspaceScopedURL(c.endpoint, accountID, workspaceID, "work_pools"),
}, nil
}
// Create returns details for a new work pool.
func (c *WorkPoolsClient) Create(ctx context.Context, data api.WorkPoolCreate) (*api.WorkPool, error) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(&data); err != nil {
return nil, fmt.Errorf("failed to encode data: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.routePrefix+"/", &buf)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
setDefaultHeaders(req, c.apiKey)
resp, err := c.hc.Do(req)
if err != nil {
return nil, fmt.Errorf("http error: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
errorBody, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("status code %s, error=%s", resp.Status, errorBody)
}
var pool api.WorkPool
if err := json.NewDecoder(resp.Body).Decode(&pool); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &pool, nil
}
// List returns a list of work pools matching filter criteria.
func (c *WorkPoolsClient) List(ctx context.Context, filter api.WorkPoolFilter) ([]*api.WorkPool, error) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(&filter); err != nil {
return nil, fmt.Errorf("failed to encode filter: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.routePrefix+"/filter", &buf)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
setDefaultHeaders(req, c.apiKey)
resp, err := c.hc.Do(req)
if err != nil {
return nil, fmt.Errorf("http error: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
errorBody, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("status code %s, error=%s", resp.Status, errorBody)
}
var pools []*api.WorkPool
if err := json.NewDecoder(resp.Body).Decode(&pools); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return pools, nil
}
// Get returns details for a work pool by name.
func (c *WorkPoolsClient) Get(ctx context.Context, name string) (*api.WorkPool, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.routePrefix+"/"+name, http.NoBody)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
setDefaultHeaders(req, c.apiKey)
resp, err := c.hc.Do(req)
if err != nil {
return nil, fmt.Errorf("http error: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
errorBody, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("status code %s, error=%s", resp.Status, errorBody)
}
var pool api.WorkPool
if err := json.NewDecoder(resp.Body).Decode(&pool); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &pool, nil
}
// Update modifies an existing work pool by name.
func (c *WorkPoolsClient) Update(ctx context.Context, name string, data api.WorkPoolUpdate) error {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(&data); err != nil {
return fmt.Errorf("failed to encode data: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, c.routePrefix+"/"+name, &buf)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}
setDefaultHeaders(req, c.apiKey)
resp, err := c.hc.Do(req)
if err != nil {
return fmt.Errorf("http error: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
errorBody, _ := io.ReadAll(resp.Body)
return fmt.Errorf("status code %s, error=%s", resp.Status, errorBody)
}
return nil
}
// Delete removes a work pool by name.
func (c *WorkPoolsClient) Delete(ctx context.Context, name string) error {
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, c.routePrefix+"/"+name, http.NoBody)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}
setDefaultHeaders(req, c.apiKey)
resp, err := c.hc.Do(req)
if err != nil {
return fmt.Errorf("http error: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
errorBody, _ := io.ReadAll(resp.Body)
return fmt.Errorf("status code %s, error=%s", resp.Status, errorBody)
}
return nil
}