File: requests.go

package info (click to toggle)
golang-github-gophercloud-gophercloud 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,416 kB
  • sloc: sh: 99; makefile: 21
file content (328 lines) | stat: -rw-r--r-- 9,875 bytes parent folder | download
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package qos

import (
	"github.com/gophercloud/gophercloud"
	"github.com/gophercloud/gophercloud/pagination"
)

type CreateOptsBuilder interface {
	ToQoSCreateMap() (map[string]interface{}, error)
}

// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
	ToQoSListQuery() (string, error)
}

type QoSConsumer string

const (
	ConsumerFront QoSConsumer = "front-end"
	ConsumerBack  QoSConsumer = "back-end"
	ConsumerBoth  QoSConsumer = "both"
)

// CreateOpts contains options for creating a QoS specification.
// This object is passed to the qos.Create function.
type CreateOpts struct {
	// The name of the QoS spec
	Name string `json:"name"`
	// The consumer of the QoS spec. Possible values are
	// both, front-end, back-end.
	Consumer QoSConsumer `json:"consumer,omitempty"`
	// Specs is a collection of miscellaneous key/values used to set
	// specifications for the QoS
	Specs map[string]string `json:"-"`
}

// ToQoSCreateMap assembles a request body based on the contents of a
// CreateOpts.
func (opts CreateOpts) ToQoSCreateMap() (map[string]interface{}, error) {
	b, err := gophercloud.BuildRequestBody(opts, "qos_specs")
	if err != nil {
		return nil, err
	}

	if opts.Specs != nil {
		if v, ok := b["qos_specs"].(map[string]interface{}); ok {
			for key, value := range opts.Specs {
				v[key] = value
			}
		}
	}

	return b, nil
}

// Create will create a new QoS based on the values in CreateOpts. To extract
// the QoS object from the response, call the Extract method on the
// CreateResult.
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
	b, err := opts.ToQoSCreateMap()
	if err != nil {
		r.Err = err
		return
	}
	resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
		OkCodes: []int{200},
	})
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}

// DeleteOptsBuilder allows extensions to add additional parameters to the
// Delete request.
type DeleteOptsBuilder interface {
	ToQoSDeleteQuery() (string, error)
}

// DeleteOpts contains options for deleting a QoS. This object is passed to
// the qos.Delete function.
type DeleteOpts struct {
	// Delete a QoS specification even if it is in-use
	Force bool `q:"force"`
}

// ToQoSDeleteQuery formats a DeleteOpts into a query string.
func (opts DeleteOpts) ToQoSDeleteQuery() (string, error) {
	q, err := gophercloud.BuildQueryString(opts)
	return q.String(), err
}

// Delete will delete the existing QoS with the provided ID.
func Delete(client *gophercloud.ServiceClient, id string, opts DeleteOptsBuilder) (r DeleteResult) {
	url := deleteURL(client, id)
	if opts != nil {
		query, err := opts.ToQoSDeleteQuery()
		if err != nil {
			r.Err = err
			return
		}
		url += query
	}
	resp, err := client.Delete(url, nil)
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}

type ListOpts struct {
	// Sort is Comma-separated list of sort keys and optional sort
	// directions in the form of < key > [: < direction > ]. A valid
	//direction is asc (ascending) or desc (descending).
	Sort string `q:"sort"`

	// Marker and Limit control paging.
	// Marker instructs List where to start listing from.
	Marker string `q:"marker"`

	// Limit instructs List to refrain from sending excessively large lists of
	// QoS.
	Limit int `q:"limit"`
}

// ToQoSListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToQoSListQuery() (string, error) {
	q, err := gophercloud.BuildQueryString(opts)
	return q.String(), err
}

// List instructs OpenStack to provide a list of QoS.
// You may provide criteria by which List curtails its results for easier
// processing.
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
	url := listURL(client)
	if opts != nil {
		query, err := opts.ToQoSListQuery()
		if err != nil {
			return pagination.Pager{Err: err}
		}
		url += query
	}
	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
		return QoSPage{pagination.LinkedPageBase{PageResult: r}}
	})
}

// Get retrieves details of a single qos. Use Extract to convert its
// result into a QoS.
func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
	resp, err := client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{
		OkCodes: []int{200},
	})
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}

// CreateQosSpecsOptsBuilder allows extensions to add additional parameters to the
// CreateQosSpecs requests.
type CreateQosSpecsOptsBuilder interface {
	ToQosSpecsCreateMap() (map[string]interface{}, error)
}

// UpdateOpts contains options for creating a QoS specification.
// This object is passed to the qos.Update function.
type UpdateOpts struct {
	// The consumer of the QoS spec. Possible values are
	// both, front-end, back-end.
	Consumer QoSConsumer `json:"consumer,omitempty"`
	// Specs is a collection of miscellaneous key/values used to set
	// specifications for the QoS
	Specs map[string]string `json:"-"`
}

type UpdateOptsBuilder interface {
	ToQoSUpdateMap() (map[string]interface{}, error)
}

// ToQoSUpdateMap assembles a request body based on the contents of a
// UpdateOpts.
func (opts UpdateOpts) ToQoSUpdateMap() (map[string]interface{}, error) {
	b, err := gophercloud.BuildRequestBody(opts, "qos_specs")
	if err != nil {
		return nil, err
	}

	if opts.Specs != nil {
		if v, ok := b["qos_specs"].(map[string]interface{}); ok {
			for key, value := range opts.Specs {
				v[key] = value
			}
		}
	}

	return b, nil
}

// Update will update an existing QoS based on the values in UpdateOpts.
// To extract the QoS object from the response, call the Extract method
// on the UpdateResult.
func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r updateResult) {
	b, err := opts.ToQoSUpdateMap()
	if err != nil {
		r.Err = err
		return
	}
	resp, err := client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
		OkCodes: []int{200},
	})
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}

// DeleteKeysOptsBuilder allows extensions to add additional parameters to the
// CreateExtraSpecs requests.
type DeleteKeysOptsBuilder interface {
	ToDeleteKeysCreateMap() (map[string]interface{}, error)
}

// DeleteKeysOpts is a string slice that contains keys to be deleted.
type DeleteKeysOpts []string

// ToDeleteKeysCreateMap assembles a body for a Create request based on
// the contents of ExtraSpecsOpts.
func (opts DeleteKeysOpts) ToDeleteKeysCreateMap() (map[string]interface{}, error) {
	return map[string]interface{}{"keys": opts}, nil
}

// DeleteKeys will delete the keys/specs from the specified QoS
func DeleteKeys(client *gophercloud.ServiceClient, qosID string, opts DeleteKeysOptsBuilder) (r DeleteResult) {
	b, err := opts.ToDeleteKeysCreateMap()
	if err != nil {
		r.Err = err
		return
	}
	resp, err := client.Put(deleteKeysURL(client, qosID), b, nil, &gophercloud.RequestOpts{
		OkCodes: []int{202},
	})
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}

// AssociateOpitsBuilder allows extensions to define volume type id
// to the associate query
type AssociateOptsBuilder interface {
	ToQosAssociateQuery() (string, error)
}

// AssociateOpts contains options for associating a QoS with a
// volume type
type AssociateOpts struct {
	VolumeTypeID string `q:"vol_type_id" required:"true"`
}

// ToQosAssociateQuery formats an AssociateOpts into a query string
func (opts AssociateOpts) ToQosAssociateQuery() (string, error) {
	q, err := gophercloud.BuildQueryString(opts)
	return q.String(), err
}

// Associate will associate a qos with a volute type
func Associate(client *gophercloud.ServiceClient, qosID string, opts AssociateOptsBuilder) (r AssociateResult) {
	url := associateURL(client, qosID)
	query, err := opts.ToQosAssociateQuery()
	if err != nil {
		r.Err = err
		return
	}
	url += query

	resp, err := client.Get(url, nil, &gophercloud.RequestOpts{
		OkCodes: []int{202},
	})
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}

// DisassociateOpitsBuilder allows extensions to define volume type id
// to the disassociate query
type DisassociateOptsBuilder interface {
	ToQosDisassociateQuery() (string, error)
}

// DisassociateOpts contains options for disassociating a QoS from a
// volume type
type DisassociateOpts struct {
	VolumeTypeID string `q:"vol_type_id" required:"true"`
}

// ToQosDisassociateQuery formats a DisassociateOpts into a query string
func (opts DisassociateOpts) ToQosDisassociateQuery() (string, error) {
	q, err := gophercloud.BuildQueryString(opts)
	return q.String(), err
}

// Disassociate will disassociate a qos from a volute type
func Disassociate(client *gophercloud.ServiceClient, qosID string, opts DisassociateOptsBuilder) (r DisassociateResult) {
	url := disassociateURL(client, qosID)
	query, err := opts.ToQosDisassociateQuery()
	if err != nil {
		r.Err = err
		return
	}
	url += query

	resp, err := client.Get(url, nil, &gophercloud.RequestOpts{
		OkCodes: []int{202},
	})
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}

// DisassociateAll will disassociate a qos from all volute types
func DisassociateAll(client *gophercloud.ServiceClient, qosID string) (r DisassociateAllResult) {
	resp, err := client.Get(disassociateAllURL(client, qosID), nil, &gophercloud.RequestOpts{
		OkCodes: []int{202},
	})
	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
	return
}

// ListAssociations retrieves the associations of a QoS.
func ListAssociations(client *gophercloud.ServiceClient, qosID string) pagination.Pager {
	url := listAssociationsURL(client, qosID)

	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
		return AssociationPage{pagination.SinglePageBase(r)}
	})
}