Skip to content

Commit

Permalink
Add regional ssl certificate mock and update logs
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Sep 5, 2024
1 parent ffb4f23 commit e8bc7ec
Show file tree
Hide file tree
Showing 33 changed files with 2,724 additions and 82 deletions.
1 change: 0 additions & 1 deletion config/tests/samples/create/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,6 @@ func MaybeSkip(t *testing.T, name string, resources []*unstructured.Unstructured
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeManagedSSLCertificate"}:
//case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeServiceAttachment"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeSSLCertificate"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeServiceAttachment"}:
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"}:
Expand Down
2 changes: 1 addition & 1 deletion mockgcp/mockcompute/globalsslcertificatesv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (n *globalSSLCertificateName) String() string {
}

// parseGlobalSslCertificateName parses a string into a globalSslCertificateName.
// The expected form is `projects/*/regions/*/sslcertificate/*`.
// The expected form is `projects/*/global/sslcertificate/*`.
func (s *MockService) parseGlobalSslCertificateName(name string) (*globalSSLCertificateName, error) {
tokens := strings.Split(name, "/")

Expand Down
2 changes: 1 addition & 1 deletion mockgcp/mockcompute/regionaladdress.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ type regionalAddressName struct {
}

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

// parseAddressName parses a string into a addressName.
Expand Down
138 changes: 138 additions & 0 deletions mockgcp/mockcompute/regionalsslcertificatesv1.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 RegionalSSLCertificatesV1 struct {
*MockService
pb.UnimplementedRegionSslCertificatesServer
}

func (s *RegionalSSLCertificatesV1) Get(ctx context.Context, req *pb.GetRegionSslCertificateRequest) (*pb.SslCertificate, error) {
reqName := "projects/" + req.GetProject() + "/regions/" + req.Region + "/sslCertificates/" + req.GetSslCertificate()
name, err := s.parseRegionalSslCertificateName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

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

return obj, nil
}

func (s *RegionalSSLCertificatesV1) Insert(ctx context.Context, req *pb.InsertRegionSslCertificateRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/regions/" + req.GetRegion() + "/sslCertificates/" + req.GetSslCertificateResource().GetName()
name, err := s.parseRegionalSslCertificateName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

id := s.generateID()

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

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

fqn := name.String()

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

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

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

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

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

return name, nil
}

return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid", name)
}
6 changes: 5 additions & 1 deletion mockgcp/mockcompute/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (s *MockService) Register(grpcServer *grpc.Server) {
pb.RegisterAddressesServer(grpcServer, &RegionalAddressesV1{MockService: s})
pb.RegisterGlobalAddressesServer(grpcServer, &GlobalAddressesV1{MockService: s})
pb.RegisterSslCertificatesServer(grpcServer, &GlobalSSLCertificatesV1{MockService: s})
pb.RegisterRegionSslCertificatesServer(grpcServer, &RegionalSSLCertificatesV1{MockService: s})
pb.RegisterTargetSslProxiesServer(grpcServer, &TargetSslProxyV1{MockService: s})

pb.RegisterServiceAttachmentsServer(grpcServer, &RegionalServiceAttachmentV1{MockService: s})
Expand Down Expand Up @@ -181,10 +182,13 @@ func (s *MockService) NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (ht
return nil, err
}

// for global ssl certs and the managedsslcerts
// for ssl certs and the managedsslcerts
if err := pb.RegisterSslCertificatesHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
}
if err := pb.RegisterRegionSslCertificatesHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
}

if err := pb.RegisterServiceAttachmentsHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,46 @@ X-Xss-Protection: 0
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "insert",
"progress": 0,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
"status": "DONE"
"status": "RUNNING",
"targetId": "${addressesId}",
"targetLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/addresses/computeaddress-${uniqueId}",
"user": "[email protected]"
}

---

GET https://compute.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}?alt=json&prettyPrint=false
User-Agent: google-api-go-client/0.5 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

{
"endTime": "2024-04-01T12:34:56.123456Z",
"id": "000000000000000000000",
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "insert",
"progress": 100,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
"status": "DONE",
"targetId": "${addressesId}",
"targetLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/addresses/computeaddress-${uniqueId}",
"user": "[email protected]"
}

---
Expand Down Expand Up @@ -1107,10 +1143,46 @@ X-Xss-Protection: 0
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "delete",
"progress": 0,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
"status": "DONE"
"status": "RUNNING",
"targetId": "${addressesId}",
"targetLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/addresses/computeaddress-${uniqueId}",
"user": "[email protected]"
}

---

GET https://compute.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}?alt=json&prettyPrint=false
User-Agent: google-api-go-client/0.5 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

{
"endTime": "2024-04-01T12:34:56.123456Z",
"id": "000000000000000000000",
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "delete",
"progress": 100,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
"status": "DONE",
"targetId": "${addressesId}",
"targetLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/addresses/computeaddress-${uniqueId}",
"user": "[email protected]"
}

---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,46 @@ X-Xss-Protection: 0
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "insert",
"progress": 0,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
"status": "DONE"
"status": "RUNNING",
"targetId": "${addressesId}",
"targetLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/addresses/computeaddress-${uniqueId}",
"user": "[email protected]"
}

---

GET https://compute.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}?alt=json&prettyPrint=false
User-Agent: google-api-go-client/0.5 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

{
"endTime": "2024-04-01T12:34:56.123456Z",
"id": "000000000000000000000",
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "insert",
"progress": 100,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
"status": "DONE",
"targetId": "${addressesId}",
"targetLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/addresses/computeaddress-${uniqueId}",
"user": "[email protected]"
}

---
Expand Down Expand Up @@ -942,10 +978,46 @@ X-Xss-Protection: 0
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "delete",
"progress": 0,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
"status": "DONE"
"status": "RUNNING",
"targetId": "${addressesId}",
"targetLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/addresses/computeaddress-${uniqueId}",
"user": "[email protected]"
}

---

GET https://compute.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}?alt=json&prettyPrint=false
User-Agent: google-api-go-client/0.5 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

{
"endTime": "2024-04-01T12:34:56.123456Z",
"id": "000000000000000000000",
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "delete",
"progress": 100,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
"status": "DONE",
"targetId": "${addressesId}",
"targetLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/addresses/computeaddress-${uniqueId}",
"user": "[email protected]"
}

---
Expand Down
Loading

0 comments on commit e8bc7ec

Please sign in to comment.