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

1# -*- coding: utf-8 -*- 

2 

3""" 

4Zenoss properties_router 

5""" 

6 

7from zenossapi.apiclient import ZenossAPIClientError 

8from zenossapi.routers import ZenossRouter 

9 

10 

11class PropertiesRouter(ZenossRouter): 

12 """ 

13 Class for interacting with Zenoss properties. 

14 """ 

15 

16 def __init__(self, url, headers, ssl_verify): 

17 super(PropertiesRouter, self).__init__(url, headers, ssl_verify, 

18 'properties_router', 

19 'PropertiesRouter') 

20 

21 self.id = None 

22 

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))) 

28 

29 return '<{0} object {1}>'.format( 

30 type(self).__name__, identifier 

31 ) 

32 

33 def _refresh(self, property_data): 

34 """ 

35 Refresh the attributes of the object, e.g. after deleting a local copy. 

36 

37 Arguments: 

38 property_data (dict): New attribute values 

39 """ 

40 skip = ['api_action', 'api_endpoint', 'api_headers', 'api_url', 'ssl_verify'] 

41 

42 for pd in property_data: 

43 if pd in skip: 

44 continue 

45 setattr(self, pd, property_data[pd]) 

46 

47 def list_properties(self, uid, params=None, sort=None, sort_dir='ASC'): 

48 """ 

49 Get a list of ZenProperties for the uid context. 

50 

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 

56 

57 Returns: 

58 dict(int, list(dict)): :: 

59 

60 { 

61 'total': Total count of properties returned. 

62 'properties': List of properties found. 

63 } 

64 

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 ) 

77 

78 return dict( 

79 total=properties_data['totalCount'], 

80 properties=properties_data['data'], 

81 ) 

82 

83 def get_properties(self, uid, params=None): 

84 """ 

85 Get ZenossProperties objects for the properties of a uid context. 

86 

87 Arguments: 

88 uid (str): UID of the object to get properties for. 

89 params (dict): Search parameters for filter the properties on. 

90 

91 Returns: 

92 dict(int, list(ZenossProperty)): :: 

93 

94 { 

95 'total': Total count of properties returned. 

96 'properties': List of ZenossProperty objects. 

97 } 

98 

99 """ 

100 properties_data = self.list_properties(uid, params=params) 

101 

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 ) 

115 

116 return props 

117 

118 def list_local_properties(self, uid): 

119 """ 

120 Get a list of properties set locally to the specified UID. 

121 

122 Arguments: 

123 uid (str): UID to get local properties for. 

124 

125 Returns: 

126 dict(int, list(dict)): :: 

127 

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')) 

134 

135 def get_local_properties(self, uid): 

136 """ 

137 Get ZenossProperty objects for the local properties of a specified uid. 

138 

139 Arguments: 

140 uid (str): UID to get local properties for. 

141 

142 Returns: 

143 dict(int, list(ZenossProperty)): :: 

144 

145 { 

146 'total': Total count of properties returned. 

147 'properties': List of ZenossProperty objects. 

148 } 

149 

150 """ 

151 properties_data = self.list_local_properties(uid) 

152 

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 ) 

166 

167 return props 

168 

169 def get_property(self, uid, zproperty): 

170 """ 

171 Get a single ZenossProperty 

172 

173 Arguments: 

174 uid (str): UID to get the property of. 

175 zproperty (str): ID of the property to get. 

176 

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 ) 

191 

192 return ZenossProperty( 

193 self.api_url, 

194 self.api_headers, 

195 self.ssl_verify, 

196 prop_data['data'][0] 

197 ) 

198 

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. 

202 

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 

208 

209 Returns: 

210 dict(int, list(dict)): :: 

211 

212 { 

213 'total': Total count of properties returned. 

214 'properties': List of properties found. 

215 } 

216 

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 ) 

229 

230 return dict( 

231 total=properties_data['totalCount'], 

232 properties=properties_data['data'], 

233 ) 

234 

235 def get_custom_properties(self, uid, params=None): 

236 """ 

237 Get ZenossCustomProperties objects for the cProperties of a uid context. 

238 

239 Arguments: 

240 uid (str): UID of the object to get properties for. 

241 params (dict): Search parameters for filter the properties on. 

242 

243 Returns: 

244 dict(int, list(ZenossCustomProperty)): :: 

245 

246 { 

247 'total': Total count of properties returned. 

248 'properties': List of ZenossCustomProperty objects. 

249 } 

250 

251 """ 

252 properties_data = self.list_custom_properties(uid, params=params) 

253 

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 ) 

267 

268 return props 

269 

270 def get_custom_property(self, uid, cproperty): 

271 """ 

272 Get a single ZenossCustomProperty 

273 

274 Arguments: 

275 uid (str): UID to get the property of. 

276 cproperty (str): ID of the property to get. 

277 

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 ) 

292 

293 return ZenossCustomProperty( 

294 self.api_url, 

295 self.api_headers, 

296 self.ssl_verify, 

297 prop_data['data'][0] 

298 ) 

299 

300 def set_property_value(self, uid, id, value=None): 

301 """ 

302 Sets (or updates) the local value of a property 

303 

304 Arguments: 

305 value: The new value for the property, type varies by property. 

306 

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 ) 

320 

321 return True 

322 

323 def delete_property(self, uid, zproperty): 

324 """ 

325 Delete a ZenProperty. 

326 

327 Arguments: 

328 uid (str): UID to delete the property from 

329 zproperty (str): ID of the property to delete. 

330 

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 ) 

343 

344 return True 

345 

346 

347class ZenossProperty(PropertiesRouter): 

348 """ 

349 Class for ZenProperties 

350 """ 

351 

352 def __init__(self, url, headers, ssl_verify, property_data): 

353 super(ZenossProperty, self).__init__(url, headers, ssl_verify) 

354 

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 = [] 

371 

372 def set_value(self, path=None, value=None): 

373 """ 

374 Sets (or updates) the local value of a property 

375 

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. 

379 

380 Returns: 

381 bool: 

382 """ 

383 if path: 

384 self.path = path 

385 

386 property_data = self.set_property_value(self.path, self.id, value=value) 

387 

388 self.value = value 

389 self.valueAsString = str(value) 

390 self.islocal = '1' 

391 

392 return True 

393 

394 def delete(self): 

395 """ 

396 Delete the local instance of a property. 

397 

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 

406 

407 return False 

408 

409 

410class ZenossCustomProperty(PropertiesRouter): 

411 """ 

412 Class for Zenoss CustomProperties 

413 """ 

414 

415 def __init__(self, url, headers, ssl_verify, property_data): 

416 super(ZenossCustomProperty, self).__init__(url, headers, ssl_verify) 

417 

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'] 

432 

433 def set_value(self, path=None, value=None): 

434 """ 

435 Sets (or updates) the local value of a custom property 

436 

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. 

440 

441 Returns: 

442 bool: 

443 """ 

444 if path: 

445 self.path = path 

446 

447 property_data = self.set_property_value(self.path, self.id, value=value) 

448 

449 self.value = value 

450 self.valueAsString = str(value) 

451 self.islocal = '1' 

452 

453 return True 

454 

455 def delete(self): 

456 """ 

457 Delete the local instance of a property. 

458 

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 

467 

468 return False