Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#1558 from justinsb/mockgcp_net…
Browse files Browse the repository at this point in the history
…work_fidelity

mockgcp: improve fidelity of ComputeNetwork mock
  • Loading branch information
google-oss-prow[bot] authored Jun 7, 2024
2 parents e03fc16 + 0b29283 commit 039e227
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 154 deletions.
16 changes: 12 additions & 4 deletions mockgcp/mockcompute/networksv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func (s *NetworksV1) Get(ctx context.Context, req *pb.GetNetworkRequest) (*pb.Ne

obj := &pb.Network{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
if status.Code(err) == codes.NotFound {
return nil, status.Errorf(codes.NotFound, "The resource '%s' was not found", fqn)
}
return nil, err
}

Expand All @@ -60,10 +63,10 @@ func (s *NetworksV1) Insert(ctx context.Context, req *pb.InsertNetworkRequest) (
id := s.generateID()

obj := proto.Clone(req.GetNetworkResource()).(*pb.Network)
obj.SelfLink = PtrTo("https://compute.googleapis.com/compute/v1/" + name.String())
obj.CreationTimestamp = PtrTo(s.nowString())
obj.Id = &id
obj.SelfLinkWithId = PtrTo(fmt.Sprintf("https://compute.googleapis.com/compute/v1/projects/%s/global/networks/%d", name.Project.ID, id))
obj.SelfLink = PtrTo("https://www.googleapis.com/compute/beta/" + name.String())
obj.SelfLinkWithId = PtrTo(fmt.Sprintf("https://www.googleapis.com/compute/beta/projects/%s/global/networks/%d", name.Project.ID, id))
obj.Kind = PtrTo("compute#network")

if err := s.storage.Create(ctx, fqn, obj); err != nil {
Expand All @@ -72,8 +75,9 @@ func (s *NetworksV1) Insert(ctx context.Context, req *pb.InsertNetworkRequest) (

op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLinkWithId,
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
Expand Down Expand Up @@ -109,8 +113,9 @@ func (s *NetworksV1) Patch(ctx context.Context, req *pb.PatchNetworkRequest) (*p

op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLinkWithId,
TargetLink: obj.SelfLink,
OperationType: PtrTo("compute.networks.patch"),
User: PtrTo("[email protected]"),
}
return s.startGlobalLRO(ctx, name.Project.ID, op, func() (proto.Message, error) {
return obj, nil
Expand All @@ -131,7 +136,10 @@ func (s *NetworksV1) Delete(ctx context.Context, req *pb.DeleteNetworkRequest) (
}

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
Expand Down
2 changes: 1 addition & 1 deletion mockgcp/mockcompute/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (s *computeOperations) startLRO0(ctx context.Context, op *pb.Operation, fqn
op.Progress = PtrTo(int32(0))

op.Kind = PtrTo("compute#operation")
op.SelfLink = PtrTo("https://compute.googleapis.com/compute/v1/" + fqn)
op.SelfLink = PtrTo("https://www.googleapis.com/compute/beta/" + fqn)

op.Status = PtrTo(pb.Operation_RUNNING)

Expand Down
6 changes: 6 additions & 0 deletions mockgcp/mockcompute/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,11 @@ func (s *MockService) NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (ht
return nil, err
}

// Returns slightly non-standard errors
mux.RewriteError = func(ctx context.Context, error *httpmux.ErrorResponse) {
// Does not return status (at least for 404)
error.Status = ""
}

return mux, nil
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# 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.

apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeNetwork
metadata:
Expand All @@ -30,6 +16,7 @@ metadata:
namespace: ${uniqueId}
spec:
autoCreateSubnetworks: false
enableUlaInternalIpv6: true
networkFirewallPolicyEnforcementOrder: AFTER_CLASSIC_FIREWALL
resourceID: computenetwork-${uniqueId}
routingMode: GLOBAL
Expand All @@ -41,4 +28,4 @@ status:
status: "True"
type: Ready
observedGeneration: 3
selfLink: https://compute.googleapis.com/compute/v1/projects/${projectId}/global/networks/computenetwork-${uniqueId}
selfLink: https://www.googleapis.com/compute/v1/projects/${projectId}/global/networks/computenetwork-${uniqueId}
Loading

0 comments on commit 039e227

Please sign in to comment.