forked from gophercloud/utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
139 lines (115 loc) · 3.59 KB
/
results.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
package resourcetypes
import (
"encoding/json"
"github.com/cro8ox/gophercloud"
"github.com/cro8ox/gophercloud/pagination"
)
type commonResult struct {
gophercloud.Result
}
// Extract is a function that accepts a result and extracts a Gnocchi resource type.
func (r commonResult) Extract() (*ResourceType, error) {
var s *ResourceType
err := r.ExtractInto(&s)
return s, err
}
// GetResult represents the result of a get operation. Call its Extract
// method to interpret it as a Gnocchi resource type.
type GetResult struct {
commonResult
}
// CreateResult represents the result of a create operation. Call its Extract
// method to interpret it as a Gnocchi resource type.
type CreateResult struct {
commonResult
}
// UpdateResult represents the result of an update operation. Call its Extract
// method to interpret it as a Gnocchi resource type.
type UpdateResult struct {
commonResult
}
// DeleteResult represents the result of a delete operation. Call its
// ExtractErr method to determine if the request succeeded or failed.
type DeleteResult struct {
gophercloud.ErrResult
}
// ResourceType represents custom Gnocchi resource type.
type ResourceType struct {
// Attributes is a collection of keys and values of different resource types.
Attributes map[string]Attribute `json:"-"`
// Name is a human-readable resource type identifier.
Name string `json:"name"`
// State represents current status of a resource type.
State string `json:"state"`
}
// Attribute represents single attribute of a Gnocchi resource type.
type Attribute struct {
// Type is an attribute type.
Type string `json:"type"`
// Details represents different attribute fields.
Details map[string]interface{}
}
// UnmarshalJSON helps to unmarshal ResourceType fields into needed values.
func (r *ResourceType) UnmarshalJSON(b []byte) error {
type tmp ResourceType
var s struct {
tmp
Attributes map[string]interface{} `json:"attributes"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*r = ResourceType(s.tmp)
if s.Attributes == nil {
return nil
}
// Populate attributes from the JSON map structure.
attributes := make(map[string]Attribute)
for attributeName, attributeValues := range s.Attributes {
attribute := new(Attribute)
attribute.Details = make(map[string]interface{})
attributeValuesMap, ok := attributeValues.(map[string]interface{})
if !ok {
// Got some strange resource type attribute representation, skip it.
continue
}
// Populate extra and type attribute values.
for k, v := range attributeValuesMap {
if k == "type" {
if attributeType, ok := v.(string); ok {
attribute.Type = attributeType
}
} else {
attribute.Details[k] = v
}
}
attributes[attributeName] = *attribute
}
r.Attributes = attributes
return err
}
// ResourceTypePage abstracts the raw results of making a List() request against
// the Gnocchi API.
//
// As Gnocchi API may freely alter the response bodies of structures
// returned to the client, you may only safely access the data provided through
// the ExtractResources call.
type ResourceTypePage struct {
pagination.SinglePageBase
}
// IsEmpty checks whether a ResourceTypePage struct is empty.
func (r ResourceTypePage) IsEmpty() (bool, error) {
is, err := ExtractResourceTypes(r)
return len(is) == 0, err
}
// ExtractResourceTypes interprets the results of a single page from a List() call,
// producing a slice of ResourceType structs.
func ExtractResourceTypes(r pagination.Page) ([]ResourceType, error) {
var s []ResourceType
err := (r.(ResourceTypePage)).ExtractInto(&s)
if err != nil {
return nil, err
}
return s, err
}