-
Notifications
You must be signed in to change notification settings - Fork 4
/
system.go
357 lines (297 loc) · 10.4 KB
/
system.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package system provides gNOI system operations.
package system
import (
"context"
"io"
"time"
spb "github.com/openconfig/gnoi/system"
tpb "github.com/openconfig/gnoi/types"
"github.com/openconfig/gnoigo/internal"
)
// KillProcessOperation represents the parameters of a KillProcess operation.
type KillProcessOperation struct {
req *spb.KillProcessRequest
}
// NewKillProcessOperation creates an empty KillProcessOperation.
func NewKillProcessOperation() *KillProcessOperation {
return &KillProcessOperation{req: &spb.KillProcessRequest{}}
}
// PID specifies the process ID of the process to be killed.
func (k *KillProcessOperation) PID(pid uint32) *KillProcessOperation {
k.req.Pid = pid
return k
}
// Name specifies the name of the process to be killed.
func (k *KillProcessOperation) Name(n string) *KillProcessOperation {
k.req.Name = n
return k
}
// Signal specifies the termination signal sent to the process.
func (k *KillProcessOperation) Signal(s spb.KillProcessRequest_Signal) *KillProcessOperation {
k.req.Signal = s
return k
}
// Restart specifies whether the process should be restarted after termination.
func (k *KillProcessOperation) Restart(r bool) *KillProcessOperation {
k.req.Restart = r
return k
}
// Execute performs the KillProcess operation.
func (k *KillProcessOperation) Execute(ctx context.Context, c *internal.Clients) (*spb.KillProcessResponse, error) {
return c.System().KillProcess(ctx, k.req)
}
// PingOperation represents the parameters of a Ping operation.
type PingOperation struct {
req *spb.PingRequest
}
// NewPingOperation creates an empty PingOperation.
func NewPingOperation() *PingOperation {
return &PingOperation{req: &spb.PingRequest{}}
}
// Destination specifies the address to ping.
func (p *PingOperation) Destination(dst string) *PingOperation {
p.req.Destination = dst
return p
}
// Source specifies the address to ping from.
func (p *PingOperation) Source(src string) *PingOperation {
p.req.Source = src
return p
}
// Count specifies the number of packets.
func (p *PingOperation) Count(c int32) *PingOperation {
p.req.Count = c
return p
}
// Interval specifies the duration between ping requests.
func (p *PingOperation) Interval(i time.Duration) *PingOperation {
p.req.Interval = i.Nanoseconds()
return p
}
// Wait specifies the duration to wait for a response.
func (p *PingOperation) Wait(w time.Duration) *PingOperation {
p.req.Wait = w.Nanoseconds()
return p
}
// Size specifies the size of request packet (excluding ICMP header).
func (p *PingOperation) Size(s int32) *PingOperation {
p.req.Size = s
return p
}
// DoNotFragment sets the do not fragment bit (IPv4 destinations).
func (p *PingOperation) DoNotFragment(dnf bool) *PingOperation {
p.req.DoNotFragment = dnf
return p
}
// DoNotResolve specifies if address returned should be resolved.
func (p *PingOperation) DoNotResolve(dnr bool) *PingOperation {
p.req.DoNotResolve = dnr
return p
}
// L3Protocol specifies layer3 protocol for the ping.
func (p *PingOperation) L3Protocol(l3p tpb.L3Protocol) *PingOperation {
p.req.L3Protocol = l3p
return p
}
// Execute performs the Ping operation.
func (p *PingOperation) Execute(ctx context.Context, c *internal.Clients) ([]*spb.PingResponse, error) {
ping, err := c.System().Ping(ctx, p.req)
if err != nil {
return nil, err
}
var pingResp []*spb.PingResponse
for {
resp, err := ping.Recv()
switch {
case err == io.EOF:
return pingResp, nil
case err != nil:
return nil, err
default:
pingResp = append(pingResp, resp)
}
}
}
// RebootOperation represents the parameters of a Reboot operation.
type RebootOperation struct {
req *spb.RebootRequest
}
// NewRebootOperation creates an empty RebootOperation.
func NewRebootOperation() *RebootOperation {
return &RebootOperation{req: &spb.RebootRequest{}}
}
// RebootMethod specifies method to reboot.
func (r *RebootOperation) RebootMethod(rebootMethod spb.RebootMethod) *RebootOperation {
r.req.Method = rebootMethod
return r
}
// Delay specifies time in nanoseconds to wait before issuing reboot.
func (r *RebootOperation) Delay(delay time.Duration) *RebootOperation {
r.req.Delay = uint64(delay.Nanoseconds())
return r
}
// Message specifies informational reason for the reboot or cancel reboot.
func (r *RebootOperation) Message(message string) *RebootOperation {
r.req.Message = message
return r
}
// Subcomponents specifies the sub-components to reboot.
func (r *RebootOperation) Subcomponents(subcomponents []*tpb.Path) *RebootOperation {
r.req.Subcomponents = subcomponents
return r
}
// Force reboot if sanity checks fail.
func (r *RebootOperation) Force(force bool) *RebootOperation {
r.req.Force = force
return r
}
// Execute performs the Reboot operation.
func (r *RebootOperation) Execute(ctx context.Context, c *internal.Clients) (*spb.RebootResponse, error) {
return c.System().Reboot(ctx, r.req)
}
// RebootStatusOperation represents the parameters of a RebootStatus operation.
type RebootStatusOperation struct {
req *spb.RebootStatusRequest
}
// NewRebootStatusOperation creates an empty RebootStatusOperation.
func NewRebootStatusOperation() *RebootStatusOperation {
return &RebootStatusOperation{req: &spb.RebootStatusRequest{}}
}
// Subcomponents specifies the sub-components whose status will be checked.
func (r *RebootStatusOperation) Subcomponents(subcomponents []*tpb.Path) *RebootStatusOperation {
r.req.Subcomponents = subcomponents
return r
}
// Execute performs the RebootStatus operation.
func (r *RebootStatusOperation) Execute(ctx context.Context, c *internal.Clients) (*spb.RebootStatusResponse, error) {
return c.System().RebootStatus(ctx, r.req)
}
// SwitchControlProcessorOperation represents the parameters of a SwitchControlProcessor operation.
type SwitchControlProcessorOperation struct {
req *spb.SwitchControlProcessorRequest
}
// NewSwitchControlProcessorOperation creates an empty SwitchControlProcessorOperation.
func NewSwitchControlProcessorOperation() *SwitchControlProcessorOperation {
return &SwitchControlProcessorOperation{req: &spb.SwitchControlProcessorRequest{}}
}
// PathFromSubcomponentName sets the path of the target route processor to `/openconfig/components/component[name=<n>]`.
func (s *SwitchControlProcessorOperation) PathFromSubcomponentName(n string) *SwitchControlProcessorOperation {
return s.Path(&tpb.Path{
Origin: "openconfig",
Elem: []*tpb.PathElem{
{Name: "components"},
{Name: "component", Key: map[string]string{"name": n}},
},
})
}
// Path sets the path of the target route processor.
func (s *SwitchControlProcessorOperation) Path(p *tpb.Path) *SwitchControlProcessorOperation {
s.req.ControlProcessor = p
return s
}
// Execute performs the SwitchControlProcessor operation.
func (s *SwitchControlProcessorOperation) Execute(ctx context.Context, c *internal.Clients) (*spb.SwitchControlProcessorResponse, error) {
return c.System().SwitchControlProcessor(ctx, s.req)
}
// TimeOperation represents the parameters of a Time operation.
type TimeOperation struct {
req *spb.TimeRequest
}
// NewTimeOperation creates an empty TimeOperation.
func NewTimeOperation() *TimeOperation {
return &TimeOperation{req: &spb.TimeRequest{}}
}
// Execute performs the Time operation.
func (t *TimeOperation) Execute(ctx context.Context, c *internal.Clients) (*spb.TimeResponse, error) {
return c.System().Time(ctx, t.req)
}
// TracerouteOperation represents the parameters of a Traceroute operation.
type TracerouteOperation struct {
req *spb.TracerouteRequest
}
// NewTracerouteOperation creates an empty TracerouteOperation.
func NewTracerouteOperation() *TracerouteOperation {
return &TracerouteOperation{req: &spb.TracerouteRequest{}}
}
// Source specifies address to perform traceroute from.
func (t *TracerouteOperation) Source(src string) *TracerouteOperation {
t.req.Source = src
return t
}
// Destination specifies address to perform traceroute to.
func (t *TracerouteOperation) Destination(dst string) *TracerouteOperation {
t.req.Destination = dst
return t
}
// InitialTTL specifies traceroute ttl (default is 1).
func (t *TracerouteOperation) InitialTTL(ttl uint32) *TracerouteOperation {
t.req.InitialTtl = ttl
return t
}
// MaxTTL specifies maximum number of hops.
func (t *TracerouteOperation) MaxTTL(ttl int32) *TracerouteOperation {
t.req.MaxTtl = ttl
return t
}
// Wait specifies the duration to wait for a response.
func (t *TracerouteOperation) Wait(wait time.Duration) *TracerouteOperation {
t.req.Wait = wait.Nanoseconds()
return t
}
// DoNotFragment sets the do not fragment bit. (IPv4 destinations)
func (t *TracerouteOperation) DoNotFragment(dnf bool) *TracerouteOperation {
t.req.DoNotFragment = dnf
return t
}
// DoNotResolve specifies if address returned should be resolved.
func (t *TracerouteOperation) DoNotResolve(dnr bool) *TracerouteOperation {
t.req.DoNotFragment = dnr
return t
}
// L3Protocol specifies layer3 protocol for the traceroute.
func (t *TracerouteOperation) L3Protocol(l3 tpb.L3Protocol) *TracerouteOperation {
t.req.L3Protocol = l3
return t
}
// L4Protocol specifies layer3 protocol for the traceroute.
func (t *TracerouteOperation) L4Protocol(l4 spb.TracerouteRequest_L4Protocol) *TracerouteOperation {
t.req.L4Protocol = l4
return t
}
// DoNotLookupASN specifies if traceroute should try to lookup ASN.
func (t *TracerouteOperation) DoNotLookupASN(asn bool) *TracerouteOperation {
t.req.DoNotLookupAsn = asn
return t
}
// Execute performs the Traceroute operation.
func (t *TracerouteOperation) Execute(ctx context.Context, c *internal.Clients) ([]*spb.TracerouteResponse, error) {
traceroute, err := c.System().Traceroute(ctx, t.req)
if err != nil {
return nil, err
}
var tracerouteResp []*spb.TracerouteResponse
for {
resp, err := traceroute.Recv()
switch {
case err == io.EOF:
return tracerouteResp, nil
case err != nil:
return nil, err
default:
tracerouteResp = append(tracerouteResp, resp)
}
}
}