Skip to content
Snippets Groups Projects
Commit 62fc55d4 authored by Erik Reid's avatar Erik Reid
Browse files

added some standard result format utilities

parent 09734075
Branches
Tags
No related merge requests found
...@@ -78,22 +78,45 @@ class InventoryTask(Task): ...@@ -78,22 +78,45 @@ class InventoryTask(Task):
logger.exception(exc) logger.exception(exc)
super().on_failure(exc, task_id, args, kwargs, einfo) super().on_failure(exc, task_id, args, kwargs, einfo)
def _task_return_value(self, warning, message):
"""
common method for constructing a standard task return value
:param warning: boolean (False for normal, warning-free response)
:param message: text message to include in return value
:return: a serializable dict
"""
return {
'task': self.name,
'warning': warning,
'message': message
}
def success(self, message='OK'):
return self._task_return_value(warning=False, message=message)
def warning(self, message='WARNING'):
return self._task_return_value(warning=True, message=message)
@app.task(base=InventoryTask, bind=True, name='snmp_refresh_interfaces') @app.task(base=InventoryTask, bind=True, name='snmp_refresh_interfaces')
@log_task_entry_and_exit @log_task_entry_and_exit
def snmp_refresh_interfaces(self, hostname, community): def snmp_refresh_interfaces(self, hostname, community):
# TODO: [DBOARD3-242] copy from current redis in case of error
value = list(snmp.get_router_snmp_indexes(hostname, community)) value = list(snmp.get_router_snmp_indexes(hostname, community))
r = get_next_redis(InventoryTask.config) r = get_next_redis(InventoryTask.config)
r.set('snmp-interfaces:' + hostname, json.dumps(value)) r.set('snmp-interfaces:' + hostname, json.dumps(value))
return self.success(message=f'snmp info loaded from {hostname}')
@app.task(base=InventoryTask, bind=True, name='netconf_refresh_config') @app.task(base=InventoryTask, bind=True, name='netconf_refresh_config')
@log_task_entry_and_exit @log_task_entry_and_exit
def netconf_refresh_config(self, hostname): def netconf_refresh_config(self, hostname):
# TODO: [DBOARD3-242] copy from current redis in case of error
netconf_doc = juniper.load_config(hostname, InventoryTask.config["ssh"]) netconf_doc = juniper.load_config(hostname, InventoryTask.config["ssh"])
netconf_str = etree.tostring(netconf_doc, encoding='unicode') netconf_str = etree.tostring(netconf_doc, encoding='unicode')
r = get_next_redis(InventoryTask.config) r = get_next_redis(InventoryTask.config)
r.set('netconf:' + hostname, netconf_str) r.set('netconf:' + hostname, netconf_str)
return self.success(message=f'netconf info loaded from {hostname}')
@app.task(base=InventoryTask, bind=True, name='update_interfaces_to_services') @app.task(base=InventoryTask, bind=True, name='update_interfaces_to_services')
...@@ -119,6 +142,8 @@ def update_interfaces_to_services(self): ...@@ -119,6 +142,8 @@ def update_interfaces_to_services(self):
json.dumps(services)) json.dumps(services))
rp.execute() rp.execute()
return self.success()
@app.task(base=InventoryTask, bind=True, name='import_unmanaged_interfaces') @app.task(base=InventoryTask, bind=True, name='import_unmanaged_interfaces')
@log_task_entry_and_exit @log_task_entry_and_exit
...@@ -151,6 +176,8 @@ def import_unmanaged_interfaces(self): ...@@ -151,6 +176,8 @@ def import_unmanaged_interfaces(self):
json.dumps([ifc])) json.dumps([ifc]))
rp.execute() rp.execute()
return self.success()
@app.task(base=InventoryTask, bind=True, name='update_access_services') @app.task(base=InventoryTask, bind=True, name='update_access_services')
@log_task_entry_and_exit @log_task_entry_and_exit
...@@ -180,6 +207,8 @@ def update_access_services(self): ...@@ -180,6 +207,8 @@ def update_access_services(self):
json.dumps(service)) json.dumps(service))
rp.execute() rp.execute()
return self.success()
@app.task(base=InventoryTask, bind=True, name='update_lg_routers') @app.task(base=InventoryTask, bind=True, name='update_lg_routers')
@log_task_entry_and_exit @log_task_entry_and_exit
...@@ -197,6 +226,8 @@ def update_lg_routers(self): ...@@ -197,6 +226,8 @@ def update_lg_routers(self):
rp.set(f'opsdb:lg:{router["equipment name"]}', json.dumps(router)) rp.set(f'opsdb:lg:{router["equipment name"]}', json.dumps(router))
rp.execute() rp.execute()
return self.success()
@app.task(base=InventoryTask, bind=True, name='update_equipment_locations') @app.task(base=InventoryTask, bind=True, name='update_equipment_locations')
@log_task_entry_and_exit @log_task_entry_and_exit
...@@ -216,6 +247,8 @@ def update_equipment_locations(self): ...@@ -216,6 +247,8 @@ def update_equipment_locations(self):
rp.set('opsdb:location:%s' % h, json.dumps(locations)) rp.set('opsdb:location:%s' % h, json.dumps(locations))
rp.execute() rp.execute()
return self.success()
@app.task(base=InventoryTask, bind=True, name='update_circuit_hierarchy') @app.task(base=InventoryTask, bind=True, name='update_circuit_hierarchy')
@log_task_entry_and_exit @log_task_entry_and_exit
...@@ -246,6 +279,8 @@ def update_circuit_hierarchy(self): ...@@ -246,6 +279,8 @@ def update_circuit_hierarchy(self):
rp.set('opsdb:services:children:%d' % cid, json.dumps(children)) rp.set('opsdb:services:children:%d' % cid, json.dumps(children))
rp.execute() rp.execute()
return self.success()
@app.task(base=InventoryTask, bind=True, name='update_geant_lambdas') @app.task(base=InventoryTask, bind=True, name='update_geant_lambdas')
@log_task_entry_and_exit @log_task_entry_and_exit
...@@ -265,6 +300,8 @@ def update_geant_lambdas(self): ...@@ -265,6 +300,8 @@ def update_geant_lambdas(self):
json.dumps(ld)) json.dumps(ld))
rp.execute() rp.execute()
return self.success()
@app.task(base=InventoryTask, bind=True, @app.task(base=InventoryTask, bind=True,
name='update_neteng_managed_device_list') name='update_neteng_managed_device_list')
...@@ -288,10 +325,7 @@ def update_neteng_managed_device_list(self): ...@@ -288,10 +325,7 @@ def update_neteng_managed_device_list(self):
r = get_next_redis(InventoryTask.config) r = get_next_redis(InventoryTask.config)
r.set('netdash', json.dumps(routers).encode('utf-8')) r.set('netdash', json.dumps(routers).encode('utf-8'))
return { return self.success(f'saved {len(routers)} managed routers')
'task': 'update_neteng_managed_device_list',
'message': 'saved %d managed routers' % len(routers)
}
def load_netconf_data(hostname): def load_netconf_data(hostname):
...@@ -440,7 +474,7 @@ def reload_router_config(self, hostname): ...@@ -440,7 +474,7 @@ def reload_router_config(self, hostname):
except InventoryTaskError: except InventoryTaskError:
pass # ok at this point if not found pass # ok at this point if not found
# load new netconf data # load new netconf data, in this thread
netconf_refresh_config.apply(args=[hostname]) netconf_refresh_config.apply(args=[hostname])
netconf_doc = load_netconf_data(hostname) netconf_doc = load_netconf_data(hostname)
...@@ -451,11 +485,7 @@ def reload_router_config(self, hostname): ...@@ -451,11 +485,7 @@ def reload_router_config(self, hostname):
'no timestamp available for new netconf data' 'no timestamp available for new netconf data'
if new_netconf_timestamp == current_netconf_timestamp: if new_netconf_timestamp == current_netconf_timestamp:
logger.debug('no netconf change timestamp change, aborting') logger.debug('no netconf change timestamp change, aborting')
return { return self.success(f'no change (timestamp not updated)')
'task': 'reload_router_config',
'hostname': hostname,
'message': 'OK (no change)'
}
# clear cached classifier responses for this router, and # clear cached classifier responses for this router, and
# refresh peering data # refresh peering data
...@@ -487,11 +517,7 @@ def reload_router_config(self, hostname): ...@@ -487,11 +517,7 @@ def reload_router_config(self, hostname):
clear_cached_classifier_responses(None) clear_cached_classifier_responses(None)
return { return self.success(f'updated config for {hostname}')
'task': 'reload_router_config',
'hostname': hostname,
'message': 'OK'
}
def _erase_next_db(config): def _erase_next_db(config):
...@@ -532,7 +558,8 @@ def internal_refresh_phase_2(self): ...@@ -532,7 +558,8 @@ def internal_refresh_phase_2(self):
t = refresh_finalizer.apply_async(args=[json.dumps(pending_task_ids)]) t = refresh_finalizer.apply_async(args=[json.dumps(pending_task_ids)])
pending_task_ids.append(t.id) pending_task_ids.append(t.id)
return pending_task_ids
return self.success()
def launch_refresh_cache_all(config): def launch_refresh_cache_all(config):
...@@ -628,6 +655,8 @@ def refresh_finalizer(self, pending_task_ids_json): ...@@ -628,6 +655,8 @@ def refresh_finalizer(self, pending_task_ids_json):
latch_db(InventoryTask.config) latch_db(InventoryTask.config)
_update('latched current/next dbs') _update('latched current/next dbs')
return self.success()
def _build_service_category_interface_list(update_callback=lambda s: None): def _build_service_category_interface_list(update_callback=lambda s: None):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment