2023-03-27 15:35:39 +00:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/internal/storage"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) Read(ctx context.Context, req *pbresource.ReadRequest) (*pbresource.ReadResponse, error) {
|
|
|
|
// check type exists
|
2023-03-27 21:25:27 +00:00
|
|
|
if _, err := s.resolveType(req.Id.Type); err != nil {
|
|
|
|
return nil, err
|
2023-03-27 15:35:39 +00:00
|
|
|
}
|
|
|
|
|
2023-03-27 21:25:27 +00:00
|
|
|
resource, err := s.backend.Read(ctx, readConsistencyFrom(ctx), req.Id)
|
2023-03-27 15:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, storage.ErrNotFound) {
|
|
|
|
return nil, status.Error(codes.NotFound, err.Error())
|
|
|
|
}
|
|
|
|
if errors.As(err, &storage.GroupVersionMismatchError{}) {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &pbresource.ReadResponse{Resource: resource}, nil
|
|
|
|
}
|