forked from hashicorp/atlas-go
/
workspaces.go
147 lines (130 loc) · 3.62 KB
/
workspaces.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
package tfe
import (
"bytes"
"fmt"
"log"
"reflect"
"github.com/korfuri/jsonapi"
)
func (c *Client) listWorkspaces(organization string, page int) ([]*Workspace, bool, error) {
ro := &RequestOptions{
Params: map[string]string{
"sort": "name",
"page[size]": "20",
"page[number]": fmt.Sprintf("%d", page),
},
}
request, err := c.NewRequest("GET", fmt.Sprintf("/organizations/%s/workspaces", organization), ro)
if err != nil {
return nil, false, err
}
response, err := CheckResp(c.HTTPClient.Do(request))
if err != nil {
return nil, false, err
}
objs, err := jsonapi.UnmarshalManyPayload(response.Body, reflect.TypeOf(new(Workspace)))
if err != nil {
return nil, false, err
}
workspaces := make([]*Workspace, len(objs))
for i, o := range objs {
w, ok := o.(*Workspace)
if !ok {
return nil, false, fmt.Errorf("Invalid type during unmarshaling, data was %v", o)
}
workspaces[i] = w
}
more := len(workspaces) >= 20
return workspaces, more, nil
}
func (c *Client) GetWorkspaceByID(organization string, workspaceId string) (*Workspace, error) {
page := 1
for more := true; more; {
workspaces, evenmore, err := c.listWorkspaces(organization, page)
if err != nil {
return nil, err
}
for _, w := range workspaces {
if w.ID == workspaceId {
return w, nil
}
}
more = evenmore
page = page + 1
}
return nil, ErrNotFound
}
func (c *Client) GetWorkspaceByName(organization string, workspaceName string) (*Workspace, error) {
request, err := c.NewRequest("GET", fmt.Sprintf("/organizations/%s/workspaces/%s", organization, workspaceName), nil)
if err != nil {
return nil, err
}
response, err := CheckResp(c.HTTPClient.Do(request))
if err != nil {
return nil, err
}
out_ws := new(Workspace)
if err := jsonapi.UnmarshalPayload(response.Body, out_ws); err != nil {
return nil, err
}
return out_ws, nil
}
// CreateWorkspace creates a workspace.
func (c *Client) CreateWorkspace(organization string, workspace *Workspace) (*Workspace, error) {
buf := new(bytes.Buffer)
if err := jsonapi.MarshalOnePayloadEmbedded(buf, workspace); err != nil {
return nil, err
}
ro := &RequestOptions{
Body: buf,
}
log.Printf("[DEBUG] Request body: %s", buf.String())
request, err := c.NewRequest("POST", fmt.Sprintf("/organizations/%s/workspaces", organization), ro)
if err != nil {
return nil, err
}
response, err := CheckResp(c.HTTPClient.Do(request))
if err != nil {
return nil, err
}
out_workspace := new(Workspace)
if err := jsonapi.UnmarshalPayload(response.Body, out_workspace); err != nil {
return nil, err
}
return out_workspace, nil
}
func (c *Client) DeleteWorkspace(organization string, workspaceName string) error {
request, err := c.NewRequest("DELETE", fmt.Sprintf("/organizations/%s/workspaces/%s", organization, workspaceName), nil)
if err != nil {
return err
}
_, err = CheckResp(c.HTTPClient.Do(request))
if err != nil {
return err
}
return nil
}
func (c *Client) UpdateWorkspace(organization string, w *Workspace) (*Workspace, error) {
buf := new(bytes.Buffer)
if err := jsonapi.MarshalOnePayloadEmbedded(buf, w); err != nil {
return nil, err
}
ro := &RequestOptions{
Body: buf,
}
log.Printf("[DEBUG] Request body: %s", buf.String())
name := w.Name
request, err := c.NewRequest("PATCH", fmt.Sprintf("/organizations/%s/workspaces/%s", organization, name), ro)
if err != nil {
return nil, err
}
response, err := CheckResp(c.HTTPClient.Do(request))
if err != nil {
return nil, err
}
out_workspace := new(Workspace)
if err := jsonapi.UnmarshalPayload(response.Body, out_workspace); err != nil {
return nil, err
}
return out_workspace, nil
}