blob: c45e9704d566ca7d603c21a0c6333ea262848f80 [file] [log] [blame]
kairat_kushaev5c8626d2018-06-09 18:15:15 +04001"""
2Management of Cinder resources
3"""
4
5import ast
6import logging
7
8LOG = logging.getLogger(__name__)
9
10
11def __virtual__():
12 return 'cinderv3'
13
14
15def _cinder_call(fname, *args, **kwargs):
16 return __salt__['cinderv3.{}'.format(fname)](*args, **kwargs)
17
18
19def volume_type_present(name=None, cloud_name=None):
20 """
21 Ensures that the specified volume type is present.
22 """
23 ret = {
24 'name': name,
25 'changes': {},
26 'result': True,
27 'comment': 'Volume type "{0}" already exists'.format(name)
28 }
29 type_req = _cinder_call('volume_type_get', name=name, cloud_name=cloud_name)
30 if type_req.get("result"):
31 return ret
32 else:
33 create_req = _cinder_call('volume_type_create', name=name,
34 cloud_name=cloud_name)
35 if create_req.get("result") is False:
36 ret = {
37 'name': name,
38 'changes': {},
39 'result': False,
40 'comment': 'Volume type "{0}" failed to create'.format(name)
41 }
42 else:
43 ret['comment'] = 'Volume type {0} has been created'.format(name)
44 ret['changes']['Volume type'] = 'Created'
45 return ret
46
47
48def volume_type_absent(name=None, cloud_name=None):
49 """
50 Ensures that the specified volume type is absent.
51 """
52 ret = {
53 'name': name,
54 'changes': {},
55 'result': True,
56 'comment': 'Volume type "{0}" not found'.format(name)
57 }
58 type_req = _cinder_call('volume_type_get', name=name, cloud_name=cloud_name)
59 if not type_req.get("result"):
60 return ret
61 else:
62 delete_req = _cinder_call('volume_type_delete', name=name,
63 cloud_name=cloud_name)
64 if delete_req.get("result") is False:
65 ret = {
66 'name': name,
67 'changes': {},
68 'result': False,
69 'comment': 'Volume type "{0}" failed to delete'.format(name)
70 }
71 else:
72 ret['comment'] = 'Volume type {0} has been deleted'.format(name)
73 ret['changes']['Volume type'] = 'Deleted'
74 return ret
75
76
77def volume_type_key_present(name=None, key=None, value=None, cloud_name=None):
78 """
79 Ensures that the extra specs are present on a volume type.
80 """
81 keys = "{u'" + key + "': u'" + value + "'}"
82 keys = ast.literal_eval(keys)
83 signal_create = _cinder_call('keys_volume_type_set',
84 name=name, keys=keys, cloud_name=cloud_name)
85 if signal_create["result"] is True:
86 ret = {
87 'name': name,
88 'changes': keys,
89 'result': True,
90 'comment': 'Volume type "{0}" was updated'.format(name)
91 }
92 else:
93 ret = {
94 'name': name,
95 'changes': {},
96 'result': False,
97 'comment': signal_create.get("comment")
98 }
99 return ret