Skip to content

Commit

Permalink
mockGCP for ComputeTargetVpnGateway
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Jul 9, 2024
1 parent 173bbc7 commit d7e14d7
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 23 deletions.
30 changes: 8 additions & 22 deletions mockgcp/mockcompute/targetvpngatewayv1.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 TargetVpnGatewaysV1 struct {
Expand All @@ -43,11 +41,7 @@ func (s *TargetVpnGatewaysV1) Get(ctx context.Context, req *pb.GetTargetVpnGatew

obj := &pb.TargetVpnGateway{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
if apierrors.IsNotFound(err) {
return nil, status.Errorf(codes.NotFound, "targetVpnGateway %q not found", name)
} else {
return nil, status.Errorf(codes.Internal, "error reading targetVpnGateway: %v", err)
}
return nil, err
}

return obj, nil
Expand All @@ -71,7 +65,7 @@ func (s *TargetVpnGatewaysV1) Insert(ctx context.Context, req *pb.InsertTargetVp
obj.Kind = PtrTo("compute#targetVpnGateway")

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

return s.newLRO(ctx, name.Project.ID)
Expand All @@ -88,11 +82,7 @@ func (s *TargetVpnGatewaysV1) Delete(ctx context.Context, req *pb.DeleteTargetVp

deleted := &pb.TargetVpnGateway{}
if err := s.storage.Delete(ctx, fqn, deleted); err != nil {
if apierrors.IsNotFound(err) {
return nil, status.Errorf(codes.NotFound, "targetVpnGateway %q not found", name)
} else {
return nil, status.Errorf(codes.Internal, "error deleting targetVpnGateway: %v", err)
}
return nil, err
}

return s.newLRO(ctx, name.Project.ID)
Expand All @@ -109,16 +99,12 @@ func (s *TargetVpnGatewaysV1) SetLabels(ctx context.Context, req *pb.SetLabelsTa

obj := &pb.TargetVpnGateway{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
if apierrors.IsNotFound(err) {
return nil, status.Errorf(codes.NotFound, "targetVpnGateway %q not found", name)
} else {
return nil, status.Errorf(codes.Internal, "error reading targetVpnGateway: %v", err)
}
return nil, err
}

obj.Labels = req.GetRegionSetLabelsRequestResource().GetLabels()
if err := s.storage.Update(ctx, fqn, obj); err != nil {
return nil, status.Errorf(codes.Internal, "error updating targetVpnGateway: %v", err)
return nil, err
}

return s.newLRO(ctx, name.Project.ID)
Expand All @@ -140,7 +126,7 @@ func (s *MockService) parseRegionalTargetVpnGatewayName(name string) (*regionalT
tokens := strings.Split(name, "/")

if len(tokens) == 6 && tokens[0] == "projects" && tokens[2] == "regions" && tokens[4] == "targetVpnGateways" {
project, err := s.projects.GetProjectByID(tokens[1])
project, err := s.Projects.GetProjectByID(tokens[1])
if err != nil {
return nil, err
}
Expand Down
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}
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
GET https://compute.googleapis.com/compute/beta/projects/${projectId}/global/networks/${networkID}?alt=json
Content-Type: application/json
User-Agent: Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

200 OK
Cache-Control: private
Content-Type: application/json; charset=UTF-8
Server: ESF
Vary: Origin
Vary: X-Origin
Vary: Referer
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"creationTimestamp": "2024-04-01T12:34:56.123456Z",
"description": "Default network for the project",
"id": "000000000000000000000",
"kind": "compute#network",
"name": "${networkID}",
"selfLink": "https://www.googleapis.com/compute/beta/projects/${projectId}/global/networks/${networkID}",
"selfLinkWithId": "https://www.googleapis.com/compute/beta/projects/${projectId}/global/networks/${networkID}"
}

---

PATCH https://compute.googleapis.com/compute/beta/projects/${projectId}/global/networks/${networkID}?alt=json
Content-Type: application/json
User-Agent: Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

{
"networkFirewallPolicyEnforcementOrder": "AFTER_CLASSIC_FIREWALL"
}

200 OK
Cache-Control: private
Content-Type: application/json; charset=UTF-8
Server: ESF
Vary: Origin
Vary: X-Origin
Vary: Referer
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"id": "000000000000000000000",
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "compute.networks.patch",
"progress": 0,
"selfLink": "https://www.googleapis.com/compute/beta/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
"status": "RUNNING",
"targetId": "${networkID}",
"targetLink": "https://www.googleapis.com/compute/beta/projects/${projectId}/global/networks/${networkID}",
"user": "[email protected]"
}

---

GET https://compute.googleapis.com/compute/beta/projects/${projectId}/global/networks/${networkID}?alt=json
Content-Type: application/json
User-Agent: Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

200 OK
Cache-Control: private
Content-Type: application/json; charset=UTF-8
Server: ESF
Vary: Origin
Vary: X-Origin
Vary: Referer
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"creationTimestamp": "2024-04-01T12:34:56.123456Z",
"description": "Default network for the project",
"id": "000000000000000000000",
"kind": "compute#network",
"name": "${networkID}",
"selfLink": "https://www.googleapis.com/compute/beta/projects/${projectId}/global/networks/${networkID}",
"selfLinkWithId": "https://www.googleapis.com/compute/beta/projects/${projectId}/global/networks/${networkID}"
}

---

PATCH https://compute.googleapis.com/compute/beta/projects/${projectId}/global/networks/${networkID}?alt=json
Content-Type: application/json
User-Agent: Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

{
"networkFirewallPolicyEnforcementOrder": "AFTER_CLASSIC_FIREWALL"
}

200 OK
Cache-Control: private
Content-Type: application/json; charset=UTF-8
Server: ESF
Vary: Origin
Vary: X-Origin
Vary: Referer
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"id": "000000000000000000000",
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "compute.networks.patch",
"progress": 0,
"selfLink": "https://www.googleapis.com/compute/beta/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
"status": "RUNNING",
"targetId": "${networkID}",
"targetLink": "https://www.googleapis.com/compute/beta/projects/${projectId}/global/networks/${networkID}",
"user": "[email protected]"
}

---

GET https://compute.googleapis.com/compute/beta/projects/${projectId}/global/networks/${networkID}?alt=json
Content-Type: application/json
User-Agent: Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

200 OK
Cache-Control: private
Content-Type: application/json; charset=UTF-8
Server: ESF
Vary: Origin
Vary: X-Origin
Vary: Referer
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"creationTimestamp": "2024-04-01T12:34:56.123456Z",
"description": "Default network for the project",
"id": "000000000000000000000",
"kind": "compute#network",
"name": "${networkID}",
"selfLink": "https://www.googleapis.com/compute/beta/projects/${projectId}/global/networks/${networkID}",
"selfLinkWithId": "https://www.googleapis.com/compute/beta/projects/${projectId}/global/networks/${networkID}"
}

---

GET https://compute.googleapis.com/compute/beta/projects/${projectId}/regions/us-central1/targetVpnGateways/computevpngateway-${uniqueId}?alt=json
Content-Type: application/json
User-Agent: Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

404 Not Found
Cache-Control: private
Content-Type: application/json; charset=UTF-8
Server: ESF
Vary: Origin
Vary: X-Origin
Vary: Referer
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"error": {
"code": 404,
"errors": [
{
"domain": "global",
"message": "targetVpnGateway \"projects/${projectId}/regions/us-central1/targetVpnGateways/computevpngateway-${uniqueId}\" not found",
"reason": "notFound"
}
],
"message": "targetVpnGateway \"projects/${projectId}/regions/us-central1/targetVpnGateways/computevpngateway-${uniqueId}\" not found"
}
}

---

POST https://compute.googleapis.com/compute/beta/projects/${projectId}/regions/us-central1/targetVpnGateways?alt=json
Content-Type: application/json
User-Agent: Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

{
"name": "computevpngateway-${uniqueId}",
"network": "projects/${projectId}/global/networks/${networkID}",
"region": "projects/${projectId}/global/regions/us-central1"
}

200 OK
Cache-Control: private
Content-Type: application/json; charset=UTF-8
Server: ESF
Vary: Origin
Vary: X-Origin
Vary: Referer
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"id": "000000000000000000000",
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"progress": 0,
"selfLink": "https://compute.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
"status": "DONE"
}

---

GET https://compute.googleapis.com/compute/beta/projects/${projectId}/regions/us-central1/targetVpnGateways/computevpngateway-${uniqueId}?alt=json
Content-Type: application/json
User-Agent: Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

200 OK
Cache-Control: private
Content-Type: application/json; charset=UTF-8
Server: ESF
Vary: Origin
Vary: X-Origin
Vary: Referer
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"creationTimestamp": "2024-04-01T12:34:56.123456Z",
"id": "000000000000000000000",
"kind": "compute#targetVpnGateway",
"name": "computevpngateway-${uniqueId}",
"network": "projects/${projectId}/global/networks/${networkID}",
"region": "projects/${projectId}/global/regions/us-central1",
"selfLink": "https://compute.googleapis.com/compute/v1/projects/${projectId}/regions/us-central1/targetVpnGateways/computevpngateway-${uniqueId}"
}

---

DELETE https://compute.googleapis.com/compute/beta/projects/${projectId}/regions/us-central1/targetVpnGateways/computevpngateway-${uniqueId}?alt=json
Content-Type: application/json
User-Agent: Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

200 OK
Cache-Control: private
Content-Type: application/json; charset=UTF-8
Server: ESF
Vary: Origin
Vary: X-Origin
Vary: Referer
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"id": "000000000000000000000",
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"progress": 0,
"selfLink": "https://compute.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
"status": "DONE"
}
2 changes: 1 addition & 1 deletion tests/e2e/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func normalizeKRMObject(u *unstructured.Unstructured, project testgcp.GCPProject
// Specific to Compute
visitor.replacePaths[".status.observedState.certificateID"] = "1.719337333063698e+18"
visitor.replacePaths[".status.instanceId"] = "1111111111111111"
visitor.replacePaths[".status.gatewayId"] = "1.719337333063698e+18"

// Specific to MonitoringDashboard
visitor.stringTransforms = append(visitor.stringTransforms, func(path string, s string) string {
Expand Down Expand Up @@ -414,7 +415,6 @@ func normalizeHTTPResponses(t *testing.T, events test.LogEntries) {

// Compute operations
visitor.replacePaths[".fingerprint"] = "abcdef0123A="

visitor.replacePaths[".startTime"] = "2024-04-01T12:34:56.123456Z"

events.PrettifyJSON(func(obj map[string]any) {
Expand Down

0 comments on commit d7e14d7

Please sign in to comment.