/
requests.go
217 lines (177 loc) · 6.37 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
package resources
import (
"time"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
"github.com/gophercloud/utils/gnocchi"
)
// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
ToResourceListQuery() (string, error)
}
// ListOpts allows the limiting and sorting of paginated collections through
// the Gnocchi API.
type ListOpts struct {
// Details allows to list resources with all attributes.
Details bool `q:"details"`
// Limit allows to limits count of resources in the response.
Limit int `q:"limit"`
// Marker is used for pagination.
Marker string `q:"marker"`
// SortKey allows to sort resources in the response by key.
SortKey string `q:"sort_key"`
// SortDir allows to set the direction of sorting.
// Can be `asc` or `desc`.
SortDir string `q:"sort_dir"`
}
// ToResourceListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToResourceListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
return q.String(), err
}
// List returns a Pager which allows you to iterate over a collection of
// resources. It accepts a ListOpts struct, which allows you to limit and sort
// the returned collection for a greater efficiency.
func List(c *gophercloud.ServiceClient, opts ListOptsBuilder, resourceType string) pagination.Pager {
url := listURL(c, resourceType)
if opts != nil {
query, err := opts.ToResourceListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
p := ResourcePage{pagination.MarkerPageBase{PageResult: r}}
p.MarkerPageBase.Owner = p
return p
})
return pager
}
// Get retrieves a specific Gnocchi resource based on its type and ID.
func Get(c *gophercloud.ServiceClient, resourceType string, resourceID string) (r GetResult) {
_, r.Err = c.Get(getURL(c, resourceType, resourceID), &r.Body, nil)
return
}
// CreateOptsBuilder allows to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToResourceCreateMap() (map[string]interface{}, error)
}
// CreateOpts specifies parameters of a new Gnocchi resource.
type CreateOpts struct {
// ID uniquely identifies the Gnocchi resource.
ID string `json:"id" required:"true"`
// Metrics field can be used to link existing metrics in the resource
// or to create metrics with the resource at the same time to save
// some requests.
Metrics map[string]interface{} `json:"metrics,omitempty"`
// ProjectID is the Identity project of the resource.
ProjectID string `json:"project_id,omitempty"`
// UserID is the Identity user of the resource.
UserID string `json:"user_id,omitempty"`
// StartedAt is a resource creation timestamp.
StartedAt *time.Time `json:"-"`
// EndedAt is a timestamp of when the resource has ended.
EndedAt *time.Time `json:"-"`
// ExtraAttributes is a collection of keys and values that can be found in resources
// of different resource types.
ExtraAttributes map[string]interface{} `json:"-"`
}
// ToResourceCreateMap constructs a request body from CreateOpts.
func (opts CreateOpts) ToResourceCreateMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
if opts.StartedAt != nil {
b["started_at"] = opts.StartedAt.Format(gnocchi.RFC3339NanoTimezone)
}
if opts.EndedAt != nil {
b["ended_at"] = opts.EndedAt.Format(gnocchi.RFC3339NanoTimezone)
}
if opts.ExtraAttributes != nil {
for key, value := range opts.ExtraAttributes {
b[key] = value
}
}
return b, nil
}
// Create requests the creation of a new Gnocchi resource on the server.
func Create(client *gophercloud.ServiceClient, resourceType string, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToResourceCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(createURL(client, resourceType), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{201},
})
return
}
// UpdateOptsBuilder allows extensions to add additional parameters to the
// Update request.
type UpdateOptsBuilder interface {
ToResourceUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts represents options used to update a network.
type UpdateOpts struct {
// Metrics field can be used to link existing metrics in the resource
// or to create metrics and update the resource at the same time to save
// some requests.
Metrics *map[string]interface{} `json:"metrics,omitempty"`
// ProjectID is the Identity project of the resource.
ProjectID string `json:"project_id,omitempty"`
// UserID is the Identity user of the resource.
UserID string `json:"user_id,omitempty"`
// StartedAt is a resource creation timestamp.
StartedAt *time.Time `json:"-"`
// EndedAt is a timestamp of when the resource has ended.
EndedAt *time.Time `json:"-"`
// ExtraAttributes is a collection of keys and values that can be found in resources
// of different resource types.
ExtraAttributes map[string]interface{} `json:"-"`
}
// ToResourceUpdateMap builds a request body from UpdateOpts.
func (opts UpdateOpts) ToResourceUpdateMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
if opts.StartedAt != nil {
b["started_at"] = opts.StartedAt.Format(gnocchi.RFC3339NanoTimezone)
}
if opts.EndedAt != nil {
b["ended_at"] = opts.EndedAt.Format(gnocchi.RFC3339NanoTimezone)
}
if opts.ExtraAttributes != nil {
for key, value := range opts.ExtraAttributes {
b[key] = value
}
}
return b, nil
}
// Update accepts a UpdateOpts struct and updates an existing Gnocchi resource using the
// values provided.
func Update(c *gophercloud.ServiceClient, resourceType, resourceID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToResourceUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = c.Patch(updateURL(c, resourceType, resourceID), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
return
}
// Delete accepts a unique ID and deletes the Gnocchi resource associated with it.
func Delete(c *gophercloud.ServiceClient, resourceType, resourceID string) (r DeleteResult) {
requestOpts := &gophercloud.RequestOpts{
MoreHeaders: map[string]string{
"Accept": "application/json, */*",
},
}
_, r.Err = c.Delete(deleteURL(c, resourceType, resourceID), requestOpts)
return
}