forked from gophercloud/gophercloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
170 lines (134 loc) · 3.87 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
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
package orders
import (
"encoding/json"
"time"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
// Order represents an order in the key manager service.
type Order struct {
// ContainerRef is the container URL.
ContainerRef string `json:"container_ref"`
// Created is when the order was created.
Created time.Time `json:"-"`
// CreatorID is the creator of the order.
CreatorID string `json:"creator_id"`
// ErrorReason is the reason of the error.
ErrorReason string `json:"error_reason"`
// ErrorStatusCode is the error status code.
ErrorStatusCode string `json:"error_status_code"`
// OrderRef is the order URL.
OrderRef string `json:"order_ref"`
// Meta is secret data about the order.
Meta Meta `json:"meta"`
// SecretRef is the secret URL.
SecretRef string `json:"secret_ref"`
// Status is the status of the order.
Status string `json:"status"`
// SubStatus is the status of the order.
SubStatus string `json:"sub_status"`
// SubStatusMessage is the message of the sub status.
SubStatusMessage string `json:"sub_status_message"`
// Type is the order type.
Type string `json:"type"`
// Updated is when the order was updated.
Updated time.Time `json:"-"`
}
func (r *Order) UnmarshalJSON(b []byte) error {
type tmp Order
var s struct {
tmp
Created gophercloud.JSONRFC3339NoZ `json:"created"`
Updated gophercloud.JSONRFC3339NoZ `json:"updated"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*r = Order(s.tmp)
r.Created = time.Time(s.Created)
r.Updated = time.Time(s.Updated)
return nil
}
type Meta struct {
// Algorithm is the algorithm of the secret.
Algorithm string `json:"algorithm"`
// BitLength is the bit length of the secret.
BitLength int `json:"bit_length"`
// Expiration is the expiration date of the order.
Expiration time.Time `json:"-"`
// Mode is the mode of the secret.
Mode string `json:"mode"`
// Name is the name of the secret.
Name string `json:"name"`
// PayloadContentType is the content type of the secret payload.
PayloadContentType string `json:"payload_content_type"`
}
func (r *Meta) UnmarshalJSON(b []byte) error {
type tmp Meta
var s struct {
tmp
Expiration gophercloud.JSONRFC3339NoZ `json:"expiration"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*r = Meta(s.tmp)
r.Expiration = time.Time(s.Expiration)
return nil
}
type commonResult struct {
gophercloud.Result
}
// GetResult is the response from a Get operation. Call its Extract method
// to interpret it as a orders.
type GetResult struct {
commonResult
}
// CreateResult is the response from a Create operation. Call its Extract method
// to interpret it as a orders.
type CreateResult struct {
commonResult
}
// DeleteResult is the response from a Delete operation. Call its ExtractErr to
// determine if the request succeeded or failed.
type DeleteResult struct {
gophercloud.ErrResult
}
// OrderPage is a single page of orders results.
type OrderPage struct {
pagination.LinkedPageBase
}
// IsEmpty determines whether or not a page of ordersS contains any results.
func (r OrderPage) IsEmpty() (bool, error) {
orders, err := ExtractOrders(r)
return len(orders) == 0, err
}
// NextPageURL extracts the "next" link from the links section of the result.
func (r OrderPage) NextPageURL() (string, error) {
var s struct {
Next string `json:"next"`
Previous string `json:"previous"`
}
err := r.ExtractInto(&s)
if err != nil {
return "", err
}
return s.Next, err
}
// ExtractOrders returns a slice of Orders contained in a single page of
// results.
func ExtractOrders(r pagination.Page) ([]Order, error) {
var s struct {
Orders []Order `json:"orders"`
}
err := (r.(OrderPage)).ExtractInto(&s)
return s.Orders, err
}
// Extract interprets any commonResult as a Order.
func (r commonResult) Extract() (*Order, error) {
var s *Order
err := r.ExtractInto(&s)
return s, err
}