This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
requests.go
231 lines (201 loc) · 7.13 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
package hosts
import (
"reflect"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/pagination"
)
// AllocateOptsBuilder allows extensions to add additional parameters to the
// Allocate request.
type AllocateOptsBuilder interface {
ToDeHAllocateMap() (map[string]interface{}, error)
}
// AllocateOpts contains all the values needed to allocate a new DeH.
type AllocateOpts struct {
Name string `json:"name" required:"true"`
Az string `json:"availability_zone" required:"true"`
AutoPlacement string `json:"auto_placement,omitempty"`
HostType string `json:"host_type" required:"true"`
Quantity int `json:"quantity" required:"true"`
}
// ToDeHAllocateMap builds a allocate request body from AllocateOpts.
func (opts AllocateOpts) ToDeHAllocateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Allocate accepts a AllocateOpts struct and uses the values to allocate a new DeH.
func Allocate(c *golangsdk.ServiceClient, opts AllocateOptsBuilder) (r AllocateResult) {
b, err := opts.ToDeHAllocateMap()
if err != nil {
r.Err = err
return
}
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200, 201}}
_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
return
}
// UpdateOptsBuilder allows extensions to add additional parameters to the
// Update request.
type UpdateOptsBuilder interface {
ToDeHUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts contains all the values needed to update a DeH.
type UpdateOpts struct {
Name string `json:"name,omitempty"`
AutoPlacement string `json:"auto_placement,omitempty"`
}
// ToDeHUpdateMap builds a update request body from UpdateOpts.
func (opts UpdateOpts) ToDeHUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "dedicated_host")
}
// Update accepts a UpdateOpts struct and uses the values to update a DeH.The response code from api is 204
func Update(c *golangsdk.ServiceClient, hostID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToDeHUpdateMap()
if err != nil {
r.Err = err
return
}
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}}
_, r.Err = c.Put(resourceURL(c, hostID), b, nil, reqOpt)
return
}
//Deletes the DeH using the specified hostID.
func Delete(c *golangsdk.ServiceClient, hostid string) (r DeleteResult) {
_, r.Err = c.Delete(resourceURL(c, hostid), nil)
return
}
// ListOpts allows the filtering and sorting of paginated collections through
// the API.
type ListOpts struct {
// Specifies Dedicated Host ID.
ID string `q:"dedicated_host_id"`
// Specifies the Dedicated Host name.
Name string `q:"name"`
// Specifes the Dedicated Host type.
HostType string `q:"host_type"`
// Specifes the Dedicated Host name of type.
HostTypeName string `q:"host_type_name"`
// Specifies flavor ID.
Flavor string `q:"flavor"`
// Specifies the Dedicated Host status.
// The value can be available, fault or released.
State string `q:"state"`
// Specifies the AZ to which the Dedicated Host belongs.
Az string `q:"availability_zone"`
// Specifies the number of entries displayed on each page.
Limit string `q:"limit"`
// The value is the ID of the last record on the previous page.
Marker string `q:"marker"`
// Filters the response by a date and time stamp when the dedicated host last changed status.
ChangesSince string `q:"changes-since"`
// Specifies the UUID of the tenant in a multi-tenancy cloud.
TenantId string `q:"tenant"`
}
// ListOptsBuilder allows extensions to add parameters to the List request.
type ListOptsBuilder interface {
ToHostListQuery() (string, error)
}
// ToRegionListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToHostListQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
return q.String(), err
}
// List returns a Pager which allows you to iterate over a collection of
// dedicated hosts resources. It accepts a ListOpts struct, which allows you to
// filter the returned collection for greater efficiency.
func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := rootURL(c)
if opts != nil {
query, err := opts.ToHostListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
return HostPage{pagination.LinkedPageBase{PageResult: r}}
})
}
// Get retrieves a particular host based on its unique ID.
func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
return
}
// ListServerOpts allows the filtering and sorting of paginated collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the server attributes you want to see returned. Marker and Limit are used
// for pagination.
type ListServerOpts struct {
// Specifies the number of entries displayed on each page.
Limit int `q:"limit"`
// The value is the ID of the last record on the previous page.
// If the marker value is invalid, error code 400 will be returned.
Marker string `q:"marker"`
// ID uniquely identifies this server amongst all other servers,
// including those not accessible to the current tenant.
ID string `json:"id"`
// Name contains the human-readable name for the server.
Name string `json:"name"`
// Status contains the current operational status of the server,
// such as IN_PROGRESS or ACTIVE.
Status string `json:"status"`
// UserID uniquely identifies the user account owning the tenant.
UserID string `json:"user_id"`
}
// ListServer returns a Pager which allows you to iterate over a collection of
// dedicated hosts Server resources. It accepts a ListServerOpts struct, which allows you to
// filter the returned collection for greater efficiency.
func ListServer(c *golangsdk.ServiceClient, id string, opts ListServerOpts) ([]Server, error) {
q, err := golangsdk.BuildQueryString(&opts)
if err != nil {
return nil, err
}
u := listServerURL(c, id) + q.String()
pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
return ServerPage{pagination.LinkedPageBase{PageResult: r}}
}).AllPages()
if err != nil {
return nil, err
}
allservers, err := ExtractServers(pages)
if err != nil {
return nil, err
}
return FilterServers(allservers, opts)
}
func FilterServers(servers []Server, opts ListServerOpts) ([]Server, error) {
var refinedServers []Server
var matched bool
m := map[string]interface{}{}
if opts.ID != "" {
m["ID"] = opts.ID
}
if opts.Name != "" {
m["Name"] = opts.Name
}
if opts.Status != "" {
m["Status"] = opts.Status
}
if opts.UserID != "" {
m["UserID"] = opts.UserID
}
if len(m) > 0 && len(servers) > 0 {
for _, server := range servers {
matched = true
for key, value := range m {
if sVal := getStructServerField(&server, key); !(sVal == value) {
matched = false
}
}
if matched {
refinedServers = append(refinedServers, server)
}
}
} else {
refinedServers = servers
}
return refinedServers, nil
}
func getStructServerField(v *Server, field string) string {
r := reflect.ValueOf(v)
f := reflect.Indirect(r).FieldByName(field)
return string(f.String())
}