From 34631dfcf51c2f034fe2831978bab30ba5e172a0 Mon Sep 17 00:00:00 2001 From: Juan Font Date: Sat, 26 Nov 2022 00:03:39 +0000 Subject: [PATCH] Refactored route grpc glue code --- grpcv1.go | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/grpcv1.go b/grpcv1.go index 998b9902..0c14a5ee 100644 --- a/grpcv1.go +++ b/grpcv1.go @@ -364,36 +364,60 @@ func (api headscaleV1APIServer) MoveMachine( return &v1.MoveMachineResponse{Machine: machine.toProto()}, nil } -func (api headscaleV1APIServer) GetMachineRoute( +func (api headscaleV1APIServer) GetRoutes( ctx context.Context, - request *v1.GetMachineRouteRequest, -) (*v1.GetMachineRouteResponse, error) { - machine, err := api.h.GetMachineByID(request.GetMachineId()) + request *v1.GetRoutesRequest, +) (*v1.GetRoutesResponse, error) { + routes, err := api.h.GetRoutes() if err != nil { return nil, err } - return &v1.GetMachineRouteResponse{ - Routes: api.h.RoutesToProto(machine), + return &v1.GetRoutesResponse{ + Routes: Routes(routes).toProto(), }, nil } -func (api headscaleV1APIServer) EnableMachineRoutes( +func (api headscaleV1APIServer) EnableRoute( ctx context.Context, - request *v1.EnableMachineRoutesRequest, -) (*v1.EnableMachineRoutesResponse, error) { + request *v1.EnableRouteRequest, +) (*v1.EnableRouteResponse, error) { + err := api.h.EnableRoute(request.GetRouteId()) + if err != nil { + return nil, err + } + + return &v1.EnableRouteResponse{}, nil +} + +func (api headscaleV1APIServer) DisableRoute( + ctx context.Context, + request *v1.DisableRouteRequest, +) (*v1.DisableRouteResponse, error) { + err := api.h.DisableRoute(request.GetRouteId()) + if err != nil { + return nil, err + } + + return &v1.DisableRouteResponse{}, nil +} + +func (api headscaleV1APIServer) GetMachineRoutes( + ctx context.Context, + request *v1.GetMachineRoutesRequest, +) (*v1.GetMachineRoutesResponse, error) { machine, err := api.h.GetMachineByID(request.GetMachineId()) if err != nil { return nil, err } - err = api.h.EnableRoutes(machine, request.GetRoutes()...) + routes, err := api.h.GetMachineRoutes(machine) if err != nil { return nil, err } - return &v1.EnableMachineRoutesResponse{ - Routes: api.h.RoutesToProto(machine), + return &v1.GetMachineRoutesResponse{ + Routes: Routes(routes).toProto(), }, nil }