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
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-25 05:47 +0000
1# -*- coding: utf-8 -*-
3"""
4Zenoss CMDBIntegrationNGRouter
5"""
7from zenossapi.routers import ZenossRouter
8from zenossapi.routers.device import DeviceRouter
9from zenossapi.apiclient import ZenossAPIClientError
12class CmdbRouter(ZenossRouter):
13 """
14 Class for interacting with the Zenoss cmdb router
15 """
17 def __init__(self, url, headers, ssl_verify):
18 super(CmdbRouter, self).__init__(url, headers, ssl_verify, 'CMDBIntegrationNGRouter', 'CMDBIntegrationNGRouter')
19 self.uuid = None
21 def __repr__(self):
22 if self.uuid:
23 identifier = self.uuid
24 else:
25 identifier = hex(id(self))
27 return '<{0} object at {1}>'.format(
28 type(self).__name__, identifier
29 )
31 def list_configs(self):
32 """
33 List all CMDB configurations
35 Returns:
36 list[dict]: List of dicts containing config data for each CMDB config
38 """
40 config_data = self._router_request(
41 self._make_request_data(
42 'getInfos'
43 )
44 )
46 return config_data['data']
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)
53 Returns:
54 dict: Dictionary of active config data (if any)
56 """
58 config_data = self.list_configs()
60 for config in config_data:
61 if config['enabled'] is True:
62 return config
64 return None
66 def get_stats(self):
67 """
68 Return stats for the currently active config (calls get_active_config and returns stats)
70 Returns:
71 dict: Dictionary of stats for currently active config (if any)
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 }
87 return stats
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.
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.
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 )
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
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
117 Returns:
118 list[dict]: List of dicts containing cmdb fields for the given uid or name
120 """
122 if uid is None and name is None:
123 raise ValueError('Either uid or name must be specified')
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))
133 cmdb_fields_data = self._router_request(
134 self._make_request_data(
135 'getCmdbFields',
136 dict(uid=uid)
137 )
138 )
140 return cmdb_fields_data['data']