Skip to content

Commit

Permalink
Update mocks with API default spec field
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Aug 13, 2024
1 parent 3903cba commit 76f8dc9
Show file tree
Hide file tree
Showing 18 changed files with 804 additions and 51 deletions.
313 changes: 313 additions & 0 deletions go.work.sum

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion mockgcp/mockcompute/globaladdress.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ func (s *GlobalAddressesV1) Insert(ctx context.Context, req *pb.InsertGlobalAddr
obj.Id = &id
obj.Kind = PtrTo("compute#address")
obj.Address = PtrTo("8.8.8.8")
obj.LabelFingerprint = PtrTo("abcdef0123A=")
if obj.LabelFingerprint == nil {
obj.LabelFingerprint = PtrTo(computeFingerprint(obj))
}

if err := s.storage.Create(ctx, fqn, obj); err != nil {
return nil, err
Expand Down
77 changes: 72 additions & 5 deletions mockgcp/mockcompute/globalforwardingrulesv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package mockcompute

import (
"context"
"fmt"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
Expand Down Expand Up @@ -63,13 +64,53 @@ func (s *GlobalForwardingRulesV1) Insert(ctx context.Context, req *pb.InsertGlob
obj.CreationTimestamp = PtrTo(s.nowString())
obj.Id = &id
obj.Kind = PtrTo("compute#forwardingRule")
obj.LabelFingerprint = PtrTo("abcdef0123A=")
// If below values are not provided by user, it appears to default by GCP
if obj.LabelFingerprint == nil {
obj.LabelFingerprint = PtrTo(computeFingerprint(obj))
}
if obj.Fingerprint == nil {
obj.Fingerprint = PtrTo(computeFingerprint(obj))
}
if obj.IPProtocol == nil {
obj.IPProtocol = PtrTo("TCP")
}
if obj.NetworkTier == nil {
obj.NetworkTier = PtrTo("PREMIUM")
}

// pattern: \d+(?:-\d+)?
if obj.PortRange != nil {
r := obj.GetPortRange()
token := strings.Split(r, "-")
if len(token) == 1 {
obj.PortRange = PtrTo(fmt.Sprintf("%s-%s", token[0], token[0]))
} else if len(token) == 2 {
obj.PortRange = PtrTo(fmt.Sprintf("%s-%s", token[0], token[1]))
} else {
return nil, status.Errorf(codes.InvalidArgument, "portRange %s is not valid", obj.GetPortRange())
}
}

if obj.Network != nil {
networkName, err := s.parseNetworkName(obj.GetNetwork())
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "network %q is not valid", obj.GetNetwork())
}
obj.Network = PtrTo(fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", networkName.Project.ID, networkName.Name))
}
if err := s.storage.Create(ctx, fqn, obj); err != nil {
return nil, err
}

return s.newLRO(ctx, name.Project.ID)
op := &pb.Operation{
TargetId: obj.Id,
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
})
}

func (s *GlobalForwardingRulesV1) Delete(ctx context.Context, req *pb.DeleteGlobalForwardingRuleRequest) (*pb.Operation, error) {
Expand All @@ -86,7 +127,15 @@ func (s *GlobalForwardingRulesV1) Delete(ctx context.Context, req *pb.DeleteGlob
return nil, err
}

return s.newLRO(ctx, name.Project.ID)
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
})
}

func (s *GlobalForwardingRulesV1) SetLabels(ctx context.Context, req *pb.SetLabelsGlobalForwardingRuleRequest) (*pb.Operation, error) {
Expand All @@ -108,7 +157,17 @@ func (s *GlobalForwardingRulesV1) SetLabels(ctx context.Context, req *pb.SetLabe
return nil, err
}

return s.newLRO(ctx, name.Project.ID)
op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLink,
OperationType: PtrTo("SetLabels"),
User: PtrTo("[email protected]"),
// SetLabels operation has EndTime in response
EndTime: PtrTo("2024-04-01T12:34:56.123456Z"),
}
return s.startGlobalLRO(ctx, name.Project.ID, op, func() (proto.Message, error) {
return obj, nil
})
}

func (s *GlobalForwardingRulesV1) SetTarget(ctx context.Context, req *pb.SetTargetGlobalForwardingRuleRequest) (*pb.Operation, error) {
Expand All @@ -130,7 +189,15 @@ func (s *GlobalForwardingRulesV1) SetTarget(ctx context.Context, req *pb.SetTarg
return nil, err
}

return s.newLRO(ctx, name.Project.ID)
op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLink,
OperationType: PtrTo("SetTarget"),
User: PtrTo("[email protected]"),
}
return s.startGlobalLRO(ctx, name.Project.ID, op, func() (proto.Message, error) {
return obj, nil
})
}

type globalForwardingRuleName struct {
Expand Down
3 changes: 3 additions & 0 deletions mockgcp/mockcompute/instancesv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func (s *InstancesV1) Insert(ctx context.Context, req *pb.InsertInstanceRequest)
obj.Kind = PtrTo("compute#instance")
obj.Zone = PtrTo(fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/zones/%s", name.Project.ID, name.Zone))
obj.Status = PtrTo("RUNNING")
if obj.LabelFingerprint == nil {
obj.LabelFingerprint = PtrTo(computeFingerprint(obj))
}
// if obj.MachineType == nil {
// machineType := "pd-standard"
// obj.MachineType = PtrTo(fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/%s", name.Project.ID, name.Zone, machineType))
Expand Down
78 changes: 73 additions & 5 deletions mockgcp/mockcompute/regionalforwardingrulev1.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package mockcompute

import (
"context"
"fmt"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
Expand Down Expand Up @@ -63,13 +64,54 @@ func (s *RegionalForwardingRulesV1) Insert(ctx context.Context, req *pb.InsertFo
obj.CreationTimestamp = PtrTo(s.nowString())
obj.Id = &id
obj.Kind = PtrTo("compute#forwardingRule")
obj.LabelFingerprint = PtrTo("abcdef0123A=")
// If below values are not provided by user, it appears to default by GCP
if obj.LabelFingerprint == nil {
obj.LabelFingerprint = PtrTo(computeFingerprint(obj))
}
if obj.Fingerprint == nil {
obj.Fingerprint = PtrTo(computeFingerprint(obj))
}
if obj.IPProtocol == nil {
obj.IPProtocol = PtrTo("TCP")
}
if obj.NetworkTier == nil {
obj.NetworkTier = PtrTo("PREMIUM")
}

// pattern: \d+(?:-\d+)?
if obj.PortRange != nil {
r := obj.GetPortRange()
token := strings.Split(r, "-")
if len(token) == 1 {
obj.PortRange = PtrTo(fmt.Sprintf("%s-%s", token[0], token[0]))
} else if len(token) == 2 {
obj.PortRange = PtrTo(fmt.Sprintf("%s-%s", token[0], token[1]))
} else {
return nil, status.Errorf(codes.InvalidArgument, "portRange %s is not valid", obj.GetPortRange())
}
}

if obj.Network != nil {
networkName, err := s.parseNetworkName(obj.GetNetwork())
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "network %q is not valid", obj.GetNetwork())
}
obj.Network = PtrTo(fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", networkName.Project.ID, networkName.Name))
}

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

return s.newLRO(ctx, name.Project.ID)
op := &pb.Operation{
TargetId: obj.Id,
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
})
}

func (s *RegionalForwardingRulesV1) Delete(ctx context.Context, req *pb.DeleteForwardingRuleRequest) (*pb.Operation, error) {
Expand All @@ -86,7 +128,15 @@ func (s *RegionalForwardingRulesV1) Delete(ctx context.Context, req *pb.DeleteFo
return nil, err
}

return s.newLRO(ctx, name.Project.ID)
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
})
}

func (s *RegionalForwardingRulesV1) SetLabels(ctx context.Context, req *pb.SetLabelsForwardingRuleRequest) (*pb.Operation, error) {
Expand All @@ -108,7 +158,17 @@ func (s *RegionalForwardingRulesV1) SetLabels(ctx context.Context, req *pb.SetLa
return nil, err
}

return s.newLRO(ctx, name.Project.ID)
op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLink,
OperationType: PtrTo("SetLabels"),
User: PtrTo("[email protected]"),
// SetLabels operation has EndTime in response
EndTime: PtrTo("2024-04-01T12:34:56.123456Z"),
}
return s.startGlobalLRO(ctx, name.Project.ID, op, func() (proto.Message, error) {
return obj, nil
})
}

func (s *RegionalForwardingRulesV1) SetTarget(ctx context.Context, req *pb.SetTargetForwardingRuleRequest) (*pb.Operation, error) {
Expand All @@ -130,7 +190,15 @@ func (s *RegionalForwardingRulesV1) SetTarget(ctx context.Context, req *pb.SetTa
return nil, err
}

return s.newLRO(ctx, name.Project.ID)
op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLink,
OperationType: PtrTo("SetTarget"),
User: PtrTo("[email protected]"),
}
return s.startGlobalLRO(ctx, name.Project.ID, op, func() (proto.Message, error) {
return obj, nil
})
}

type regionalForwardingRuleName struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ Content-Type: application/json
User-Agent: Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

{
"labelFingerprint": "",
"labelFingerprint": "abcdef0123A=",
"labels": {
"cnrm-test": "true",
"managed-by-cnrm": "true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ spec:
description: A global forwarding rule
ipAddress:
ip: 0.0.0.0
ipProtocol: TCP
loadBalancingScheme: INTERNAL_SELF_MANAGED
location: global
networkRef:
Expand Down
Loading

0 comments on commit 76f8dc9

Please sign in to comment.