Refactored route grpc glue code

This commit is contained in:
Juan Font 2022-11-26 00:03:39 +00:00 committed by Kristoffer Dalby
parent 8fa9755b55
commit 34631dfcf5

View File

@ -364,36 +364,60 @@ func (api headscaleV1APIServer) MoveMachine(
return &v1.MoveMachineResponse{Machine: machine.toProto()}, nil return &v1.MoveMachineResponse{Machine: machine.toProto()}, nil
} }
func (api headscaleV1APIServer) GetMachineRoute( func (api headscaleV1APIServer) GetRoutes(
ctx context.Context, ctx context.Context,
request *v1.GetMachineRouteRequest, request *v1.GetRoutesRequest,
) (*v1.GetMachineRouteResponse, error) { ) (*v1.GetRoutesResponse, error) {
machine, err := api.h.GetMachineByID(request.GetMachineId()) routes, err := api.h.GetRoutes()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &v1.GetMachineRouteResponse{ return &v1.GetRoutesResponse{
Routes: api.h.RoutesToProto(machine), Routes: Routes(routes).toProto(),
}, nil }, nil
} }
func (api headscaleV1APIServer) EnableMachineRoutes( func (api headscaleV1APIServer) EnableRoute(
ctx context.Context, ctx context.Context,
request *v1.EnableMachineRoutesRequest, request *v1.EnableRouteRequest,
) (*v1.EnableMachineRoutesResponse, error) { ) (*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()) machine, err := api.h.GetMachineByID(request.GetMachineId())
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = api.h.EnableRoutes(machine, request.GetRoutes()...) routes, err := api.h.GetMachineRoutes(machine)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &v1.EnableMachineRoutesResponse{ return &v1.GetMachineRoutesResponse{
Routes: api.h.RoutesToProto(machine), Routes: Routes(routes).toProto(),
}, nil }, nil
} }