1package orders
2
3import (
4	"encoding/json"
5	"time"
6
7	"github.com/gophercloud/gophercloud"
8	"github.com/gophercloud/gophercloud/pagination"
9)
10
11// Order represents an order in the key manager service.
12type Order struct {
13	// ContainerRef is the container URL.
14	ContainerRef string `json:"container_ref"`
15
16	// Created is when the order was created.
17	Created time.Time `json:"-"`
18
19	// CreatorID is the creator of the order.
20	CreatorID string `json:"creator_id"`
21
22	// ErrorReason is the reason of the error.
23	ErrorReason string `json:"error_reason"`
24
25	// ErrorStatusCode is the error status code.
26	ErrorStatusCode string `json:"error_status_code"`
27
28	// OrderRef is the order URL.
29	OrderRef string `json:"order_ref"`
30
31	// Meta is secret data about the order.
32	Meta Meta `json:"meta"`
33
34	// SecretRef is the secret URL.
35	SecretRef string `json:"secret_ref"`
36
37	// Status is the status of the order.
38	Status string `json:"status"`
39
40	// SubStatus is the status of the order.
41	SubStatus string `json:"sub_status"`
42
43	// SubStatusMessage is the message of the sub status.
44	SubStatusMessage string `json:"sub_status_message"`
45
46	// Type is the order type.
47	Type string `json:"type"`
48
49	// Updated is when the order was updated.
50	Updated time.Time `json:"-"`
51}
52
53func (r *Order) UnmarshalJSON(b []byte) error {
54	type tmp Order
55	var s struct {
56		tmp
57		Created gophercloud.JSONRFC3339NoZ `json:"created"`
58		Updated gophercloud.JSONRFC3339NoZ `json:"updated"`
59	}
60	err := json.Unmarshal(b, &s)
61	if err != nil {
62		return err
63	}
64	*r = Order(s.tmp)
65
66	r.Created = time.Time(s.Created)
67	r.Updated = time.Time(s.Updated)
68
69	return nil
70}
71
72type Meta struct {
73	// Algorithm is the algorithm of the secret.
74	Algorithm string `json:"algorithm"`
75
76	// BitLength is the bit length of the secret.
77	BitLength int `json:"bit_length"`
78
79	// Expiration is the expiration date of the order.
80	Expiration time.Time `json:"-"`
81
82	// Mode is the mode of the secret.
83	Mode string `json:"mode"`
84
85	// Name is the name of the secret.
86	Name string `json:"name"`
87
88	// PayloadContentType is the content type of the secret payload.
89	PayloadContentType string `json:"payload_content_type"`
90}
91
92func (r *Meta) UnmarshalJSON(b []byte) error {
93	type tmp Meta
94	var s struct {
95		tmp
96		Expiration gophercloud.JSONRFC3339NoZ `json:"expiration"`
97	}
98	err := json.Unmarshal(b, &s)
99	if err != nil {
100		return err
101	}
102	*r = Meta(s.tmp)
103
104	r.Expiration = time.Time(s.Expiration)
105
106	return nil
107}
108
109type commonResult struct {
110	gophercloud.Result
111}
112
113// GetResult is the response from a Get operation. Call its Extract method
114// to interpret it as a orders.
115type GetResult struct {
116	commonResult
117}
118
119// CreateResult is the response from a Create operation. Call its Extract method
120// to interpret it as a orders.
121type CreateResult struct {
122	commonResult
123}
124
125// DeleteResult is the response from a Delete operation. Call its ExtractErr to
126// determine if the request succeeded or failed.
127type DeleteResult struct {
128	gophercloud.ErrResult
129}
130
131// OrderPage is a single page of orders results.
132type OrderPage struct {
133	pagination.LinkedPageBase
134}
135
136// IsEmpty determines whether or not a page of ordersS contains any results.
137func (r OrderPage) IsEmpty() (bool, error) {
138	orders, err := ExtractOrders(r)
139	return len(orders) == 0, err
140}
141
142// NextPageURL extracts the "next" link from the links section of the result.
143func (r OrderPage) NextPageURL() (string, error) {
144	var s struct {
145		Next     string `json:"next"`
146		Previous string `json:"previous"`
147	}
148	err := r.ExtractInto(&s)
149	if err != nil {
150		return "", err
151	}
152	return s.Next, err
153}
154
155// ExtractOrders returns a slice of Orders contained in a single page of
156// results.
157func ExtractOrders(r pagination.Page) ([]Order, error) {
158	var s struct {
159		Orders []Order `json:"orders"`
160	}
161	err := (r.(OrderPage)).ExtractInto(&s)
162	return s.Orders, err
163}
164
165// Extract interprets any commonResult as a Order.
166func (r commonResult) Extract() (*Order, error) {
167	var s *Order
168	err := r.ExtractInto(&s)
169	return s, err
170}
171