-
Notifications
You must be signed in to change notification settings - Fork 20
/
security_group_show.go
248 lines (204 loc) · 7.2 KB
/
security_group_show.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package cmd
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/table"
"github.com/exoscale/cli/utils"
exoapi "github.com/exoscale/egoscale/v2/api"
)
type securityGroupRuleOutput struct {
ID string `json:"id"`
Description string `json:"description"`
ICMPCode *int64 `json:"icmp_code,omitempty"`
ICMPType *int64 `json:"icmp_type,omitempty"`
Network *string `json:"network,omitempty"`
Protocol string `json:"protocol"`
SecurityGroup *string `json:"security_group,omitempty"`
StartPort *uint16 `json:"start_port,omitempty"`
EndPort *uint16 `json:"end_port,omitempty"`
}
type securityGroupShowOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
ExternalSources []string `json:"external_sources"`
IngressRules []securityGroupRuleOutput `json:"ingress_rules"`
EgressRules []securityGroupRuleOutput `json:"egress_rules"`
Instances []securityGroupInstanceOutput `json:"instances"`
}
type securityGroupInstanceOutput struct {
Name string `json:"name"`
PublicIP string `json:"public_ip"`
ID string `json:"id"`
Zone string `json:"zone"`
}
func (o *securityGroupShowOutput) ToJSON() { output.JSON(o) }
func (o *securityGroupShowOutput) ToText() { output.Text(o) }
func (o *securityGroupShowOutput) ToTable() {
formatExternalSources := func(sources []string) string {
if len(sources) > 0 {
return strings.Join(sources, ", ")
}
return "-"
}
formatRule := func(rules []securityGroupRuleOutput) string {
if len(rules) > 0 {
buf := bytes.NewBuffer(nil)
at := table.NewEmbeddedTable(buf)
at.SetHeader([]string{" "})
at.SetAlignment(tablewriter.ALIGN_LEFT)
for _, rule := range rules {
r := []string{rule.ID, rule.Description, strings.ToUpper(rule.Protocol)}
if rule.Network != nil {
r = append(r, *rule.Network)
} else {
r = append(r, *rule.SecurityGroup)
}
if strings.HasPrefix(rule.Protocol, "icmp") {
r = append(r, fmt.Sprintf("ICMP code:%d type:%d", *rule.ICMPCode, *rule.ICMPType))
} else if rule.StartPort != nil {
r = append(r, func() string {
if *rule.StartPort == *rule.EndPort {
return fmt.Sprint(*rule.StartPort)
}
return fmt.Sprintf("%d-%d", *rule.StartPort, *rule.EndPort)
}())
}
at.Append(r)
}
at.Render()
return buf.String()
}
return "-"
}
formatInstances := func(instances []securityGroupInstanceOutput) string {
if len(instances) < 1 {
return "-"
}
buf := bytes.NewBuffer(nil)
at := table.NewEmbeddedTable(buf)
at.SetHeader([]string{" "})
at.SetAlignment(tablewriter.ALIGN_LEFT)
for _, instance := range instances {
r := []string{instance.Name, instance.ID}
r = append(r, instance.PublicIP)
r = append(r, instance.Zone)
at.Append(r)
}
at.Render()
return buf.String()
}
t := table.NewTable(os.Stdout)
t.SetHeader([]string{"Security Group"})
defer t.Render()
t.Append([]string{"ID", o.ID})
t.Append([]string{"Name", o.Name})
t.Append([]string{"Description", o.Description})
t.Append([]string{"Ingress Rules", formatRule(o.IngressRules)})
t.Append([]string{"Egress Rules", formatRule(o.EgressRules)})
t.Append([]string{"External Sources", formatExternalSources(o.ExternalSources)})
t.Append([]string{"Instances", formatInstances(o.Instances)})
}
type securityGroupShowCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"show"`
SecurityGroup string `cli-arg:"#" cli-usage:"NAME|ID"`
}
func (c *securityGroupShowCmd) cmdAliases() []string { return gShowAlias }
func (c *securityGroupShowCmd) cmdShort() string {
return "Show a Security Group details"
}
func (c *securityGroupShowCmd) cmdLong() string {
return fmt.Sprintf(`This command shows a Compute instance Security Group details.
Supported output template annotations for Security Group: %s
Supported output template annotations for Security Group rules: %s`,
strings.Join(output.TemplateAnnotations(&securityGroupShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&securityGroupRuleOutput{}), ", "))
}
func (c *securityGroupShowCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *securityGroupShowCmd) cmdRun(_ *cobra.Command, _ []string) error {
zone := account.CurrentAccount.DefaultZone
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))
securityGroup, err := globalstate.EgoscaleClient.FindSecurityGroup(ctx, zone, c.SecurityGroup)
if err != nil {
return err
}
externalSources := make([]string, 0)
if securityGroup.ExternalSources != nil {
externalSources = *securityGroup.ExternalSources
}
out := securityGroupShowOutput{
ID: *securityGroup.ID,
Name: *securityGroup.Name,
Description: utils.DefaultString(securityGroup.Description, ""),
ExternalSources: externalSources,
IngressRules: make([]securityGroupRuleOutput, 0),
EgressRules: make([]securityGroupRuleOutput, 0),
}
for _, rule := range securityGroup.Rules {
or := securityGroupRuleOutput{
ID: *rule.ID,
Description: utils.DefaultString(rule.Description, ""),
ICMPCode: rule.ICMPCode,
ICMPType: rule.ICMPType,
Network: func() *string {
if rule.Network != nil {
v := rule.Network.String()
return &v
}
return nil
}(),
Protocol: *rule.Protocol,
StartPort: rule.StartPort,
EndPort: rule.EndPort,
}
if rule.SecurityGroupID != nil {
ruleSecurityGroup, err := globalstate.EgoscaleClient.GetSecurityGroup(ctx, zone, *rule.SecurityGroupID)
if err != nil {
return fmt.Errorf("error retrieving Security Group: %w", err)
}
ruleSG := "SG:" + *ruleSecurityGroup.Name
or.SecurityGroup = &ruleSG
}
if rule.SecurityGroupName != nil {
ruleSG := "PUBLIC-SG:" + *rule.SecurityGroupName
or.SecurityGroup = &ruleSG
}
if *rule.FlowDirection == "ingress" {
out.IngressRules = append(out.IngressRules, or)
} else {
out.EgressRules = append(out.EgressRules, or)
}
}
instances, err := utils.GetInstancesInSecurityGroup(ctx, globalstate.EgoscaleClient, *securityGroup.ID, zone)
if err != nil {
return fmt.Errorf("error retrieving instances in Security Group: %w", err)
}
for _, instance := range instances {
publicIP := emptyIPAddressVisualization
if instance.PublicIPAddress != nil && (!instance.PublicIPAddress.IsUnspecified() || len(*instance.PublicIPAddress) > 0) {
publicIP = instance.PublicIPAddress.String()
}
out.Instances = append(out.Instances, securityGroupInstanceOutput{
Name: utils.DefaultString(instance.Name, "-"),
PublicIP: publicIP,
ID: utils.DefaultString(instance.ID, "-"),
Zone: utils.DefaultString(instance.Zone, "-"),
})
}
return c.outputFunc(&out, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(securityGroupCmd, &securityGroupShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}