def delete(port_id: Optional[UUID], name: Optional[str]) -> None: """Delete Orchestrator port if not in use (or previously used). A port can only be deleted if it was never used in a reservation. Once used a port cannot be deleted again. A port can be disabled though! This will take it out of the pool of ports reservations (and hence connections) are made against. See the `disable` command. """ init_app(with_scheduler=False) from supa.db.model import Port from supa.db.session import db_session if port_id is None and name is None: click.echo("Please specify either --port-id or --name.", err=True) try: with db_session() as session: port = session.query(Port) if port_id is not None: port = port.get(port_id) else: port = port.filter(Port.name == name).one() session.delete(port) except sqlalchemy.exc.IntegrityError: click.echo( "Port is in use. Could not delete it. (You could disable it instead to prevent further use).", err=True) except sqlalchemy.orm.exc.NoResultFound: click.echo("Port could not be found.", err=True)
def init(tmp_path_factory: pytest.TempPathFactory) -> Generator: """Initialize application and start the connection provider gRPC server.""" settings.database_file = tmp_path_factory.mktemp("supa") / "supa.db" init_app() server = grpc.server(futures.ThreadPoolExecutor(max_workers=settings.grpc_server_max_workers)) # Safe to import, now that `init_app()` has been called from supa.connection.provider.server import ConnectionProviderService connection_provider_pb2_grpc.add_ConnectionProviderServicer_to_server(ConnectionProviderService(), server) server.add_insecure_port(settings.grpc_server_insecure_address_port) server.start() yield # better to check if all jobs are finished, for now we just sleep on it time.sleep(1)
def add(port_id: UUID, name: str, vlans: str, remote_stp: Optional[str], bandwidth: int, enabled: bool) -> None: """Add Orchestrator port to SuPA.""" init_app(with_scheduler=False) # Safe to import, now that `init_app()` has been called from supa.db.model import Port from supa.db.session import db_session port = Port( port_id=port_id, name=name, vlans=str(VlanRanges(vlans)), remote_stp=remote_stp, bandwidth=bandwidth, enabled=enabled, ) with db_session() as session: session.add(port)
def serve( grpc_server_max_workers: int, grpc_server_insecure_address_port: str, scheduler_max_workers: int, domain: str, network_type: str, grpc_client_insecure_address_port: str, nsa_id: str, ) -> None: """Start the gRPC server and listen for incoming requests.""" # Command-line options take precedence. settings.grpc_server_max_workers = grpc_server_max_workers settings.grpc_server_insecure_address_port = grpc_server_insecure_address_port settings.scheduler_max_workers = scheduler_max_workers settings.domain = domain settings.network_type = network_type settings.grpc_client_insecure_address_port = grpc_client_insecure_address_port settings.nsa_id = nsa_id init_app() server = grpc.server( futures.ThreadPoolExecutor( max_workers=settings.grpc_server_max_workers)) log = logger.bind(grpc_server_max_workers=settings.grpc_server_max_workers) # Safe to import, now that `init_app()` has been called from supa.connection.provider.server import ConnectionProviderService connection_provider_pb2_grpc.add_ConnectionProviderServicer_to_server( ConnectionProviderService(), server) server.add_insecure_port(settings.grpc_server_insecure_address_port) log = log.bind(grpc_server_insecure_address_port=settings. grpc_server_insecure_address_port) server.start() log.info("Started Connection Provider gRPC Service.") recover_jobs() server.wait_for_termination()
def list_cmd(only: Optional[str]) -> None: """List Orchestrator ports made available to SuPA.""" init_app(with_scheduler=False) from supa.db.model import Port from supa.db.session import db_session with db_session() as session: ports = session.query(Port) if only == "enabled": ports = ports.filter(Port.enabled.is_(True)) elif only == "disabled": ports = ports.filter(Port.enabled.is_(False)) ports = ports.values(Port.port_id, Port.name, Port.vlans, Port.bandwidth, Port.remote_stp, Port.enabled) click.echo( tabulate( tuple(ports), headers=("port_id", "name", "vlans", "bandwidth", "remote_stp", "enabled"), tablefmt="psql", ))
def _set_enable(port_id: Optional[UUID], name: Optional[str], enabled: bool) -> None: """Enable or disable a specific port.""" init_app(with_scheduler=False) from supa.db.model import Port from supa.db.session import db_session if port_id is None and name is None: click.echo("Please specify either --port-id or --name.", err=True) try: with db_session() as session: port = session.query(Port) if port_id is not None: port = port.get(port_id) else: port = port.filter(Port.name == name).one() port.enabled = enabled click.echo( f"Port '{port.name}' has been {'enabled' if enabled else 'disabled'}." ) except sqlalchemy.orm.exc.NoResultFound: click.echo("Port could not be found.", err=True)