-
Notifications
You must be signed in to change notification settings - Fork 280
/
tcp_funcs.go
154 lines (140 loc) · 4.44 KB
/
tcp_funcs.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
package targetscmd
import (
"fmt"
"strconv"
"time"
"github.com/hashicorp/boundary/api/targets"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/go-bexpr"
)
func init() {
extraTcpActionsFlagsMapFunc = extraTcpActionsFlagsMapFuncImpl
extraTcpFlagsFunc = extraTcpFlagsFuncImpl
extraTcpFlagsHandlingFunc = extraTcpFlagsHandlingFuncImpl
}
func extraTcpActionsFlagsMapFuncImpl() map[string][]string {
return map[string][]string{
"create": {"default-port", "session-max-seconds", "session-connection-limit", "worker-filter"},
"update": {"default-port", "session-max-seconds", "session-connection-limit", "worker-filter"},
}
}
type extraTcpCmdVars struct {
flagDefaultPort string
flagSessionMaxSeconds string
flagSessionConnectionLimit string
flagWorkerFilter string
}
func (c *TcpCommand) extraTcpHelpFunc(helpMap map[string]func() string) string {
var helpStr string
switch c.Func {
case "create":
helpStr = base.WrapForHelpText([]string{
"Usage: boundary targets create tcp [options] [args]",
"",
" Create a tcp-type target. Example:",
"",
` $ boundary targets create tcp -name prodops -description "Tcp target for ProdOps"`,
"",
"",
})
case "update":
helpStr = base.WrapForHelpText([]string{
"Usage: boundary targets tcp update [options] [args]",
"",
" Update a tcp-type target given its ID. Example:",
"",
` $ boundary targets tcp update -id ttcp_1234567890 -name "devops" -description "Tcp target for DevOps"`,
"",
"",
})
}
return helpStr + c.Flags().Help()
}
func extraTcpFlagsFuncImpl(c *TcpCommand, set *base.FlagSets, f *base.FlagSet) {
fs := set.NewFlagSet("TCP Target Options")
for _, name := range flagsTcpMap[c.Func] {
switch name {
case "default-port":
fs.StringVar(&base.StringVar{
Name: "default-port",
Target: &c.flagDefaultPort,
Usage: "The default port to set on the target.",
})
case "session-max-seconds":
fs.StringVar(&base.StringVar{
Name: "session-max-seconds",
Target: &c.flagSessionMaxSeconds,
Usage: `The maximum lifetime of the session, including all connections. Can be specified as an integer number of seconds or a duration string.`,
})
case "session-connection-limit":
fs.StringVar(&base.StringVar{
Name: "session-connection-limit",
Target: &c.flagSessionConnectionLimit,
Usage: "The maximum number of connections allowed for a session. -1 means unlimited.",
})
case "worker-filter":
fs.StringVar(&base.StringVar{
Name: "worker-filter",
Target: &c.flagWorkerFilter,
Usage: "A boolean expression to filter which workers can handle sessions for this target.",
})
}
}
}
func extraTcpFlagsHandlingFuncImpl(c *TcpCommand, _ *base.FlagSets, opts *[]targets.Option) bool {
switch c.flagDefaultPort {
case "":
case "null":
*opts = append(*opts, targets.DefaultTcpTargetDefaultPort())
default:
port, err := strconv.ParseUint(c.flagDefaultPort, 10, 32)
if err != nil {
c.UI.Error(fmt.Sprintf("Error parsing %q: %s", c.flagDefaultPort, err))
return false
}
*opts = append(*opts, targets.WithTcpTargetDefaultPort(uint32(port)))
}
switch c.flagSessionMaxSeconds {
case "":
case "null":
*opts = append(*opts, targets.DefaultSessionMaxSeconds())
default:
var final uint32
dur, err := strconv.ParseUint(c.flagSessionMaxSeconds, 10, 32)
if err == nil {
final = uint32(dur)
} else {
dur, err := time.ParseDuration(c.flagSessionMaxSeconds)
if err != nil {
c.UI.Error(fmt.Sprintf("Error parsing %q: %s", c.flagSessionMaxSeconds, err))
return false
}
final = uint32(dur.Seconds())
}
*opts = append(*opts, targets.WithSessionMaxSeconds(final))
}
switch c.flagSessionConnectionLimit {
case "":
case "null":
*opts = append(*opts, targets.DefaultSessionConnectionLimit())
default:
limit, err := strconv.ParseInt(c.flagSessionConnectionLimit, 10, 32)
if err != nil {
c.UI.Error(fmt.Sprintf("Error parsing %q: %s", c.flagSessionConnectionLimit, err))
return false
}
*opts = append(*opts, targets.WithSessionConnectionLimit(int32(limit)))
}
switch c.flagWorkerFilter {
case "":
case "null":
*opts = append(*opts, targets.DefaultWorkerFilter())
default:
if _, err := bexpr.CreateEvaluator(c.flagWorkerFilter); err != nil {
c.UI.Error(fmt.Sprintf("Unable to successfully parse filter expression: %s", err))
return false
}
*opts = append(*opts, targets.WithWorkerFilter(c.flagWorkerFilter))
}
return true
}