add machine list command and corresponding RPC endpoint

This commit is contained in:
Pavel Sviderski
2024-09-12 14:52:07 +10:00
parent 47073f017f
commit 4e042be7b7
10 changed files with 521 additions and 188 deletions
+16
View File
@@ -7,6 +7,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"
"log/slog"
"net/netip"
"uncloud/internal/machine/api/pb"
@@ -163,6 +164,21 @@ func (c *Cluster) AddMachine(ctx context.Context, req *pb.AddMachineRequest) (*p
return resp, nil
}
func (c *Cluster) ListMachines(ctx context.Context, _ *emptypb.Empty) (*pb.ListMachinesResponse, error) {
if c.state == nil {
return nil, status.Error(codes.FailedPrecondition, "cluster is not initialized")
}
// TODO: consider creating MachineInfo type that pb.MachineInfo is mapped to always carry valid data to reduce
// error handling caused by conversions and nil pointers.
machines := make([]*pb.MachineInfo, 0, len(c.state.State.Machines))
for _, sm := range c.state.State.Machines {
m := proto.Clone(sm).(*pb.MachineInfo)
m.Network.Endpoints = c.state.State.Endpoints[m.Id].Endpoints
machines = append(machines, m)
}
return &pb.ListMachinesResponse{Machines: machines}, nil
}
func (c *Cluster) ListMachineEndpoints(
ctx context.Context, req *pb.ListMachineEndpointsRequest,
) (*pb.ListMachineEndpointsResponse, error) {