Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#2255 from gemmahou/mock-comput…
Browse files Browse the repository at this point in the history
…etargetvpngateway

MockGCP: Support for ComputeTargetVPNGateway
  • Loading branch information
google-oss-prow[bot] authored Jul 9, 2024
2 parents 2f091a5 + d7e14d7 commit 4ff5c14
Show file tree
Hide file tree
Showing 6 changed files with 452 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/tests/samples/create/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ 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: "ComputeSubnetwork"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeTargetVPNGateway"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeVPNGateway"}:
// ok

Expand Down
5 changes: 5 additions & 0 deletions mockgcp/mockcompute/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (s *MockService) Register(grpcServer *grpc.Server) {
pb.RegisterNetworksServer(grpcServer, &NetworksV1{MockService: s})
pb.RegisterSubnetworksServer(grpcServer, &SubnetsV1{MockService: s})
pb.RegisterVpnGatewaysServer(grpcServer, &VPNGatewaysV1{MockService: s})
pb.RegisterTargetVpnGatewaysServer(grpcServer, &TargetVpnGatewaysV1{MockService: s})

pb.RegisterRegionHealthChecksServer(grpcServer, &RegionalHealthCheckV1{MockService: s})
pb.RegisterHealthChecksServer(grpcServer, &GlobalHealthCheckV1{MockService: s})
Expand Down Expand Up @@ -95,6 +96,10 @@ func (s *MockService) NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (ht
return nil, err
}

if err := pb.RegisterTargetVpnGatewaysHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
}

if err := pb.RegisterNodeGroupsHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
}
Expand Down
144 changes: 144 additions & 0 deletions mockgcp/mockcompute/targetvpngatewayv1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright 2022 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 TargetVpnGatewaysV1 struct {
*MockService
pb.UnimplementedTargetVpnGatewaysServer
}

func (s *TargetVpnGatewaysV1) Get(ctx context.Context, req *pb.GetTargetVpnGatewayRequest) (*pb.TargetVpnGateway, error) {
reqName := "projects/" + req.GetProject() + "/regions/" + req.GetRegion() + "/targetVpnGateways/" + req.GetTargetVpnGateway()
name, err := s.parseRegionalTargetVpnGatewayName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

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

return obj, nil
}

func (s *TargetVpnGatewaysV1) Insert(ctx context.Context, req *pb.InsertTargetVpnGatewayRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/regions/" + req.GetRegion() + "/targetVpnGateways/" + req.GetTargetVpnGatewayResource().GetName()
name, err := s.parseRegionalTargetVpnGatewayName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

id := s.generateID()

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

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

return s.newLRO(ctx, name.Project.ID)
}

func (s *TargetVpnGatewaysV1) Delete(ctx context.Context, req *pb.DeleteTargetVpnGatewayRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/regions/" + req.GetRegion() + "/targetVpnGateways/" + req.GetTargetVpnGateway()
name, err := s.parseRegionalTargetVpnGatewayName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

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

return s.newLRO(ctx, name.Project.ID)
}

func (s *TargetVpnGatewaysV1) SetLabels(ctx context.Context, req *pb.SetLabelsTargetVpnGatewayRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/regions/" + req.GetRegion() + "/targetVpnGateways/" + req.GetResource()
name, err := s.parseRegionalTargetVpnGatewayName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

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

obj.Labels = req.GetRegionSetLabelsRequestResource().GetLabels()
if err := s.storage.Update(ctx, fqn, obj); err != nil {
return nil, err
}

return s.newLRO(ctx, name.Project.ID)
}

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

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

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

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

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

return name, nil
} else {
return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid", name)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeTargetVPNGateway
metadata:
annotations:
cnrm.cloud.google.com/management-conflict-prevention-policy: none
cnrm.cloud.google.com/project-id: ${projectId}
cnrm.cloud.google.com/state-into-spec: merge
finalizers:
- cnrm.cloud.google.com/finalizer
- cnrm.cloud.google.com/deletion-defender
generation: 2
labels:
cnrm-test: "true"
name: computevpngateway-${uniqueId}
namespace: ${uniqueId}
spec:
networkRef:
name: default
region: us-central1
resourceID: computevpngateway-${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"
gatewayId: "1.719337333063698e+18"
observedGeneration: 2
selfLink: https://compute.googleapis.com/compute/v1/projects/${projectId}/regions/us-central1/targetVpnGateways/computevpngateway-${uniqueId}
Loading

0 comments on commit 4ff5c14

Please sign in to comment.