-
Notifications
You must be signed in to change notification settings - Fork 7
/
rest_sshuser.go
200 lines (183 loc) · 5.46 KB
/
rest_sshuser.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
package ne
import (
"fmt"
"net/http"
"net/url"
"github.com/equinix/ne-go/internal/api"
"github.com/equinix/rest-go"
)
const (
associateDevice = "ADD"
unassociateDevice = "DELETE"
)
type restSSHUserUpdateRequest struct {
uuid string
newPassword string
oldDevices []string
newDevices []string
c RestClient
}
//CreateSSHUser creates new Network Edge SSH user with a given parameters and returns its UUID upon successful creation
func (c RestClient) CreateSSHUser(username string, password string, device string) (*string, error) {
path := "/ne/v1/sshUsers"
reqBody := api.SSHUserRequest{
Username: &username,
Password: &password,
DeviceUUID: &device,
}
req := c.R().SetBody(&reqBody)
resp, err := c.Do(http.MethodPost, path, req)
if err != nil {
return nil, err
}
uuid, err := getResourceIDFromLocationHeader(resp)
if err != nil {
return nil, err
}
return uuid, nil
}
//GetSSHUsers retrieves list of all SSH users (with details)
func (c RestClient) GetSSHUsers() ([]SSHUser, error) {
path := "/ne/v1/sshUsers"
content, err := c.GetOffsetPaginated(path, &api.SSHUsersResponse{},
rest.DefaultOffsetPagingConfig().
SetAdditionalParams(map[string]string{"verbose": "true"}))
if err != nil {
return nil, err
}
transformed := make([]SSHUser, len(content))
for i := range content {
transformed[i] = *mapSSHUserAPIToDomain(content[i].(api.SSHUser))
}
return transformed, nil
}
//GetSSHUser fetches details of a SSH user with a given UUID
func (c RestClient) GetSSHUser(uuid string) (*SSHUser, error) {
path := "/ne/v1/sshUsers/" + url.PathEscape(uuid)
respBody := api.SSHUser{}
req := c.R().SetResult(&respBody)
if err := c.Execute(req, http.MethodGet, path); err != nil {
return nil, err
}
return mapSSHUserAPIToDomain(respBody), nil
}
//NewSSHUserUpdateRequest creates new composite update request for a user with a given UUID
func (c RestClient) NewSSHUserUpdateRequest(uuid string) SSHUserUpdateRequest {
return &restSSHUserUpdateRequest{
uuid: uuid,
c: c}
}
//DeleteSSHUser deletes ssh user with a given UUID
func (c RestClient) DeleteSSHUser(uuid string) error {
user, err := c.GetSSHUser(uuid)
if err != nil {
return err
}
updateErr := UpdateError{}
for _, dev := range user.DeviceUUIDs {
if err := c.changeDeviceAssociation(unassociateDevice, uuid, dev); err != nil {
updateErr.AddChangeError(changeTypeDelete, "devices", dev, err)
}
}
if updateErr.ChangeErrorsCount() > 0 {
return updateErr
}
return nil
}
func (req *restSSHUserUpdateRequest) WithNewPassword(password string) SSHUserUpdateRequest {
req.newPassword = password
return req
}
func (req *restSSHUserUpdateRequest) WithDeviceChange(old []string, new []string) SSHUserUpdateRequest {
req.oldDevices = old
req.newDevices = new
return req
}
func (req *restSSHUserUpdateRequest) Execute() error {
updateErr := UpdateError{}
if req.newPassword != "" {
if err := req.c.changeUserPassword(req.uuid, req.newPassword); err != nil {
updateErr.AddChangeError(changeTypeUpdate, "password", req.newPassword, err)
}
}
removed, added := diffStringSlices(req.oldDevices, req.newDevices)
for _, dev := range added {
if err := req.c.changeDeviceAssociation(associateDevice, req.uuid, dev); err != nil {
updateErr.AddChangeError(changeTypeCreate, "devices", dev, err)
}
}
for _, dev := range removed {
if err := req.c.changeDeviceAssociation(unassociateDevice, req.uuid, dev); err != nil {
updateErr.AddChangeError(changeTypeDelete, "devices", dev, err)
}
}
if updateErr.ChangeErrorsCount() > 0 {
return updateErr
}
return nil
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Unexported package methods
//_______________________________________________________________________
func (c RestClient) changeUserPassword(userID string, newPassword string) error {
path := "/ne/v1/sshUsers/" + url.PathEscape(userID)
reqBody := api.SSHUserUpdateRequest{Password: &newPassword}
req := c.R().SetBody(&reqBody)
if err := c.Execute(req, http.MethodPut, path); err != nil {
return err
}
return nil
}
func (c RestClient) changeDeviceAssociation(changeType string, userID string, deviceID string) error {
path := fmt.Sprintf("/ne/v1/sshUsers/%s/devices/%s",
url.PathEscape(userID), url.PathEscape(deviceID))
var method string
switch changeType {
case associateDevice:
method = http.MethodPost
case unassociateDevice:
method = http.MethodDelete
default:
return fmt.Errorf("unsupported association change type")
}
req := c.R().
//due to bug in NE API that requires content type and content len = 0 altough there is no content needed in any case
SetHeader("Content-Type", "application/json").
SetBody("{}")
if err := c.Execute(req, method, path); err != nil {
return err
}
return nil
}
func mapSSHUserAPIToDomain(apiUser api.SSHUser) *SSHUser {
return &SSHUser{
UUID: apiUser.UUID,
Username: apiUser.Username,
DeviceUUIDs: apiUser.DeviceUUIDs}
}
func diffStringSlices(a, b []string) (extraA, extraB []string) {
visited := make([]bool, len(b))
for i := range a {
found := false
for j := range b {
if visited[j] {
continue
}
if a[i] == b[j] {
visited[j] = true
found = true
break
}
}
if !found {
extraA = append(extraA, a[i])
}
}
for j := range b {
if visited[j] {
continue
}
extraB = append(extraB, b[j])
}
return
}