forked from GoogleCloudPlatform/k8s-config-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request GoogleCloudPlatform#2635 from gemmahou/mock-target…
…tcpproxy Support Mock Compute TargetTcpProxy
- Loading branch information
Showing
12 changed files
with
2,559 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// 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 GlobalTargetTcpProxyV1 struct { | ||
*MockService | ||
pb.UnimplementedTargetTcpProxiesServer | ||
} | ||
|
||
func (s *GlobalTargetTcpProxyV1) Get(ctx context.Context, req *pb.GetTargetTcpProxyRequest) (*pb.TargetTcpProxy, error) { | ||
reqName := "projects/" + req.GetProject() + "/global/targetTcpProxies/" + req.GetTargetTcpProxy() | ||
name, err := s.parseTargetTcpProxyName(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 *GlobalTargetTcpProxyV1) Insert(ctx context.Context, req *pb.InsertTargetTcpProxyRequest) (*pb.Operation, error) { | ||
reqName := "projects/" + req.GetProject() + "/global/targetTcpProxies/" + req.GetTargetTcpProxyResource().GetName() | ||
name, err := s.parseTargetTcpProxyName(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.startGlobalLRO(ctx, name.Project.ID, op, func() (proto.Message, error) { | ||
return obj, nil | ||
}) | ||
} | ||
|
||
func (s *GlobalTargetTcpProxyV1) Delete(ctx context.Context, req *pb.DeleteTargetTcpProxyRequest) (*pb.Operation, error) { | ||
reqName := "projects/" + req.GetProject() + "/global/targetTcpProxies/" + req.GetTargetTcpProxy() | ||
name, err := s.parseTargetTcpProxyName(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.startGlobalLRO(ctx, name.Project.ID, op, func() (proto.Message, error) { | ||
return deleted, nil | ||
}) | ||
} | ||
|
||
func (s *GlobalTargetTcpProxyV1) SetProxyHeader(ctx context.Context, req *pb.SetProxyHeaderTargetTcpProxyRequest) (*pb.Operation, error) { | ||
reqName := "projects/" + req.GetProject() + "/global/targetTcpProxies/" + req.GetTargetTcpProxy() | ||
name, err := s.parseTargetTcpProxyName(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 | ||
} | ||
|
||
obj.ProxyHeader = req.GetTargetTcpProxiesSetProxyHeaderRequestResource().ProxyHeader | ||
if err := s.storage.Update(ctx, fqn, obj); err != nil { | ||
return nil, err | ||
} | ||
|
||
op := &pb.Operation{ | ||
TargetId: obj.Id, | ||
TargetLink: obj.SelfLink, | ||
OperationType: PtrTo("setProxyHeader"), | ||
User: PtrTo("[email protected]"), | ||
} | ||
return s.startGlobalLRO(ctx, name.Project.ID, op, func() (proto.Message, error) { | ||
return obj, nil | ||
}) | ||
} | ||
|
||
type targetTcpProxyName struct { | ||
Project *projects.ProjectData | ||
Name string | ||
} | ||
|
||
func (n *targetTcpProxyName) String() string { | ||
return "projects/" + n.Project.ID + "/global/targetTcpProxies/" + n.Name | ||
} | ||
|
||
// parseTargetTcpProxyName parses a string into a targetTcpProxyName. | ||
// The expected form is `projects/*/global/targetTcpProxies/*`. | ||
func (s *MockService) parseTargetTcpProxyName(name string) (*targetTcpProxyName, error) { | ||
tokens := strings.Split(name, "/") | ||
|
||
if len(tokens) == 5 && tokens[0] == "projects" && tokens[3] == "targetTcpProxies" { | ||
project, err := s.Projects.GetProjectByID(tokens[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
name := &targetTcpProxyName{ | ||
Project: project, | ||
Name: tokens[4], | ||
} | ||
|
||
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
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
39 changes: 39 additions & 0 deletions
39
...obalcomputeforwardingruletcp/_generated_object_globalcomputeforwardingruletcp.golden.yaml
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,39 @@ | ||
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: computeglobalforwardingrule-${uniqueId} | ||
namespace: ${uniqueId} | ||
spec: | ||
description: A global forwarding rule | ||
ipAddress: | ||
addressRef: | ||
name: computeaddress-${uniqueId} | ||
ipProtocol: TCP | ||
loadBalancingScheme: EXTERNAL | ||
location: global | ||
portRange: "110" | ||
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}/global/forwardingrules/computeglobalforwardingrule-${uniqueId} | ||
labelFingerprint: abcdef0123A= | ||
observedGeneration: 2 | ||
selfLink: https://www.googleapis.com/compute/v1/projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId} |
Oops, something went wrong.