Skip to content

Commit 4d5b258

Browse files
authored
Merge pull request #106 from sdcio/deviation-mgr
add deviations manager
2 parents a5a710e + 45d56f5 commit 4d5b258

File tree

5 files changed

+325
-53
lines changed

5 files changed

+325
-53
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2024 Nokia
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"context"
19+
"encoding/json"
20+
"fmt"
21+
22+
"github.com/sdcio/data-server/pkg/utils"
23+
sdcpb "github.com/sdcio/sdc-protos/sdcpb"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
// datastoreWatchDeviationCmd represents the watch deviation command
28+
var datastoreWatchDeviationCmd = &cobra.Command{
29+
Use: "watch-dev",
30+
Short: "watch deviation datastores",
31+
SilenceUsage: true,
32+
RunE: func(cmd *cobra.Command, _ []string) error {
33+
ctx, cancel := context.WithCancel(cmd.Context())
34+
defer cancel()
35+
dataClient, err := createDataClient(ctx, addr)
36+
if err != nil {
37+
return err
38+
}
39+
req := &sdcpb.WatchDeviationRequest{Name: []string{datastoreName}}
40+
stream, err := dataClient.WatchDeviations(ctx, req)
41+
if err != nil {
42+
return err
43+
}
44+
count := 0
45+
for {
46+
rsp, err := stream.Recv()
47+
if err != nil {
48+
return err
49+
}
50+
switch format {
51+
case "":
52+
switch rsp.Event {
53+
case sdcpb.DeviationEvent_START:
54+
fmt.Printf("%s: %s: %s\n",
55+
rsp.GetName(), rsp.GetIntent(), rsp.GetEvent())
56+
case sdcpb.DeviationEvent_END:
57+
fmt.Printf("%s: %s: %s\n",
58+
rsp.GetName(), rsp.GetIntent(), rsp.GetEvent())
59+
60+
fmt.Printf("Received %d deviations\n", count)
61+
count = 0
62+
default:
63+
count++
64+
fmt.Printf("%s: %s: %s: %s: %s : %s -> %s\n",
65+
rsp.GetName(), rsp.GetIntent(), rsp.GetEvent(),
66+
rsp.GetReason(), utils.ToXPath(rsp.GetPath(), false),
67+
rsp.GetExpectedValue(), rsp.GetCurrentValue())
68+
}
69+
case "json":
70+
b, err := json.MarshalIndent(rsp, "", " ")
71+
if err != nil {
72+
return err
73+
}
74+
fmt.Println(string(b))
75+
}
76+
}
77+
},
78+
}
79+
80+
func init() {
81+
datastoreCmd.AddCommand(datastoreWatchDeviationCmd)
82+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ require (
2020
github.com/scrapli/scrapligo v1.2.0
2121
github.com/sdcio/cache v0.0.33
2222
github.com/sdcio/schema-server v0.0.16
23-
github.com/sdcio/sdc-protos v0.0.22
23+
github.com/sdcio/sdc-protos v0.0.23
2424
github.com/sdcio/yang-parser v0.0.5
2525
github.com/sirupsen/logrus v1.9.3
2626
github.com/spf13/cobra v1.8.0

go.sum

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,12 @@ github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
225225
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
226226
github.com/scrapli/scrapligo v1.2.0 h1:jn83HPkKAPDzvth7i9V/70BAPuVgriU+/tHHv3eAtC4=
227227
github.com/scrapli/scrapligo v1.2.0/go.mod h1:rRx/rT2oNPYztiT3/ik0FRR/Ro7AdzN/eR9AtF8A81Y=
228-
github.com/sdcio/cache v0.0.32 h1:otQNqweBYhpUUJsllhSGF8g5uVYXLecQEKhOHL1ZxD4=
229-
github.com/sdcio/cache v0.0.32/go.mod h1:SQLzQrXw48HED4i6nWCRk3VY0Ec8j053Jp3dDl+Jyjk=
230228
github.com/sdcio/cache v0.0.33 h1:Avb+oFVCCcJSd1NMfN+pg6vk33sJEEE3aCxWWT5l6xI=
231229
github.com/sdcio/cache v0.0.33/go.mod h1:SQLzQrXw48HED4i6nWCRk3VY0Ec8j053Jp3dDl+Jyjk=
232-
github.com/sdcio/schema-server v0.0.15 h1:NSBbLkytnMzDrUCqam+rBVvrJVt36p3lU7ddegxJPLw=
233-
github.com/sdcio/schema-server v0.0.15/go.mod h1:Wp7DRUkX0DQZNARIgcJtCjOuLp5pwUCDVCAuvM5LA5g=
234230
github.com/sdcio/schema-server v0.0.16 h1:mBm/hj0WQlNN7/S+tBwsf2Mc0XW4coaah27WDcxbTOQ=
235231
github.com/sdcio/schema-server v0.0.16/go.mod h1:Wp7DRUkX0DQZNARIgcJtCjOuLp5pwUCDVCAuvM5LA5g=
236-
github.com/sdcio/sdc-protos v0.0.22 h1:miAONxMvf0zlCYQz2exUS59y8VndR5HNtrVWCPbbTMg=
237-
github.com/sdcio/sdc-protos v0.0.22/go.mod h1:iVVPmQTRzoCV4abQ4QL5mJethvRVfdV8cHcVyDtSNs4=
232+
github.com/sdcio/sdc-protos v0.0.23 h1:vbz4WeMQfPegCDJY2qanpezcmdvJVlm+E3N1lRoG25s=
233+
github.com/sdcio/sdc-protos v0.0.23/go.mod h1:iVVPmQTRzoCV4abQ4QL5mJethvRVfdV8cHcVyDtSNs4=
238234
github.com/sdcio/yang-parser v0.0.5 h1:YwpgQt2/n3BHFrEa0n0sQ4Q2hLCrTFY2VDKO+2BgoVg=
239235
github.com/sdcio/yang-parser v0.0.5/go.mod h1:cLYFD8ChaFyn+G0EYpOlGDrI/3J2Zgf7uzO/CKW6l1Q=
240236
github.com/sirikothe/gotextfsm v1.0.1-0.20200816110946-6aa2cfd355e4 h1:FHUL2HofYJuslFOQdy/JjjP36zxqIpd/dcoiwLMIs7k=

0 commit comments

Comments
 (0)