feat: view and enable routes on machines

This commit is contained in:
Johan Siebens
2022-05-15 07:10:05 +02:00
parent 52aa221cd0
commit 3aceacbc8d
9 changed files with 860 additions and 98 deletions
+76
View File
@@ -7,6 +7,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
"inet.af/netaddr"
)
func (s *Service) ListMachines(ctx context.Context, req *api.ListMachinesRequest) (*api.ListMachinesResponse, error) {
@@ -75,3 +76,78 @@ func (s *Service) DeleteMachine(ctx context.Context, req *api.DeleteMachineReque
return &api.DeleteMachineResponse{}, nil
}
func (s *Service) GetMachineRoutes(ctx context.Context, req *api.GetMachineRoutesRequest) (*api.GetMachineRoutesResponse, error) {
m, err := s.repository.GetMachine(ctx, req.MachineId)
if err != nil {
return nil, err
}
if m == nil {
return nil, status.Error(codes.NotFound, "machine does not exist")
}
var routes []*api.RoutableIP
for _, r := range m.HostInfo.RoutableIPs {
routes = append(routes, &api.RoutableIP{
Advertised: r.String(),
Allowed: m.IsAllowedIP(r),
})
}
response := api.GetMachineRoutesResponse{
Routes: routes,
}
return &response, nil
}
func (s *Service) SetMachineRoutes(ctx context.Context, req *api.SetMachineRoutesRequest) (*api.GetMachineRoutesResponse, error) {
m, err := s.repository.GetMachine(ctx, req.MachineId)
if err != nil {
return nil, err
}
if m == nil {
return nil, status.Error(codes.NotFound, "machine does not exist")
}
var allowedIps []netaddr.IPPrefix
for _, r := range req.AllowedIps {
prefix, err := netaddr.ParseIPPrefix(r)
if err != nil {
return nil, err
}
allowedIps = append(allowedIps, prefix)
}
m.AllowIPs = allowedIps
if err := s.repository.SaveMachine(ctx, m); err != nil {
return nil, err
}
s.brokers(m.TailnetID).SignalPeerUpdated(m.ID)
var routes []*api.RoutableIP
for _, r := range m.HostInfo.RoutableIPs {
routes = append(routes, &api.RoutableIP{
Advertised: r.String(),
Allowed: m.IsAllowedIP(r),
})
}
response := api.GetMachineRoutesResponse{
Routes: routes,
}
return &response, nil
}
func mapIp(ip []netaddr.IPPrefix) []string {
var x = []string{}
for _, i := range ip {
x = append(x, i.String())
}
return x
}