-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworkspaces.go
More file actions
98 lines (80 loc) · 2.51 KB
/
workspaces.go
File metadata and controls
98 lines (80 loc) · 2.51 KB
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
// Package workspaces provides methods for managing controller workspaces.
package workspaces
import (
"encoding/json"
"fmt"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
)
// List lists workspaces visible to the current user.
func List(c *drycc.Client, results int) (api.Workspaces, int, error) {
body, count, reqErr := c.LimitedRequest("/v2/workspaces", results)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return []api.Workspace{}, -1, reqErr
}
var workspaces []api.Workspace
if err := json.Unmarshal([]byte(body), &workspaces); err != nil {
return []api.Workspace{}, -1, err
}
return workspaces, count, reqErr
}
// Create creates a workspace.
func Create(c *drycc.Client, name, email string) (api.Workspace, error) {
req := api.WorkspaceCreateRequest{Name: name, Email: email}
body, err := json.Marshal(req)
if err != nil {
return api.Workspace{}, err
}
res, reqErr := c.Request("POST", "/v2/workspaces", body)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return api.Workspace{}, reqErr
}
defer res.Body.Close()
workspace := api.Workspace{}
if err = json.NewDecoder(res.Body).Decode(&workspace); err != nil {
return api.Workspace{}, err
}
return workspace, reqErr
}
// Get fetches a workspace by name.
func Get(c *drycc.Client, name string) (api.Workspace, error) {
u := fmt.Sprintf("/v2/workspaces/%s", name)
res, reqErr := c.Request("GET", u, nil)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return api.Workspace{}, reqErr
}
defer res.Body.Close()
workspace := api.Workspace{}
if err := json.NewDecoder(res.Body).Decode(&workspace); err != nil {
return api.Workspace{}, err
}
return workspace, reqErr
}
// Update updates workspace attributes.
func Update(c *drycc.Client, name, email string) (api.Workspace, error) {
u := fmt.Sprintf("/v2/workspaces/%s", name)
req := api.WorkspaceUpdateRequest{Email: email}
body, err := json.Marshal(req)
if err != nil {
return api.Workspace{}, err
}
res, reqErr := c.Request("PATCH", u, body)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return api.Workspace{}, reqErr
}
defer res.Body.Close()
workspace := api.Workspace{}
if err = json.NewDecoder(res.Body).Decode(&workspace); err != nil {
return api.Workspace{}, err
}
return workspace, reqErr
}
// Delete removes a workspace.
func Delete(c *drycc.Client, name string) error {
u := fmt.Sprintf("/v2/workspaces/%s", name)
res, err := c.Request("DELETE", u, nil)
if err == nil {
res.Body.Close()
}
return err
}