def get_switch_instance(device): switches = config.get_devices_cfg() if not switches.has_key(device): raise DeviceNotFound("Switch not known/configured => '%s'" % device) if not switches[device].get("mgmt-api") or not switches[device].get("mgmt-user") \ or not switches[device].get("mgmt-pass"): raise MisconfiguredDevice("The device '%s' are misconfigured - see the devices.cfg or redis-devices.cfg" % device) swapi = get_sw_handler_class(switches[device]["mgmt-api"]) return swapi(host=switches[device]["mgmt-host"], port=int(switches[device]["mgmt-port"]), username=switches[device]["mgmt-user"], passwd=switches[device]["mgmt-pass"])
def devices_list(): # MUST return ONLY switch names -- for CLI completion purpose #logger.info("Listing available devices -- context: %s" % request["context"]) return get_devices_cfg().keys()
""" use @log_request_ahead between @authorize and @cached """ def proxy(f): def log(*args, **kwargs): if msg is not None: lmsg = msg lmsg_args = msg_args if msg_args is not None: lmsg = lmsg % tuple([kwargs.get(a) for a in lmsg_args]) logger.info("%s -- context: %s" % (lmsg, request["context"])) return f(*args, **kwargs) return log return proxy # Force Exception if using devices.cfg and permissions are wrong dev_cfg = get_devices_cfg() @get("/devices") @context @log_request_ahead("Listing available devices") @reply_json def devices_list(): # MUST return ONLY switch names -- for CLI completion purpose #logger.info("Listing available devices -- context: %s" % request["context"]) return get_devices_cfg().keys() @get("/info/<device>") @context @log_request_ahead("Showing generic information for device %s", ("device",)) @reply_json