/
update.go
145 lines (124 loc) · 3.96 KB
/
update.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
// SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
//
// SPDX-License-Identifier: Apache-2.0
package topo
import (
"context"
"time"
"github.com/gogo/protobuf/types"
topoapi "github.com/onosproject/onos-api/go/onos/topo"
"github.com/onosproject/onos-lib-go/pkg/cli"
"github.com/spf13/cobra"
)
func getUpdateEntityCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "entity <id> [args]",
Args: cobra.MinimumNArgs(1),
Short: "Update Entity",
RunE: runUpdateEntityCommand,
}
cmd.Flags().StringToStringP("aspect", "a", map[string]string{}, "aspect of this entity")
cmd.Flags().StringToStringP("label", "l", map[string]string{}, "classification label")
cmd.Flags().StringToStringP("set", "s", map[string]string{}, "set single attribute of an aspect")
return cmd
}
func getUpdateRelationCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "relation <id> [args]",
Args: cobra.MinimumNArgs(1),
Short: "Update Relation",
RunE: runUpdateRelationCommand,
}
cmd.Flags().StringToStringP("aspect", "a", map[string]string{}, "aspect of this entity")
cmd.Flags().StringToStringP("label", "l", map[string]string{}, "classification label")
cmd.Flags().StringToStringP("set", "s", map[string]string{}, "set single attribute of an aspect")
return cmd
}
func getUpdateKindCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "kind <id> [args]",
Args: cobra.MinimumNArgs(1),
Short: "Update Kind",
RunE: runUpdateKindCommand,
}
cmd.Flags().StringP("name", "n", "", "Kind Name")
cmd.Flags().StringToStringP("aspect", "a", map[string]string{}, "aspect of this entity")
cmd.Flags().StringToStringP("label", "l", map[string]string{}, "classification label")
cmd.Flags().StringToStringP("set", "s", map[string]string{}, "set single attribute of an aspect")
return cmd
}
func runUpdateEntityCommand(cmd *cobra.Command, args []string) error {
return updateObject(cmd, args, topoapi.Object_ENTITY)
}
func runUpdateRelationCommand(cmd *cobra.Command, args []string) error {
return updateObject(cmd, args, topoapi.Object_RELATION)
}
func runUpdateKindCommand(cmd *cobra.Command, args []string) error {
return updateObject(cmd, args, topoapi.Object_KIND)
}
const deleteKeyword = "--delete"
func updateObject(cmd *cobra.Command, args []string, objectType topoapi.Object_Type) error {
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()
// Fetch the object
id := topoapi.ID(args[0])
response, err := client.Get(ctx, &topoapi.GetRequest{ID: id})
if err != nil {
return err
}
object := response.Object
// Apply label changes
labels, err := cmd.Flags().GetStringToString("label")
if err != nil {
return err
}
if object.Labels == nil && len(labels) > 0 {
object.Labels = make(map[string]string)
}
for labelKey, labelValue := range labels {
if len(labelValue) > 0 && labelValue != deleteKeyword {
object.Labels[labelKey] = labelValue
} else {
delete(object.Labels, labelKey)
}
}
// If kind, change name if needed
if objectType == topoapi.Object_KIND {
if cmd.Flags().Changed("name") {
name, err := cmd.Flags().GetString("name")
if err == nil {
object.GetKind().Name = name
}
}
}
// Apply changed aspects
aspects, err := cmd.Flags().GetStringToString("aspect")
if err == nil {
for aspectType, aspectValue := range aspects {
if len(aspectValue) > 0 && aspectValue != deleteKeyword {
if object.Aspects == nil {
object.Aspects = make(map[string]*types.Any)
}
object.Aspects[aspectType] = &types.Any{
TypeUrl: aspectType,
Value: []byte(aspectValue),
}
} else {
delete(object.Aspects, aspectType)
}
}
}
// TODO: Apply individual aspect attribute changes
// Update the object
_, err = client.Update(ctx, &topoapi.UpdateRequest{Object: object})
if err != nil {
return err
}
return nil
}