-
Notifications
You must be signed in to change notification settings - Fork 1
/
firewall.go
73 lines (59 loc) · 2.05 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
package tumblebug
import (
"fmt"
"net/http"
"github.com/cloud-barista/poc-cicd-ladybug/src/core/model"
logger "github.com/sirupsen/logrus"
)
type Firewall struct {
Model
Config string `json:"connectionName"`
VPCId string `json:"vNetId"`
Description string `json:"description"`
FirewallRules []FirewallRules `json:"firewallRules"`
CspSecurityGroupId string `json:"cspSecurityGroupId"` // output
CspSecurityGroupName string `json:"cspSecurityGroupName"` // output
KeyValueList []KeyValue `json:"keyValueList"` // output
}
type FirewallRules struct {
From string `json:"fromPort"`
To string `json:"toPort"`
Protocol string `json:"ipProtocol"`
Direction string `json:"direction"`
}
func NewFirewall(ns string, name string, conf string) *Firewall {
return &Firewall{
Model: Model{Name: name, namespace: ns},
Config: conf,
FirewallRules: []FirewallRules{
{Protocol: "tcp", Direction: "inbound", From: "1", To: "65535"},
{Protocol: "udp", Direction: "inbound", From: "1", To: "65535"},
{Protocol: "icmp", Direction: "inbound", From: "-1", To: "-1"},
},
}
}
func (self *Firewall) GET() (bool, error) {
return self.execute(http.MethodGet, fmt.Sprintf("/ns/%s/resources/securityGroup/%s", self.namespace, self.Name), fmt.Sprintf(`{"connectionName" : "%s"}`, self.Config), &self)
}
func (self *Firewall) POST() error {
_, err := self.execute(http.MethodPost, fmt.Sprintf("/ns/%s/resources/securityGroup", self.namespace), self, &self)
if err != nil {
return err
}
return nil
}
func (self *Firewall) DELETE(ns string) error {
exist, err := self.GET()
if err != nil {
return err
}
if exist {
_, err := self.execute(http.MethodDelete, fmt.Sprintf("/ns/%s/resources/securityGroup/%s", self.namespace, self.Name), fmt.Sprintf(`{"connectionName" : "%s"}`, self.Config), model.Status{})
if err != nil {
return err
}
} else {
logger.Infof("delete firewall skip (name=%s, cause=not found)", self.Name)
}
return nil
}