forked from G-Core/gcorelabscloud-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.go
298 lines (258 loc) · 9.97 KB
/
requests.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package securitygroups
import (
"net/http"
gcorecloud "github.com/alexandrpaliy/gcorelabscloud-go"
"github.com/alexandrpaliy/gcorelabscloud-go/gcore/instance/v1/instances"
"github.com/alexandrpaliy/gcorelabscloud-go/gcore/securitygroup/v1/types"
"github.com/alexandrpaliy/gcorelabscloud-go/pagination"
)
// ListOpts allows the filtering and sorting of paginated collections through the API.
type ListOpts struct {
MetadataK string `q:"metadata_k" validate:"omitempty"`
MetadataKV map[string]string `q:"metadata_kv" validate:"omitempty"`
}
// ToSequirityGroupListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToSecurityGroupListQuery() (string, error) {
if err := gcorecloud.ValidateStruct(opts); err != nil {
return "", err
}
q, err := gcorecloud.BuildQueryString(opts)
if err != nil {
return "", err
}
return q.String(), err
}
// ListOptsBuilder allows extensions to add additional parameters to the List request.
type ListOptsBuilder interface {
ToSecurityGroupListQuery() (string, error)
}
func List(c *gcorecloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := listURL(c)
if opts != nil {
query, err := opts.ToSecurityGroupListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
return SecurityGroupPage{pagination.LinkedPageBase{PageResult: r}}
})
}
// Get retrieves a specific security group based on its unique ID.
func Get(c *gcorecloud.ServiceClient, id string) (r GetResult) {
url := getURL(c, id)
_, r.Err = c.Get(url, &r.Body, nil)
return
}
// CreateOptsBuilder allows extensions to add additional parameters to the request.
type CreateOptsBuilder interface {
ToSecurityGroupCreateMap() (map[string]interface{}, error)
}
// CreateRuleOptsBuilder allows extensions to add additional parameters to the request.
type CreateRuleOptsBuilder interface {
ToRuleCreateMap() (map[string]interface{}, error)
}
// CreateSecurityGroupRuleOpts represents options used to create a security group rule.
type CreateSecurityGroupRuleOpts struct {
Direction types.RuleDirection `json:"direction" required:"true"`
EtherType types.EtherType `json:"ethertype,omitempty" required:"true"`
Protocol types.Protocol `json:"protocol,omitempty" required:"true"`
SecurityGroupID *string `json:"security_group_id,omitempty"`
RemoteGroupID *string `json:"remote_group_id,omitempty"`
PortRangeMax *int `json:"port_range_max,omitempty"`
PortRangeMin *int `json:"port_range_min,omitempty"`
Description *string `json:"description,omitempty"`
RemoteIPPrefix *string `json:"remote_ip_prefix,omitempty"`
}
// ToRuleCreateMap builds a request body from CreateSecurityGroupRuleOpts.
func (opts CreateSecurityGroupRuleOpts) ToRuleCreateMap() (map[string]interface{}, error) {
return gcorecloud.BuildRequestBody(opts, "")
}
type CreateSecurityGroupOpts struct {
Name string `json:"name" required:"true"`
Description *string `json:"description,omitempty"`
SecurityGroupRules []CreateSecurityGroupRuleOpts `json:"security_group_rules"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
// CreateOpts represents options used to create a security group.
type CreateOpts struct {
SecurityGroup CreateSecurityGroupOpts `json:"security_group" required:"true"`
Instances []string `json:"instances,omitempty"`
}
// ToSecurityGroupCreateMap builds a request body from CreateOpts.
func (opts CreateOpts) ToSecurityGroupCreateMap() (map[string]interface{}, error) {
if err := gcorecloud.ValidateStruct(opts); err != nil {
return nil, err
}
return gcorecloud.BuildRequestBody(opts, "")
}
// Create accepts a CreateOpts struct and creates a new security group using the values provided.
func Create(c *gcorecloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToSecurityGroupCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = c.Post(createURL(c), b, &r.Body, nil)
return
}
// UpdateOptsBuilder allows extensions to add additional parameters to the Update request.
type UpdateOptsBuilder interface {
ToSecurityGroupUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts represents options used to update a security group.
type UpdateOpts struct {
Name string `json:"name" required:"true"`
}
// ToSecurityGroupUpdateMap builds a request body from UpdateOpts.
func (opts UpdateOpts) ToSecurityGroupUpdateMap() (map[string]interface{}, error) {
if err := gcorecloud.ValidateStruct(opts); err != nil {
return nil, err
}
return gcorecloud.BuildRequestBody(opts, "")
}
// Update accepts a UpdateOpts struct and updates an existing security group using the
// values provided. For more information, see the Create function.
func Update(c *gcorecloud.ServiceClient, securityGroupID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToSecurityGroupUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = c.Patch(updateURL(c, securityGroupID), b, &r.Body, &gcorecloud.RequestOpts{
OkCodes: []int{200, 201},
})
return
}
// Delete accepts a unique ID and deletes the security group associated with it.
func Delete(c *gcorecloud.ServiceClient, securityGroupID string) (r DeleteResult) {
_, r.Err = c.Delete(deleteURL(c, securityGroupID), nil)
return
}
// ListAll returns all SGs
func ListAll(c *gcorecloud.ServiceClient, opts ListOptsBuilder) ([]SecurityGroup, error) {
page, err := List(c, opts).AllPages()
if err != nil {
return nil, err
}
return ExtractSecurityGroups(page)
}
// AddRule accepts a CreateSecurityGroupRuleOpts struct and add rule to existed group.
func AddRule(c *gcorecloud.ServiceClient, securityGroupID string, opts CreateRuleOptsBuilder) (r CreateRuleResult) {
b, err := opts.ToRuleCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = c.Post(addRulesURL(c, securityGroupID), b, &r.Body, nil)
return
}
// ListInstances returns page of instances for SG
func ListInstances(c *gcorecloud.ServiceClient, securityGroupID string) pagination.Pager {
url := listInstancesURL(c, securityGroupID)
return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
return SecurityGroupInstancesPage{pagination.LinkedPageBase{PageResult: r}}
})
}
// ListAllInstances returns all instances for SG
func ListAllInstances(c *gcorecloud.ServiceClient, securityGroupID string) ([]instances.Instance, error) {
page, err := ListInstances(c, securityGroupID).AllPages()
if err != nil {
return nil, err
}
return ExtractSecurityGroupInstances(page)
}
// IDFromName is a convenience function that returns a security group's ID,
// given its name.
func IDFromName(client *gcorecloud.ServiceClient, name string) (string, error) {
count := 0
id := ""
sgs, err := ListAll(client, nil)
if err != nil {
return "", err
}
for _, s := range sgs {
if s.Name == name {
count++
id = s.ID
}
}
switch count {
case 0:
return "", gcorecloud.ErrResourceNotFound{Name: name, ResourceType: "security group"}
case 1:
return id, nil
default:
return "", gcorecloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "security group"}
}
}
// DeepCopyOptsBuilder allows extensions to add additional parameters to the request.
type DeepCopyOptsBuilder interface {
ToDeepCopyMap() (map[string]interface{}, error)
}
// DeepCopyOpts represents options used to deep copy a security group.
type DeepCopyOpts struct {
Name string `json:"name" required:"true"`
}
// ToDeepCopyMap builds a request body from DeepCopyOpts.
func (opts DeepCopyOpts) ToDeepCopyMap() (map[string]interface{}, error) {
if err := gcorecloud.ValidateStruct(opts); err != nil {
return nil, err
}
return gcorecloud.BuildRequestBody(opts, "")
}
// DeepCopy accepts a DeepCopyOpts struct and create a deep copy of security group.
func DeepCopy(c *gcorecloud.ServiceClient, securityGroupID string, opts DeepCopyOptsBuilder) (r DeepCopyResult) {
b, err := opts.ToDeepCopyMap()
if err != nil {
r.Err = err
return
}
_, r.Err = c.Post(deepCopyURL(c, securityGroupID), b, nil, nil)
return
}
func MetadataList(client *gcorecloud.ServiceClient, id string) pagination.Pager {
url := metadataURL(client, id)
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return MetadataPage{pagination.LinkedPageBase{PageResult: r}}
})
}
func MetadataListAll(client *gcorecloud.ServiceClient, id string) ([]Metadata, error) {
pages, err := MetadataList(client, id).AllPages()
if err != nil {
return nil, err
}
all, err := ExtractMetadata(pages)
if err != nil {
return nil, err
}
return all, nil
}
// MetadataCreateOrUpdate creates or update a metadata for an security group.
func MetadataCreateOrUpdate(client *gcorecloud.ServiceClient, id string, opts map[string]interface{}) (r MetadataActionResult) {
_, r.Err = client.Post(metadataURL(client, id), opts, nil, &gcorecloud.RequestOpts{ // nolint
OkCodes: []int{http.StatusNoContent, http.StatusOK},
})
return
}
// MetadataReplace replace a metadata for an security group.
func MetadataReplace(client *gcorecloud.ServiceClient, id string, opts map[string]interface{}) (r MetadataActionResult) {
_, r.Err = client.Put(metadataURL(client, id), opts, nil, &gcorecloud.RequestOpts{ // nolint
OkCodes: []int{http.StatusNoContent, http.StatusOK},
})
return
}
// MetadataDelete deletes defined metadata key for a security group.
func MetadataDelete(client *gcorecloud.ServiceClient, id string, key string) (r MetadataActionResult) {
_, r.Err = client.Delete(metadataItemURL(client, id, key), &gcorecloud.RequestOpts{ // nolint
OkCodes: []int{http.StatusNoContent, http.StatusOK},
})
return
}
// MetadataGet gets defined metadata key for a security group.
func MetadataGet(client *gcorecloud.ServiceClient, id string, key string) (r MetadataResult) {
url := metadataItemURL(client, id, key)
_, r.Err = client.Get(url, &r.Body, nil) // nolint
return
}