-
Notifications
You must be signed in to change notification settings - Fork 56
/
firewall_group.go
135 lines (108 loc) · 4.1 KB
/
firewall_group.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
package govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// FirewallGroupService is the interface to interact with the firewall group endpoints on the Vultr API
// Link : https://www.vultr.com/api/#tag/firewall
type FirewallGroupService interface { //nolint:dupl
Create(ctx context.Context, fwGroupReq *FirewallGroupReq) (*FirewallGroup, *http.Response, error)
Get(ctx context.Context, groupID string) (*FirewallGroup, *http.Response, error)
Update(ctx context.Context, fwGroupID string, fwGroupReq *FirewallGroupReq) error
Delete(ctx context.Context, fwGroupID string) error
List(ctx context.Context, options *ListOptions) ([]FirewallGroup, *Meta, *http.Response, error)
}
// FireWallGroupServiceHandler handles interaction with the firewall group methods for the Vultr API
type FireWallGroupServiceHandler struct {
client *Client
}
// FirewallGroup represents a Vultr firewall group
type FirewallGroup struct {
ID string `json:"id"`
Description string `json:"description"`
DateCreated string `json:"date_created"`
DateModified string `json:"date_modified"`
InstanceCount int `json:"instance_count"`
RuleCount int `json:"rule_count"`
MaxRuleCount int `json:"max_rule_count"`
}
// FirewallGroupReq struct is used to create and update a Firewall Group.
type FirewallGroupReq struct {
Description string `json:"description"`
}
type firewallGroupsBase struct {
FirewallGroups []FirewallGroup `json:"firewall_groups"`
Meta *Meta `json:"meta"`
}
type firewallGroupBase struct {
FirewallGroup *FirewallGroup `json:"firewall_group"`
}
// Create will create a new firewall group on your Vultr account
func (f *FireWallGroupServiceHandler) Create(ctx context.Context, fwGroupReq *FirewallGroupReq) (*FirewallGroup, *http.Response, error) {
uri := "/v2/firewalls"
req, err := f.client.NewRequest(ctx, http.MethodPost, uri, fwGroupReq)
if err != nil {
return nil, nil, err
}
firewall := new(firewallGroupBase)
resp, err := f.client.DoWithContext(ctx, req, firewall)
if err != nil {
return nil, resp, err
}
return firewall.FirewallGroup, resp, nil
}
// Get will return a firewall group based on provided groupID from your Vultr account
func (f *FireWallGroupServiceHandler) Get(ctx context.Context, fwGroupID string) (*FirewallGroup, *http.Response, error) {
uri := fmt.Sprintf("/v2/firewalls/%s", fwGroupID)
req, err := f.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
firewall := new(firewallGroupBase)
resp, err := f.client.DoWithContext(ctx, req, firewall)
if err != nil {
return nil, resp, err
}
return firewall.FirewallGroup, resp, nil
}
// Update will change the description of a firewall group
func (f *FireWallGroupServiceHandler) Update(ctx context.Context, fwGroupID string, fwGroupReq *FirewallGroupReq) error {
uri := fmt.Sprintf("/v2/firewalls/%s", fwGroupID)
req, err := f.client.NewRequest(ctx, http.MethodPut, uri, fwGroupReq)
if err != nil {
return err
}
_, err = f.client.DoWithContext(ctx, req, nil)
return err
}
// Delete will delete a firewall group from your Vultr account
func (f *FireWallGroupServiceHandler) Delete(ctx context.Context, fwGroupID string) error {
uri := fmt.Sprintf("/v2/firewalls/%s", fwGroupID)
req, err := f.client.NewRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
_, err = f.client.DoWithContext(ctx, req, nil)
return err
}
// List will return a list of all firewall groups on your Vultr account
func (f *FireWallGroupServiceHandler) List(ctx context.Context, options *ListOptions) ([]FirewallGroup, *Meta, *http.Response, error) { //nolint:dupl,lll
uri := "/v2/firewalls"
req, err := f.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
firewalls := new(firewallGroupsBase)
resp, err := f.client.DoWithContext(ctx, req, firewalls)
if err != nil {
return nil, nil, resp, err
}
return firewalls.FirewallGroups, firewalls.Meta, resp, nil
}