-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Aiming to support PubSubTopic
- Loading branch information
Showing
8 changed files
with
409 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package mockpubsub | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects" | ||
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/pubsub/v1" | ||
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/pkg/storage" | ||
"github.com/golang/protobuf/ptypes/empty" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
type schemaService struct { | ||
*MockService | ||
pb.UnimplementedSchemaServiceServer | ||
} | ||
|
||
func (s *schemaService) CreateSchema(ctx context.Context, req *pb.CreateSchemaRequest) (*pb.Schema, error) { | ||
reqName := req.Parent + "/schemas/" + req.GetSchemaId() | ||
name, err := s.parseSchemaName(reqName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fqn := name.String() | ||
now := time.Now() | ||
obj := proto.Clone(req.GetSchema()).(*pb.Schema) | ||
obj.Name = name.String() | ||
obj.RevisionId = fmt.Sprintf("r%d", now.Unix()) | ||
obj.RevisionCreateTime = timestamppb.New(now) | ||
s.populateDefaultsForSchema(name, obj) | ||
if err := s.storage.Create(ctx, fqn, obj); err != nil { | ||
return nil, err | ||
} | ||
return obj, nil | ||
} | ||
|
||
func (s *schemaService) populateDefaultsForSchema(name *schemaName, obj *pb.Schema) { | ||
// TODO: populate any default values here | ||
} | ||
|
||
func (s *schemaService) GetSchema(ctx context.Context, req *pb.GetSchemaRequest) (*pb.Schema, error) { | ||
name, err := s.parseSchemaName(req.Name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fqn := name.String() | ||
obj := &pb.Schema{} | ||
if err := s.storage.Get(ctx, fqn, obj); err != nil { | ||
if status.Code(err) == codes.NotFound { | ||
return nil, status.Errorf(codes.NotFound, "Resource not found (resource=%s).", name.String()) | ||
} | ||
return nil, err | ||
} | ||
return obj, nil | ||
} | ||
|
||
func (s *schemaService) ListSchemas(ctx context.Context, req *pb.ListSchemasRequest) (*pb.ListSchemasResponse, error) { | ||
project, err := s.Projects.GetProjectByID(req.Parent) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
findPrefix := fmt.Sprintf("projects/%v/", project.ID) | ||
|
||
var schemas []*pb.Schema | ||
|
||
schemaKind := (&pb.Schema{}).ProtoReflect().Descriptor() | ||
if err := s.storage.List(ctx, schemaKind, storage.ListOptions{}, func(obj proto.Message) error { | ||
schema := obj.(*pb.Schema) | ||
if strings.HasPrefix(schema.GetName(), findPrefix) { | ||
schemas = append(schemas, schema) | ||
} | ||
return nil | ||
}); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &pb.ListSchemasResponse{ | ||
Schemas: schemas, | ||
}, nil | ||
} | ||
|
||
func (s *schemaService) DeleteSchema(ctx context.Context, req *pb.DeleteSchemaRequest) (*empty.Empty, error) { | ||
name, err := s.parseSchemaName(req.Name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fqn := name.String() | ||
deletedObj := &pb.Schema{} | ||
if err := s.storage.Delete(ctx, fqn, deletedObj); err != nil { | ||
return nil, err | ||
} | ||
return &empty.Empty{}, nil | ||
} | ||
|
||
type schemaName struct { | ||
Project *projects.ProjectData | ||
ID string | ||
} | ||
|
||
func (n *schemaName) String() string { | ||
return "projects/" + n.Project.ID + "/schemas/" + n.ID | ||
} | ||
|
||
// parseSchemaName parses a string into a schemaName. | ||
// The expected form is `projects/*/schemas/*`. | ||
func (s *MockService) parseSchemaName(name string) (*schemaName, error) { | ||
tokens := strings.Split(name, "/") | ||
if len(tokens) == 4 && tokens[0] == "projects" && tokens[2] == "schemas" { | ||
project, err := s.Projects.GetProjectByID(tokens[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
name := &schemaName{ | ||
Project: project, | ||
ID: tokens[3], | ||
} | ||
return name, nil | ||
} else { | ||
return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid", name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package mockpubsub | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"google.golang.org/grpc" | ||
|
||
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common" | ||
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/httpmux" | ||
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/operations" | ||
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/pubsub/v1" | ||
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/pkg/storage" | ||
) | ||
|
||
// MockService represents a mocked pubsub service. | ||
type MockService struct { | ||
*common.MockEnvironment | ||
storage storage.Storage | ||
operations *operations.Operations | ||
} | ||
|
||
// New creates a MockService. | ||
func New(env *common.MockEnvironment, storage storage.Storage) *MockService { | ||
s := &MockService{ | ||
MockEnvironment: env, | ||
storage: storage, | ||
operations: operations.NewOperationsService(storage), | ||
} | ||
return s | ||
} | ||
|
||
func (s *MockService) ExpectedHost() string { | ||
return "pubsub.googleapis.com" | ||
} | ||
|
||
func (s *MockService) Register(grpcServer *grpc.Server) { | ||
pb.RegisterPublisherServer(grpcServer, &publisherService{MockService: s}) | ||
// pb.RegisterSubscriberServer(grpcServer, &subscriberService{MockService: s}) | ||
pb.RegisterSchemaServiceServer(grpcServer, &schemaService{MockService: s}) | ||
} | ||
|
||
func (s *MockService) NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (http.Handler, error) { | ||
mux, err := httpmux.NewServeMux(ctx, conn, httpmux.Options{}, | ||
pb.RegisterPublisherHandler, | ||
pb.RegisterSubscriberHandler, | ||
pb.RegisterSchemaServiceHandler, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Returns slightly non-standard errors | ||
mux.RewriteError = func(ctx context.Context, error *httpmux.ErrorResponse) { | ||
if error.Code == 404 { | ||
error.Errors = nil | ||
} | ||
} | ||
|
||
return mux, nil | ||
} |
Oops, something went wrong.