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