Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#3237 from gemmahou/update-back…
Browse files Browse the repository at this point in the history
…end-service

test: test update backend service in ComputeTCPProxy
  • Loading branch information
google-oss-prow[bot] authored Nov 25, 2024
2 parents 588ce26 + b19a791 commit bb99030
Show file tree
Hide file tree
Showing 19 changed files with 1,167 additions and 34 deletions.
31 changes: 31 additions & 0 deletions mockgcp/mockcompute/globaltargettcpproxyv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (s *GlobalTargetTcpProxyV1) Insert(ctx context.Context, req *pb.InsertTarge
obj.CreationTimestamp = PtrTo(s.nowString())
obj.Id = &id
obj.Kind = PtrTo("compute#targetTcpProxy")

if obj.ProxyHeader == nil {
obj.ProxyHeader = PtrTo("NONE")
}
Expand Down Expand Up @@ -107,6 +108,36 @@ func (s *GlobalTargetTcpProxyV1) Delete(ctx context.Context, req *pb.DeleteTarge
})
}

func (s *GlobalTargetTcpProxyV1) SetBackendService(ctx context.Context, req *pb.SetBackendServiceTargetTcpProxyRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/global/targetTcpProxies/" + req.GetTargetTcpProxy()
name, err := s.parseTargetTcpProxyName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

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

obj.Service = req.GetTargetTcpProxiesSetBackendServiceRequestResource().Service
if err := s.storage.Update(ctx, fqn, obj); err != nil {
return nil, err
}

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

func (s *GlobalTargetTcpProxyV1) SetProxyHeader(ctx context.Context, req *pb.SetProxyHeaderTargetTcpProxyRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/global/targetTcpProxies/" + req.GetTargetTcpProxy()
name, err := s.parseTargetTcpProxyName(reqName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ func (m *targetTCPProxyModel) AdapterForObject(ctx context.Context, reader clien
}
location := parent.Region

// Handle API/TF default values
if obj.Spec.ProxyBind != nil && *obj.Spec.ProxyBind == false {
obj.Spec.ProxyBind = nil
}
if obj.Spec.ProxyHeader == nil {
obj.Spec.ProxyHeader = direct.PtrTo("NONE")
}

// Get GCP client
if location == "global" {
gcpClient, err := m.client(ctx)
Expand Down Expand Up @@ -256,42 +264,43 @@ func (a *targetTCPProxyAdapter) Update(ctx context.Context, updateOp *directbase
targetTCPProxy.Name = direct.LazyPtr(tokens[len(tokens)-1])

// Regional API does not support Update
if !reflect.DeepEqual(targetTCPProxy.Service, a.actual.Service) && region == "global" {
setBackendServiceReq := &computepb.SetBackendServiceTargetTcpProxyRequest{
if !reflect.DeepEqual(targetTCPProxy.ProxyHeader, a.actual.ProxyHeader) && region == "global" {
setProxyHeaderReq := &computepb.SetProxyHeaderTargetTcpProxyRequest{
Project: parent.ProjectID,
TargetTcpProxiesSetBackendServiceRequestResource: &computepb.TargetTcpProxiesSetBackendServiceRequest{Service: targetTCPProxy.Service},
TargetTcpProxiesSetProxyHeaderRequestResource: &computepb.TargetTcpProxiesSetProxyHeaderRequest{ProxyHeader: targetTCPProxy.ProxyHeader},
TargetTcpProxy: tokens[len(tokens)-1],
}
op, err = a.targetTcpProxiesClient.SetBackendService(ctx, setBackendServiceReq)
op, err = a.targetTcpProxiesClient.SetProxyHeader(ctx, setProxyHeaderReq)
if err != nil {
return fmt.Errorf("updating ComputeTargetTCPProxy backend service %s: %w", a.id.External, err)
return fmt.Errorf("updating ComputeTargetTCPProxy proxy header %s: %w", a.id.External, err)
}
if !op.Done() {
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("waiting ComputeTargetTCPProxy backend service %s update failed: %w", a.id.External, err)
return fmt.Errorf("waiting ComputeTargetTCPProxy proxy header %s update failed: %w", a.id.External, err)
}
}
log.V(2).Info("successfully updated ComputeTargetTCPProxy backend service", "name", a.id.External)

log.V(2).Info("successfully updated ComputeTargetTCPProxy proxy header", "name", a.id.External)
}
if !reflect.DeepEqual(targetTCPProxy.ProxyHeader, a.actual.ProxyHeader) && region == "global" {
setProxyHeaderReq := &computepb.SetProxyHeaderTargetTcpProxyRequest{

if !reflect.DeepEqual(targetTCPProxy.Service, a.actual.Service) && region == "global" {
setBackendServiceReq := &computepb.SetBackendServiceTargetTcpProxyRequest{
Project: parent.ProjectID,
TargetTcpProxiesSetProxyHeaderRequestResource: &computepb.TargetTcpProxiesSetProxyHeaderRequest{ProxyHeader: targetTCPProxy.ProxyHeader},
TargetTcpProxiesSetBackendServiceRequestResource: &computepb.TargetTcpProxiesSetBackendServiceRequest{Service: targetTCPProxy.Service},
TargetTcpProxy: tokens[len(tokens)-1],
}
op, err = a.targetTcpProxiesClient.SetProxyHeader(ctx, setProxyHeaderReq)
op, err = a.targetTcpProxiesClient.SetBackendService(ctx, setBackendServiceReq)
if err != nil {
return fmt.Errorf("updating ComputeTargetTCPProxy proxy header %s: %w", a.id.External, err)
return fmt.Errorf("updating ComputeTargetTCPProxy backend service %s: %w", a.id.External, err)
}
if !op.Done() {
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("waiting ComputeTargetTCPProxy proxy header %s update failed: %w", a.id.External, err)
return fmt.Errorf("waiting ComputeTargetTCPProxy backend service %s update failed: %w", a.id.External, err)
}
}
log.V(2).Info("successfully updated ComputeTargetTCPProxy proxy header", "name", a.id.External)
log.V(2).Info("successfully updated ComputeTargetTCPProxy backend service", "name", a.id.External)

}

// Get the updated resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ metadata:
finalizers:
- cnrm.cloud.google.com/finalizer
- cnrm.cloud.google.com/deletion-defender
generation: 1
generation: 2
labels:
cnrm-test: "true"
name: computetargettcpproxy-${uniqueId}
namespace: ${uniqueId}
spec:
backendServiceRef:
name: computebackendservice-${uniqueId}
name: computebackendservice-2-${uniqueId}
status:
conditions:
- lastTransitionTime: "1970-01-01T00:00:00Z"
Expand All @@ -25,6 +25,6 @@ status:
type: Ready
creationTimestamp: "1970-01-01T00:00:00Z"
externalRef: projects/${projectId}/global/targetTcpProxies/computetargettcpproxy-${uniqueId}
observedGeneration: 1
observedGeneration: 2
proxyId: 1111111111111111
selfLink: https://www.googleapis.com/compute/v1/projects/${projectId}/global/targetTcpProxies/computetargettcpproxy-${uniqueId}
Loading

0 comments on commit bb99030

Please sign in to comment.