def detach_network_from_router(network_id): network = Network.objects.get(pk=network_id) subnet = network.subnet_set.filter(deleted=False)[0] rc = create_rc_by_network(network) interface_set = RouterInterface.objects.filter(network_id=network.id, subnet=subnet, deleted=False) LOG.info("Start to detach network[%s]", network.name) try: for router_interface in interface_set: LOG.info("Start to delete router interface, router:[%s], " "subnet[%s], id:[%s], port_id:[%s]", router_interface.router.name, router_interface.subnet.name, router_interface.id, router_interface.os_port_id) neutron.router_remove_interface(rc, router_interface.router.router_id, subnet.subnet_id, router_interface.os_port_id) router_interface.fake_delete() except Exception as e: LOG.exception("Failed to delete router interface, router:[%s], " "subnet[%s], id:[%s], port_id:[%s], exception:%s", router_interface.router.name, router_interface.subnet.name, router_interface.id, router_interface.os_port_id, e) network.change_status(NETWORK_STATE_ERROR) raise e else: network.change_status(NETWORK_STATE_ACTIVE)
def attach_network_to_router(network_id, router_id, subnet_id): network = Network.objects.get(pk=network_id) router = Router.objects.get(pk=router_id) subnet = Subnet.objects.get(pk=subnet_id) rc = create_rc_by_router(router) begin = datetime.datetime.now() try: LOG.info("Start to attach network[%s] to router[%s]", network.name, router.name) router_inf = neutron.router_add_interface( rc, router.router_id, subnet_id=subnet.subnet_id) except Exception as e: end = datetime.datetime.now() LOG.info("Attach network to router api apply [%s] seconds", \ (end-begin).seconds) LOG.exception("Failed to attach network[%s] to router[%s], " "exception:%s", network.name, router.name, e) network.change_status(NETWORK_STATE_ERROR) else: end = datetime.datetime.now() LOG.info("Attach network to router api apply [%s] seconds", \ (end-begin).seconds) RouterInterface.objects.create( network_id=network_id, router=router, subnet=subnet, user=subnet.user, user_data_center=subnet.user_data_center, os_port_id=router_inf['port_id']) network.change_status(NETWORK_STATE_ACTIVE)