/
requests.go
261 lines (221 loc) · 7.83 KB
/
requests.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package containers
import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
// ContainerType represents the valid types of containers.
type ContainerType string
const (
GenericContainer ContainerType = "generic"
RSAContainer ContainerType = "rsa"
CertificateContainer ContainerType = "certificate"
)
// ListOptsBuilder allows extensions to add additional parameters to
// the List request
type ListOptsBuilder interface {
ToContainerListQuery() (string, error)
}
// ListOpts provides options to filter the List results.
type ListOpts struct {
// Limit is the amount of containers to retrieve.
Limit int `q:"limit"`
// Name is the name of the container
Name string `q:"name"`
// Offset is the index within the list to retrieve.
Offset int `q:"offset"`
}
// ToContainerListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToContainerListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
return q.String(), err
}
// List retrieves a list of containers.
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := listURL(client)
if opts != nil {
query, err := opts.ToContainerListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return ContainerPage{pagination.LinkedPageBase{PageResult: r}}
})
}
// Get retrieves details of a container.
func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
resp, err := client.Get(getURL(client, id), &r.Body, nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// CreateOptsBuilder allows extensions to add additional parameters to
// the Create request.
type CreateOptsBuilder interface {
ToContainerCreateMap() (map[string]interface{}, error)
}
// CreateOpts provides options used to create a container.
type CreateOpts struct {
// Type represents the type of container.
Type ContainerType `json:"type" required:"true"`
// Name is the name of the container.
Name string `json:"name"`
// SecretRefs is a list of secret refs for the container.
SecretRefs []SecretRef `json:"secret_refs,omitempty"`
}
// ToContainerCreateMap formats a CreateOpts into a create request.
func (opts CreateOpts) ToContainerCreateMap() (map[string]interface{}, error) {
return gophercloud.BuildRequestBody(opts, "")
}
// Create creates a new container.
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToContainerCreateMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Post(createURL(client), &b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{201},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// Delete deletes a container.
func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
resp, err := client.Delete(deleteURL(client, id), nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// ListConsumersOptsBuilder allows extensions to add additional parameters to
// the ListConsumers request
type ListConsumersOptsBuilder interface {
ToContainerListConsumersQuery() (string, error)
}
// ListConsumersOpts provides options to filter the List results.
type ListConsumersOpts struct {
// Limit is the amount of consumers to retrieve.
Limit int `q:"limit"`
// Offset is the index within the list to retrieve.
Offset int `q:"offset"`
}
// ToContainerListConsumersQuery formats a ListConsumersOpts into a query
// string.
func (opts ListOpts) ToContainerListConsumersQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
return q.String(), err
}
// ListConsumers retrieves a list of consumers from a container.
func ListConsumers(client *gophercloud.ServiceClient, containerID string, opts ListConsumersOptsBuilder) pagination.Pager {
url := listConsumersURL(client, containerID)
if opts != nil {
query, err := opts.ToContainerListConsumersQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return ConsumerPage{pagination.LinkedPageBase{PageResult: r}}
})
}
// CreateConsumerOptsBuilder allows extensions to add additional parameters to
// the Create request.
type CreateConsumerOptsBuilder interface {
ToContainerConsumerCreateMap() (map[string]interface{}, error)
}
// CreateConsumerOpts provides options used to create a container.
type CreateConsumerOpts struct {
// Name is the name of the consumer.
Name string `json:"name"`
// URL is the URL to the consumer resource.
URL string `json:"URL"`
}
// ToContainerConsumerCreateMap formats a CreateConsumerOpts into a create
// request.
func (opts CreateConsumerOpts) ToContainerConsumerCreateMap() (map[string]interface{}, error) {
return gophercloud.BuildRequestBody(opts, "")
}
// CreateConsumer creates a new consumer.
func CreateConsumer(client *gophercloud.ServiceClient, containerID string, opts CreateConsumerOptsBuilder) (r CreateConsumerResult) {
b, err := opts.ToContainerConsumerCreateMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Post(createConsumerURL(client, containerID), &b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// DeleteConsumerOptsBuilder allows extensions to add additional parameters to
// the Delete request.
type DeleteConsumerOptsBuilder interface {
ToContainerConsumerDeleteMap() (map[string]interface{}, error)
}
// DeleteConsumerOpts represents options used for deleting a consumer.
type DeleteConsumerOpts struct {
// Name is the name of the consumer.
Name string `json:"name"`
// URL is the URL to the consumer resource.
URL string `json:"URL"`
}
// ToContainerConsumerDeleteMap formats a DeleteConsumerOpts into a create
// request.
func (opts DeleteConsumerOpts) ToContainerConsumerDeleteMap() (map[string]interface{}, error) {
return gophercloud.BuildRequestBody(opts, "")
}
// DeleteConsumer deletes a consumer.
func DeleteConsumer(client *gophercloud.ServiceClient, containerID string, opts DeleteConsumerOptsBuilder) (r DeleteConsumerResult) {
url := deleteConsumerURL(client, containerID)
b, err := opts.ToContainerConsumerDeleteMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Request("DELETE", url, &gophercloud.RequestOpts{
JSONBody: b,
JSONResponse: &r.Body,
OkCodes: []int{200},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// SecretRefBuilder allows extensions to add additional parameters to the
// Create request.
type SecretRefBuilder interface {
ToContainerSecretRefMap() (map[string]interface{}, error)
}
// ToContainerSecretRefMap formats a SecretRefBuilder into a create
// request.
func (opts SecretRef) ToContainerSecretRefMap() (map[string]interface{}, error) {
return gophercloud.BuildRequestBody(opts, "")
}
// CreateSecret creates a new consumer.
func CreateSecretRef(client *gophercloud.ServiceClient, containerID string, opts SecretRefBuilder) (r CreateSecretRefResult) {
b, err := opts.ToContainerSecretRefMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Post(createSecretRefURL(client, containerID), &b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{201},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// DeleteSecret deletes a consumer.
func DeleteSecretRef(client *gophercloud.ServiceClient, containerID string, opts SecretRefBuilder) (r DeleteSecretRefResult) {
url := deleteSecretRefURL(client, containerID)
b, err := opts.ToContainerSecretRefMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Request("DELETE", url, &gophercloud.RequestOpts{
JSONBody: b,
OkCodes: []int{204},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}