-
Notifications
You must be signed in to change notification settings - Fork 20
/
security_group_show.go
185 lines (154 loc) · 5.34 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
package cmd
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/exoscale/cli/table"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
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"`
}
func (o *securityGroupShowOutput) toJSON() { outputJSON(o) }
func (o *securityGroupShowOutput) toText() { outputText(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, "SG:"+*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 "-"
}
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)})
}
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(outputterTemplateAnnotations(&securityGroupShowOutput{}), ", "),
strings.Join(outputterTemplateAnnotations(&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 := gCurrentAccount.DefaultZone
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, zone))
securityGroup, err := cs.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: defaultString(securityGroup.Description, ""),
ExternalSources: externalSources,
IngressRules: make([]securityGroupRuleOutput, 0),
EgressRules: make([]securityGroupRuleOutput, 0),
}
for _, rule := range securityGroup.Rules {
or := securityGroupRuleOutput{
ID: *rule.ID,
Description: 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 := cs.GetSecurityGroup(ctx, zone, *rule.SecurityGroupID)
if err != nil {
return fmt.Errorf("error retrieving Security Group: %w", err)
}
or.SecurityGroup = ruleSecurityGroup.Name
}
if *rule.FlowDirection == "ingress" {
out.IngressRules = append(out.IngressRules, or)
} else {
out.EgressRules = append(out.EgressRules, or)
}
}
return c.outputFunc(&out, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(securityGroupCmd, &securityGroupShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}