-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsecurity_groups.go
226 lines (189 loc) · 9.14 KB
/
security_groups.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package egoscale
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
)
// SecurityGroup represent a firewalling set of rules
type SecurityGroup struct {
Account string `json:"account,omitempty" doc:"the account owning the security group"`
Description string `json:"description,omitempty" doc:"the description of the security group"`
EgressRule []EgressRule `json:"egressrule,omitempty" doc:"the list of egress rules associated with the security group"`
ID *UUID `json:"id" doc:"the ID of the security group"`
IngressRule []IngressRule `json:"ingressrule,omitempty" doc:"the list of ingress rules associated with the security group"`
Name string `json:"name,omitempty" doc:"the name of the security group"`
}
// UserSecurityGroup converts a SecurityGroup to a UserSecurityGroup
func (sg SecurityGroup) UserSecurityGroup() UserSecurityGroup {
return UserSecurityGroup{
Group: sg.Name,
}
}
// ListRequest builds the ListSecurityGroups request
func (sg SecurityGroup) ListRequest() (ListCommand, error) {
req := &ListSecurityGroups{
ID: sg.ID,
SecurityGroupName: sg.Name,
}
return req, nil
}
// Delete deletes the given Security Group
func (sg SecurityGroup) Delete(ctx context.Context, client *Client) error {
if sg.ID == nil && sg.Name == "" {
return fmt.Errorf("a SecurityGroup may only be deleted using ID or Name")
}
req := &DeleteSecurityGroup{}
if sg.ID != nil {
req.ID = sg.ID
} else {
req.Name = sg.Name
}
return client.BooleanRequestWithContext(ctx, req)
}
// RuleByID returns IngressRule or EgressRule by a rule ID
func (sg SecurityGroup) RuleByID(ruleID UUID) (*IngressRule, *EgressRule) {
for i, in := range sg.IngressRule {
if in.RuleID.Equal(ruleID) {
return &sg.IngressRule[i], nil
}
}
for i, out := range sg.EgressRule {
if out.RuleID.Equal(ruleID) {
return nil, &sg.EgressRule[i]
}
}
return nil, nil
}
// IngressRule represents the ingress rule
type IngressRule struct {
CIDR *CIDR `json:"cidr,omitempty" doc:"the CIDR notation for the base IP address of the security group rule"`
Description string `json:"description,omitempty" doc:"description of the security group rule"`
EndPort uint16 `json:"endport,omitempty" doc:"the ending port of the security group rule "`
IcmpCode int `json:"icmpcode,omitempty" doc:"the code for the ICMP message response"`
IcmpType int `json:"icmptype,omitempty" doc:"the type of the ICMP message response"`
Protocol string `json:"protocol,omitempty" doc:"the protocol of the security group rule"`
RuleID *UUID `json:"ruleid" doc:"the id of the security group rule"`
SecurityGroupName string `json:"securitygroupname,omitempty" doc:"security group name"`
StartPort uint16 `json:"startport,omitempty" doc:"the starting port of the security group rule"`
}
// EgressRule represents the ingress rule
type EgressRule IngressRule
// UserSecurityGroup represents the traffic of another security group
type UserSecurityGroup struct {
Group string `json:"group,omitempty"`
}
// String gives the UserSecurityGroup name
func (usg UserSecurityGroup) String() string {
return usg.Group
}
// CreateSecurityGroup represents a security group creation
type CreateSecurityGroup struct {
Name string `json:"name" doc:"name of the security group"`
Description string `json:"description,omitempty" doc:"the description of the security group"`
_ bool `name:"createSecurityGroup" description:"Creates a security group"`
}
// Response returns the struct to unmarshal
func (CreateSecurityGroup) Response() interface{} {
return new(SecurityGroup)
}
// DeleteSecurityGroup represents a security group deletion
type DeleteSecurityGroup struct {
ID *UUID `json:"id,omitempty" doc:"The ID of the security group. Mutually exclusive with name parameter"`
Name string `json:"name,omitempty" doc:"The ID of the security group. Mutually exclusive with id parameter"`
_ bool `name:"deleteSecurityGroup" description:"Deletes security group"`
}
// Response returns the struct to unmarshal
func (DeleteSecurityGroup) Response() interface{} {
return new(BooleanResponse)
}
// AuthorizeSecurityGroupIngress (Async) represents the ingress rule creation
type AuthorizeSecurityGroupIngress struct {
CIDRList []CIDR `json:"cidrlist,omitempty" doc:"the cidr list associated"`
Description string `json:"description,omitempty" doc:"the description of the ingress/egress rule"`
EndPort uint16 `json:"endport,omitempty" doc:"end port for this ingress/egress rule"`
IcmpCode int `json:"icmpcode,omitempty" doc:"error code for this icmp message"`
IcmpType int `json:"icmptype,omitempty" doc:"type of the icmp message being sent"`
Protocol string `json:"protocol,omitempty" doc:"TCP is default. UDP, ICMP, ICMPv6, AH, ESP, GRE, IPIP are the other supported protocols"`
SecurityGroupID *UUID `json:"securitygroupid,omitempty" doc:"The ID of the security group. Mutually exclusive with securitygroupname parameter"`
SecurityGroupName string `json:"securitygroupname,omitempty" doc:"The name of the security group. Mutually exclusive with securitygroupid parameter"`
StartPort uint16 `json:"startport,omitempty" doc:"start port for this ingress/egress rule"`
UserSecurityGroupList []UserSecurityGroup `json:"usersecuritygrouplist,omitempty" doc:"user to security group mapping"`
_ bool `name:"authorizeSecurityGroupIngress" description:"Authorize a particular ingress/egress rule for this security group"`
}
// Response returns the struct to unmarshal
func (AuthorizeSecurityGroupIngress) Response() interface{} {
return new(AsyncJobResult)
}
// AsyncResponse returns the struct to unmarshal the async job
func (AuthorizeSecurityGroupIngress) AsyncResponse() interface{} {
return new(SecurityGroup)
}
func (req AuthorizeSecurityGroupIngress) onBeforeSend(params url.Values) error {
// ICMP code and type may be zero but can also be omitted...
if strings.HasPrefix(strings.ToLower(req.Protocol), "icmp") {
params.Set("icmpcode", strconv.FormatInt(int64(req.IcmpCode), 10))
params.Set("icmptype", strconv.FormatInt(int64(req.IcmpType), 10))
}
// StartPort may be zero but can also be omitted...
if req.EndPort != 0 && req.StartPort == 0 {
params.Set("startport", "0")
}
return nil
}
// AuthorizeSecurityGroupEgress (Async) represents the egress rule creation
type AuthorizeSecurityGroupEgress AuthorizeSecurityGroupIngress
// Response returns the struct to unmarshal
func (AuthorizeSecurityGroupEgress) Response() interface{} {
return new(AsyncJobResult)
}
// AsyncResponse returns the struct to unmarshal the async job
func (AuthorizeSecurityGroupEgress) AsyncResponse() interface{} {
return new(SecurityGroup)
}
func (req AuthorizeSecurityGroupEgress) onBeforeSend(params url.Values) error {
return (AuthorizeSecurityGroupIngress)(req).onBeforeSend(params)
}
// RevokeSecurityGroupIngress (Async) represents the ingress/egress rule deletion
type RevokeSecurityGroupIngress struct {
ID *UUID `json:"id" doc:"The ID of the ingress rule"`
_ bool `name:"revokeSecurityGroupIngress" description:"Deletes a particular ingress rule from this security group"`
}
// Response returns the struct to unmarshal
func (RevokeSecurityGroupIngress) Response() interface{} {
return new(AsyncJobResult)
}
// AsyncResponse returns the struct to unmarshal the async job
func (RevokeSecurityGroupIngress) AsyncResponse() interface{} {
return new(BooleanResponse)
}
// RevokeSecurityGroupEgress (Async) represents the ingress/egress rule deletion
type RevokeSecurityGroupEgress struct {
ID *UUID `json:"id" doc:"The ID of the egress rule"`
_ bool `name:"revokeSecurityGroupEgress" description:"Deletes a particular egress rule from this security group"`
}
// Response returns the struct to unmarshal
func (RevokeSecurityGroupEgress) Response() interface{} {
return new(AsyncJobResult)
}
// AsyncResponse returns the struct to unmarshal the async job
func (RevokeSecurityGroupEgress) AsyncResponse() interface{} {
return new(BooleanResponse)
}
//go:generate go run generate/main.go -interface=Listable ListSecurityGroups
// ListSecurityGroups represents a search for security groups
type ListSecurityGroups struct {
ID *UUID `json:"id,omitempty" doc:"list the security group by the id provided"`
Keyword string `json:"keyword,omitempty" doc:"List by keyword"`
Page int `json:"page,omitempty"`
PageSize int `json:"pagesize,omitempty"`
SecurityGroupName string `json:"securitygroupname,omitempty" doc:"lists security groups by name"`
VirtualMachineID *UUID `json:"virtualmachineid,omitempty" doc:"lists security groups by virtual machine id"`
_ bool `name:"listSecurityGroups" description:"Lists security groups"`
}
// ListSecurityGroupsResponse represents a list of security groups
type ListSecurityGroupsResponse struct {
Count int `json:"count"`
SecurityGroup []SecurityGroup `json:"securitygroup"`
}