Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 150a922

Browse files
committed
initial telemetry v2
1 parent b8b50b9 commit 150a922

File tree

5 files changed

+220
-3
lines changed

5 files changed

+220
-3
lines changed

openstack/client.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ func NewClient(endpoint string) (*gophercloud.ProviderClient, error) {
3939
}
4040

4141
parts := strings.Split(path[0:len(path)-1], "/")
42-
for index,version := range(parts) {
42+
for index, version := range parts {
4343
if 2 <= len(version) && len(version) <= 4 && strings.HasPrefix(version, "v") {
4444
_, err := strconv.ParseFloat(version[1:], 64)
4545
if err == nil {
4646
// post version suffixes in path are not supported
4747
// version must be on the last index
48-
if index < len(parts) - 1 {
48+
if index < len(parts)-1 {
4949
return nil, fmt.Errorf("Path suffixes (after version) are not supported.")
5050
}
5151
switch version {
5252
case "v2.0", "v3":
5353
// valid version found, strip from base
5454
return &gophercloud.ProviderClient{
55-
IdentityBase: base[0:len(base)-len(version)-1],
55+
IdentityBase: base[0 : len(base)-len(version)-1],
5656
IdentityEndpoint: endpoint,
5757
}, nil
5858
default:
@@ -356,3 +356,13 @@ func NewImageServiceV2(client *gophercloud.ProviderClient, eo gophercloud.Endpoi
356356
Endpoint: url,
357357
ResourceBase: url + "v2/"}, nil
358358
}
359+
360+
// NewTelemetryV2 creates a ServiceClient that may be used to access the v2 telemetry service.
361+
func NewTelemetryV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
362+
eo.ApplyDefaults("metering")
363+
url, err := client.EndpointLocator(eo)
364+
if err != nil {
365+
return nil, err
366+
}
367+
return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
368+
}

openstack/telemetry/v2/meters/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package meters
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package meters
2+
3+
import "github.com/rackspace/gophercloud"
4+
5+
// ListOptsBuilder allows extensions to add additional parameters to the
6+
// List request.
7+
type ListOptsBuilder interface {
8+
ToMeterListQuery() (string, error)
9+
}
10+
11+
// ListOpts allows the filtering and sorting of collections through
12+
// the API. Filtering is achieved by passing in struct field values that map to
13+
// the server attributes you want to see returned.
14+
type ListOpts struct {
15+
}
16+
17+
// ToServerListQuery formats a ListOpts into a query string.
18+
func (opts ListOpts) ToMeterListQuery() (string, error) {
19+
q, err := gophercloud.BuildQueryString(opts)
20+
if err != nil {
21+
return "", err
22+
}
23+
return q.String(), nil
24+
}
25+
26+
// List makes a request against the API to list meters accessible to you.
27+
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) (res listResult, err error) {
28+
url := listURL(client)
29+
30+
if opts != nil {
31+
query, err := opts.ToMeterListQuery()
32+
if err != nil {
33+
return res, err
34+
}
35+
url += query
36+
}
37+
38+
_, err = client.Get(url, &res.Body, &gophercloud.RequestOpts{})
39+
40+
return
41+
}
42+
43+
// StatisticsOptsBuilder allows extensions to add additional parameters to the
44+
// List request.
45+
type MeterStatisticsOptsBuilder interface {
46+
ToMeterStatisticsQuery() (string, error)
47+
}
48+
49+
// StatisticsOpts allows the filtering and sorting of collections through
50+
// the API. Filtering is achieved by passing in struct field values that map to
51+
// the server attributes you want to see returned.
52+
type MeterStatisticsOpts struct {
53+
QueryField string `q:"q.field"`
54+
QueryOp string `q:"q.op"`
55+
QueryValue string `q:"q.value"`
56+
57+
// Optional group by
58+
GroupBy string `q:"groupby"`
59+
60+
// Optional number of seconds in a period
61+
Period int `q:"period"`
62+
}
63+
64+
// ToStatisticsQuery formats a StatisticsOpts into a query string.
65+
func (opts MeterStatisticsOpts) ToMeterStatisticsQuery() (string, error) {
66+
q, err := gophercloud.BuildQueryString(opts)
67+
if err != nil {
68+
return "", err
69+
}
70+
return q.String(), nil
71+
}
72+
73+
// List makes a request against the API to list meters accessible to you.
74+
func MeterStatistics(client *gophercloud.ServiceClient, n string, opts MeterStatisticsOptsBuilder) (res statisticsResult, err error) {
75+
url := statisticsURL(client, n)
76+
77+
if opts != nil {
78+
query, err := opts.ToMeterStatisticsQuery()
79+
if err != nil {
80+
return res, err
81+
}
82+
url += query
83+
}
84+
85+
_, err = client.Get(url, &res.Body, &gophercloud.RequestOpts{})
86+
87+
return
88+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package meters
2+
3+
import (
4+
"reflect"
5+
6+
"github.com/mitchellh/mapstructure"
7+
"github.com/rackspace/gophercloud"
8+
)
9+
10+
type MeterListResult struct {
11+
MeterId string `json:"meter_id"`
12+
Name string `json:"name"`
13+
ProjectId string `json:"project_id"`
14+
ResourceId string `json:"resource_id"`
15+
Source string `json:"source"`
16+
Type string `json:"type"`
17+
Unit string `json:"user"`
18+
UserId string `json:"user_id"`
19+
}
20+
21+
type listResult struct {
22+
gophercloud.Result
23+
}
24+
25+
// Extract interprets any serverResult as a Server, if possible.
26+
func (r listResult) Extract() (*[]MeterListResult, error) {
27+
if r.Err != nil {
28+
return nil, r.Err
29+
}
30+
31+
var response []MeterListResult
32+
33+
config := &mapstructure.DecoderConfig{
34+
DecodeHook: toMapFromString,
35+
Result: &response,
36+
}
37+
decoder, err := mapstructure.NewDecoder(config)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
err = decoder.Decode(r.Body)
43+
if err != nil {
44+
return nil, err
45+
}
46+
47+
return &response, nil
48+
}
49+
50+
type MeterStatisticsResult struct {
51+
Avg float32 `json:"avg"`
52+
Count int `json:"count"`
53+
Duration float32 `json:"duration"`
54+
DurationEnd string `json:"duration_end"`
55+
DurationStart string `json:"duration_start"`
56+
Max float32 `json:"max"`
57+
Min float32 `json:"min"`
58+
Period int `json:"user_id"`
59+
PeriodEnd string `json:"period_end"`
60+
PeriodStart string `json:"period_start"`
61+
Sum float32 `json:"sum"`
62+
Unit string `json:"unit"`
63+
}
64+
65+
type statisticsResult struct {
66+
gophercloud.Result
67+
}
68+
69+
// Extract interprets any serverResult as a Server, if possible.
70+
func (r statisticsResult) Extract() (*[]MeterStatisticsResult, error) {
71+
if r.Err != nil {
72+
return nil, r.Err
73+
}
74+
75+
var response []MeterStatisticsResult
76+
77+
config := &mapstructure.DecoderConfig{
78+
DecodeHook: toMapFromString,
79+
Result: &response,
80+
}
81+
decoder, err := mapstructure.NewDecoder(config)
82+
if err != nil {
83+
return nil, err
84+
}
85+
86+
err = decoder.Decode(r.Body)
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
return &response, nil
92+
}
93+
94+
func toMapFromString(from reflect.Kind, to reflect.Kind, data interface{}) (interface{}, error) {
95+
if (from == reflect.String) && (to == reflect.Map) {
96+
return map[string]interface{}{}, nil
97+
}
98+
return data, nil
99+
}

openstack/telemetry/v2/meters/urls.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package meters
2+
3+
import "github.com/rackspace/gophercloud"
4+
5+
func listURL(client *gophercloud.ServiceClient) string {
6+
return client.ServiceURL("v2", "meters")
7+
}
8+
9+
func showURL(client *gophercloud.ServiceClient, name string) string {
10+
return client.ServiceURL("v2", "meters", name)
11+
}
12+
13+
func createURL(client *gophercloud.ServiceClient, name string) string {
14+
return client.ServiceURL("v2", "meters", name)
15+
}
16+
17+
func statisticsURL(client *gophercloud.ServiceClient, name string) string {
18+
return client.ServiceURL("v2", "meters", name, "statistics")
19+
}

0 commit comments

Comments
 (0)