-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.go
342 lines (268 loc) · 7.82 KB
/
platform.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package scrapligocfg
import (
"fmt"
"os"
"github.com/scrapli/scrapligo/logging"
"github.com/scrapli/scrapligo/driver/network"
"github.com/scrapli/scrapligocfg/response"
"github.com/scrapli/scrapligocfg/util"
)
const (
// GetVersion is the name of the get version operation.
GetVersion = "GetVersion"
// GetConfig is the name of the get config operation.
GetConfig = "GetConfig"
// LoadConfig is the name of the load config operation.
LoadConfig = "LoadConfig"
// CommitConfig is the name of the commit config operation.
CommitConfig = "CommitConfig"
// AbortConfig is the name of the abort config operation.
AbortConfig = "AbortConfig"
)
// Platform defines the required methods that a scrapligocfg "platform" needs to implement.
type Platform interface {
GetVersion() (*response.PlatformResponse, error)
GetConfig(source string) (*response.PlatformResponse, error)
LoadConfig(
f, config string,
replace bool,
options *util.OperationOptions,
) (*response.PlatformResponse, error)
AbortConfig() (*response.PlatformResponse, error)
CommitConfig() (*response.PlatformResponse, error)
GetDeviceDiff(source string) (*response.PlatformResponse, error)
NormalizeConfig(config string) string
Cleanup() error
}
// WriteToFSPlatform defines additional Platform methods for those platforms that have candidate
// configurations written to the filesystem.
type WriteToFSPlatform interface {
Platform
SetFilesystem(s string)
SetSpaceAvailBuffPerc(f float32)
}
// Cfg is the primary point of interaction for scrapligocfg users -- this struct wraps the target
// Platform implementation and provides a consistent look and feel for users regardless of the
// underlying device type.
type Cfg struct {
Logger *logging.Instance
Impl Platform
Conn *network.Driver
OnPrepare func(*network.Driver) error
Dedicated bool
prepared bool
Candidate string
CandidateName string
CandidateTimestamp bool
}
func (c *Cfg) open() error {
if c.Conn.Transport.IsAlive() {
return nil
}
if c.Dedicated {
return c.Conn.Open()
}
return fmt.Errorf(
"%w: core connection not open and dedicated is false",
util.ErrConnectionError,
)
}
// Prepare opens the scrapligo Conn object and executes the optional OnPrepare function.
func (c *Cfg) Prepare() error {
err := c.open()
if err != nil {
return err
}
if c.OnPrepare != nil {
err = c.OnPrepare(c.Conn)
if err != nil {
return err
}
}
c.prepared = true
return nil
}
func (c *Cfg) close() error {
var err error
if c.Dedicated && c.Conn.Transport.IsAlive() {
err = c.Conn.Close()
}
return err
}
// Cleanup executes the Platform implementations Cleanup method and closes the scrapligo Conn.
func (c *Cfg) Cleanup() error {
err := c.Impl.Cleanup()
if err != nil {
return err
}
c.prepared = false
c.Candidate = ""
return c.close()
}
// GetVersion captures target device version information and stores it in the response.Response.
func (c *Cfg) GetVersion() (*response.Response, error) {
c.Logger.Info("GetVersion requested")
r := response.NewResponse(GetVersion, c.Conn.Transport.GetHost())
pr, err := c.Impl.GetVersion()
if err != nil {
return nil, err
}
r.Record(pr.ScrapliResponses, pr.Result)
return r, nil
}
// GetConfig fetches the source configuration from the target device -- the source is usually one
// of 'running', 'startup', or 'candidate', but valid options may vary from platform to platform.
func (c *Cfg) GetConfig(source string) (*response.Response, error) {
c.Logger.Infof("GetConfig requested, source '%s'", source)
if !c.prepared {
return nil, fmt.Errorf(
"%w: connection not prepared, must call `Prepare`",
util.ErrPrepareError,
)
}
r := response.NewResponse(GetConfig, c.Conn.Transport.GetHost())
pr, err := c.Impl.GetConfig(source)
if err != nil {
return nil, err
}
r.Record(pr.ScrapliResponses, pr.Result)
return r, nil
}
// LoadConfig loads a candidate configuration 'config' onto the target device. The replace argument
// is required -- when set to 'true' this means that scrapligocfg will load the provided candidate
// config in "replace" mode, which, when "committed" will fully replace the devices target config.
// If replace is false, the configuration will be loaded as a "merge" mode.
func (c *Cfg) LoadConfig(
config string,
replace bool,
opts ...util.Option,
) (*response.Response, error) {
c.Logger.Infof("LoadConfig requested, replace '%v'", replace)
if !c.prepared {
return nil, fmt.Errorf(
"%w: connection not prepared, must call `Prepare`",
util.ErrPrepareError,
)
}
if c.Candidate != "" {
return nil, fmt.Errorf(
"%w: candidate config already present, abort or commit before proceeding",
util.ErrCandidateError,
)
}
c.Candidate = config
r := response.NewResponse(LoadConfig, c.Conn.Transport.GetHost())
op, err := util.NewOperationOptions(opts...)
if err != nil {
return nil, err
}
pr, err := c.Impl.LoadConfig(
util.CreateCandidateConfigName(c.CandidateName, c.CandidateTimestamp),
config,
replace,
op,
)
if err != nil {
return nil, err
}
r.Record(pr.ScrapliResponses, pr.Result)
return r, nil
}
// LoadConfigFromFile is a convenience method wrapping LoadConfig -- this method accepts a filepath
// which will be read and passed to LoadConfig.
func (c *Cfg) LoadConfigFromFile(
f string,
replace bool,
opts ...util.Option,
) (*response.Response, error) {
b, err := os.ReadFile(f) //nolint: gosec
if err != nil {
return nil, err
}
return c.LoadConfig(string(b), replace, opts...)
}
// AbortConfig aborts a loaded candidate configuration.
func (c *Cfg) AbortConfig() (*response.Response, error) {
c.Logger.Info("AbortConfig requested")
if !c.prepared {
return nil, fmt.Errorf(
"%w: connection not prepared, must call `Prepare`",
util.ErrPrepareError,
)
}
if c.Candidate == "" {
return nil, fmt.Errorf(
"%w: cannot abort config, candidate config is not set",
util.ErrCandidateError,
)
}
r := response.NewResponse(AbortConfig, c.Conn.Transport.GetHost())
pr, err := c.Impl.AbortConfig()
if err != nil {
return nil, err
}
r.Record(pr.ScrapliResponses, pr.Result)
c.Candidate = ""
return r, nil
}
// CommitConfig commits a loaded candidate configuration.
func (c *Cfg) CommitConfig() (*response.Response, error) {
c.Logger.Info("CommitConfig requested")
if !c.prepared {
return nil, fmt.Errorf(
"%w: connection not prepared, must call `Prepare`",
util.ErrPrepareError,
)
}
if c.Candidate == "" {
return nil, fmt.Errorf(
"%w: cannot commit config, candidate config is not set",
util.ErrCandidateError,
)
}
r := response.NewResponse(CommitConfig, c.Conn.Transport.GetHost())
pr, err := c.Impl.CommitConfig()
if err != nil {
return nil, err
}
r.Record(pr.ScrapliResponses, pr.Result)
// reset the candidate config now that we've committed
c.Candidate = ""
return r, nil
}
// DiffConfig diffs the requested `source` config with the candidate config. Supports `WithDiff`
// options to modify the DiffResponse behavior.
func (c *Cfg) DiffConfig(source string, opts ...util.Option) (*response.DiffResponse, error) {
c.Logger.Infof("DiffConfig requested, source '%s'", source)
if !c.prepared {
return nil, fmt.Errorf(
"%w: connection not prepared, must call `Prepare`",
util.ErrPrepareError,
)
}
if c.Candidate == "" {
return nil, fmt.Errorf(
"%w: cannot diff config, candidate config is not set",
util.ErrCandidateError,
)
}
dr := response.NewDiffResponse(c.Conn.Transport.GetHost(), opts...)
pr, err := c.Impl.GetDeviceDiff(source)
if err != nil {
return nil, err
}
deviceDiff := pr.Result
dr.Record(pr.ScrapliResponses, pr.Result)
pr, err = c.Impl.GetConfig(source)
if err != nil {
return nil, err
}
dr.Record(pr.ScrapliResponses, "")
currentConfig := pr.Result
dr.RecordDiff(
deviceDiff,
c.Impl.NormalizeConfig(c.Candidate),
c.Impl.NormalizeConfig(currentConfig),
)
return dr, nil
}