Skip to content

Commit

Permalink
Read(...) endpoint for the resource service (#16655)
Browse files Browse the repository at this point in the history
  • Loading branch information
analogue authored Mar 27, 2023
1 parent d4603a4 commit 3415689
Show file tree
Hide file tree
Showing 5 changed files with 410 additions and 13 deletions.
176 changes: 176 additions & 0 deletions agent/grpc-external/services/resource/mock_Backend.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions agent/grpc-external/services/resource/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package resource

import (
"context"
"errors"
"fmt"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

"github.com/hashicorp/consul/internal/resource"
"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
_, ok := s.registry.Resolve(req.Id.Type)
if !ok {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("resource type %s not registered", resource.ToGVK(req.Id.Type)))
}

consistency := storage.EventualConsistency
if isConsistentRead(ctx) {
consistency = storage.StrongConsistency
}

resource, err := s.backend.Read(ctx, consistency, req.Id)
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
}

func isConsistentRead(ctx context.Context) bool {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return false
}

vals := md.Get("x-consul-consistency-mode")
if len(vals) == 0 {
return false
}

return vals[0] == "consistent"
}
Loading

0 comments on commit 3415689

Please sign in to comment.