1package containers
2
3import (
4	"encoding/json"
5	"time"
6
7	"github.com/gophercloud/gophercloud"
8	"github.com/gophercloud/gophercloud/pagination"
9)
10
11// Container represents a container in the key manager service.
12type Container struct {
13	// Consumers are the consumers of the container.
14	Consumers []ConsumerRef `json:"consumers"`
15
16	// ContainerRef is the URL to the container
17	ContainerRef string `json:"container_ref"`
18
19	// Created is the date the container was created.
20	Created time.Time `json:"-"`
21
22	// CreatorID is the creator of the container.
23	CreatorID string `json:"creator_id"`
24
25	// Name is the name of the container.
26	Name string `json:"name"`
27
28	// SecretRefs are the secret references of the container.
29	SecretRefs []SecretRef `json:"secret_refs"`
30
31	// Status is the status of the container.
32	Status string `json:"status"`
33
34	// Type is the type of container.
35	Type string `json:"type"`
36
37	// Updated is the date the container was updated.
38	Updated time.Time `json:"-"`
39}
40
41func (r *Container) UnmarshalJSON(b []byte) error {
42	type tmp Container
43	var s struct {
44		tmp
45		Created gophercloud.JSONRFC3339NoZ `json:"created"`
46		Updated gophercloud.JSONRFC3339NoZ `json:"updated"`
47	}
48	err := json.Unmarshal(b, &s)
49	if err != nil {
50		return err
51	}
52	*r = Container(s.tmp)
53
54	r.Created = time.Time(s.Created)
55	r.Updated = time.Time(s.Updated)
56
57	return nil
58}
59
60// ConsumerRef represents a consumer reference in a container.
61type ConsumerRef struct {
62	// Name is the name of the consumer.
63	Name string `json:"name"`
64
65	// URL is the URL to the consumer resource.
66	URL string `json:"url"`
67}
68
69// SecretRef is a reference to a secret.
70type SecretRef struct {
71	SecretRef string `json:"secret_ref"`
72	Name      string `json:"name"`
73}
74
75type commonResult struct {
76	gophercloud.Result
77}
78
79// Extract interprets any commonResult as a Container.
80func (r commonResult) Extract() (*Container, error) {
81	var s *Container
82	err := r.ExtractInto(&s)
83	return s, err
84}
85
86// GetResult is the response from a Get operation. Call its Extract method
87// to interpret it as a container.
88type GetResult struct {
89	commonResult
90}
91
92// CreateResult is the response from a Create operation. Call its Extract method
93// to interpret it as a container.
94type CreateResult struct {
95	commonResult
96}
97
98// DeleteResult is the response from a Delete operation. Call its ExtractErr to
99// determine if the request succeeded or failed.
100type DeleteResult struct {
101	gophercloud.ErrResult
102}
103
104// ContainerPage is a single page of container results.
105type ContainerPage struct {
106	pagination.LinkedPageBase
107}
108
109// IsEmpty determines whether or not a page of Container contains any results.
110func (r ContainerPage) IsEmpty() (bool, error) {
111	containers, err := ExtractContainers(r)
112	return len(containers) == 0, err
113}
114
115// NextPageURL extracts the "next" link from the links section of the result.
116func (r ContainerPage) NextPageURL() (string, error) {
117	var s struct {
118		Next     string `json:"next"`
119		Previous string `json:"previous"`
120	}
121	err := r.ExtractInto(&s)
122	if err != nil {
123		return "", err
124	}
125	return s.Next, err
126}
127
128// ExtractContainers returns a slice of Containers contained in a single page of
129// results.
130func ExtractContainers(r pagination.Page) ([]Container, error) {
131	var s struct {
132		Containers []Container `json:"containers"`
133	}
134	err := (r.(ContainerPage)).ExtractInto(&s)
135	return s.Containers, err
136}
137
138// Consumer represents a consumer in a container.
139type Consumer struct {
140	// Created is the date the container was created.
141	Created time.Time `json:"-"`
142
143	// Name is the name of the container.
144	Name string `json:"name"`
145
146	// Status is the status of the container.
147	Status string `json:"status"`
148
149	// Updated is the date the container was updated.
150	Updated time.Time `json:"-"`
151
152	// URL is the url to the consumer.
153	URL string `json:"url"`
154}
155
156func (r *Consumer) UnmarshalJSON(b []byte) error {
157	type tmp Consumer
158	var s struct {
159		tmp
160		Created gophercloud.JSONRFC3339NoZ `json:"created"`
161		Updated gophercloud.JSONRFC3339NoZ `json:"updated"`
162	}
163	err := json.Unmarshal(b, &s)
164	if err != nil {
165		return err
166	}
167	*r = Consumer(s.tmp)
168
169	r.Created = time.Time(s.Created)
170	r.Updated = time.Time(s.Updated)
171
172	return nil
173}
174
175type consumerResult struct {
176	gophercloud.Result
177}
178
179// Extract interprets any consumerResult as a Consumer.
180func (r consumerResult) Extract() (*Consumer, error) {
181	var s *Consumer
182	err := r.ExtractInto(&s)
183	return s, err
184}
185
186// CreateConsumerResult is the response from a CreateConsumer operation.
187// Call its Extract method to interpret it as a container.
188type CreateConsumerResult struct {
189	// This is not a typo.
190	commonResult
191}
192
193// DeleteConsumerResult is the response from a DeleteConsumer operation.
194// Call its Extract to interpret it as a container.
195type DeleteConsumerResult struct {
196	// This is not a typo.
197	commonResult
198}
199
200// ConsumerPage is a single page of consumer results.
201type ConsumerPage struct {
202	pagination.LinkedPageBase
203}
204
205// IsEmpty determines whether or not a page of consumers contains any results.
206func (r ConsumerPage) IsEmpty() (bool, error) {
207	consumers, err := ExtractConsumers(r)
208	return len(consumers) == 0, err
209}
210
211// NextPageURL extracts the "next" link from the links section of the result.
212func (r ConsumerPage) NextPageURL() (string, error) {
213	var s struct {
214		Next     string `json:"next"`
215		Previous string `json:"previous"`
216	}
217	err := r.ExtractInto(&s)
218	if err != nil {
219		return "", err
220	}
221	return s.Next, err
222}
223
224// ExtractConsumers returns a slice of Consumers contained in a single page of
225// results.
226func ExtractConsumers(r pagination.Page) ([]Consumer, error) {
227	var s struct {
228		Consumers []Consumer `json:"consumers"`
229	}
230	err := (r.(ConsumerPage)).ExtractInto(&s)
231	return s.Consumers, err
232}
233
234// Extract interprets any CreateSecretRefResult as a Container
235func (r CreateSecretRefResult) Extract() (*Container, error) {
236	var c *Container
237	err := r.ExtractInto(&c)
238	return c, err
239}
240
241// CreateSecretRefResult is the response from a CreateSecretRef operation.
242// Call its Extract method to interpret it as a container.
243type CreateSecretRefResult struct {
244	// This is not a typo.
245	commonResult
246}
247
248// DeleteSecretRefResult is the response from a DeleteSecretRef operation.
249type DeleteSecretRefResult struct {
250	gophercloud.ErrResult
251}
252