Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#2628 from gemmahou/mock-target…
Browse files Browse the repository at this point in the history
…sslproxy

Support mock compute TargetSslProxy
  • Loading branch information
google-oss-prow[bot] authored Sep 12, 2024
2 parents 7207210 + 5152bf9 commit 3d7022e
Show file tree
Hide file tree
Showing 44 changed files with 5,730 additions and 96 deletions.
2 changes: 2 additions & 0 deletions config/tests/samples/create/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,10 +671,12 @@ func MaybeSkip(t *testing.T, name string, resources []*unstructured.Unstructured
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeNodeTemplate"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeManagedSSLCertificate"}:
//case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeServiceAttachment"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeSSLCertificate"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeSubnetwork"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeTargetVPNGateway"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeVPNGateway"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeTargetHTTPProxy"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeTargetSSLProxy"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeURLMap"}:
// ok

Expand Down
20 changes: 18 additions & 2 deletions mockgcp/mockcompute/globaladdress.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,15 @@ func (s *GlobalAddressesV1) Insert(ctx context.Context, req *pb.InsertGlobalAddr
return nil, err
}

return s.newLRO(ctx, name.Project.ID)
op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLink,
OperationType: PtrTo("insert"),
User: PtrTo("[email protected]"),
}
return s.startGlobalLRO(ctx, name.Project.ID, op, func() (proto.Message, error) {
return obj, nil
})
}

func (s *GlobalAddressesV1) Delete(ctx context.Context, req *pb.DeleteGlobalAddressRequest) (*pb.Operation, error) {
Expand All @@ -93,7 +101,15 @@ func (s *GlobalAddressesV1) Delete(ctx context.Context, req *pb.DeleteGlobalAddr
return nil, err
}

return s.newLRO(ctx, name.Project.ID)
op := &pb.Operation{
TargetId: deleted.Id,
TargetLink: deleted.SelfLink,
OperationType: PtrTo("delete"),
User: PtrTo("[email protected]"),
}
return s.startGlobalLRO(ctx, name.Project.ID, op, func() (proto.Message, error) {
return deleted, nil
})
}

func (s *GlobalAddressesV1) SetLabels(ctx context.Context, req *pb.SetLabelsGlobalAddressRequest) (*pb.Operation, error) {
Expand Down
16 changes: 5 additions & 11 deletions mockgcp/mockcompute/globalsslcertificatesv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ import (
"context"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/compute/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
apierrors "k8s.io/apimachinery/pkg/api/errors"

"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/compute/v1"
)

type GlobalSSLCertificatesV1 struct {
Expand Down Expand Up @@ -67,7 +65,7 @@ func (s *GlobalSSLCertificatesV1) Insert(ctx context.Context, req *pb.InsertSslC
obj.Kind = PtrTo("compute#sslCertificate")

if err := s.storage.Create(ctx, fqn, obj); err != nil {
return nil, status.Errorf(codes.Internal, "error creating sslCertificate: %v", err)
return nil, err
}

op := &pb.Operation{
Expand All @@ -92,11 +90,7 @@ func (s *GlobalSSLCertificatesV1) Delete(ctx context.Context, req *pb.DeleteSslC

deleted := &pb.SslCertificate{}
if err := s.storage.Delete(ctx, fqn, deleted); err != nil {
if apierrors.IsNotFound(err) {
return nil, status.Errorf(codes.NotFound, "sslCertificate %q not found", name)
}

return nil, status.Errorf(codes.Internal, "error deleting sslCertificate: %v", err)
return nil, err
}

op := &pb.Operation{
Expand All @@ -120,7 +114,7 @@ func (n *globalSSLCertificateName) String() string {
}

// parseGlobalSslCertificateName parses a string into a globalSslCertificateName.
// The expected form is `projects/*/regions/*/sslcertificate/*`.
// The expected form is `projects/*/global/sslcertificate/*`.
func (s *MockService) parseGlobalSslCertificateName(name string) (*globalSSLCertificateName, error) {
tokens := strings.Split(name, "/")

Expand Down
24 changes: 20 additions & 4 deletions mockgcp/mockcompute/regionaladdress.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,18 @@ func (s *RegionalAddressesV1) Insert(ctx context.Context, req *pb.InsertAddressR
}

if err := s.storage.Create(ctx, fqn, obj); err != nil {
return nil, status.Errorf(codes.Internal, "error creating address: %v", err)
return nil, err
}

return s.newLRO(ctx, name.Project.ID)
op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLink,
OperationType: PtrTo("insert"),
User: PtrTo("[email protected]"),
}
return s.startRegionalLRO(ctx, name.Project.ID, name.Region, op, func() (proto.Message, error) {
return obj, nil
})
}

func (s *RegionalAddressesV1) Delete(ctx context.Context, req *pb.DeleteAddressRequest) (*pb.Operation, error) {
Expand All @@ -89,7 +97,15 @@ func (s *RegionalAddressesV1) Delete(ctx context.Context, req *pb.DeleteAddressR
return nil, err
}

return s.newLRO(ctx, name.Project.ID)
op := &pb.Operation{
TargetId: deleted.Id,
TargetLink: deleted.SelfLink,
OperationType: PtrTo("delete"),
User: PtrTo("[email protected]"),
}
return s.startRegionalLRO(ctx, name.Project.ID, name.Region, op, func() (proto.Message, error) {
return deleted, nil
})
}

func (s *RegionalAddressesV1) SetLabels(ctx context.Context, req *pb.SetLabelsAddressRequest) (*pb.Operation, error) {
Expand Down Expand Up @@ -121,7 +137,7 @@ type regionalAddressName struct {
}

func (n *regionalAddressName) String() string {
return "projects/" + n.Project.ID + "/regions/" + n.Region + "/networks/" + n.Name
return "projects/" + n.Project.ID + "/regions/" + n.Region + "/addresses/" + n.Name
}

// parseAddressName parses a string into a addressName.
Expand Down
138 changes: 138 additions & 0 deletions mockgcp/mockcompute/regionalsslcertificatesv1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// 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 mockcompute

import (
"context"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/compute/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)

type RegionalSSLCertificatesV1 struct {
*MockService
pb.UnimplementedRegionSslCertificatesServer
}

func (s *RegionalSSLCertificatesV1) Get(ctx context.Context, req *pb.GetRegionSslCertificateRequest) (*pb.SslCertificate, error) {
reqName := "projects/" + req.GetProject() + "/regions/" + req.Region + "/sslCertificates/" + req.GetSslCertificate()
name, err := s.parseRegionalSslCertificateName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

obj := &pb.SslCertificate{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
return nil, err
}

return obj, nil
}

func (s *RegionalSSLCertificatesV1) Insert(ctx context.Context, req *pb.InsertRegionSslCertificateRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/regions/" + req.GetRegion() + "/sslCertificates/" + req.GetSslCertificateResource().GetName()
name, err := s.parseRegionalSslCertificateName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

id := s.generateID()

obj := proto.Clone(req.GetSslCertificateResource()).(*pb.SslCertificate)
obj.SelfLink = PtrTo("https://www.googleapis.com/compute/v1/" + name.String())
obj.CreationTimestamp = PtrTo(s.nowString())
obj.Id = &id
obj.Kind = PtrTo("compute#sslCertificate")

if err := s.storage.Create(ctx, fqn, obj); err != nil {
return nil, err
}

op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLink,
OperationType: PtrTo("insert"),
User: PtrTo("[email protected]"),
}
return s.startRegionalLRO(ctx, name.Project.ID, name.Region, op, func() (proto.Message, error) {
return obj, nil
})
}

func (s *RegionalSSLCertificatesV1) Delete(ctx context.Context, req *pb.DeleteRegionSslCertificateRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/regions/" + req.GetRegion() + "/sslCertificates/" + req.GetSslCertificate()
name, err := s.parseRegionalSslCertificateName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

deleted := &pb.SslCertificate{}
if err := s.storage.Delete(ctx, fqn, deleted); err != nil {
return nil, err
}

op := &pb.Operation{
TargetId: deleted.Id,
TargetLink: deleted.SelfLink,
OperationType: PtrTo("delete"),
User: PtrTo("[email protected]"),
}
return s.startRegionalLRO(ctx, name.Project.ID, name.Region, op, func() (proto.Message, error) {
return deleted, nil
})
}

type regionalSSLCertificateName struct {
Project *projects.ProjectData
Region string
Name string
}

func (n *regionalSSLCertificateName) String() string {
return "projects/" + n.Project.ID + "/regions/" + n.Region + "/sslCertificates/" + n.Name
}

// parseRegionalSslCertificateName parses a string into a regionalSSLCertificateName.
// The expected form is `projects/*/regions/*/sslcertificate/*`.
func (s *MockService) parseRegionalSslCertificateName(name string) (*regionalSSLCertificateName, error) {
tokens := strings.Split(name, "/")

if len(tokens) == 6 && tokens[0] == "projects" && tokens[2] == "regions" && tokens[4] == "sslCertificates" {
project, err := s.Projects.GetProjectByID(tokens[1])
if err != nil {
return nil, err
}

name := &regionalSSLCertificateName{
Project: project,
Region: tokens[3],
Name: tokens[5],
}

return name, nil
}

return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid", name)
}
10 changes: 9 additions & 1 deletion mockgcp/mockcompute/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ func (s *MockService) Register(grpcServer *grpc.Server) {
pb.RegisterAddressesServer(grpcServer, &RegionalAddressesV1{MockService: s})
pb.RegisterGlobalAddressesServer(grpcServer, &GlobalAddressesV1{MockService: s})
pb.RegisterSslCertificatesServer(grpcServer, &GlobalSSLCertificatesV1{MockService: s})
pb.RegisterRegionSslCertificatesServer(grpcServer, &RegionalSSLCertificatesV1{MockService: s})
pb.RegisterTargetSslProxiesServer(grpcServer, &TargetSslProxyV1{MockService: s})

pb.RegisterServiceAttachmentsServer(grpcServer, &RegionalServiceAttachmentV1{MockService: s})

Expand Down Expand Up @@ -127,6 +129,9 @@ func (s *MockService) NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (ht
if err := pb.RegisterRegionTargetHttpProxiesHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
}
if err := pb.RegisterTargetSslProxiesHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
}

if err := pb.RegisterUrlMapsHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
Expand Down Expand Up @@ -177,10 +182,13 @@ func (s *MockService) NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (ht
return nil, err
}

// for global ssl certs and the managedsslcerts
// for ssl certs and the managedsslcerts
if err := pb.RegisterSslCertificatesHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
}
if err := pb.RegisterRegionSslCertificatesHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
}

if err := pb.RegisterServiceAttachmentsHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 3d7022e

Please sign in to comment.