blob: 5cadd65762a37c24ab987c7eda4a43cd82473e36 [file] [log] [blame]
Mykyta Karpinfa53b302018-06-13 14:52:16 +03001# -*- coding: utf-8 -*-
2'''
3Managing entities in Gnocchi
4===================================
5'''
6# Import python libs
7import logging
8
9# Import Gnocchi libs
10def __virtual__():
11 return 'gnocchiv1' if 'gnocchiv1.archive_policy_list' in __salt__ else False
12
13
14log = logging.getLogger(__name__)
15
16
17def _gnocchiv1_call(fname, *args, **kwargs):
18 return __salt__['gnocchiv1.{}'.format(fname)](*args, **kwargs)
19
20
21def archive_policy_present(name, cloud_name, definition, **kwargs):
22 """
23 Creates an archive policy
24
25 This state checks if an archive policy is present and, if not, creates an
26 archive policy with specified name, definition, aggregation methods and
27 back_window.
28
29 :param name: name of the archive policy
30 :param cloud_name: name of the cloud in cloud.yaml
31 :param definition: List of dictionaries. Every dictionary may contain:
32 :param granularity: String indicating policy's granularity e.g. '0:00:01'
33 :param points: Integer indicating policy's points e.g. 10
34 :param: timespan parameters of archive policy. e.g. '0:00:10'
35 :param aggregation_methods: List of aggregation methods used in archive policy
36 :param back_window: Integer specifies the number of coarsest periods to keep
37 """
38 try:
39 archive_policy = _gnocchiv1_call(
40 'archive_policy_read', name, cloud_name=cloud_name
41 )
42 except Exception as e:
43 if 'NotFound' in repr(e):
44 try:
45 resp = _gnocchiv1_call(
46 'archive_policy_create', name=name, cloud_name=cloud_name,
47 definition=definition, **kwargs
48 )
49 except Exception as e:
50 log.error('Gnocchi archive policy create failed with {}'.format(e))
51 return _create_failed(name, 'archive_policy')
52 return _created(name, 'archive_policy', resp)
53 else:
54 raise
55 if archive_policy:
56 # TODO: Implement and design archive policy update procedure
57 return _no_changes(name, 'archive_policy')
58 else:
59 return _find_failed(name, 'archive_policy')
60
61
62def archive_policy_absent(name, cloud_name):
63 try:
64 archive_policy = _gnocchiv1_call(
65 'archive_policy_read', name, cloud_name=cloud_name
66 )
67 except Exception as e:
68 if 'NotFound' in repr(e):
69 return _absent(name, 'archive_policy')
70 if archive_policy:
71 try:
72 resp = _gnocchiv1_call(
73 'archive_policy_delete', name, cloud_name=cloud_name
74 )
75 except Exception as e:
76 log.error('Archive policy delete failed with {}'.format(e))
77 return _delete_failed(name, 'archive_policy')
78 return _deleted(name, 'archive_policy')
79 else:
80 return _find_failed(name, 'archive_policy')
81
82
83def archive_policy_rule_present(name, cloud_name, archive_policy_name, metric_pattern):
84 """
85 Creates an archive policy rule
86
87 This state checks if an archive policy rule is present and, if not, creates an
88 archive policy rule with specified name, archive policy name, and metric_pattern.
89
90 :param name: name of the archive policy rule
91 :param cloud_name: name of the cloud in cloud.yaml
92 :param archive_policy_name: name of archive policy for specified rule
93 :param metric_pattern: pattern for metrics classified by the rule
94 """
95 try:
96 archive_policy_rule = _gnocchiv1_call(
97 'archive_policy_rule_read', name, cloud_name=cloud_name
98 )
99 except Exception as e:
100 if 'NotFound' in repr(e):
101 try:
102 resp = _gnocchiv1_call(
103 'archive_policy_rule_create', name=name, cloud_name=cloud_name,
104 archive_policy_name=archive_policy_name, metric_pattern=metric_pattern,
105 )
106 except Exception as e:
107 log.error('Gnocchi archive policy rule create failed with {}'.format(e))
108 return _create_failed(name, 'archive_policy_rule')
109 return _created(name, 'archive_policy_rule', resp)
110 else:
111 raise
112 if archive_policy_rule:
113 # Currently Gnocchi API doesn't allow to update properties in rules,
114 # but they can be recreated.
115 if ((archive_policy_rule['archive_policy_name'] != archive_policy_name) or
116 (archive_policy_rule['metric_pattern'] != metric_pattern)):
117 try:
118 resp = _gnocchiv1_call(
119 'archive_policy_rule_delete', name, cloud_name=cloud_name
120 )
121 resp = _gnocchiv1_call(
122 'archive_policy_rule_create', name=name, cloud_name=cloud_name,
123 archive_policy_name=archive_policy_name, metric_pattern=metric_pattern
124 )
125 except Exception as e:
126 log.error('Archive policy rule update failed with {}'.format(e))
127 return _update_failed(name, 'archive_policy_rule')
128 return _updated(name, 'archive_policy_rule', resp)
129 else:
130 return _no_changes(name, 'archive_policy_rule')
131 else:
132 return _find_failed(name, 'archive_policy_rule')
133
134def archive_policy_rule_absent(name, cloud_name):
135 try:
136 archive_policy_rule = _gnocchiv1_call(
137 'archive_policy_rule_read', name, cloud_name=cloud_name
138 )
139 except Exception as e:
140 if 'NotFound' in repr(e):
141 return _absent(name, 'archive_policy_rule')
142 if archive_policy_rule:
143 try:
144 resp = _gnocchiv1_call(
145 'archive_policy_rule_delete', name, cloud_name=cloud_name
146 )
147 except Exception as e:
148 log.error('Archive policy rule delete failed with {}'.format(e))
149 return _delete_failed(name, 'archive_policy_rule')
150 return _deleted(name, 'archive_policy_rule')
151 else:
152 return _find_failed(name, 'archive_policy_rule')
153
154
155def _created(name, resource, resource_definition):
156 changes_dict = {
157 'name': name,
158 'changes': resource_definition,
159 'result': True,
160 'comment': '{}{} created'.format(resource, name)
161 }
162 return changes_dict
163
164
165def _updated(name, resource, resource_definition):
166 changes_dict = {
167 'name': name,
168 'changes': resource_definition,
169 'result': True,
170 'comment': '{}{} updated'.format(resource, name)
171 }
172 return changes_dict
173
174
175def _no_changes(name, resource):
176 changes_dict = {
177 'name': name,
178 'changes': {},
179 'result': True,
180 'comment': '{}{} is in desired state'.format(resource, name)
181 }
182 return changes_dict
183
184
185def _deleted(name, resource):
186 changes_dict = {
187 'name': name,
188 'changes': {},
189 'result': True,
190 'comment': '{}{} removed'.format(resource, name)
191 }
192 return changes_dict
193
194
195def _absent(name, resource):
196 changes_dict = {'name': name,
197 'changes': {},
198 'comment': '{0} {1} not present'.format(resource, name),
199 'result': True}
200 return changes_dict
201
202
203def _delete_failed(name, resource):
204 changes_dict = {'name': name,
205 'changes': {},
206 'comment': '{0} {1} failed to delete'.format(resource,
207 name),
208 'result': False}
209 return changes_dict
210
211
212def _create_failed(name, resource):
213 changes_dict = {'name': name,
214 'changes': {},
215 'comment': '{0} {1} failed to create'.format(resource,
216 name),
217 'result': False}
218 return changes_dict
219
220
221def _update_failed(name, resource):
222 changes_dict = {'name': name,
223 'changes': {},
224 'comment': '{0} {1} failed to update'.format(resource,
225 name),
226 'result': False}
227 return changes_dict
228
229
230def _find_failed(name, resource):
231 changes_dict = {
232 'name': name,
233 'changes': {},
234 'comment': '{0} {1} found multiple {0}'.format(resource, name),
235 'result': False,
236 }
237 return changes_dict
238