blob: 5dff5c229f0cd7af35091416ddc6f0b0401d3f39 [file] [log] [blame]
Oleksiy Petrenko9910bba2018-11-20 18:02:05 +02001import logging
2
3
4def __virtual__():
5 return 'aodhv2' if 'aodhv2.alarm_list' in __salt__ else False # noqa
6
7
8log = logging.getLogger(__name__)
9
10
11def _aodhv2_call(fname, *args, **kwargs):
12 return __salt__['aodhv2.{}'.format(fname)](*args, **kwargs) # noqa
13
14
15def _resource_present(resource, name, cloud_name, **kwargs):
16 try:
17 method_name = '{}_get_details'.format(resource)
18 exact_resource = _aodhv2_call(
19 method_name, name, cloud_name=cloud_name
20 )[resource]
21 except Exception as e:
22 if 'ResourceNotFound' in repr(e):
23 try:
24 method_name = '{}_create'.format(resource)
25 resp = _aodhv2_call(
26 method_name, name=name, cloud_name=cloud_name, **kwargs
27 )
28 except Exception as e:
29 log.exception('Aodh {0} create failed with {1}'.
30 format(resource, e))
31 return _failed('create', name, resource)
32 return _succeeded('create', name, resource, resp)
33 elif 'MultipleResourcesFound' in repr(e):
34 return _failed('find', name, resource)
35 else:
36 raise
37
38 to_update = {}
39 for key in kwargs:
40 if key not in exact_resource or kwargs[key] != exact_resource[key]:
41 to_update[key] = kwargs[key]
42 try:
43 method_name = '{}_update'.format(resource)
44 resp = _aodhv2_call(
45 method_name, name, cloud_name=cloud_name, **to_update
46 )
47 except Exception as e:
48 log.exception('Aodh {0} update failed with {1}'.format(resource, e))
49 return _failed('update', name, resource)
50 return _succeeded('update', name, resource, resp)
51
52
53def _resource_absent(resource, name, cloud_name):
54 try:
55 method_name = '{}_get_details'.format(resource)
56 _aodhv2_call(
57 method_name, name, cloud_name=cloud_name
58 )[resource]
59 except Exception as e:
60 if 'ResourceNotFound' in repr(e):
61 return _succeeded('absent', name, resource)
62 if 'MultipleResourcesFound' in repr(e):
63 return _failed('find', name, resource)
64 try:
65 method_name = '{}_delete'.format(resource)
66 _aodhv2_call(
67 method_name, name, cloud_name=cloud_name
68 )
69 except Exception as e:
70 log.error('Aodh delete {0} failed with {1}'.format(resource, e))
71 return _failed('delete', name, resource)
72 return _succeeded('delete', name, resource)
73
74
75def alarm_present(name, cloud_name, **kwargs):
76 return _resource_present('alarm', name, cloud_name, **kwargs)
77
78
79def alarm_absent(name, cloud_name):
80 return _resource_absent('alarm', name, cloud_name)
81
82
83def _succeeded(op, name, resource, changes=None):
84 msg_map = {
85 'create': '{0} {1} created',
86 'delete': '{0} {1} removed',
87 'update': '{0} {1} updated',
88 'no_changes': '{0} {1} is in desired state',
89 'absent': '{0} {1} not present',
90 'resources_moved': '{1} resources were moved from {0}',
91 }
92 changes_dict = {
93 'name': name,
94 'result': True,
95 'comment': msg_map[op].format(resource, name),
96 'changes': changes or {},
97 }
98 return changes_dict
99
100
101def _failed(op, name, resource):
102 msg_map = {
103 'create': '{0} {1} failed to create',
104 'delete': '{0} {1} failed to delete',
105 'update': '{0} {1} failed to update',
106 'find': '{0} {1} found multiple {0}',
107 'resources_moved': 'failed to move {1} from {0}',
108 }
109 changes_dict = {
110 'name': name,
111 'result': False,
112 'comment': msg_map[op].format(resource, name),
113 'changes': {},
114 }
115 return changes_dict