Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
| 2 | ''' |
| 3 | Management of Cinder resources |
| 4 | =============================== |
| 5 | :depends: - cinderclient Python module |
| 6 | ''' |
| 7 | import ast |
| 8 | import logging |
| 9 | from functools import wraps |
| 10 | LOG = logging.getLogger(__name__) |
| 11 | |
| 12 | |
| 13 | def __virtual__(): |
| 14 | ''' |
| 15 | Only load if python-cinderclient is present in __salt__ |
| 16 | ''' |
| 17 | return 'cinderng' |
| 18 | |
| 19 | def volume_type_present(name=None, profile=None): |
| 20 | ''' |
| 21 | Ensures that the specified volume type is present. |
| 22 | ''' |
| 23 | ret = {'name': name, |
| 24 | 'changes': {}, |
| 25 | 'result': True, |
| 26 | 'comment': 'Volume type "{0}" already exists'.format(name)} |
| 27 | signal = __salt__['cinderng.create_volume_type'](name, profile) |
| 28 | if 'exists' in signal: |
| 29 | pass |
| 30 | elif 'created' in signal: |
| 31 | ret['comment'] = 'Volume type {0} has been created'.format(name) |
| 32 | ret['changes']['Volume type'] = 'Created' |
| 33 | elif 'failed' in signal: |
| 34 | ret = {'name': name, |
| 35 | 'changes': {}, |
| 36 | 'result': False, |
| 37 | 'comment': 'Volume type "{0}" failed to create'.format(name)} |
| 38 | return ret |
| 39 | |
| 40 | def volume_type_key_present(name=None, key=None, value=None, profile=None): |
| 41 | ''' |
| 42 | Ensures that the extra specs are present on a volume type. |
| 43 | ''' |
| 44 | keys = "{u'" + key + "': u'" + value + "'}" |
| 45 | keys = ast.literal_eval(keys) |
| 46 | ret = {'name': name, |
| 47 | 'changes': {}, |
| 48 | 'result': True, |
| 49 | 'comment': 'Volume type keys "{0}" in volume type "{1}" already exist'.format(keys, name)} |
| 50 | signal = __salt__['cinderng.set_keys_volume_type'](name, keys, profile) |
| 51 | if 'exist' in signal: |
| 52 | pass |
| 53 | elif 'updated' in signal: |
| 54 | ret['comment'] = 'Volume type keys "{0}" in volume type "{1}" have been updated'.format(keys, name) |
| 55 | ret['changes']['Volume type keys'] = 'Updated' |
| 56 | elif 'failed' in signal: |
| 57 | ret = {'name': name, |
| 58 | 'changes': {}, |
| 59 | 'result': False, |
| 60 | 'comment': 'Volume type keys "{0}" in volume type "{1}" failed to update'.format(keys, name)} |
| 61 | elif 'not found' in signal: |
| 62 | ret = {'name': name, |
| 63 | 'changes': {}, |
| 64 | 'result': False, |
| 65 | 'comment': 'Volume type "{0}" was not found'.format(name)} |
| 66 | return ret |
| 67 | |
| 68 | |
| 69 | def _already_exists(name, resource): |
| 70 | changes_dict = {'name': name, |
| 71 | 'changes': {}, |
| 72 | 'result': True} |
| 73 | changes_dict['comment'] = \ |
| 74 | '{0} {1} already exists'.format(resource, name) |
| 75 | return changes_dict |
| 76 | |
| 77 | |
| 78 | def _created(name, resource, resource_definition): |
| 79 | changes_dict = {'name': name, |
| 80 | 'changes': resource_definition, |
| 81 | 'result': True, |
| 82 | 'comment': '{0} {1} created'.format(resource, name)} |
| 83 | return changes_dict |
| 84 | |
| 85 | def _updated(name, resource, resource_definition): |
| 86 | changes_dict = {'name': name, |
| 87 | 'changes': resource_definition, |
| 88 | 'result': True, |
| 89 | 'comment': '{0} {1} tenant was updated'.format(resource, name)} |
| 90 | return changes_dict |
| 91 | |
| 92 | def _update_failed(name, resource): |
| 93 | changes_dict = {'name': name, |
| 94 | 'changes': {}, |
| 95 | 'comment': '{0} {1} failed to update'.format(resource, name), |
| 96 | 'result': False} |
| 97 | return changes_dict |
| 98 | |
| 99 | def _no_change(name, resource, test=False): |
| 100 | changes_dict = {'name': name, |
| 101 | 'changes': {}, |
| 102 | 'result': True} |
| 103 | if test: |
| 104 | changes_dict['comment'] = \ |
| 105 | '{0} {1} will be {2}'.format(resource, name, test) |
| 106 | else: |
| 107 | changes_dict['comment'] = \ |
| 108 | '{0} {1} is in correct state'.format(resource, name) |
| 109 | return changes_dict |