2023-03-09 19:40:23 +00:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
2023-03-27 19:37:54 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
2023-03-27 21:25:27 +00:00
|
|
|
"google.golang.org/grpc/metadata"
|
2023-03-27 19:37:54 +00:00
|
|
|
"google.golang.org/grpc/status"
|
2023-03-09 19:40:23 +00:00
|
|
|
|
2023-03-27 15:35:39 +00:00
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
|
|
|
"github.com/hashicorp/consul/internal/storage"
|
2023-03-09 19:40:23 +00:00
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
Config
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2023-03-27 19:37:54 +00:00
|
|
|
registry Registry
|
|
|
|
backend Backend
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:generate mockery --name Registry --inpackage
|
|
|
|
type Registry interface {
|
|
|
|
resource.Registry
|
2023-03-27 15:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//go:generate mockery --name Backend --inpackage
|
|
|
|
type Backend interface {
|
|
|
|
storage.Backend
|
2023-03-09 19:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(cfg Config) *Server {
|
|
|
|
return &Server{cfg}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ pbresource.ResourceServiceServer = (*Server)(nil)
|
|
|
|
|
|
|
|
func (s *Server) Register(grpcServer *grpc.Server) {
|
|
|
|
pbresource.RegisterResourceServiceServer(grpcServer, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Write(ctx context.Context, req *pbresource.WriteRequest) (*pbresource.WriteResponse, error) {
|
|
|
|
// TODO
|
|
|
|
return &pbresource.WriteResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) WriteStatus(ctx context.Context, req *pbresource.WriteStatusRequest) (*pbresource.WriteStatusResponse, error) {
|
|
|
|
// TODO
|
|
|
|
return &pbresource.WriteStatusResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Delete(ctx context.Context, req *pbresource.DeleteRequest) (*pbresource.DeleteResponse, error) {
|
|
|
|
// TODO
|
|
|
|
return &pbresource.DeleteResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2023-03-27 21:25:27 +00:00
|
|
|
//nolint:unparam
|
2023-03-27 19:37:54 +00:00
|
|
|
func (s *Server) resolveType(typ *pbresource.Type) (*resource.Registration, error) {
|
|
|
|
v, ok := s.registry.Resolve(typ)
|
|
|
|
if ok {
|
|
|
|
return &v, nil
|
|
|
|
}
|
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.InvalidArgument,
|
|
|
|
"resource type %s not registered", resource.ToGVK(typ),
|
|
|
|
)
|
2023-03-09 19:40:23 +00:00
|
|
|
}
|
2023-03-27 21:25:27 +00:00
|
|
|
|
|
|
|
func readConsistencyFrom(ctx context.Context) storage.ReadConsistency {
|
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return storage.EventualConsistency
|
|
|
|
}
|
|
|
|
|
|
|
|
vals := md.Get("x-consul-consistency-mode")
|
|
|
|
if len(vals) == 0 {
|
|
|
|
return storage.EventualConsistency
|
|
|
|
}
|
|
|
|
|
|
|
|
if vals[0] == "consistent" {
|
|
|
|
return storage.StrongConsistency
|
|
|
|
}
|
|
|
|
return storage.EventualConsistency
|
|
|
|
}
|