Skip to content

Commit

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

MockGCP: Support for ComputeVPNGateway
  • Loading branch information
google-oss-prow[bot] authored Jul 9, 2024
2 parents 7b379cd + e7224aa commit 2f091a5
Show file tree
Hide file tree
Showing 7 changed files with 602 additions and 0 deletions.
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: "ComputeVPNGateway"}:
// ok

case schema.GroupKind{Group: "container.cnrm.cloud.google.com", Kind: "ContainerCluster"}:
Expand Down
5 changes: 5 additions & 0 deletions mockgcp/mockcompute/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (s *MockService) ExpectedHost() string {
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.RegisterRegionHealthChecksServer(grpcServer, &RegionalHealthCheckV1{MockService: s})
pb.RegisterHealthChecksServer(grpcServer, &GlobalHealthCheckV1{MockService: s})
Expand Down Expand Up @@ -90,6 +91,10 @@ func (s *MockService) NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (ht
return nil, err
}

if err := pb.RegisterVpnGatewaysHandler(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/vpngatewayv1.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 VPNGatewaysV1 struct {
*MockService
pb.UnimplementedVpnGatewaysServer
}

func (s *VPNGatewaysV1) Get(ctx context.Context, req *pb.GetVpnGatewayRequest) (*pb.VpnGateway, error) {
reqName := "projects/" + req.GetProject() + "/regions/" + req.GetRegion() + "/vpnGateways/" + req.GetVpnGateway()
name, err := s.parseRegionalVpnGatewayName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

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

return obj, nil
}

func (s *VPNGatewaysV1) Insert(ctx context.Context, req *pb.InsertVpnGatewayRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/regions/" + req.GetRegion() + "/vpnGateways/" + req.GetVpnGatewayResource().GetName()
name, err := s.parseRegionalVpnGatewayName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

id := s.generateID()

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

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

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

func (s *VPNGatewaysV1) Delete(ctx context.Context, req *pb.DeleteVpnGatewayRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/regions/" + req.GetRegion() + "/vpnGateways/" + req.GetVpnGateway()
name, err := s.parseRegionalVpnGatewayName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

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

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

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

fqn := name.String()

obj := &pb.VpnGateway{}
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 regionalVpnGatewayName struct {
Project *projects.ProjectData
Region string
Name string
}

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

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

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

name := &regionalVpnGatewayName{
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: ComputeVPNGateway
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"
label-one: value-one
name: computevpngateway-${uniqueId}
namespace: ${uniqueId}
spec:
networkRef:
name: default
region: us-central1
resourceID: computevpngateway-${uniqueId}
stackType: IPV4_ONLY
status:
conditions:
- lastTransitionTime: "1970-01-01T00:00:00Z"
message: The resource is up to date
reason: UpToDate
status: "True"
type: Ready
observedGeneration: 2
selfLink: https://compute.googleapis.com/compute/v1/projects/${projectId}/regions/us-central1/vpnGateways/computevpngateway-${uniqueId}
Loading

0 comments on commit 2f091a5

Please sign in to comment.