forked from G-Core/gcorelabscloud-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.go
100 lines (85 loc) · 2.73 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
package regionsaccess
import (
"net/http"
gcorecloud "github.com/alexandrpaliy/gcorelabscloud-go"
"github.com/alexandrpaliy/gcorelabscloud-go/pagination"
)
// ListOptsBuilder allows extensions to add additional parameters to the List request.
type ListOptsBuilder interface {
ToRegionAccessListQuery() (string, error)
}
// ListOpts allows the filtering and sorting of paginated collections through the API.
type ListOpts struct {
ResellerID int `q:"reseller_id"`
ClientID int `q:"client_id"`
}
// ToRegionAccessListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToRegionAccessListQuery() (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
}
// CreateOptsBuilder allows extensions to add additional parameters to the Create requets.
type CreateOptsBuilder interface {
ToRegionAccessCreateMap() (map[string]interface{}, error)
}
// CreateOpts set parameters for Create operation
type CreateOpts struct {
AccessAllEdgeRegions bool `json:"access_all_edge_regions"`
RegionIDs []int `json:"region_ids"`
ClientID *int `json:"client_id"`
ResellerID *int `json:"reseller_id"`
}
// Validate
func (opts CreateOpts) Validate() error {
return gcorecloud.ValidateStruct(opts)
}
// ToRegionAccessCreateMap builds a request body form CreateOpts
func (opts CreateOpts) ToRegionAccessCreateMap() (map[string]interface{}, error) {
if err := opts.Validate(); err != nil {
return nil, err
}
return gcorecloud.BuildRequestBody(opts, "")
}
func List(c *gcorecloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := rootURL(c)
if opts != nil {
query, err := opts.ToRegionAccessListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
return RegionAccessPage{pagination.LinkedPageBase{PageResult: r}}
})
}
func ListAll(c *gcorecloud.ServiceClient, opts ListOptsBuilder) ([]RegionAccess, error) {
pages, err := List(c, opts).AllPages()
if err != nil {
return nil, err
}
all, err := ExtractRegionsAccess(pages)
if err != nil {
return nil, err
}
return all, nil
}
func Delete(c *gcorecloud.ServiceClient, resellerID int) (r DeleteResult) {
_, r.Err = c.Delete(resourceURL(c, resellerID), &gcorecloud.RequestOpts{OkCodes: []int{http.StatusNoContent}})
return
}
func Create(c *gcorecloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToRegionAccessCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = c.Put(rootURL(c), b, &r.Body, &gcorecloud.RequestOpts{OkCodes: []int{http.StatusOK}})
return
}