-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathcerts.go
More file actions
74 lines (55 loc) · 1.43 KB
/
certs.go
File metadata and controls
74 lines (55 loc) · 1.43 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
package certs
import (
"encoding/json"
"errors"
"fmt"
"github.com/deis/deis/client-go/controller/api"
"github.com/deis/deis/client-go/controller/client"
)
// List certs registered with the controller.
func List(c *client.Client) ([]api.Cert, error) {
body, status, err := c.BasicRequest("GET", "/v1/certs/", nil)
if err != nil {
return []api.Cert{}, err
}
if status != 200 {
return []api.Cert{}, errors.New(body)
}
res := api.Certs{}
if err = json.Unmarshal([]byte(body), &res); err != nil {
return []api.Cert{}, err
}
return res.Certs, nil
}
// New creates a new cert.
func New(c *client.Client, cert string, key string, commonName string) (api.Cert, error) {
req := api.CertCreateRequest{Certificate: cert, Key: key, Name: commonName}
reqBody, err := json.Marshal(req)
if err != nil {
return api.Cert{}, err
}
resBody, status, err := c.BasicRequest("POST", "/v1/certs/", reqBody)
if err != nil {
return api.Cert{}, err
}
if status != 201 {
return api.Cert{}, errors.New(resBody)
}
resCert := api.Cert{}
if err = json.Unmarshal([]byte(resBody), &resCert); err != nil {
return api.Cert{}, err
}
return resCert, nil
}
// Delete removes a cert.
func Delete(c *client.Client, commonName string) error {
u := fmt.Sprintf("/v1/certs/%s", commonName)
resBody, status, err := c.BasicRequest("DELETE", u, nil)
if err != nil {
return err
}
if status != 204 {
return errors.New(resBody)
}
return nil
}