-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_firewall.go
184 lines (150 loc) · 4.28 KB
/
vm_firewall.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
package cmd
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/egoscale"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/table"
)
var vmFirewallCmd = &cobra.Command{
Use: "firewall",
Short: "Virtual machines firewall management",
}
var vmFirewallSetCmd = &cobra.Command{
Use: "set INSTANCE-NAME|ID SECURITY-GROUP-NAME|ID...",
Short: "Set the Security Groups for a Compute instance",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return cmd.Usage()
}
sgs, err := getSecurityGroupsByNameOrID(args[1:])
if err != nil {
return err
}
vm, err := getVirtualMachineByNameOrID(args[0])
if err != nil {
return err
}
if err := setVirtualMachineSecurityGroups(vm, sgs); err != nil {
return err
}
return printVirtualMachineSecurityGroups(vm)
},
}
var vmFirewallAddCmd = &cobra.Command{
Use: "add INSTANCE-NAME|ID SECURITY-GROUP-NAME|ID...",
Short: "Add Security Groups to a Compute instance",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return cmd.Usage()
}
sgs, err := getSecurityGroupsByNameOrID(args[1:])
if err != nil {
return err
}
vm, err := getVirtualMachineByNameOrID(args[0])
if err != nil {
return err
}
sgToAdd := make([]egoscale.SecurityGroup, 0)
next:
// Check if requested Security Groups are not already set to the VM instance
for i := range sgs {
for j := range vm.SecurityGroup {
if sgs[i].ID.Equal(*vm.SecurityGroup[j].ID) {
continue next
}
}
sgToAdd = append(sgToAdd, sgs[i])
}
if err := setVirtualMachineSecurityGroups(vm, append(vm.SecurityGroup, sgToAdd...)); err != nil {
return err
}
return printVirtualMachineSecurityGroups(vm)
},
}
var vmFirewallRemoveCmd = &cobra.Command{
Use: "remove INSTANCE-NAME|ID SECURITY-GROUP-NAME|ID...",
Short: "Remove Security Groups from a Compute instance",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return cmd.Usage()
}
sgs, err := getSecurityGroupsByNameOrID(args[1:])
if err != nil {
return err
}
vm, err := getVirtualMachineByNameOrID(args[0])
if err != nil {
return err
}
sgRemaining := make([]egoscale.SecurityGroup, 0)
next:
for i := range vm.SecurityGroup {
for j := range sgs {
if sgs[j].ID.Equal(*vm.SecurityGroup[i].ID) {
continue next
}
}
sgRemaining = append(sgRemaining, vm.SecurityGroup[i])
}
if err := setVirtualMachineSecurityGroups(vm, sgRemaining); err != nil {
return err
}
return printVirtualMachineSecurityGroups(vm)
},
}
// setVirtualMachineSecurityGroups sets a Compute instance Security Groups.
func setVirtualMachineSecurityGroups(vm *egoscale.VirtualMachine, sgs []egoscale.SecurityGroup) error {
ids := make([]egoscale.UUID, len(sgs))
for i := range sgs {
ids[i] = *sgs[i].ID
}
_, err := globalstate.EgoscaleClient.RequestWithContext(gContext, &egoscale.UpdateVirtualMachineSecurityGroups{
ID: vm.ID,
SecurityGroupIDs: ids,
})
if err != nil {
return err
}
return nil
}
// printVirtualMachineSecurityGroups prints a Compute instance Security Groups to standard output.
func printVirtualMachineSecurityGroups(vm *egoscale.VirtualMachine) error {
if !globalstate.Quiet {
// Refresh the vm object to ensure its properties are up-to-date
vm, err := getVirtualMachineByNameOrID(vm.ID.String())
if err != nil {
return err
}
sgs := []string{}
for _, sgN := range vm.SecurityGroup {
sgs = append(sgs, sgN.Name)
}
table := table.NewTable(os.Stdout)
table.SetHeader([]string{vm.Name})
table.Append([]string{"Security Groups", strings.Join(sgs, " - ")})
table.Render()
}
return nil
}
// getSecurityGroupsByNameOrID tries to retrieve a list of Security Groups by their name or ID.
func getSecurityGroupsByNameOrID(sgs []string) ([]egoscale.SecurityGroup, error) {
res := make([]egoscale.SecurityGroup, len(sgs))
for i, s := range sgs {
sg, err := getSecurityGroupByNameOrID(s)
if err != nil {
return nil, fmt.Errorf("unable to retrieve Security Group %q: %s", s, err)
}
res[i] = *sg
}
return res, nil
}
func init() {
vmFirewallCmd.AddCommand(vmFirewallAddCmd)
vmFirewallCmd.AddCommand(vmFirewallRemoveCmd)
vmFirewallCmd.AddCommand(vmFirewallSetCmd)
vmCmd.AddCommand(vmFirewallCmd)
}