grpc.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package plugin
  2. import (
  3. "context"
  4. "github.com/opencost/opencost/core/pkg/model/pb"
  5. )
  6. // GRPCClient is an implementation of CustomCostsSource that talks over RPC.
  7. type GRPCClient struct{ client pb.CustomCostsSourceClient }
  8. func (m *GRPCClient) GetCustomCosts(req *pb.CustomCostRequest) []*pb.CustomCostResponse {
  9. resp, err := m.client.GetCustomCosts(context.Background(), req)
  10. if err != nil {
  11. return []*pb.CustomCostResponse{
  12. {
  13. Errors: []string{err.Error()},
  14. },
  15. }
  16. }
  17. derefs := []*pb.CustomCostResponse{}
  18. for _, resp := range resp.Resps {
  19. derefs = append(derefs, resp)
  20. }
  21. return derefs
  22. }
  23. // Here is the gRPC server that GRPCClient talks to.
  24. type GRPCServer struct {
  25. pb.UnimplementedCustomCostsSourceServer
  26. // This is the real implementation
  27. Impl CustomCostSource
  28. }
  29. func (m *GRPCServer) GetCustomCosts(
  30. ctx context.Context,
  31. req *pb.CustomCostRequest) (*pb.CustomCostResponseSet, error) {
  32. ptrs := []*pb.CustomCostResponse{}
  33. costs := m.Impl.GetCustomCosts(req)
  34. for _, cost := range costs {
  35. ptrs = append(ptrs, cost)
  36. }
  37. return &pb.CustomCostResponseSet{
  38. Resps: ptrs,
  39. }, nil
  40. }