Coverage for src/zenossapi/routers/cmdb.py: 27%

41 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-25 05:47 +0000

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

2 

3""" 

4Zenoss CMDBIntegrationNGRouter 

5""" 

6 

7from zenossapi.routers import ZenossRouter 

8from zenossapi.routers.device import DeviceRouter 

9from zenossapi.apiclient import ZenossAPIClientError 

10 

11 

12class CmdbRouter(ZenossRouter): 

13 """ 

14 Class for interacting with the Zenoss cmdb router 

15 """ 

16 

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

18 super(CmdbRouter, self).__init__(url, headers, ssl_verify, 'CMDBIntegrationNGRouter', 'CMDBIntegrationNGRouter') 

19 self.uuid = None 

20 

21 def __repr__(self): 

22 if self.uuid: 

23 identifier = self.uuid 

24 else: 

25 identifier = hex(id(self)) 

26 

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

28 type(self).__name__, identifier 

29 ) 

30 

31 def list_configs(self): 

32 """ 

33 List all CMDB configurations 

34 

35 Returns: 

36 list[dict]: List of dicts containing config data for each CMDB config 

37 

38 """ 

39 

40 config_data = self._router_request( 

41 self._make_request_data( 

42 'getInfos' 

43 ) 

44 ) 

45 

46 return config_data['data'] 

47 

48 def get_active_config(self): 

49 """ 

50 Return object of the currently active config (Zenoss only allows one active config) 

51 (calls list_configs and returns only active config) 

52 

53 Returns: 

54 dict: Dictionary of active config data (if any) 

55 

56 """ 

57 

58 config_data = self.list_configs() 

59 

60 for config in config_data: 

61 if config['enabled'] is True: 

62 return config 

63 

64 return None 

65 

66 def get_stats(self): 

67 """ 

68 Return stats for the currently active config (calls get_active_config and returns stats) 

69 

70 Returns: 

71 dict: Dictionary of stats for currently active config (if any) 

72 

73 """ 

74 active_config = self.get_active_config() 

75 if active_config is None: 

76 return None 

77 stats = { 

78 'run_interval': active_config['runInterval'], 

79 'full_run_interval': active_config['fullRunInterval'], 

80 'next_run': active_config['nextRun'], 

81 'next_full_run': active_config['nextFullRun'], 

82 'last_run_started': active_config['lastRunStarted'], 

83 'last_run_finished': active_config['lastRunFinished'], 

84 'last_successful_run_finished': active_config['lastRunSuccessFinished'] 

85 } 

86 

87 return stats 

88 

89 def do_cmdb_run(self, uid, type=""): 

90 """ 

91 Schedules an immediate run of the specified type for the given UID. 

92 If type isn't given a regular run is performed. 

93 

94 Args: 

95 uid (str): The UID of the CMDB configuration to run 

96 type (str): Type of CMDB sync job to schedule, not needed for regular, "Full" for full. 

97 

98 Returns: 

99 none 

100 """ 

101 cmdb_run_status = self._router_request( 

102 self._make_request_data( 

103 'doCMDBRun', 

104 dict(uid=uid, runType=type) 

105 ) 

106 ) 

107 

108 def get_cmdb_fields(self, uid=None, name=None): 

109 """ 

110 Return list of cmdb fields for the given uid or name 

111 Note: instantiantes a DeviceRouter object to get the uid for a device name 

112 

113 Arguments: 

114 uid (str): UID of the cmdb config to get fields for 

115 name (str): Name of the cmdb config to get fields for 

116 

117 Returns: 

118 list[dict]: List of dicts containing cmdb fields for the given uid or name 

119 

120 """ 

121 

122 if uid is None and name is None: 

123 raise ValueError('Either uid or name must be specified') 

124 

125 if uid is not None: 

126 pass 

127 elif name is not None: 

128 dr = DeviceRouter(self.api_url, self.api_headers, self.ssl_verify) 

129 uid = dr.get_device_uid_by_name(name) 

130 if uid is None: 

131 raise ZenossAPIClientError('Device with name {0} not found'.format(name)) 

132 

133 cmdb_fields_data = self._router_request( 

134 self._make_request_data( 

135 'getCmdbFields', 

136 dict(uid=uid) 

137 ) 

138 ) 

139 

140 return cmdb_fields_data['data']