File: requests.go

package info (click to toggle)
golang-github-rackspace-gophercloud 1.0.0%2Bgit20161013.1012.e00690e8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,148 kB
  • ctags: 6,414
  • sloc: sh: 16; makefile: 6
file content (149 lines) | stat: -rw-r--r-- 4,003 bytes parent folder | download
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
package meters

import (
	"github.com/rackspace/gophercloud"
)

// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
	ToMeterListQuery() (string, error)
}

// ListOpts allows the filtering and sorting of collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the server attributes you want to see returned.
type ListOpts struct {
	QueryField string `q:"q.field"`
	QueryOp    string `q:"q.op"`
	QueryValue string `q:"q.value"`

	// ID of the last-seen item from the previous response
	Marker string `q:"marker"`

	// Optional, maximum number of results to return
	Limit int `q:"limit"`
}

// ToMeterListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToMeterListQuery() (string, error) {
	q, err := gophercloud.BuildQueryString(opts)
	if err != nil {
		return "", err
	}
	return q.String(), nil
}

// List makes a request against the API to list meters accessible to you.
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) ListResult {
	var res ListResult
	url := listURL(client)

	if opts != nil {
		query, err := opts.ToMeterListQuery()
		if err != nil {
			res.Err = err
			return res
		}
		url += query
	}

	_, res.Err = client.Get(url, &res.Body, &gophercloud.RequestOpts{})
	return res
}

// ShowOptsBuilder allows extensions to add additional parameters to the
// Show request.
type ShowOptsBuilder interface {
	ToShowQuery() (string, error)
}

// ShowOpts allows the filtering and sorting of collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the server attributes you want to see returned.
type ShowOpts struct {
	QueryField string `q:"q.field"`
	QueryOp    string `q:"q.op"`
	QueryValue string `q:"q.value"`

	// ID of the last-seen item from the previous response
	Marker string `q:"marker"`

	// Optional, maximum number of results to return
	Limit int `q:"limit"`
}

// ToMeterShowQuery formats a ShowOpts into a query string.
func (opts ShowOpts) ToShowQuery() (string, error) {
	q, err := gophercloud.BuildQueryString(opts)
	if err != nil {
		return "", err
	}
	return q.String(), nil
}

// Show makes a request against the API to show a specific meter
func Show(client *gophercloud.ServiceClient, meterName string, opts ShowOptsBuilder) ShowResult {
	var res ShowResult
	url := showURL(client, meterName)

	if opts != nil {
		query, err := opts.ToShowQuery()
		if err != nil {
			res.Err = err
			return res
		}
		url += query
	}

	_, res.Err = client.Get(url, &res.Body, &gophercloud.RequestOpts{})
	return res
}

// StatisticsOptsBuilder allows extensions to add additional parameters to the
// List request.
type MeterStatisticsOptsBuilder interface {
	ToMeterStatisticsQuery() (string, error)
}

// StatisticsOpts allows the filtering and sorting of collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the server attributes you want to see returned.
type MeterStatisticsOpts struct {
	QueryField string `q:"q.field"`
	QueryOp    string `q:"q.op"`
	QueryValue string `q:"q.value"`

	// Optional group by
	GroupBy string `q:"groupby"`

	// Optional number of seconds in a period
	Period int `q:"period"`
}

// ToStatisticsQuery formats a StatisticsOpts into a query string.
func (opts MeterStatisticsOpts) ToMeterStatisticsQuery() (string, error) {
	q, err := gophercloud.BuildQueryString(opts)
	if err != nil {
		return "", err
	}
	return q.String(), nil
}

// List makes a request against the API to list meters accessible to you.
func MeterStatistics(client *gophercloud.ServiceClient, n string, opts MeterStatisticsOptsBuilder) StatisticsResult {
	var res StatisticsResult
	url := statisticsURL(client, n)

	if opts != nil {
		query, err := opts.ToMeterStatisticsQuery()
		if err != nil {
			res.Err = err
			return res
		}
		url += query
	}

	_, res.Err = client.Get(url, &res.Body, &gophercloud.RequestOpts{})
	return res
}