Coverage for src/zenossapi/routers/properties.py: 92%
117 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-25 05:47 +0000
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-25 05:47 +0000
1# -*- coding: utf-8 -*-
3"""
4Zenoss properties_router
5"""
7from zenossapi.apiclient import ZenossAPIClientError
8from zenossapi.routers import ZenossRouter
11class PropertiesRouter(ZenossRouter):
12 """
13 Class for interacting with Zenoss properties.
14 """
16 def __init__(self, url, headers, ssl_verify):
17 super(PropertiesRouter, self).__init__(url, headers, ssl_verify,
18 'properties_router',
19 'PropertiesRouter')
21 self.id = None
23 def __repr__(self):
24 if self.id:
25 identifier = "id {0}".format(self.id)
26 else:
27 identifier = "at {0}".format(hex(id(self)))
29 return '<{0} object {1}>'.format(
30 type(self).__name__, identifier
31 )
33 def _refresh(self, property_data):
34 """
35 Refresh the attributes of the object, e.g. after deleting a local copy.
37 Arguments:
38 property_data (dict): New attribute values
39 """
40 skip = ['api_action', 'api_endpoint', 'api_headers', 'api_url', 'ssl_verify']
42 for pd in property_data:
43 if pd in skip:
44 continue
45 setattr(self, pd, property_data[pd])
47 def list_properties(self, uid, params=None, sort=None, sort_dir='ASC'):
48 """
49 Get a list of ZenProperties for the uid context.
51 Arguments:
52 uid (str): UID of the object to list properties for.
53 params (dict): Search parameters to filter the properties list on.
54 sort (str): Sort key for the properties list.
55 sort_dir (str): Sort direction, either ASC or DESC
57 Returns:
58 dict(int, list(dict)): ::
60 {
61 'total': Total count of properties returned.
62 'properties': List of properties found.
63 }
65 """
66 properties_data = self._router_request(
67 self._make_request_data(
68 'getZenProperties',
69 dict(
70 uid=uid,
71 params=params,
72 sort=sort,
73 dir=sort_dir,
74 )
75 )
76 )
78 return dict(
79 total=properties_data['totalCount'],
80 properties=properties_data['data'],
81 )
83 def get_properties(self, uid, params=None):
84 """
85 Get ZenossProperties objects for the properties of a uid context.
87 Arguments:
88 uid (str): UID of the object to get properties for.
89 params (dict): Search parameters for filter the properties on.
91 Returns:
92 dict(int, list(ZenossProperty)): ::
94 {
95 'total': Total count of properties returned.
96 'properties': List of ZenossProperty objects.
97 }
99 """
100 properties_data = self.list_properties(uid, params=params)
102 props = dict(
103 total=properties_data['total'],
104 properties=[]
105 )
106 for prop in properties_data['properties']:
107 props['properties'].append(
108 ZenossProperty(
109 self.api_url,
110 self.api_headers,
111 self.ssl_verify,
112 prop,
113 )
114 )
116 return props
118 def list_local_properties(self, uid):
119 """
120 Get a list of properties set locally to the specified UID.
122 Arguments:
123 uid (str): UID to get local properties for.
125 Returns:
126 dict(int, list(dict)): ::
128 {
129 'total': Total count of properties returned.
130 'properties': List of properties found.
131 }
132 """
133 return self.list_properties(uid, params=dict(islocal='1'))
135 def get_local_properties(self, uid):
136 """
137 Get ZenossProperty objects for the local properties of a specified uid.
139 Arguments:
140 uid (str): UID to get local properties for.
142 Returns:
143 dict(int, list(ZenossProperty)): ::
145 {
146 'total': Total count of properties returned.
147 'properties': List of ZenossProperty objects.
148 }
150 """
151 properties_data = self.list_local_properties(uid)
153 props = dict(
154 total=properties_data['total'],
155 properties=[]
156 )
157 for prop in properties_data['properties']:
158 props['properties'].append(
159 ZenossProperty(
160 self.api_url,
161 self.api_headers,
162 self.ssl_verify,
163 prop,
164 )
165 )
167 return props
169 def get_property(self, uid, zproperty):
170 """
171 Get a single ZenossProperty
173 Arguments:
174 uid (str): UID to get the property of.
175 zproperty (str): ID of the property to get.
177 Returns:
178 ZenossProperty:
179 """
180 prop_data = self._router_request(
181 self._make_request_data(
182 'getZenProperties',
183 dict(
184 uid=uid,
185 params=dict(
186 id=zproperty,
187 )
188 )
189 )
190 )
192 return ZenossProperty(
193 self.api_url,
194 self.api_headers,
195 self.ssl_verify,
196 prop_data['data'][0]
197 )
199 def list_custom_properties(self, uid, params=None, sort=None, sort_dir='ASC'):
200 """
201 Get a list of cProperties for the uid context.
203 Arguments:
204 uid (str): UID of the object to list properties for.
205 params (dict): Search parameters to filter the properties list on.
206 sort (str): Sort key for the properties list.
207 sort_dir (str): Sort direction, either ASC or DESC
209 Returns:
210 dict(int, list(dict)): ::
212 {
213 'total': Total count of properties returned.
214 'properties': List of properties found.
215 }
217 """
218 properties_data = self._router_request(
219 self._make_request_data(
220 'getCustomProperties',
221 dict(
222 uid=uid,
223 params=params,
224 sort=sort,
225 dir=sort_dir,
226 )
227 )
228 )
230 return dict(
231 total=properties_data['totalCount'],
232 properties=properties_data['data'],
233 )
235 def get_custom_properties(self, uid, params=None):
236 """
237 Get ZenossCustomProperties objects for the cProperties of a uid context.
239 Arguments:
240 uid (str): UID of the object to get properties for.
241 params (dict): Search parameters for filter the properties on.
243 Returns:
244 dict(int, list(ZenossCustomProperty)): ::
246 {
247 'total': Total count of properties returned.
248 'properties': List of ZenossCustomProperty objects.
249 }
251 """
252 properties_data = self.list_custom_properties(uid, params=params)
254 props = dict(
255 total=properties_data['total'],
256 properties=[]
257 )
258 for prop in properties_data['properties']:
259 props['properties'].append(
260 ZenossCustomProperty(
261 self.api_url,
262 self.api_headers,
263 self.ssl_verify,
264 prop,
265 )
266 )
268 return props
270 def get_custom_property(self, uid, cproperty):
271 """
272 Get a single ZenossCustomProperty
274 Arguments:
275 uid (str): UID to get the property of.
276 cproperty (str): ID of the property to get.
278 Returns:
279 ZenossCustomProperty:
280 """
281 prop_data = self._router_request(
282 self._make_request_data(
283 'getCustomProperties',
284 dict(
285 uid=uid,
286 params=dict(
287 id=cproperty,
288 )
289 )
290 )
291 )
293 return ZenossCustomProperty(
294 self.api_url,
295 self.api_headers,
296 self.ssl_verify,
297 prop_data['data'][0]
298 )
300 def set_property_value(self, uid, id, value=None):
301 """
302 Sets (or updates) the local value of a property
304 Arguments:
305 value: The new value for the property, type varies by property.
307 Returns:
308 bool:
309 """
310 property_data = self._router_request(
311 self._make_request_data(
312 'update',
313 dict(
314 uid=uid,
315 id=id,
316 value=value,
317 )
318 )
319 )
321 return True
323 def delete_property(self, uid, zproperty):
324 """
325 Delete a ZenProperty.
327 Arguments:
328 uid (str): UID to delete the property from
329 zproperty (str): ID of the property to delete.
331 Returns:
332 bool:
333 """
334 self._router_request(
335 self._make_request_data(
336 'deleteZenProperty',
337 dict(
338 uid=uid,
339 zProperty=zproperty
340 )
341 )
342 )
344 return True
347class ZenossProperty(PropertiesRouter):
348 """
349 Class for ZenProperties
350 """
352 def __init__(self, url, headers, ssl_verify, property_data):
353 super(ZenossProperty, self).__init__(url, headers, ssl_verify)
355 self.id = property_data['id']
356 self.category = property_data['category']
357 self.description = property_data['description']
358 self.islocal = property_data['islocal']
359 self.label = property_data['label']
360 if 'value' not in property_data:
361 self.value = 'REDACTED'
362 else:
363 self.value = property_data['value']
364 if 'valueAsString' not in property_data:
365 self.valueAsString = 'REDACTED'
366 else:
367 self.valueAsString = property_data['valueAsString']
368 self.path = 'Devices{0}'.format(property_data['path'])
369 self.type = property_data['type']
370 self.options = []
372 def set_value(self, path=None, value=None):
373 """
374 Sets (or updates) the local value of a property
376 Arguments:
377 path (str): UID of the node to set the property for.
378 value (str): The new value for the property, type varies by property.
380 Returns:
381 bool:
382 """
383 if path:
384 self.path = path
386 property_data = self.set_property_value(self.path, self.id, value=value)
388 self.value = value
389 self.valueAsString = str(value)
390 self.islocal = '1'
392 return True
394 def delete(self):
395 """
396 Delete the local instance of a property.
398 Returns:
399 bool:
400 """
401 if self.islocal:
402 self.delete_property(self.path, self.id)
403 defprop = self.list_properties(self.path, params=dict(id=self.id))
404 self._refresh(defprop['properties'][0])
405 return True
407 return False
410class ZenossCustomProperty(PropertiesRouter):
411 """
412 Class for Zenoss CustomProperties
413 """
415 def __init__(self, url, headers, ssl_verify, property_data):
416 super(ZenossCustomProperty, self).__init__(url, headers, ssl_verify)
418 self.id = property_data['id']
419 self.islocal = property_data['islocal']
420 self.label = property_data['label']
421 if 'value' not in property_data:
422 self.value = 'REDACTED'
423 else:
424 self.value = property_data['value']
425 if 'valueAsString' not in property_data:
426 self.valueAsString = 'REDACTED'
427 else:
428 self.valueAsString = property_data['valueAsString']
429 self.path = 'Devices{0}'.format(property_data['path'])
430 self.type = property_data['type']
431 self.options = property_data['options']
433 def set_value(self, path=None, value=None):
434 """
435 Sets (or updates) the local value of a custom property
437 Arguments:
438 path (str): UID of the node to set the property for.
439 value (str): The new value for the property, type varies by property.
441 Returns:
442 bool:
443 """
444 if path:
445 self.path = path
447 property_data = self.set_property_value(self.path, self.id, value=value)
449 self.value = value
450 self.valueAsString = str(value)
451 self.islocal = '1'
453 return True
455 def delete(self):
456 """
457 Delete the local instance of a property.
459 Returns:
460 bool:
461 """
462 if self.islocal:
463 self.delete_property(self.path, self.id)
464 defprop = self.list_custom_properties(self.path, params=dict(id=self.id))
465 self._refresh(defprop['properties'][0])
466 return True
468 return False