forked from plouc/go-gitlab-client
/
ssh_keys.go
130 lines (95 loc) · 3.19 KB
/
ssh_keys.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
package gitlab
import (
"encoding/json"
"io"
"net/url"
"strconv"
)
const (
CurrentUserSshKeysApiPath = "/user/keys"
UserSshKeysApiPath = "/users/:id/keys"
CurrentUserSshKeyApiPath = "/user/keys/:key_id"
UserSshKeyApiPath = "/user/:id/keys/:key_id"
)
type SshKey struct {
Id int `json:"id,omitempty" yaml:"id,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Key string `json:"key,omitempty" yaml:"key,omitempty"`
CreatedAtRaw string `json:"created_at,omitempty" yaml:"created_at,omitempty"`
}
type SshKeyCollection struct {
Items []*SshKey
}
func (s *SshKey) RenderJson(w io.Writer) error {
return renderJson(w, s)
}
func (s *SshKey) RenderYaml(w io.Writer) error {
return renderYaml(w, s)
}
func (c *SshKeyCollection) RenderJson(w io.Writer) error {
return renderJson(w, c.Items)
}
func (c *SshKeyCollection) RenderYaml(w io.Writer) error {
return renderYaml(w, c.Items)
}
func (g *Gitlab) getSshKeys(u *url.URL) (*SshKeyCollection, *ResponseMeta, error) {
collection := new(SshKeyCollection)
contents, meta, err := g.buildAndExecRequest("GET", u.String(), nil)
if err == nil {
err = json.Unmarshal(contents, &collection.Items)
}
return collection, meta, err
}
func (g *Gitlab) UserSshKeys(userId int, o *PaginationOptions) (*SshKeyCollection, *ResponseMeta, error) {
u := g.ResourceUrlQ(UserSshKeysApiPath, map[string]string{
":id": strconv.Itoa(userId),
}, o)
return g.getSshKeys(u)
}
func (g *Gitlab) CurrentUserSshKeys(o *PaginationOptions) (*SshKeyCollection, *ResponseMeta, error) {
u := g.ResourceUrlQ(CurrentUserSshKeysApiPath, nil, o)
return g.getSshKeys(u)
}
func (g *Gitlab) CurrentUserSshKey(id string) (*SshKey, *ResponseMeta, error) {
u := g.ResourceUrl(CurrentUserSshKeyApiPath, map[string]string{":id": id})
var key *SshKey
contents, meta, err := g.buildAndExecRequest("GET", u.String(), nil)
if err == nil {
err = json.Unmarshal(contents, &key)
}
return key, meta, err
}
func (g *Gitlab) addSshKey(u *url.URL, title, key string) (*ResponseMeta, error) {
var err error
v := url.Values{}
v.Set("title", title)
v.Set("key", key)
body := v.Encode()
_, meta, err := g.buildAndExecRequest("POST", u.String(), []byte(body))
return meta, err
}
func (g *Gitlab) AddUserSshKey(userId, title, key string) (*ResponseMeta, error) {
u := g.ResourceUrl(UserSshKeysApiPath, map[string]string{":id": userId})
return g.addSshKey(u, title, key)
}
func (g *Gitlab) AddCurrentUserSshKey(title, key string) (*ResponseMeta, error) {
u := g.ResourceUrl(CurrentUserSshKeysApiPath, nil)
return g.addSshKey(u, title, key)
}
func (g *Gitlab) deleteSshKey(u *url.URL) (*ResponseMeta, error) {
_, meta, err := g.buildAndExecRequest("DELETE", u.String(), nil)
return meta, err
}
func (g *Gitlab) DeleteCurrentUserSshKey(keyId int) (*ResponseMeta, error) {
u := g.ResourceUrl(CurrentUserSshKeyApiPath, map[string]string{
":key_id": strconv.Itoa(keyId),
})
return g.deleteSshKey(u)
}
func (g *Gitlab) DeleteUserSshKey(userId, keyId int) (*ResponseMeta, error) {
u := g.ResourceUrl(UserSshKeyApiPath, map[string]string{
":id": strconv.Itoa(userId),
":key_id": strconv.Itoa(keyId),
})
return g.deleteSshKey(u)
}