-
Notifications
You must be signed in to change notification settings - Fork 0
/
noderule.go
198 lines (170 loc) · 4.92 KB
/
noderule.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
/*
* Copyright (C) 2018 Red Hat, Inc.
*
* 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 ofthe License at
*
* http://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 specificlanguage governing permissions and
* limitations under the License.
*
*/
package client
import (
"errors"
"fmt"
"os"
"github.com/skydive-project/skydive/api/client"
api "github.com/skydive-project/skydive/api/types"
"github.com/skydive-project/skydive/graffiti/graph"
"github.com/skydive-project/skydive/logging"
usertopology "github.com/skydive-project/skydive/topology/enhancers"
"github.com/skydive-project/skydive/validator"
"github.com/spf13/cobra"
)
var (
name string
description string
nodeName string
nodeType string
metadata string
query string
action string
)
// NodeRuleCmd skydive node rule root command
var NodeRuleCmd = &cobra.Command{
Use: "node-rule",
Short: "node-rule",
Long: "node-rule",
SilenceUsage: false,
}
// NodeRuleCreate skydive node create command
var NodeRuleCreate = &cobra.Command{
Use: "create",
Short: "create",
Long: "create",
SilenceUsage: false,
Run: func(cmd *cobra.Command, args []string) {
client, err := client.NewCrudClientFromConfig(&AuthenticationOpts)
if err != nil {
exitOnError(err)
}
m, err := usertopology.DefToMetadata(metadata, graph.Metadata{})
if err != nil {
exitOnError(err)
}
if action == "create" {
if nodeName == "" || nodeType == "" {
exitOnError(errors.New("Both --node-name and --node-type are required for 'create' node rules"))
}
m["Name"] = nodeName
m["Type"] = nodeType
}
node := &api.NodeRule{
Name: name,
Description: description,
Metadata: m,
Query: query,
Action: action,
}
if err = validator.Validate(node); err != nil {
exitOnError(fmt.Errorf("Error while validating node rule: %s", err))
}
if err = client.Create("noderule", &node, nil); err != nil {
exitOnError(err)
}
printJSON(node)
},
}
// NodeRuleGet skydive node get command
var NodeRuleGet = &cobra.Command{
Use: "get",
Short: "get",
Long: "get",
SilenceUsage: false,
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Usage()
os.Exit(1)
}
},
Run: func(cmd *cobra.Command, args []string) {
var node api.NodeRule
client, err := client.NewCrudClientFromConfig(&AuthenticationOpts)
if err != nil {
logging.GetLogger().Error(err.Error())
os.Exit(1)
}
if err := client.Get("noderule", args[0], &node); err != nil {
logging.GetLogger().Error(err.Error())
os.Exit(1)
}
printJSON(&node)
},
}
// NodeRuleList skydive node list command
var NodeRuleList = &cobra.Command{
Use: "list",
Short: "list",
Long: "list",
SilenceUsage: false,
Run: func(cmd *cobra.Command, args []string) {
var nodes map[string]api.NodeRule
client, err := client.NewCrudClientFromConfig(&AuthenticationOpts)
if err != nil {
logging.GetLogger().Error(err.Error())
os.Exit(1)
}
if err := client.List("noderule", &nodes); err != nil {
logging.GetLogger().Error(err.Error())
os.Exit(1)
}
printJSON(nodes)
},
}
// NodeRuleDelete skydive node delete command
var NodeRuleDelete = &cobra.Command{
Use: "delete",
Short: "delete",
Long: "delete",
SilenceUsage: false,
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Usage()
os.Exit(1)
}
},
Run: func(cmd *cobra.Command, args []string) {
client, err := client.NewCrudClientFromConfig(&AuthenticationOpts)
if err != nil {
logging.GetLogger().Error(err.Error())
os.Exit(1)
}
for _, id := range args {
if err := client.Delete("noderule", id); err != nil {
logging.GetLogger().Error(err.Error())
}
}
},
}
func addCreateNodeRuleFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&name, "name", "", "", "rule name")
cmd.Flags().StringVarP(&description, "description", "", "", "rule description")
cmd.Flags().StringVarP(&nodeName, "node-name", "", "", "node name")
cmd.Flags().StringVarP(&nodeType, "node-type", "", "", "node type")
cmd.Flags().StringVarP(&metadata, "metadata", "", "", "node metadata, key value pairs. 'k1=v1, k2=v2'")
cmd.Flags().StringVarP(&query, "query", "", "", "gremlin query of the nodes to update")
cmd.Flags().StringVarP(&action, "action", "", "", "action: create or update")
}
func init() {
NodeRuleCmd.AddCommand(NodeRuleCreate)
NodeRuleCmd.AddCommand(NodeRuleList)
NodeRuleCmd.AddCommand(NodeRuleGet)
NodeRuleCmd.AddCommand(NodeRuleDelete)
addCreateNodeRuleFlags(NodeRuleCreate)
}