kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 1 | """ |
| 2 | Management of Cinder resources |
| 3 | """ |
| 4 | |
| 5 | import ast |
| 6 | import logging |
| 7 | |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 8 | log = logging.getLogger(__name__) |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 9 | |
| 10 | |
| 11 | def __virtual__(): |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 12 | return 'cinderv3' if 'cinderv3.volume_list' in __salt__ else False # noqa |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 13 | |
| 14 | |
| 15 | def _cinder_call(fname, *args, **kwargs): |
Sergey Galkin | 4501d0c | 2020-02-10 17:50:45 +0400 | [diff] [blame] | 16 | if __opts__.get('test') and not (fname.find('_list') > 0 or fname.find('_get_') > 0): |
| 17 | return {'changes': 'cinderv3 state {} with options {} {} will be executed'.format(fname, args, kwargs), |
| 18 | 'result': None} |
| 19 | else: |
| 20 | return __salt__['cinderv3.{}'.format(fname)](*args, **kwargs) |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 21 | |
| 22 | |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 23 | def _resource_present(resource, name, cloud_name, **kwargs): |
| 24 | try: |
| 25 | method_name = '{}_get_details'.format(resource) |
| 26 | exact_resource = _cinder_call( |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 27 | method_name, name, cloud_name=cloud_name, **kwargs |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 28 | )[resource] |
| 29 | except Exception as e: |
| 30 | if 'ResourceNotFound' in repr(e): |
| 31 | try: |
| 32 | method_name = '{}_create'.format(resource) |
| 33 | resp = _cinder_call( |
| 34 | method_name, name=name, cloud_name=cloud_name, **kwargs |
| 35 | ) |
| 36 | except Exception as e: |
| 37 | log.exception('Cinder {0} create failed with {1}'. |
| 38 | format(resource, e)) |
| 39 | return _failed('create', name, resource) |
| 40 | return _succeeded('create', name, resource, resp) |
| 41 | elif 'MultipleResourcesFound' in repr(e): |
| 42 | return _failed('find', name, resource) |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 43 | else: |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 44 | raise |
| 45 | |
| 46 | to_update = {} |
| 47 | if to_update: |
| 48 | for key in kwargs: |
| 49 | if key not in exact_resource or kwargs[key] != exact_resource[key]: |
| 50 | to_update[key] = kwargs[key] |
| 51 | try: |
| 52 | method_name = '{}_update'.format(resource) |
| 53 | resp = _cinder_call( |
| 54 | method_name, name, cloud_name=cloud_name, **to_update |
| 55 | ) |
| 56 | except Exception as e: |
| 57 | log.exception('Cinder {0} update failed with {1}'.format(resource, e)) |
| 58 | return _failed('update', name, resource) |
| 59 | return _succeeded('update', name, resource, resp) |
| 60 | return _succeeded('no_changes', name, resource) |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 61 | |
| 62 | |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 63 | def _resource_absent(resource, name, cloud_name, connection_params=None): |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 64 | try: |
| 65 | method_name = '{}_get_details'.format(resource) |
| 66 | _cinder_call( |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 67 | method_name, name, cloud_name=cloud_name, |
| 68 | connection_params=connection_params |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 69 | )[resource] |
| 70 | except Exception as e: |
| 71 | if 'ResourceNotFound' in repr(e): |
| 72 | return _succeeded('absent', name, resource) |
| 73 | if 'MultipleResourcesFound' in repr(e): |
| 74 | return _failed('find', name, resource) |
| 75 | try: |
| 76 | method_name = '{}_delete'.format(resource) |
| 77 | _cinder_call( |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 78 | method_name, name, cloud_name=cloud_name, |
| 79 | connection_params=connection_params |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 80 | ) |
| 81 | except Exception as e: |
| 82 | log.error('Cinder delete {0} failed with {1}'.format(resource, e)) |
| 83 | return _failed('delete', name, resource) |
| 84 | return _succeeded('delete', name, resource) |
| 85 | |
| 86 | |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 87 | def service_enabled(name, binary, cloud_name, connection_params=None): |
Oleksandr Shyshko | 04099ea | 2018-11-22 16:02:52 +0200 | [diff] [blame] | 88 | """Ensures that the service is enabled on the host |
| 89 | |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 90 | :param name: name of a host where service is running |
| 91 | :param binary: name of the service have to be run |
| 92 | :param connection_params: dictionary with salt internal connection params |
Oleksandr Shyshko | 04099ea | 2018-11-22 16:02:52 +0200 | [diff] [blame] | 93 | """ |
| 94 | changes = {} |
Oleksandr Shyshko | 2551382 | 2018-12-07 15:26:56 +0000 | [diff] [blame] | 95 | ret = [] |
Oleksandr Shyshko | 04099ea | 2018-11-22 16:02:52 +0200 | [diff] [blame] | 96 | |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 97 | services = _cinder_call('service_list', host=name, binary=binary, cloud_name=cloud_name, |
| 98 | connection_params=connection_params)['services'] |
Oleksandr Shyshko | 04099ea | 2018-11-22 16:02:52 +0200 | [diff] [blame] | 99 | |
Oleksandr Shyshko | 2551382 | 2018-12-07 15:26:56 +0000 | [diff] [blame] | 100 | disabled_service = [s for s in services if s['status'] == 'disabled'] |
Oleksandr Shyshko | 04099ea | 2018-11-22 16:02:52 +0200 | [diff] [blame] | 101 | |
Oleksandr Shyshko | 2551382 | 2018-12-07 15:26:56 +0000 | [diff] [blame] | 102 | if len(disabled_service): |
| 103 | for service in disabled_service: |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 104 | changes = _cinder_call('service_update', service['host'], binary, 'enable', cloud_name=cloud_name, |
| 105 | connection_params=connection_params) |
Oleksandr Shyshko | 2551382 | 2018-12-07 15:26:56 +0000 | [diff] [blame] | 106 | ret.append(changes) |
| 107 | return _succeeded('update', name, binary, {'changes':ret}) |
| 108 | return _succeeded('no_changes', name, binary) |
Oleksandr Shyshko | 04099ea | 2018-11-22 16:02:52 +0200 | [diff] [blame] | 109 | |
| 110 | |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 111 | def service_disabled(name, binary, cloud_name, disabled_reason=None, connection_params=None): |
Oleksandr Shyshko | 04099ea | 2018-11-22 16:02:52 +0200 | [diff] [blame] | 112 | """Ensures that the service is disabled on the host |
| 113 | |
| 114 | :param name: name of a host where service is running |
| 115 | :param binary: name of the service have to be disabled |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 116 | :param connection_params: dictionary with salt internal connection params |
Oleksandr Shyshko | 04099ea | 2018-11-22 16:02:52 +0200 | [diff] [blame] | 117 | """ |
Oleksandr Shyshko | 04099ea | 2018-11-22 16:02:52 +0200 | [diff] [blame] | 118 | kwargs = {} |
Oleksandr Shyshko | 2551382 | 2018-12-07 15:26:56 +0000 | [diff] [blame] | 119 | ret = [] |
Oleksandr Shyshko | 04099ea | 2018-11-22 16:02:52 +0200 | [diff] [blame] | 120 | |
Oleksandr Shyshko | 2551382 | 2018-12-07 15:26:56 +0000 | [diff] [blame] | 121 | if disabled_reason: |
Oleksandr Shyshko | 04099ea | 2018-11-22 16:02:52 +0200 | [diff] [blame] | 122 | kwargs['disabled_reason'] = disabled_reason |
| 123 | |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 124 | services = _cinder_call('service_list', host=name, binary=binary, cloud_name=cloud_name, |
| 125 | connection_params=connection_params)['services'] |
Oleksandr Shyshko | 04099ea | 2018-11-22 16:02:52 +0200 | [diff] [blame] | 126 | |
Oleksandr Shyshko | 2551382 | 2018-12-07 15:26:56 +0000 | [diff] [blame] | 127 | enabled_services = [s for s in services if s['status'] == 'enabled'] |
Oleksandr Shyshko | 04099ea | 2018-11-22 16:02:52 +0200 | [diff] [blame] | 128 | |
Oleksandr Shyshko | 2551382 | 2018-12-07 15:26:56 +0000 | [diff] [blame] | 129 | if len(enabled_services): |
| 130 | for service in enabled_services: |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 131 | changes = _cinder_call('service_update', service['host'], binary, 'disable', |
| 132 | cloud_name=cloud_name, connection_params=connection_params, |
| 133 | **kwargs) |
Oleksandr Shyshko | 2551382 | 2018-12-07 15:26:56 +0000 | [diff] [blame] | 134 | ret.append(changes) |
| 135 | return _succeeded('update', name, binary, {'changes':ret}) |
| 136 | return _succeeded('no_changes', name, binary) |
Oleksandr Shyshko | 04099ea | 2018-11-22 16:02:52 +0200 | [diff] [blame] | 137 | |
| 138 | |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 139 | def volume_type_present(name, cloud_name, **kwargs): |
| 140 | return _resource_present('volume_type', name, cloud_name, **kwargs) |
| 141 | |
| 142 | |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 143 | def volume_type_absent(name, cloud_name, connection_params=None): |
| 144 | return _resource_absent('volume_type', name, cloud_name, connection_params) |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 145 | |
| 146 | |
| 147 | def volume_present(name, cloud_name, size, **kwargs): |
| 148 | kwargs.update({ |
| 149 | 'size': size, |
| 150 | }) |
| 151 | return _resource_present('volume', name, cloud_name, **kwargs) |
| 152 | |
| 153 | |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 154 | def volume_absent(name, cloud_name, connection_params=None): |
| 155 | return _resource_absent('volume', name, cloud_name, connection_params) |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 156 | |
| 157 | |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 158 | def volume_type_key_present(name=None, key=None, value=None, cloud_name=None, |
| 159 | connection_params=None): |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 160 | """ |
| 161 | Ensures that the extra specs are present on a volume type. |
| 162 | """ |
| 163 | keys = "{u'" + key + "': u'" + value + "'}" |
| 164 | keys = ast.literal_eval(keys) |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 165 | signal_create = _cinder_call('keys_volume_type_set', name, keys=keys, |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 166 | cloud_name=cloud_name, |
| 167 | connection_params=connection_params) |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 168 | if signal_create["result"] is True: |
| 169 | ret = { |
| 170 | 'name': name, |
| 171 | 'changes': keys, |
| 172 | 'result': True, |
| 173 | 'comment': 'Volume type "{0}" was updated'.format(name) |
| 174 | } |
Sergey Galkin | 4501d0c | 2020-02-10 17:50:45 +0400 | [diff] [blame] | 175 | elif signal_create["result"] is None: |
| 176 | ret = { |
| 177 | 'name': name, |
Ivan Berezovskiy | 0578eee | 2020-03-30 19:22:28 +0400 | [diff] [blame] | 178 | 'changes': {}, |
Sergey Galkin | 4501d0c | 2020-02-10 17:50:45 +0400 | [diff] [blame] | 179 | 'result': None, |
| 180 | 'comment': 'Volume type "{0}" will be updated'.format(name) |
| 181 | } |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 182 | else: |
| 183 | ret = { |
| 184 | 'name': name, |
| 185 | 'changes': {}, |
| 186 | 'result': False, |
| 187 | 'comment': signal_create.get("comment") |
| 188 | } |
| 189 | return ret |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 190 | |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 191 | def _succeeded(op, name, resource, changes=None): |
| 192 | msg_map = { |
| 193 | 'create': '{0} {1} created', |
| 194 | 'delete': '{0} {1} removed', |
| 195 | 'update': '{0} {1} updated', |
| 196 | 'no_changes': '{0} {1} is in desired state', |
| 197 | 'absent': '{0} {1} not present', |
| 198 | 'resources_moved': '{1} resources were moved from {0}', |
| 199 | } |
| 200 | changes_dict = { |
| 201 | 'name': name, |
Ivan Berezovskiy | 0578eee | 2020-03-30 19:22:28 +0400 | [diff] [blame] | 202 | 'result': None if __opts__.get('test') else True, |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 203 | 'comment': msg_map[op].format(resource, name), |
| 204 | 'changes': changes or {}, |
| 205 | } |
| 206 | return changes_dict |
| 207 | |
| 208 | def _failed(op, name, resource): |
| 209 | msg_map = { |
| 210 | 'create': '{0} {1} failed to create', |
| 211 | 'delete': '{0} {1} failed to delete', |
| 212 | 'update': '{0} {1} failed to update', |
| 213 | 'find': '{0} {1} found multiple {0}', |
| 214 | 'resources_moved': 'failed to move {1} from {0}', |
| 215 | } |
| 216 | changes_dict = { |
| 217 | 'name': name, |
| 218 | 'result': False, |
| 219 | 'comment': msg_map[op].format(resource, name), |
| 220 | 'changes': {}, |
| 221 | } |
Oleksandr Shyshko | 04099ea | 2018-11-22 16:02:52 +0200 | [diff] [blame] | 222 | return changes_dict |