def close_port(self, req): body = jsonobject.loads(req[http.REQUEST_BODY]) rsp = models.AgentResponse() with CiscoSwitch(body.username, body.password, body.host) as client: try: result = client.shutdown_port(body.ports) except Exception as ex: raise exceptions.SwitchTaskError(error=str(ex)) if "Copy complete." in result: for port in body.ports: logger.debug("close port %s successfully." % port) return jsonobject.dumps(rsp)
def delete_limit_template(self, req): body = jsonobject.loads(req[http.REQUEST_BODY]) rsp = models.SetSwitchResponse() with CiscoSwitch(body.username, body.password, body.host) as client: try: result = client.delete_limit_template(body.templates) except Exception as ex: raise exceptions.SwitchTaskError(error=str(ex)) if "Copy complete." in result: for template in body.templates: logger.debug("delete limit template %s successfully." % template) return jsonobject.dumps(rsp)
def unset_limit(self, req): body = jsonobject.loads(req[http.REQUEST_BODY]) rsp = models.SetSwitchResponse() with CiscoSwitch(body.username, body.password, body.host) as client: try: result = client.unset_limit(body.limit_infos) except Exception as ex: raise exceptions.SwitchTaskError(error=str(ex)) if "Copy complete." in result: for info in body.limit_infos: logger.debug("unset limit for port %s successfully." % info.inbound_port) return jsonobject.dumps(rsp)
def unset_vlan(self, req): body = jsonobject.loads(req[http.REQUEST_BODY]) rsp = models.SetSwitchResponse() with CiscoSwitch(body.username, body.password, body.host) as client: try: result = client.unset_vlan(body.ports) except Exception as ex: raise exceptions.SwitchTaskError(error=str(ex)) if "Copy complete." in result: for port in body.ports: logger.debug("unset vlan for port %s successfully." % ("Eth-Trunk %s" % port)) return jsonobject.dumps(rsp)
def get_relations(self, req): body = jsonobject.loads(req[http.REQUEST_BODY]) rsp = models.GetSwitchRelationsResp() relations = [] with CiscoSwitch(body.username, body.password, body.host) as client: vlan = int(body.vlan) if body.vlan else None macs = body.macs if body.macs else [] try: relations = client.get_relations(special_vlan=vlan, special_mac=macs) except Exception as ex: raise exceptions.SwitchTaskError(error=str(ex)) rsp.relations = relations return jsonobject.dumps(rsp)
def clean_all_config(self, req): body = jsonobject.loads(req[http.REQUEST_BODY]) rsp = models.AgentResponse() for switch in body.switches: with CiscoSwitch(switch.username, switch.password, switch.host) as client: try: time.sleep(random.randint(1, 3)) result = client.clean_all_config(switch, body.template_name) except Exception as ex: raise exceptions.SwitchTaskError(error=str(ex)) if "Copy complete." in result: logger.debug( "clean switch %s port %s config successfully." % (switch.host, switch.ports)) return jsonobject.dumps(rsp)
def set_limit(self, req): body = jsonobject.loads(req[http.REQUEST_BODY]) rsp = models.SetSwitchResponse() device_cfg = { "device_type": "cisco_nxos", "ip": body.host, "username": body.username, "password": body.password } with CiscoSwitch(device_cfg) as client: try: result = client.set_limit(body.limit_infos) except Exception as ex: raise exceptions.SwitchTaskError(error=str(ex)) if "Copy complete." in result: for info in body.limit_infos: logger.debug("set limit for port %s successfully." % info.inbound_port) return jsonobject.dumps(rsp)
def create_limit_template(self, req): body = jsonobject.loads(req[http.REQUEST_BODY]) rsp = models.SetSwitchResponse() device_cfg = { "device_type": "cisco_nxos", "ip": body.host, "username": body.username, "password": body.password } with CiscoSwitch(device_cfg) as client: try: result = client.create_limit_template(body.templates) except Exception as ex: raise exceptions.SwitchTaskError(error=str(ex)) if "Copy complete." in result: for template in body.templates: logger.debug("create limit template %s successfully." % template.name) return jsonobject.dumps(rsp)
def init_all_config(self, req): body = jsonobject.loads(req[http.REQUEST_BODY]) rsp = models.AgentResponse() for switch in body.switches: device_cfg = { "device_type": "cisco_nxos", "ip": switch.host, "username": switch.username, "password": switch.password } with CiscoSwitch(device_cfg) as client: try: time.sleep(random.randint(1, 3)) result = client.init_all_config(switch, body.template_name, body.is_dhclient) except Exception as ex: raise exceptions.SwitchTaskError(error=str(ex)) if "Copy complete." in result: logger.debug("init switch %s port %s config successfully." % (switch.host, switch.ports)) else: logger.error("init switch %s port %s config result: %s." % (switch.host, switch.ports, result)) return jsonobject.dumps(rsp)