-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathconnection_update.go
More file actions
188 lines (141 loc) · 4.02 KB
/
Copy pathconnection_update.go
File metadata and controls
188 lines (141 loc) · 4.02 KB
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
package command
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
structs "github.com/seashell/drago/drago/structs"
cli "github.com/seashell/drago/pkg/cli"
"github.com/spf13/pflag"
)
// ConnectionUpdateCommand :
type ConnectionUpdateCommand struct {
UI cli.UI
Command
// Parsed flags
json bool
persistentKeepalive int
allowAll bool
}
func (c *ConnectionUpdateCommand) FlagSet() *pflag.FlagSet {
flags := c.Command.FlagSet(c.Name())
flags.Usage = func() { c.UI.Output("\n" + c.Help() + "\n") }
// General options
flags.BoolVar(&c.json, "json", false, "")
flags.IntVar(&c.persistentKeepalive, "keepalive", 0, "")
flags.BoolVar(&c.allowAll, "allow-all", false, "")
return flags
}
// Name :
func (c *ConnectionUpdateCommand) Name() string {
return "connection update"
}
// Synopsis :
func (c *ConnectionUpdateCommand) Synopsis() string {
return "Update an existing connection"
}
// Run :
func (c *ConnectionUpdateCommand) Run(ctx context.Context, args []string) int {
flags := c.FlagSet()
if err := flags.Parse(args); err != nil {
return 1
}
args = flags.Args()
if len(args) != 3 {
c.UI.Error("This command takes one argument: <connection_id>")
c.UI.Error(`For additional help, try 'drago connection update --help'`)
return 1
}
connectionID := ""
// Get the HTTP client
api, err := c.Command.APIClient()
if err != nil {
c.UI.Error(fmt.Sprintf("Error setting up API client: %s", err))
return 1
}
conn := &structs.Connection{
ID: connectionID,
PersistentKeepalive: &c.persistentKeepalive,
}
rcvConn, err := api.Connections().Update(conn)
if err != nil {
c.UI.Error(fmt.Sprintf("Error updating connection: %s", err))
return 1
}
c.UI.Output(c.formatConnection(rcvConn))
return 0
}
// Help :
func (c *ConnectionUpdateCommand) Help() string {
h := `
Usage: drago connection update <connection_id> [options]
Update is used to update an existing connection between two nodes that have interfaces in the same network.
If ACLs are enabled, this option requires a token with the 'connection:write' capability.
General Options:
` + GlobalOptions() + `
ACL Token Create Options:
--json
Enable JSON output.
--allow-all
Enables routing of all traffic in this connection.
--keepalive=<seconds>
Time interval between persistent keepalive packets. Defaults to 0, which disables the feature.
`
return strings.TrimSpace(h)
}
func (c *ConnectionUpdateCommand) formatConnection(connection *structs.Connection) string {
var b bytes.Buffer
enc := json.NewEncoder(&b)
enc.SetIndent("", " ")
formatted := map[string]interface{}{
"id": connection.ID,
"networkId": connection.NetworkID,
"persistentKeepalive": connection.PersistentKeepalive,
"peerSettings": c.formatPeerSettings(connection.PeerSettings),
}
if err := enc.Encode(formatted); err != nil {
c.UI.Error(fmt.Sprintf("Error formatting JSON output: %s", err))
}
s := b.String()
if c.json {
return s
}
return cleanJSONString(s)
}
func (c *ConnectionUpdateCommand) formatPeerSettings(peerSettings []*structs.PeerSettings) string {
var b bytes.Buffer
for _, peer := range peerSettings {
enc := json.NewEncoder(&b)
enc.SetIndent("", " ")
formatted := map[string]interface{}{
"nodeId": peer.NodeID,
"interfaceID": peer.InterfaceID,
"routingRules": c.formatRoutingRules(peer.RoutingRules),
}
if err := enc.Encode(formatted); err != nil {
c.UI.Error(fmt.Sprintf("Error formatting JSON output: %s", err))
}
}
s := b.String()
if c.json {
return s
}
return cleanJSONString(s)
}
func (c *ConnectionUpdateCommand) formatRoutingRules(rules *structs.RoutingRules) string {
var b bytes.Buffer
enc := json.NewEncoder(&b)
enc.SetIndent("", " ")
formatted := map[string]interface{}{
"allowedIps": rules.AllowedIPs,
}
if err := enc.Encode(formatted); err != nil {
c.UI.Error(fmt.Sprintf("Error formatting JSON output: %s", err))
}
s := b.String()
if c.json {
return s
}
return cleanJSONString(s)
}