blob: 15cff40837db200edc3615b7b0593f3738582238 [file] [log] [blame]
kairat_kushaev5c8626d2018-06-09 18:15:15 +04001"""
2Management of Cinder resources
3"""
4
5import ast
6import logging
7
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +02008log = logging.getLogger(__name__)
kairat_kushaev5c8626d2018-06-09 18:15:15 +04009
10
11def __virtual__():
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020012 return 'cinderv3' if 'cinderv3.volume_list' in __salt__ else False # noqa
kairat_kushaev5c8626d2018-06-09 18:15:15 +040013
14
15def _cinder_call(fname, *args, **kwargs):
16 return __salt__['cinderv3.{}'.format(fname)](*args, **kwargs)
17
18
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020019def _resource_present(resource, name, cloud_name, **kwargs):
20 try:
21 method_name = '{}_get_details'.format(resource)
22 exact_resource = _cinder_call(
23 method_name, name, cloud_name=cloud_name
24 )[resource]
25 except Exception as e:
26 if 'ResourceNotFound' in repr(e):
27 try:
28 method_name = '{}_create'.format(resource)
29 resp = _cinder_call(
30 method_name, name=name, cloud_name=cloud_name, **kwargs
31 )
32 except Exception as e:
33 log.exception('Cinder {0} create failed with {1}'.
34 format(resource, e))
35 return _failed('create', name, resource)
36 return _succeeded('create', name, resource, resp)
37 elif 'MultipleResourcesFound' in repr(e):
38 return _failed('find', name, resource)
kairat_kushaev5c8626d2018-06-09 18:15:15 +040039 else:
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020040 raise
41
42 to_update = {}
43 if to_update:
44 for key in kwargs:
45 if key not in exact_resource or kwargs[key] != exact_resource[key]:
46 to_update[key] = kwargs[key]
47 try:
48 method_name = '{}_update'.format(resource)
49 resp = _cinder_call(
50 method_name, name, cloud_name=cloud_name, **to_update
51 )
52 except Exception as e:
53 log.exception('Cinder {0} update failed with {1}'.format(resource, e))
54 return _failed('update', name, resource)
55 return _succeeded('update', name, resource, resp)
56 return _succeeded('no_changes', name, resource)
kairat_kushaev5c8626d2018-06-09 18:15:15 +040057
58
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020059def _resource_absent(resource, name, cloud_name):
60 try:
61 method_name = '{}_get_details'.format(resource)
62 _cinder_call(
63 method_name, name, cloud_name=cloud_name
64 )[resource]
65 except Exception as e:
66 if 'ResourceNotFound' in repr(e):
67 return _succeeded('absent', name, resource)
68 if 'MultipleResourcesFound' in repr(e):
69 return _failed('find', name, resource)
70 try:
71 method_name = '{}_delete'.format(resource)
72 _cinder_call(
73 method_name, name, cloud_name=cloud_name
74 )
75 except Exception as e:
76 log.error('Cinder delete {0} failed with {1}'.format(resource, e))
77 return _failed('delete', name, resource)
78 return _succeeded('delete', name, resource)
79
80
81def volume_type_present(name, cloud_name, **kwargs):
82 return _resource_present('volume_type', name, cloud_name, **kwargs)
83
84
85def volume_type_absent(name, cloud_name):
86 return _resource_absent('volume_type', name, cloud_name)
87
88
89def volume_present(name, cloud_name, size, **kwargs):
90 kwargs.update({
91 'size': size,
92 })
93 return _resource_present('volume', name, cloud_name, **kwargs)
94
95
96def volume_absent(name, cloud_name):
97 return _resource_absent('volume', name, cloud_name)
kairat_kushaev5c8626d2018-06-09 18:15:15 +040098
99
100def volume_type_key_present(name=None, key=None, value=None, cloud_name=None):
101 """
102 Ensures that the extra specs are present on a volume type.
103 """
104 keys = "{u'" + key + "': u'" + value + "'}"
105 keys = ast.literal_eval(keys)
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +0200106 signal_create = _cinder_call('keys_volume_type_set', name, keys=keys,
107 cloud_name=cloud_name)
kairat_kushaev5c8626d2018-06-09 18:15:15 +0400108 if signal_create["result"] is True:
109 ret = {
110 'name': name,
111 'changes': keys,
112 'result': True,
113 'comment': 'Volume type "{0}" was updated'.format(name)
114 }
115 else:
116 ret = {
117 'name': name,
118 'changes': {},
119 'result': False,
120 'comment': signal_create.get("comment")
121 }
122 return ret
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +0200123
124
125def _succeeded(op, name, resource, changes=None):
126 msg_map = {
127 'create': '{0} {1} created',
128 'delete': '{0} {1} removed',
129 'update': '{0} {1} updated',
130 'no_changes': '{0} {1} is in desired state',
131 'absent': '{0} {1} not present',
132 'resources_moved': '{1} resources were moved from {0}',
133 }
134 changes_dict = {
135 'name': name,
136 'result': True,
137 'comment': msg_map[op].format(resource, name),
138 'changes': changes or {},
139 }
140 return changes_dict
141
142def _failed(op, name, resource):
143 msg_map = {
144 'create': '{0} {1} failed to create',
145 'delete': '{0} {1} failed to delete',
146 'update': '{0} {1} failed to update',
147 'find': '{0} {1} found multiple {0}',
148 'resources_moved': 'failed to move {1} from {0}',
149 }
150 changes_dict = {
151 'name': name,
152 'result': False,
153 'comment': msg_map[op].format(resource, name),
154 'changes': {},
155 }
156 return changes_dict