-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.go
314 lines (274 loc) · 9.04 KB
/
project.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package client
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/adrianosela/padl/api/project"
"github.com/adrianosela/padl/api/payloads"
"github.com/adrianosela/padl/lib/padlfile"
)
// CreateProject creates a new project and returns a signed padlfile
func (p *Padl) CreateProject(name, description string, bits int) (*padlfile.File, error) {
pl := &payloads.NewProjectRequest{
Name: name,
Description: description,
KeyBits: bits,
}
plBytes, err := json.Marshal(&pl)
if err != nil {
return nil, fmt.Errorf("could not marshall payload: %s", err)
}
req, err := http.NewRequest(http.MethodPost,
fmt.Sprintf("%s/project", p.HostURL),
bytes.NewBuffer(plBytes))
if err != nil {
return nil, fmt.Errorf("could not build http requests: %s", err)
}
p.setAuth(req)
resp, err := p.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("could not send http request: %s", err)
}
respByt, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("could not read http response body: %s", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error: %s", string(respByt))
}
var pf padlfile.File
if err := json.Unmarshal(respByt, &pf); err != nil {
return nil, fmt.Errorf("could not unmarshal http response body: %s", err)
}
return &pf, nil
}
// CreateServiceAccount Creates a new service account
func (p *Padl) CreateServiceAccount(projectName, accountName, pubKeyPEM string) (*payloads.CreateServiceAccountResponse, error) {
plBytes, err := json.Marshal(&payloads.CreateServiceAccountRequest{
ServiceAccountName: accountName,
PubKey: pubKeyPEM,
})
if err != nil {
return nil, fmt.Errorf("could not marshall payload: %s", err)
}
req, err := http.NewRequest(
http.MethodPost,
fmt.Sprintf("%s/project/%s/service_account", p.HostURL, projectName),
bytes.NewBuffer(plBytes))
if err != nil {
return nil, fmt.Errorf("could not build http requests: %s", err)
}
p.setAuth(req)
resp, err := p.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("could not send http request: %s", err)
}
respByt, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("could not read http response body: %s", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error: %s", string(respByt))
}
var createKeyResp payloads.CreateServiceAccountResponse
if err := json.Unmarshal(respByt, &createKeyResp); err != nil {
return nil, fmt.Errorf("could not unmarshal http response body: %s", err)
}
return &createKeyResp, nil
}
// RemoveServiceAccount Removes service account
func (p *Padl) RemoveServiceAccount(projectName string, keyName string) error {
pl := &payloads.DeleteServiceAccountRequest{
ServiceAccountName: keyName,
}
plBytes, err := json.Marshal(&pl)
if err != nil {
return fmt.Errorf("could not marshall payload: %s", err)
}
req, err := http.NewRequest(http.MethodDelete,
fmt.Sprintf("%s/project/%s/service_account", p.HostURL, projectName),
bytes.NewBuffer(plBytes))
if err != nil {
return fmt.Errorf("could not build http requests: %s", err)
}
p.setAuth(req)
resp, err := p.HTTPClient.Do(req)
if err != nil {
return fmt.Errorf("could not send http request: %s", err)
}
respByt, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return fmt.Errorf("could not read http response body: %s", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error: %s", string(respByt))
}
return nil
}
// GetProject gets a project by name if the requesting user has access to it
func (p *Padl) GetProject(name string) (*project.Project, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/project/%s", p.HostURL, name), nil)
if err != nil {
return nil, fmt.Errorf("could not build http request: %s", err)
}
p.setAuth(req)
resp, err := p.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("could not send http request: %s", err)
}
respByt, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("could not read http response body: %s", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error: %s", string(respByt))
}
var project project.Project
if err := json.Unmarshal(respByt, &project); err != nil {
return nil, fmt.Errorf("could not unmarshal http response body: %s", err)
}
return &project, nil
}
// GetProjectKeys gets a project's list of keys by project name, the request will
// fail if the authorization token in the client is not for a user in the project
func (p *Padl) GetProjectKeys(name string) (*payloads.GetProjectKeysReponse, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/project/%s/keys", p.HostURL, name), nil)
if err != nil {
return nil, fmt.Errorf("could not build http request: %s", err)
}
p.setAuth(req)
resp, err := p.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("could not send http request: %s", err)
}
respByt, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("could not read http response body: %s", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error: %s", string(respByt))
}
var keysResp payloads.GetProjectKeysReponse
if err := json.Unmarshal(respByt, &keysResp); err != nil {
return nil, fmt.Errorf("could not unmarshal http response body: %s", err)
}
return &keysResp, nil
}
// ListProjects lists the users padl projects
func (p *Padl) ListProjects() (*payloads.ListProjectsResponse, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/projects", p.HostURL), nil)
if err != nil {
return nil, fmt.Errorf("could not build http requests: %s", err)
}
p.setAuth(req)
resp, err := p.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("could not send http request: %s", err)
}
respByt, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("could not read http response body: %s", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error: %s", string(respByt))
}
var listProjResp payloads.ListProjectsResponse
if err := json.Unmarshal(respByt, &listProjResp); err != nil {
return nil, fmt.Errorf("could not unmarshal http response body: %s", err)
}
return &listProjResp, nil
}
// AddUserToProject adds another user to a project with given privilege permission
func (p *Padl) AddUserToProject(projectName string, email string, privilegeLvl int) error {
pl := &payloads.AddUserToProjectRequest{
Email: email,
PrivilegeLvl: privilegeLvl,
}
plBytes, err := json.Marshal(&pl)
if err != nil {
return fmt.Errorf("could not marshall payload: %s", err)
}
req, err := http.NewRequest(
http.MethodPost,
fmt.Sprintf("%s/project/%s/user", p.HostURL, projectName),
bytes.NewBuffer(plBytes))
if err != nil {
return fmt.Errorf("could not build http requests: %s", err)
}
p.setAuth(req)
resp, err := p.HTTPClient.Do(req)
if err != nil {
return fmt.Errorf("could not send http request: %s", err)
}
respByt, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return fmt.Errorf("could not read http response body: %s", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error: %s", string(respByt))
}
return nil
}
// RemoveUserFromProject removes another user from the project
// fails if the current user does not have owner privilege or if an owner tries to remove themselves
func (p *Padl) RemoveUserFromProject(projectName string, email string) error {
pl := &payloads.RemoveUserFromProjectRequest{
Email: email,
}
plBytes, err := json.Marshal(&pl)
if err != nil {
return fmt.Errorf("could not marshall payload: %s", err)
}
req, err := http.NewRequest(
http.MethodDelete,
fmt.Sprintf("%s/project/%s/user", p.HostURL, projectName),
bytes.NewBuffer(plBytes))
if err != nil {
return fmt.Errorf("could not build http requests: %s", err)
}
p.setAuth(req)
resp, err := p.HTTPClient.Do(req)
if err != nil {
return fmt.Errorf("could not send http request: %s", err)
}
respByt, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return fmt.Errorf("could not read http response body: %s", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error: %s", string(respByt))
}
return nil
}
// DeleteProject deletes a project from the padl server
// falis if the user does not have owner privileges
func (p *Padl) DeleteProject(projectName string) error {
req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/project/%s", p.HostURL, projectName), nil)
if err != nil {
return fmt.Errorf("could not build http requests: %s", err)
}
p.setAuth(req)
resp, err := p.HTTPClient.Do(req)
if err != nil {
return fmt.Errorf("could not send http request: %s", err)
}
respByt, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return fmt.Errorf("could not read http response body: %s", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error: %s", string(respByt))
}
return nil
}