Skip to content

Commit

Permalink
mockGCP support for regional compute target TCP proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Oct 30, 2024
1 parent 058e2b9 commit 7dae66e
Show file tree
Hide file tree
Showing 18 changed files with 3,116 additions and 0 deletions.
138 changes: 138 additions & 0 deletions mockgcp/mockcompute/regionaltargettcpproxyv1.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 RegionalTargetTcpProxyV1 struct {
*MockService
pb.UnimplementedRegionTargetTcpProxiesServer
}

func (s *RegionalTargetTcpProxyV1) Get(ctx context.Context, req *pb.GetRegionTargetTcpProxyRequest) (*pb.TargetTcpProxy, error) {
reqName := "projects/" + req.GetProject() + "/regions/" + req.GetRegion() + "/targetTcpProxies/" + req.GetTargetTcpProxy()
name, err := s.parseRegionalTargetTcpProxyName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

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

return obj, nil
}

func (s *RegionalTargetTcpProxyV1) Insert(ctx context.Context, req *pb.InsertRegionTargetTcpProxyRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/regions/" + req.GetRegion() + "/targetTcpProxies/" + req.GetTargetTcpProxyResource().GetName()
name, err := s.parseRegionalTargetTcpProxyName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

id := s.generateID()

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

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 *RegionalTargetTcpProxyV1) Delete(ctx context.Context, req *pb.DeleteRegionTargetTcpProxyRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/regions/" + req.GetRegion() + "/targetTcpProxies/" + req.GetTargetTcpProxy()
name, err := s.parseRegionalTargetTcpProxyName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

deleted := &pb.TargetTcpProxy{}
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 regionalTargetTcpProxyName struct {
Project *projects.ProjectData
Region string
Name string
}

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

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

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

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

return name, nil
} else {
return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid", name)
}
}
1 change: 1 addition & 0 deletions mockgcp/mockcompute/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (s *MockService) Register(grpcServer *grpc.Server) {
pb.RegisterRegionSslCertificatesServer(grpcServer, &RegionalSSLCertificatesV1{MockService: s})
pb.RegisterTargetSslProxiesServer(grpcServer, &TargetSslProxyV1{MockService: s})
pb.RegisterTargetTcpProxiesServer(grpcServer, &GlobalTargetTcpProxyV1{MockService: s})
pb.RegisterRegionTargetTcpProxiesServer(grpcServer, &RegionalTargetTcpProxyV1{MockService: s})

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

Expand Down
5 changes: 5 additions & 0 deletions pkg/test/resourcefixture/contexts/compute_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ func init() {
ResourceKind: "ComputeTargetTCPProxy",
}

resourceContextMap["regionalcomputetargettcpproxy"] = ResourceContext{
ResourceKind: "ComputeTargetTCPProxy",
SkipUpdate: true, // No UPDATE/PATCH method supported in API.
}

resourceContextMap["globalcomputeforwardingrule"] = ResourceContext{
ResourceKind: "ComputeForwardingRule",
SkipUpdate: true, // The only field which supports update is targetRef, which currently cannot be used for testing updates because of b/147506185
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ spec:
backendServiceRef:
name: computebackendservice-${uniqueId}
proxyHeader: NONE
location: global
---
apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeTargetTCPProxy
Expand All @@ -64,3 +65,4 @@ spec:
backendServiceRef:
name: computebackendservice-${uniqueId}
proxyHeader: NONE
location: global
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeForwardingRule
metadata:
annotations:
cnrm.cloud.google.com/management-conflict-prevention-policy: none
cnrm.cloud.google.com/project-id: ${projectId}
finalizers:
- cnrm.cloud.google.com/finalizer
- cnrm.cloud.google.com/deletion-defender
generation: 2
labels:
cnrm-test: "true"
label-one: value-two
name: computeregionalforwardingrule-${uniqueId}
namespace: ${uniqueId}
spec:
description: A regional forwarding rule
ipAddress:
addressRef:
name: computeaddress-${uniqueId}
ipProtocol: TCP
loadBalancingScheme: EXTERNAL_MANAGED
location: us-central1
networkRef:
name: network-${uniqueId}
portRange: "90"
target:
targetTCPProxyRef:
name: computetargettcpproxy2-${uniqueId}
status:
conditions:
- lastTransitionTime: "1970-01-01T00:00:00Z"
message: The resource is up to date
reason: UpToDate
status: "True"
type: Ready
creationTimestamp: "1970-01-01T00:00:00Z"
externalRef: //compute.googleapis.com/projects/${projectId}/regions/us-central1/forwardingrules/computeregionalforwardingrule-${uniqueId}
labelFingerprint: abcdef0123A=
observedGeneration: 2
selfLink: https://www.googleapis.com/compute/v1/projects/${projectId}/regions/us-central1/forwardingRules/computeregionalforwardingrule-${uniqueId}
Loading

0 comments on commit 7dae66e

Please sign in to comment.