-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_firewall.go
190 lines (157 loc) · 4.54 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
185
186
187
188
189
190
package cmd
import (
"fmt"
"os"
"strings"
"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// vmFirewallCmd represents the vm firewall command
var vmFirewallCmd = &cobra.Command{
Use: "firewall",
Short: "Virtual machines firewall management",
}
// vmFirewallSetCmd represents the firewall set command
var vmFirewallSetCmd = &cobra.Command{
Use: "set <vm name | id> <SG name | id>+",
Short: "Set the security groups of a virtual machine",
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)
},
}
// vmFirewallAddCmd represents the firewall add command
var vmFirewallAddCmd = &cobra.Command{
Use: "add <vm name | id> <SG name | id>+",
Short: "Add security groups to a virtual machine",
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)
},
}
// vmFirewallRemoveCmd represents the firewall remove command
var vmFirewallRemoveCmd = &cobra.Command{
Use: "remove <vm name | id> <SG name | id>+",
Short: "Remove security groups from a virtual machine",
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 virtual machine instance security groups.
func setVirtualMachineSecurityGroups(vm *egoscale.VirtualMachine, sgs []egoscale.SecurityGroup) error {
state := (string)(egoscale.VirtualMachineStopped)
if vm.State != state {
return fmt.Errorf("this operation is not permitted if your VM is not stopped")
}
ids := make([]egoscale.UUID, len(sgs))
for i := range sgs {
ids[i] = *sgs[i].ID
}
_, err := cs.RequestWithContext(gContext, &egoscale.UpdateVirtualMachine{
ID: vm.ID,
SecurityGroupIDs: ids,
})
if err != nil {
return err
}
return nil
}
// printVirtualMachineSecurityGroups prints a virtual machine instance security groups to standard output.
func printVirtualMachineSecurityGroups(vm *egoscale.VirtualMachine) error {
if !gQuiet {
// 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)
}