/
api_system_agent_profiles.go
271 lines (245 loc) · 8.05 KB
/
api_system_agent_profiles.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
package apstra
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)
const (
apiUrlSystemAgentProfiles = "/api/system-agent-profiles"
apiUrlSystemAgentProfilesById = apiUrlSystemAgentProfiles + apiUrlPathDelim + "%s"
apiUrlSystemAgentProfilesAssignById = apiUrlSystemAgentProfiles + apiUrlPathDelim + "%s" + "/assign"
apstraAgentPlatformJunos = "junos"
apstraAgentPlatformEOS = "eos"
apstraAgentPlatformNXOS = "nxos"
apstraSystemAgentPlatformStringSep = "=="
apstraErrAgentProfileInUse = "Profile is in use"
)
type optionsAgentProfilesResponse struct {
Items []ObjectId `json:"items"`
Methods []string `json:"methods"`
}
type AssignAgentProfileRequest struct {
SystemAgents []ObjectId `json:"system_agents"`
ProfileId ObjectId `json:"profile_id"`
ClearPackages bool `json:"clear_packages"`
ClearOpenOptions bool `json:"clear_open_options"`
}
// AgentProfileConfig is used when creating or updating an Agent Profile
type AgentProfileConfig struct {
Label string
Username *string
Password *string
Platform *string
Packages AgentPackages
OpenOptions map[string]string
}
// raw turns a *AgentProfile (from our caller) into a rawAgentProfile
func (o *AgentProfileConfig) raw() *rawAgentProfileConfig {
result := &rawAgentProfileConfig{
Label: o.Label,
Username: o.Username,
Password: o.Password,
Platform: o.Platform,
Packages: o.Packages.raw(),
OpenOptions: o.OpenOptions,
}
if result.OpenOptions == nil { // this would result in 'null' in JSON payload
result.OpenOptions = make(map[string]string) // send '{}' instead
}
return result
}
// rawAgentProfileConfig is the nasty type expected by the API. Element
// Packages is really a map, but k,v are string-joined with "==" here.
type rawAgentProfileConfig struct {
Label string `json:"label,omitempty"`
Username *string `json:"username,omitempty"`
Password *string `json:"password,omitempty"`
Platform *string `json:"platform,omitempty"`
Packages rawAgentPackages `json:"packages,omitempty"`
OpenOptions map[string]string `json:"open_options,omitempty"`
}
type getAgentProfilesResponse struct {
Items []rawAgentProfile `json:"items"`
}
// AgentProfile describes an Agent Profile to our callers.
// It has the Packages element presented sensibly as a map.
type AgentProfile struct {
Label string `json:"label"`
HasUsername bool `json:"has_username"`
HasPassword bool `json:"has_password"`
Platform string `json:"platform"`
Packages AgentPackages `json:"packages"`
Id ObjectId `json:"id"`
OpenOptions map[string]string `json:"open_options"`
}
// HasCredentials returns true if the AgentProfile has both username and password configured.
func (o *AgentProfile) HasCredentials() bool {
return o.HasUsername && o.HasPassword
}
// rawAgentProfile represents the API's description of an Agent Profile.
// The Packages element is really a map, but has k, v string-joined with "==".
type rawAgentProfile struct {
Label string `json:"label"`
HasUsername bool `json:"has_username"`
HasPassword bool `json:"has_password"`
Platform string `json:"platform"`
Packages rawAgentPackages `json:"packages"`
Id ObjectId `json:"id"`
OpenOptions map[string]string `json:"open_options"`
}
// polish turns a rawAgentProfile (from the API) into a AgentProfile
func (o *rawAgentProfile) polish() *AgentProfile {
return &AgentProfile{
Label: o.Label,
HasUsername: o.HasUsername,
HasPassword: o.HasPassword,
Platform: o.Platform,
Packages: o.Packages.polish(),
Id: o.Id,
OpenOptions: o.OpenOptions,
}
}
func (o *Client) listAgentProfileIds(ctx context.Context) ([]ObjectId, error) {
response := &optionsAgentProfilesResponse{}
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodOptions,
urlStr: apiUrlSystemAgentProfiles,
apiResponse: response,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
return response.Items, nil
}
func (o *Client) createAgentProfile(ctx context.Context, in *AgentProfileConfig) (ObjectId, error) {
response := &objectIdResponse{}
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodPost,
urlStr: apiUrlSystemAgentProfiles,
apiInput: in.raw(),
apiResponse: response,
})
if err != nil {
var ttae TalkToApstraErr
if errors.As(err, &ttae) && ttae.Response.StatusCode == http.StatusConflict {
return "", ClientErr{
errType: ErrConflict,
err: fmt.Errorf("error Agent Profile '%s' likely already exists - %w", in.Label, err),
}
}
return "", convertTtaeToAceWherePossible(err)
}
return response.Id, nil
}
func (o *Client) getAgentProfile(ctx context.Context, id ObjectId) (*AgentProfile, error) {
response := &rawAgentProfile{}
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: fmt.Sprintf(apiUrlSystemAgentProfilesById, id),
apiResponse: response,
})
if err != nil {
var ttae TalkToApstraErr
if errors.As(err, &ttae) && ttae.Response.StatusCode == http.StatusNotFound {
return nil, ClientErr{
errType: ErrNotfound,
err: err,
}
}
return nil, convertTtaeToAceWherePossible(err)
}
return response.polish(), nil
}
func (o *Client) getAllAgentProfiles(ctx context.Context) ([]AgentProfile, error) {
response := &getAgentProfilesResponse{}
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: apiUrlSystemAgentProfiles,
apiResponse: response,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
var out []AgentProfile
for _, sap := range response.Items {
out = append(out, *sap.polish())
}
return out, nil
}
func (o *Client) updateAgentProfile(ctx context.Context, id ObjectId, in *AgentProfileConfig) error {
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodPatch,
urlStr: fmt.Sprintf(apiUrlSystemAgentProfilesById, id),
apiInput: in.raw(),
})
if err != nil {
return convertTtaeToAceWherePossible(err)
}
return nil
}
func (o *Client) deleteAgentProfile(ctx context.Context, id ObjectId) error {
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodDelete,
urlStr: fmt.Sprintf(apiUrlSystemAgentProfilesById, id),
})
if err != nil {
var ttae TalkToApstraErr
if errors.As(err, &ttae) && ttae.Response.StatusCode == http.StatusUnprocessableEntity {
body, _ := io.ReadAll(ttae.Response.Body)
var ae apstraErr
_ = json.Unmarshal(body, &apstraErr{})
if ae.Errors == apstraErrAgentProfileInUse {
return ClientErr{
errType: ErrInUse,
err: fmt.Errorf("agent profile '%s' is in use, cannot delete", id),
}
}
}
return convertTtaeToAceWherePossible(err)
}
return nil
}
func (o *Client) getAgentProfileByLabel(ctx context.Context, label string) (*AgentProfile, error) {
response := &getAgentProfilesResponse{}
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodGet,
urlStr: apiUrlSystemAgentProfiles,
apiResponse: response,
})
if err != nil {
return nil, convertTtaeToAceWherePossible(err)
}
found := -1 //slice index where the matching System Agent Profile can be found
for i, sap := range response.Items {
if sap.Label == label {
if found >= 0 {
return nil, ClientErr{
errType: ErrMultipleMatch,
err: fmt.Errorf("multiple matches for System Agent Profile with label '%s'", label),
}
}
found = i
}
}
if found < 0 {
return nil, ClientErr{
errType: ErrNotfound,
err: fmt.Errorf("no System Agent Profile with label '%s' found", label),
}
}
return response.Items[found].polish(), nil
}
func (o *Client) assignAgentProfile(ctx context.Context, req *AssignAgentProfileRequest) error {
err := o.talkToApstra(ctx, &talkToApstraIn{
method: http.MethodPost,
urlStr: fmt.Sprintf(apiUrlSystemAgentProfilesAssignById, req.ProfileId),
apiInput: req,
})
if err != nil {
return convertTtaeToAceWherePossible(err)
}
return nil
}