kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 1 | from __future__ import absolute_import |
| 2 | |
| 3 | import common |
| 4 | |
| 5 | try: |
| 6 | from urllib.parse import urlencode |
| 7 | except ImportError: |
| 8 | from urllib import urlencode |
| 9 | |
| 10 | |
| 11 | @common.send("get") |
| 12 | def volume_list(**kwargs): |
| 13 | """ |
| 14 | Return list of cinder volumes. |
| 15 | """ |
| 16 | url = '/volumes?{}'.format(urlencode(kwargs)) |
| 17 | return url, {} |
| 18 | |
| 19 | |
| 20 | @common.send("get") |
| 21 | def volume_type_list(**kwargs): |
| 22 | """ |
| 23 | Return list of volume types |
| 24 | """ |
| 25 | url = '/types?{}'.format(urlencode(kwargs)) |
| 26 | return url, {} |
| 27 | |
| 28 | |
| 29 | @common.get_by_name_or_uuid(volume_type_list, 'volume_types') |
| 30 | @common.send("get") |
| 31 | def volume_type_get(volume_type_id, **kwargs): |
| 32 | """ |
| 33 | Returns id of the specified volume type name |
| 34 | """ |
| 35 | url = "/types/{volume_type_id}".format(volume_type_id=volume_type_id) |
| 36 | return url, {} |
| 37 | |
| 38 | |
| 39 | @common.get_by_name_or_uuid(volume_type_list, 'volume_types') |
| 40 | @common.send("delete") |
| 41 | def volume_type_delete(volume_type_id, **kwargs): |
| 42 | """ |
| 43 | delete the specified volume type |
| 44 | """ |
| 45 | url = "/types/{volume_type_id}".format(volume_type_id=volume_type_id) |
| 46 | return url, {} |
| 47 | |
| 48 | |
| 49 | @common.send("post") |
| 50 | def volume_type_create(name, **kwargs): |
| 51 | """ |
| 52 | Create cinder volume type |
| 53 | """ |
| 54 | url = "/types" |
| 55 | req = {"volume_type": {"name": name}} |
| 56 | return url, {'json': req} |
| 57 | |
| 58 | |
| 59 | @common.get_by_name_or_uuid(volume_type_list, 'volume_types') |
| 60 | @common.send("get") |
| 61 | def keys_volume_type_get(volume_type_id, **kwargs): |
| 62 | """ |
| 63 | Return extra specs of the specified volume type. |
| 64 | """ |
| 65 | url = "/types/{volume_type_id}/extra_specs".format( |
| 66 | volume_type_id=volume_type_id) |
| 67 | return url, {} |
| 68 | |
| 69 | |
| 70 | @common.send("put") |
| 71 | def _key_volume_type_set(type_id, key, value, **kwargs): |
| 72 | url = "/types/{volume_type_id}/extra_specs/{key}".format( |
| 73 | volume_type_id=type_id, key=key) |
| 74 | return url, {'json': {str(key): str(value)}} |
| 75 | |
| 76 | |
| 77 | @common.get_by_name_or_uuid(volume_type_list, 'volume_types') |
| 78 | def keys_volume_type_set(volume_type_id, keys=None, **kwargs): |
| 79 | """ |
| 80 | Set extra specs of the specified volume type. |
| 81 | """ |
| 82 | if keys is None: |
| 83 | keys = {} |
| 84 | cloud_name = kwargs["cloud_name"] |
| 85 | cur_keys = keys_volume_type_get( |
Oleh Hryhorov | 1aeb0c8 | 2018-10-26 14:51:30 +0000 | [diff] [blame^] | 86 | volume_type_id, cloud_name=cloud_name)["extra_specs"] |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 87 | |
| 88 | for k, v in keys.items(): |
| 89 | if (k, v) in cur_keys.items(): |
| 90 | continue |
| 91 | resp = _key_volume_type_set(volume_type_id, k, v, cloud_name=cloud_name) |
| 92 | if resp.get("result") is False: |
| 93 | return resp |
| 94 | |
| 95 | return {"result": True} |