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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
# Copyright 2014 OpenStack Foundation
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_utils import encodeutils
import six
from six.moves.urllib import parse
import warlock
from glanceclient.common import utils
from glanceclient.v2 import schemas
DEFAULT_PAGE_SIZE = 20
SORT_DIR_VALUES = ('asc', 'desc')
SORT_KEY_VALUES = ('created_at', 'namespace')
class NamespaceController(object):
def __init__(self, http_client, schema_client):
self.http_client = http_client
self.schema_client = schema_client
@utils.memoized_property
def model(self):
schema = self.schema_client.get('metadefs/namespace')
return warlock.model_factory(schema.raw(),
base_class=schemas.SchemaBasedModel)
@utils.add_req_id_to_object()
def create(self, **kwargs):
"""Create a namespace.
:param kwargs: Unpacked namespace object.
"""
url = '/v2/metadefs/namespaces'
try:
namespace = self.model(kwargs)
except (warlock.InvalidOperation, ValueError) as e:
raise TypeError(encodeutils.exception_to_unicode(e))
resp, body = self.http_client.post(url, data=namespace)
body.pop('self', None)
return self.model(**body), resp
def update(self, namespace_name, **kwargs):
"""Update a namespace.
:param namespace_name: Name of a namespace (old one).
:param kwargs: Unpacked namespace object.
"""
namespace = self.get(namespace_name)
for (key, value) in kwargs.items():
try:
setattr(namespace, key, value)
except warlock.InvalidOperation as e:
raise TypeError(encodeutils.exception_to_unicode(e))
# Remove read-only parameters.
read_only = ['schema', 'updated_at', 'created_at']
for elem in read_only:
if elem in namespace:
del namespace[elem]
url = '/v2/metadefs/namespaces/{0}'.format(namespace_name)
# Pass the original wrapped value to http client.
resp, _ = self.http_client.put(url, data=namespace.wrapped)
# Get request id from `put` request so it can be passed to the
# following `get` call
req_id_hdr = {
'x-openstack-request-id': utils._extract_request_id(resp)
}
return self._get(namespace.namespace, header=req_id_hdr)
def get(self, namespace, **kwargs):
return self._get(namespace, **kwargs)
@utils.add_req_id_to_object()
def _get(self, namespace, header=None, **kwargs):
"""Get one namespace."""
query_params = parse.urlencode(kwargs)
if kwargs:
query_params = '?%s' % query_params
url = '/v2/metadefs/namespaces/{0}{1}'.format(namespace, query_params)
header = header or {}
resp, body = self.http_client.get(url, headers=header)
# NOTE(bcwaldon): remove 'self' for now until we have an elegant
# way to pass it into the model constructor without conflict
body.pop('self', None)
return self.model(**body), resp
@utils.add_req_id_to_generator()
def list(self, **kwargs):
"""Retrieve a listing of Namespace objects.
:param page_size: Number of items to request in each paginated request
:param limit: Use to request a specific page size. Expect a response
to a limited request to return between zero and limit
items.
:param marker: Specifies the namespace of the last-seen namespace.
The typical pattern of limit and marker is to make an
initial limited request and then to use the last
namespace from the response as the marker parameter
in a subsequent limited request.
:param sort_key: The field to sort on (for example, 'created_at')
:param sort_dir: The direction to sort ('asc' or 'desc')
:returns: generator over list of Namespaces
"""
ori_validate_fun = self.model.validate
empty_fun = lambda *args, **kwargs: None
def paginate(url):
resp, body = self.http_client.get(url)
for namespace in body['namespaces']:
# NOTE(bcwaldon): remove 'self' for now until we have
# an elegant way to pass it into the model constructor
# without conflict.
namespace.pop('self', None)
yield self.model(**namespace), resp
# NOTE(zhiyan): In order to resolve the performance issue
# of JSON schema validation for image listing case, we
# don't validate each image entry but do it only on first
# image entry for each page.
self.model.validate = empty_fun
# NOTE(zhiyan); Reset validation function.
self.model.validate = ori_validate_fun
try:
next_url = body['next']
except KeyError:
return
else:
for namespace, resp in paginate(next_url):
yield namespace, resp
filters = kwargs.get('filters', {})
filters = {} if filters is None else filters
if not kwargs.get('page_size'):
filters['limit'] = DEFAULT_PAGE_SIZE
else:
filters['limit'] = kwargs['page_size']
if 'marker' in kwargs:
filters['marker'] = kwargs['marker']
sort_key = kwargs.get('sort_key')
if sort_key is not None:
if sort_key in SORT_KEY_VALUES:
filters['sort_key'] = sort_key
else:
raise ValueError('sort_key must be one of the following: %s.'
% ', '.join(SORT_KEY_VALUES))
sort_dir = kwargs.get('sort_dir')
if sort_dir is not None:
if sort_dir in SORT_DIR_VALUES:
filters['sort_dir'] = sort_dir
else:
raise ValueError('sort_dir must be one of the following: %s.'
% ', '.join(SORT_DIR_VALUES))
for param, value in filters.items():
if isinstance(value, list):
filters[param] = encodeutils.safe_encode(','.join(value))
elif isinstance(value, six.string_types):
filters[param] = encodeutils.safe_encode(value)
url = '/v2/metadefs/namespaces?%s' % parse.urlencode(filters)
for namespace, resp in paginate(url):
yield namespace, resp
@utils.add_req_id_to_object()
def delete(self, namespace):
"""Delete a namespace."""
url = '/v2/metadefs/namespaces/{0}'.format(namespace)
resp, body = self.http_client.delete(url)
return (resp, body), resp
class ResourceTypeController(object):
def __init__(self, http_client, schema_client):
self.http_client = http_client
self.schema_client = schema_client
@utils.memoized_property
def model(self):
schema = self.schema_client.get('metadefs/resource_type')
return warlock.model_factory(schema.raw(),
base_class=schemas.SchemaBasedModel)
@utils.add_req_id_to_object()
def associate(self, namespace, **kwargs):
"""Associate a resource type with a namespace."""
try:
res_type = self.model(kwargs)
except (warlock.InvalidOperation, ValueError) as e:
raise TypeError(encodeutils.exception_to_unicode(e))
url = '/v2/metadefs/namespaces/{0}/resource_types'.format(namespace,
res_type)
resp, body = self.http_client.post(url, data=res_type)
body.pop('self', None)
return self.model(**body), resp
@utils.add_req_id_to_object()
def deassociate(self, namespace, resource):
"""Deassociate a resource type with a namespace."""
url = '/v2/metadefs/namespaces/{0}/resource_types/{1}'. \
format(namespace, resource)
resp, body = self.http_client.delete(url)
return (resp, body), resp
@utils.add_req_id_to_generator()
def list(self):
"""Retrieve a listing of available resource types.
:returns: generator over list of resource_types
"""
url = '/v2/metadefs/resource_types'
resp, body = self.http_client.get(url)
for resource_type in body['resource_types']:
yield self.model(**resource_type), resp
@utils.add_req_id_to_generator()
def get(self, namespace):
url = '/v2/metadefs/namespaces/{0}/resource_types'.format(namespace)
resp, body = self.http_client.get(url)
body.pop('self', None)
for resource_type in body['resource_type_associations']:
yield self.model(**resource_type), resp
class PropertyController(object):
def __init__(self, http_client, schema_client):
self.http_client = http_client
self.schema_client = schema_client
@utils.memoized_property
def model(self):
schema = self.schema_client.get('metadefs/property')
return warlock.model_factory(schema.raw(),
base_class=schemas.SchemaBasedModel)
@utils.add_req_id_to_object()
def create(self, namespace, **kwargs):
"""Create a property.
:param namespace: Name of a namespace the property will belong.
:param kwargs: Unpacked property object.
"""
try:
prop = self.model(kwargs)
except (warlock.InvalidOperation, ValueError) as e:
raise TypeError(encodeutils.exception_to_unicode(e))
url = '/v2/metadefs/namespaces/{0}/properties'.format(namespace)
resp, body = self.http_client.post(url, data=prop)
body.pop('self', None)
return self.model(**body), resp
def update(self, namespace, prop_name, **kwargs):
"""Update a property.
:param namespace: Name of a namespace the property belongs.
:param prop_name: Name of a property (old one).
:param kwargs: Unpacked property object.
"""
prop = self.get(namespace, prop_name)
for (key, value) in kwargs.items():
try:
setattr(prop, key, value)
except warlock.InvalidOperation as e:
raise TypeError(encodeutils.exception_to_unicode(e))
url = '/v2/metadefs/namespaces/{0}/properties/{1}'.format(namespace,
prop_name)
# Pass the original wrapped value to http client.
resp, _ = self.http_client.put(url, data=prop.wrapped)
# Get request id from `put` request so it can be passed to the
# following `get` call
req_id_hdr = {
'x-openstack-request-id': utils._extract_request_id(resp)}
return self._get(namespace, prop.name, req_id_hdr)
def get(self, namespace, prop_name):
return self._get(namespace, prop_name)
@utils.add_req_id_to_object()
def _get(self, namespace, prop_name, header=None):
url = '/v2/metadefs/namespaces/{0}/properties/{1}'.format(namespace,
prop_name)
header = header or {}
resp, body = self.http_client.get(url, headers=header)
body.pop('self', None)
body['name'] = prop_name
return self.model(**body), resp
@utils.add_req_id_to_generator()
def list(self, namespace, **kwargs):
"""Retrieve a listing of metadata properties.
:returns: generator over list of objects
"""
url = '/v2/metadefs/namespaces/{0}/properties'.format(namespace)
resp, body = self.http_client.get(url)
for key, value in body['properties'].items():
value['name'] = key
yield self.model(value), resp
@utils.add_req_id_to_object()
def delete(self, namespace, prop_name):
"""Delete a property."""
url = '/v2/metadefs/namespaces/{0}/properties/{1}'.format(namespace,
prop_name)
resp, body = self.http_client.delete(url)
return (resp, body), resp
@utils.add_req_id_to_object()
def delete_all(self, namespace):
"""Delete all properties in a namespace."""
url = '/v2/metadefs/namespaces/{0}/properties'.format(namespace)
resp, body = self.http_client.delete(url)
return (resp, body), resp
class ObjectController(object):
def __init__(self, http_client, schema_client):
self.http_client = http_client
self.schema_client = schema_client
@utils.memoized_property
def model(self):
schema = self.schema_client.get('metadefs/object')
return warlock.model_factory(schema.raw(),
base_class=schemas.SchemaBasedModel)
@utils.add_req_id_to_object()
def create(self, namespace, **kwargs):
"""Create an object.
:param namespace: Name of a namespace the object belongs.
:param kwargs: Unpacked object.
"""
try:
obj = self.model(kwargs)
except (warlock.InvalidOperation, ValueError) as e:
raise TypeError(encodeutils.exception_to_unicode(e))
url = '/v2/metadefs/namespaces/{0}/objects'.format(namespace)
resp, body = self.http_client.post(url, data=obj)
body.pop('self', None)
return self.model(**body), resp
def update(self, namespace, object_name, **kwargs):
"""Update an object.
:param namespace: Name of a namespace the object belongs.
:param object_name: Name of an object (old one).
:param kwargs: Unpacked object.
"""
obj = self.get(namespace, object_name)
for (key, value) in kwargs.items():
try:
setattr(obj, key, value)
except warlock.InvalidOperation as e:
raise TypeError(encodeutils.exception_to_unicode(e))
# Remove read-only parameters.
read_only = ['schema', 'updated_at', 'created_at']
for elem in read_only:
if elem in obj:
del obj[elem]
url = '/v2/metadefs/namespaces/{0}/objects/{1}'.format(namespace,
object_name)
# Pass the original wrapped value to http client.
resp, _ = self.http_client.put(url, data=obj.wrapped)
# Get request id from `put` request so it can be passed to the
# following `get` call
req_id_hdr = {
'x-openstack-request-id': utils._extract_request_id(resp)}
return self._get(namespace, obj.name, req_id_hdr)
def get(self, namespace, object_name):
return self._get(namespace, object_name)
@utils.add_req_id_to_object()
def _get(self, namespace, object_name, header=None):
url = '/v2/metadefs/namespaces/{0}/objects/{1}'.format(namespace,
object_name)
header = header or {}
resp, body = self.http_client.get(url, headers=header)
body.pop('self', None)
return self.model(**body), resp
@utils.add_req_id_to_generator()
def list(self, namespace, **kwargs):
"""Retrieve a listing of metadata objects.
:returns: generator over list of objects
"""
url = '/v2/metadefs/namespaces/{0}/objects'.format(namespace,)
resp, body = self.http_client.get(url)
for obj in body['objects']:
yield self.model(obj), resp
@utils.add_req_id_to_object()
def delete(self, namespace, object_name):
"""Delete an object."""
url = '/v2/metadefs/namespaces/{0}/objects/{1}'.format(namespace,
object_name)
resp, body = self.http_client.delete(url)
return (resp, body), resp
@utils.add_req_id_to_object()
def delete_all(self, namespace):
"""Delete all objects in a namespace."""
url = '/v2/metadefs/namespaces/{0}/objects'.format(namespace)
resp, body = self.http_client.delete(url)
return (resp, body), resp
class TagController(object):
def __init__(self, http_client, schema_client):
self.http_client = http_client
self.schema_client = schema_client
@utils.memoized_property
def model(self):
schema = self.schema_client.get('metadefs/tag')
return warlock.model_factory(schema.raw(),
base_class=schemas.SchemaBasedModel)
@utils.add_req_id_to_object()
def create(self, namespace, tag_name):
"""Create a tag.
:param namespace: Name of a namespace the Tag belongs.
:param tag_name: The name of the new tag to create.
"""
url = ('/v2/metadefs/namespaces/{0}/tags/{1}'.format(namespace,
tag_name))
resp, body = self.http_client.post(url)
body.pop('self', None)
return self.model(**body), resp
@utils.add_req_id_to_generator()
def create_multiple(self, namespace, **kwargs):
"""Create the list of tags.
:param namespace: Name of a namespace to which the Tags belong.
:param kwargs: list of tags.
"""
tag_names = kwargs.pop('tags', [])
md_tag_list = []
for tag_name in tag_names:
try:
md_tag_list.append(self.model(name=tag_name))
except (warlock.InvalidOperation) as e:
raise TypeError(encodeutils.exception_to_unicode(e))
tags = {'tags': md_tag_list}
url = '/v2/metadefs/namespaces/{0}/tags'.format(namespace)
resp, body = self.http_client.post(url, data=tags)
body.pop('self', None)
for tag in body['tags']:
yield self.model(tag), resp
def update(self, namespace, tag_name, **kwargs):
"""Update a tag.
:param namespace: Name of a namespace the Tag belongs.
:param tag_name: Name of the Tag (old one).
:param kwargs: Unpacked tag.
"""
tag = self.get(namespace, tag_name)
for (key, value) in kwargs.items():
try:
setattr(tag, key, value)
except warlock.InvalidOperation as e:
raise TypeError(encodeutils.exception_to_unicode(e))
# Remove read-only parameters.
read_only = ['updated_at', 'created_at']
for elem in read_only:
if elem in tag:
del tag[elem]
url = '/v2/metadefs/namespaces/{0}/tags/{1}'.format(namespace,
tag_name)
# Pass the original wrapped value to http client.
resp, _ = self.http_client.put(url, data=tag.wrapped)
# Get request id from `put` request so it can be passed to the
# following `get` call
req_id_hdr = {
'x-openstack-request-id': utils._extract_request_id(resp)}
return self._get(namespace, tag.name, req_id_hdr)
def get(self, namespace, tag_name):
return self._get(namespace, tag_name)
@utils.add_req_id_to_object()
def _get(self, namespace, tag_name, header=None):
url = '/v2/metadefs/namespaces/{0}/tags/{1}'.format(namespace,
tag_name)
header = header or {}
resp, body = self.http_client.get(url, headers=header)
body.pop('self', None)
return self.model(**body), resp
@utils.add_req_id_to_generator()
def list(self, namespace, **kwargs):
"""Retrieve a listing of metadata tags.
:returns: generator over list of tags.
"""
url = '/v2/metadefs/namespaces/{0}/tags'.format(namespace)
resp, body = self.http_client.get(url)
for tag in body['tags']:
yield self.model(tag), resp
@utils.add_req_id_to_object()
def delete(self, namespace, tag_name):
"""Delete a tag."""
url = '/v2/metadefs/namespaces/{0}/tags/{1}'.format(namespace,
tag_name)
resp, body = self.http_client.delete(url)
return (resp, body), resp
@utils.add_req_id_to_object()
def delete_all(self, namespace):
"""Delete all tags in a namespace."""
url = '/v2/metadefs/namespaces/{0}/tags'.format(namespace)
resp, body = self.http_client.delete(url)
return (resp, body), resp
|