blob: 18a09794d6d2fcff6ec03c1e4ab5b5a85214796e [file] [log] [blame]
Jiri Broulik47aa6b32017-07-10 18:39:15 +02001# -*- coding: utf-8 -*-
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +03002"""
Jiri Broulik47aa6b32017-07-10 18:39:15 +02003Management of Cinder resources
4===============================
5:depends: - cinderclient Python module
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +03006"""
7
Jiri Broulik47aa6b32017-07-10 18:39:15 +02008import ast
9import logging
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030010
Jiri Broulik47aa6b32017-07-10 18:39:15 +020011LOG = logging.getLogger(__name__)
12
13
14def __virtual__():
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030015 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020016 Only load if python-cinderclient is present in __salt__
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030017 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020018 return 'cinderng'
19
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030020
Jiri Broulik47aa6b32017-07-10 18:39:15 +020021def volume_type_present(name=None, profile=None):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030022 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020023 Ensures that the specified volume type is present.
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030024 """
25 ret = {
26 'name': name,
27 'changes': {},
28 'result': True,
29 'comment': 'Volume type "{0}" already exists'.format(name)
30 }
Jiri Broulik47aa6b32017-07-10 18:39:15 +020031 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. Meltsaykin6b1fa432017-09-18 16:49:53 +030038 ret = {
39 'name': name,
40 'changes': {},
41 'result': False,
42 'comment': 'Volume type "{0}" failed to create'.format(name)
43 }
Jiri Broulik47aa6b32017-07-10 18:39:15 +020044 return ret
45
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030046
Jiri Broulik47aa6b32017-07-10 18:39:15 +020047def volume_type_key_present(name=None, key=None, value=None, profile=None):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030048 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020049 Ensures that the extra specs are present on a volume type.
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030050 """
Jiri Broulik47aa6b32017-07-10 18:39:15 +020051 keys = "{u'" + key + "': u'" + value + "'}"
52 keys = ast.literal_eval(keys)
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030053 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 Broulik47aa6b32017-07-10 18:39:15 +020060 signal = __salt__['cinderng.set_keys_volume_type'](name, keys, profile)
61 if 'exist' in signal:
62 pass
63 elif 'updated' in signal:
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030064 ret['comment'] = 'Volume type keys "{0}" in volume type "{1}" ' \
65 'have been updated'.format(keys, name)
Jiri Broulik47aa6b32017-07-10 18:39:15 +020066 ret['changes']['Volume type keys'] = 'Updated'
67 elif 'failed' in signal:
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030068 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 Broulik47aa6b32017-07-10 18:39:15 +020075 elif 'not found' in signal:
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030076 ret = {
77 'name': name,
78 'changes': {},
79 'result': False,
80 'comment': 'Volume type "{0}" was not found'.format(name)
81 }
Jiri Broulik47aa6b32017-07-10 18:39:15 +020082 return ret
83
84
85def _already_exists(name, resource):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030086 changes_dict = {
87 'name': name,
88 'changes': {},
89 'result': True,
90 'comment': '{0} {1} already exists'.format(resource, name)
91 }
Jiri Broulik47aa6b32017-07-10 18:39:15 +020092 return changes_dict
93
94
95def _created(name, resource, resource_definition):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +030096 changes_dict = {
97 'name': name,
98 'changes': resource_definition,
99 'result': True,
100 'comment': '{0} {1} created'.format(resource, name)
101 }
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200102 return changes_dict
103
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300104
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200105def _updated(name, resource, resource_definition):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300106 changes_dict = {
107 'name': name,
108 'changes': resource_definition,
109 'result': True,
110 'comment': '{0} {1} tenant was updated'.format(resource, name)
111 }
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200112 return changes_dict
113
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300114
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200115def _update_failed(name, resource):
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300116 changes_dict = {
117 'name': name,
118 'changes': {},
119 'comment': '{0} {1} failed to update'.format(resource, name),
120 'result': False
121 }
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200122 return changes_dict
123
Denis V. Meltsaykin6b1fa432017-09-18 16:49:53 +0300124
Jiri Broulik47aa6b32017-07-10 18:39:15 +0200125def _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