/
create.go
111 lines (96 loc) · 3.04 KB
/
create.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
// SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
//
// SPDX-License-Identifier: Apache-2.0
package topo
import (
"context"
topoapi "github.com/onosproject/onos-api/go/onos/topo"
"github.com/onosproject/onos-lib-go/pkg/cli"
"github.com/spf13/cobra"
"time"
)
func getCreateEntityCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "entity <id> [args]",
Args: cobra.MinimumNArgs(1),
Short: "Create Entity",
RunE: runCreateEntityCommand,
}
cmd.Flags().StringP("kind", "k", "", "Kind ID")
cmd.Flags().StringToStringP("aspect", "a", map[string]string{}, "aspect of this entity")
cmd.Flags().StringToStringP("label", "l", map[string]string{}, "classification label")
return cmd
}
func getCreateRelationCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "relation <src-entity-id> <tgt-entity-id> [args]",
Args: cobra.MinimumNArgs(2),
Short: "Create Relation",
RunE: runCreateRelationCommand,
}
cmd.Flags().StringP("kind", "k", "", "Kind ID")
cmd.Flags().StringToStringP("aspect", "a", map[string]string{}, "aspect of this relation")
cmd.Flags().StringToStringP("label", "l", map[string]string{}, "classification label")
return cmd
}
func getCreateKindCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "kind <id> <name> [args]",
Args: cobra.MinimumNArgs(2),
Short: "Create Kind",
RunE: runCreateKindCommand,
}
cmd.Flags().StringToStringP("aspect", "a", map[string]string{}, "default aspect for entities of this kind")
cmd.Flags().StringToStringP("label", "l", map[string]string{}, "classification label")
return cmd
}
func runCreateEntityCommand(cmd *cobra.Command, args []string) error {
kindID, _ := cmd.Flags().GetString("kind")
return createObject(topoapi.NewEntity(topoapi.ID(args[0]), topoapi.ID(kindID)), cmd)
}
func runCreateRelationCommand(cmd *cobra.Command, args []string) error {
kindID, _ := cmd.Flags().GetString("kind")
return createObject(topoapi.NewRelation(topoapi.ID(args[0]), topoapi.ID(args[2]), topoapi.ID(kindID)), cmd)
}
func runCreateKindCommand(cmd *cobra.Command, args []string) error {
return createObject(&topoapi.Object{
ID: topoapi.ID(args[0]),
Type: topoapi.Object_KIND,
Obj: &topoapi.Object_Kind{
Kind: &topoapi.Kind{
Name: args[1],
},
},
}, cmd)
}
func createObject(object *topoapi.Object, cmd *cobra.Command) error {
labels, err := cmd.Flags().GetStringToString("label")
if err != nil {
return err
}
object.Labels = labels
aspects, err := cmd.Flags().GetStringToString("aspect")
if err != nil {
return err
}
// Apply all aspect values
for aspectType, aspectValue := range aspects {
err := object.SetAspectBytes(aspectType, []byte(aspectValue))
if err != nil {
return err
}
}
conn, err := cli.GetConnection(cmd)
if err != nil {
return err
}
defer conn.Close()
client := topoapi.CreateTopoClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
_, err = client.Create(ctx, &topoapi.CreateRequest{Object: object})
if err != nil {
return err
}
return nil
}