-
Notifications
You must be signed in to change notification settings - Fork 52
/
requests.go
135 lines (111 loc) · 4.12 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
package metrics
import (
"context"
"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/pagination"
)
// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
ToMetricListQuery() (string, error)
}
// ListOpts allows the limiting and sorting of paginated collections through
// the Gnocchi API.
type ListOpts struct {
// Limit allows to limits count of metrics in the response.
Limit int `q:"limit"`
// Marker is used for pagination.
Marker string `q:"marker"`
// SortKey allows to sort metrics 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"`
// Creator shows who created the metric.
// Usually it contains concatenated string with values from
// "created_by_user_id" and "created_by_project_id" fields.
Creator string `json:"creator"`
// ProjectID is the Identity project of the metric.
ProjectID string `json:"project_id"`
// UserID is the Identity user of the metric.
UserID string `json:"user_id"`
}
// ToMetricListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToMetricListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
return q.String(), err
}
// List returns a Pager which allows you to iterate over a collection of
// metrics. 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) pagination.Pager {
url := listURL(c)
if opts != nil {
query, err := opts.ToMetricListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
p := MetricPage{pagination.MarkerPageBase{PageResult: r}}
p.MarkerPageBase.Owner = p
return p
})
return pager
}
// Get retrieves a specific Gnocchi metric based on its id.
func Get(ctx context.Context, c *gophercloud.ServiceClient, metricID string) (r GetResult) {
_, r.Err = c.Get(ctx, getURL(c, metricID), &r.Body, nil)
return
}
// CreateOptsBuilder allows to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToMetricCreateMap() (map[string]interface{}, error)
}
// CreateOpts specifies parameters of a new Gnocchi metric.
type CreateOpts struct {
// ArchivePolicyName is a name of the Gnocchi archive policy that describes
// the aggregate storage policy of a metric.
// You can omit it in the request if your Gnocchi installation has the needed
// archive policy rule to assign an archive policy by a metric's name.
ArchivePolicyName string `json:"archive_policy_name,omitempty"`
// Name is a human-readable name for the Gnocchi metric.
// You must provide it if you are also providing a ResourceID in the request.
Name string `json:"name,omitempty"`
// ResourceID identifies the associated Gnocchi resource of the metric.
ResourceID string `json:"resource_id,omitempty"`
// Unit is a unit of measurement for measures of that Gnocchi metric.
Unit string `json:"unit,omitempty"`
}
// ToMetricCreateMap constructs a request body from CreateOpts.
func (opts CreateOpts) ToMetricCreateMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
return b, nil
}
// Create requests the creation of a new Gnocchi metric on the server.
func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToMetricCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(ctx, createURL(client), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{201},
})
return
}
// Delete accepts a unique ID and deletes the Gnocchi metric associated with it.
func Delete(ctx context.Context, c *gophercloud.ServiceClient, metricID string) (r DeleteResult) {
requestOpts := &gophercloud.RequestOpts{
MoreHeaders: map[string]string{
"Accept": "application/json, */*",
},
}
_, r.Err = c.Delete(ctx, deleteURL(c, metricID), requestOpts)
return
}