-
Notifications
You must be signed in to change notification settings - Fork 7
/
firewall.go
83 lines (76 loc) · 2.3 KB
/
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
package main
import (
"bufio"
"context"
"os"
"github.com/gridscale/gsclient-go/v3"
log "github.com/sirupsen/logrus"
)
var emptyCtx = context.Background()
func main() {
uuid := os.Getenv("GRIDSCALE_UUID")
token := os.Getenv("GRIDSCALE_TOKEN")
config := gsclient.DefaultConfiguration(uuid, token)
client := gsclient.NewClient(config)
log.Info("gridscale client configured")
log.Info("Create firewall: Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
labels := []string{"fw-http"}
fwRequest := gsclient.FirewallCreateRequest{
Name: "go-client-firewall",
Labels: labels,
Rules: gsclient.FirewallRules{
RulesV4In: []gsclient.FirewallRuleProperties{
{
Protocol: gsclient.TCPTransport,
Action: "accept",
Order: 1,
},
},
},
}
// Create a new firewall
cfw, err := client.CreateFirewall(emptyCtx, fwRequest)
if err != nil {
log.Error("Create firewall has failed with error", err)
return
}
log.WithFields(log.Fields{"Firewall_uuid": cfw.ObjectUUID}).Info("Firewall successfully created")
log.Info("Update firewall: Press 'Enter' to continue...")
defer func() {
err := client.DeleteFirewall(emptyCtx, cfw.ObjectUUID)
if err != nil {
log.Error("Delete firewall has failed with error", err)
return
}
log.Info("Firewall has successfully deleted")
}()
bufio.NewReader(os.Stdin).ReadBytes('\n')
// Get a firewall to update
fw, err := client.GetFirewall(emptyCtx, cfw.ObjectUUID)
if err != nil {
log.Errorf("Get firewall %s has failed with error %v", cfw.ObjectUUID, err)
return
}
fwUpdateRequest := gsclient.FirewallUpdateRequest{
Name: "Updated name",
Labels: &fw.Properties.Labels,
Rules: &fw.Properties.Rules,
}
err = client.UpdateFirewall(emptyCtx, fw.Properties.ObjectUUID, fwUpdateRequest)
if err != nil {
log.Error("Update firewall has failed with error", err)
return
}
// Get firewall events
events, err := client.GetFirewallEventList(emptyCtx, fw.Properties.ObjectUUID)
if err != nil {
log.Error("Get firewall's events has failed with error", err)
return
}
log.WithFields(log.Fields{
"firewall_uuid": fw.Properties.ObjectUUID,
"events": events}).Info("Firewall's events successfully retrieved")
log.Info("Delete firewall: Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}