From 6fa1e8c938cf3dd1a831d4e72012f4fab5d153d6 Mon Sep 17 00:00:00 2001 From: Amelia Crate Date: Tue, 30 Apr 2024 15:03:55 -0700 Subject: [PATCH] Add daisy compute client functions for manipulating some network resources Add function calls for managing RegionTargetHttpProxies, RegionUrlMaps, RegionBackendServices, RegionHealthChecks, RegionNetworkEndpointGroups and add an AggregatedListForwardingRules function. PiperOrigin-RevId: 629540319 --- compute/compute.go | 341 ++++++++++++++++++++++++++++++++++++ compute/compute_test.go | 118 +++++++++++-- compute/test_client.go | 319 ++++++++++++++++++++++++++------- compute/test_client_test.go | 6 + 4 files changed, 701 insertions(+), 83 deletions(-) diff --git a/compute/compute.go b/compute/compute.go index bf669c0e..5a7374c2 100644 --- a/compute/compute.go +++ b/compute/compute.go @@ -95,6 +95,7 @@ type Client interface { ListInstances(project, zone string, opts ...ListCallOption) ([]*compute.Instance, error) AggregatedListDisks(project string, opts ...ListCallOption) ([]*compute.Disk, error) ListDisks(project, zone string, opts ...ListCallOption) ([]*compute.Disk, error) + AggregatedListForwardingRules(project string, opts ...ListCallOption) ([]*compute.ForwardingRule, error) ListForwardingRules(project, zone string, opts ...ListCallOption) ([]*compute.ForwardingRule, error) ListFirewallRules(project string, opts ...ListCallOption) ([]*compute.Firewall, error) ListImages(project string, opts ...ListCallOption) ([]*compute.Image, error) @@ -116,6 +117,26 @@ type Client interface { GetMachineImage(project, name string) (*compute.MachineImage, error) Suspend(project, zone, instance string) error Resume(project, zone, instance string) error + DeleteRegionTargetHTTPProxy(project, region, name string) error + CreateRegionTargetHTTPProxy(project, region string, p *compute.TargetHttpProxy) error + ListRegionTargetHTTPProxies(project, region, name string, opts ...ListCallOption) ([]*compute.TargetHttpProxy, error) + GetRegionTargetHTTPProxy(project, region, name string) (*compute.TargetHttpProxy, error) + DeleteRegionURLMap(project, region, name string) error + CreateRegionURLMap(project, region string, u *compute.UrlMap) error + ListRegionURLMaps(project, region, name string, opts ...ListCallOption) ([]*compute.UrlMap, error) + GetRegionURLMap(project, region, name string) (*compute.UrlMap, error) + DeleteRegionBackendService(project, region, name string) error + CreateRegionBackendService(project, region string, b *compute.BackendService) error + ListRegionBackendServices(project, region, name string, opts ...ListCallOption) ([]*compute.BackendService, error) + GetRegionBackendService(project, region, name string) (*compute.BackendService, error) + DeleteRegionHealthCheck(project, region, name string) error + CreateRegionHealthCheck(project, region string, h *compute.HealthCheck) error + ListRegionHealthChecks(project, region, name string, opts ...ListCallOption) ([]*compute.HealthCheck, error) + GetRegionHealthCheck(project, region, name string) (*compute.HealthCheck, error) + DeleteRegionNetworkEndpointGroup(project, region, name string) error + CreateRegionNetworkEndpointGroup(project, region string, n *compute.NetworkEndpointGroup) error + ListRegionNetworkEndpointGroups(project, region, name string, opts ...ListCallOption) ([]*compute.NetworkEndpointGroup, error) + GetRegionNetworkEndpointGroup(project, region, name string) (*compute.NetworkEndpointGroup, error) Retry(f func(opts ...googleapi.CallOption) (*compute.Operation, error), opts ...googleapi.CallOption) (op *compute.Operation, err error) RetryBeta(f func(opts ...googleapi.CallOption) (*computeBeta.Operation, error), opts ...googleapi.CallOption) (op *computeBeta.Operation, err error) @@ -616,6 +637,301 @@ func (c *client) CreateImageAlpha(project string, i *computeAlpha.Image) error { return nil } +// DeleteRegionTargetHTTPProxy deletes a GCE RegionTargetHTTPProxy. +func (c *client) DeleteRegionTargetHTTPProxy(project, region, name string) error { + op, err := c.Retry(c.raw.RegionTargetHttpProxies.Delete(project, region, name).Do) + if err != nil { + return err + } + return c.i.regionOperationsWait(project, region, op.Name) +} + +// CreateRegionTargetHTTPProxy creates a GCE RegionTargetHTTPProxy. +func (c *client) CreateRegionTargetHTTPProxy(project, region string, p *compute.TargetHttpProxy) error { + op, err := c.Retry(c.raw.RegionTargetHttpProxies.Insert(project, region, p).Do) + if err != nil { + return err + } + if err := c.i.regionOperationsWait(project, region, op.Name); err != nil { + return err + } + var createdRegionTargetHTTPProxy *compute.TargetHttpProxy + if createdRegionTargetHTTPProxy, err = c.i.GetRegionTargetHTTPProxy(project, region, p.Name); err != nil { + return err + } + *p = *createdRegionTargetHTTPProxy + return nil +} + +// GetRegionTargetHTTPProxy gets a GCE RegionTargetHTTPProxy. +func (c *client) GetRegionTargetHTTPProxy(project, region, name string) (*compute.TargetHttpProxy, error) { + i, err := c.raw.RegionTargetHttpProxies.Get(project, region, name).Do() + if shouldRetryWithWait(c.hc.Transport, err, 2) { + return c.raw.RegionTargetHttpProxies.Get(project, region, name).Do() + } + return i, err +} + +// ListRegionTargetHTTPProxies lists GCE RegionTargetHTTPProxies. +func (c *client) ListRegionTargetHTTPProxies(project, region, name string, opts ...ListCallOption) ([]*compute.TargetHttpProxy, error) { + var is []*compute.TargetHttpProxy + var pt string + call := c.raw.RegionTargetHttpProxies.List(project, region) + for _, opt := range opts { + call = opt.listCallOptionApply(call).(*compute.RegionTargetHttpProxiesListCall) + } + for il, err := call.PageToken(pt).Do(); ; il, err = call.PageToken(pt).Do() { + if shouldRetryWithWait(c.hc.Transport, err, 2) { + il, err = call.PageToken(pt).Do() + } + if err != nil { + return nil, err + } + is = append(is, il.Items...) + + if il.NextPageToken == "" { + return is, nil + } + pt = il.NextPageToken + } +} + +// DeleteRegionBackendService deletes a GCE RegionBackendService. +func (c *client) DeleteRegionBackendService(project, region, name string) error { + op, err := c.Retry(c.raw.RegionBackendServices.Delete(project, region, name).Do) + if err != nil { + return err + } + return c.i.regionOperationsWait(project, region, op.Name) +} + +// CreateRegionBackendService creates a GCE RegionBackendService. +func (c *client) CreateRegionBackendService(project, region string, p *compute.BackendService) error { + op, err := c.Retry(c.raw.RegionBackendServices.Insert(project, region, p).Do) + if err != nil { + return err + } + if err := c.i.regionOperationsWait(project, region, op.Name); err != nil { + return err + } + var createdRegionBackendService *compute.BackendService + if createdRegionBackendService, err = c.i.GetRegionBackendService(project, region, p.Name); err != nil { + return err + } + *p = *createdRegionBackendService + return nil +} + +// GetRegionBackendService gets a GCE RegionBackendService. +func (c *client) GetRegionBackendService(project, region, name string) (*compute.BackendService, error) { + i, err := c.raw.RegionBackendServices.Get(project, region, name).Do() + if shouldRetryWithWait(c.hc.Transport, err, 2) { + return c.raw.RegionBackendServices.Get(project, region, name).Do() + } + return i, err +} + +// ListRegionBackendServices lists GCE RegionBackendServices. +func (c *client) ListRegionBackendServices(project, region, name string, opts ...ListCallOption) ([]*compute.BackendService, error) { + var is []*compute.BackendService + var pt string + call := c.raw.RegionBackendServices.List(project, region) + for _, opt := range opts { + call = opt.listCallOptionApply(call).(*compute.RegionBackendServicesListCall) + } + for il, err := call.PageToken(pt).Do(); ; il, err = call.PageToken(pt).Do() { + if shouldRetryWithWait(c.hc.Transport, err, 2) { + il, err = call.PageToken(pt).Do() + } + if err != nil { + return nil, err + } + is = append(is, il.Items...) + + if il.NextPageToken == "" { + return is, nil + } + pt = il.NextPageToken + } +} + +// DeleteRegionURLMap deletes a GCE RegionURLMap. +func (c *client) DeleteRegionURLMap(project, region, name string) error { + op, err := c.Retry(c.raw.RegionUrlMaps.Delete(project, region, name).Do) + if err != nil { + return err + } + return c.i.regionOperationsWait(project, region, op.Name) +} + +// CreateRegionURLMap creates a GCE RegionURLMap. +func (c *client) CreateRegionURLMap(project, region string, p *compute.UrlMap) error { + op, err := c.Retry(c.raw.RegionUrlMaps.Insert(project, region, p).Do) + if err != nil { + return err + } + if err := c.i.regionOperationsWait(project, region, op.Name); err != nil { + return err + } + var createdRegionURLMap *compute.UrlMap + if createdRegionURLMap, err = c.i.GetRegionURLMap(project, region, p.Name); err != nil { + return err + } + *p = *createdRegionURLMap + return nil +} + +// GetRegionURLMap gets a GCE RegionURLMap. +func (c *client) GetRegionURLMap(project, region, name string) (*compute.UrlMap, error) { + i, err := c.raw.RegionUrlMaps.Get(project, region, name).Do() + if shouldRetryWithWait(c.hc.Transport, err, 2) { + return c.raw.RegionUrlMaps.Get(project, region, name).Do() + } + return i, err +} + +// ListRegionURLMaps lists GCE RegionURLMaps. +func (c *client) ListRegionURLMaps(project, region, name string, opts ...ListCallOption) ([]*compute.UrlMap, error) { + var is []*compute.UrlMap + var pt string + call := c.raw.RegionUrlMaps.List(project, region) + for _, opt := range opts { + call = opt.listCallOptionApply(call).(*compute.RegionUrlMapsListCall) + } + for il, err := call.PageToken(pt).Do(); ; il, err = call.PageToken(pt).Do() { + if shouldRetryWithWait(c.hc.Transport, err, 2) { + il, err = call.PageToken(pt).Do() + } + if err != nil { + return nil, err + } + is = append(is, il.Items...) + + if il.NextPageToken == "" { + return is, nil + } + pt = il.NextPageToken + } +} + +// DeleteRegionHealthCheck deletes a GCE RegionHealthCheck. +func (c *client) DeleteRegionHealthCheck(project, region, name string) error { + op, err := c.Retry(c.raw.RegionHealthChecks.Delete(project, region, name).Do) + if err != nil { + return err + } + return c.i.regionOperationsWait(project, region, op.Name) +} + +// CreateRegionHealthCheck creates a GCE RegionHealthCheck. +func (c *client) CreateRegionHealthCheck(project, region string, p *compute.HealthCheck) error { + op, err := c.Retry(c.raw.RegionHealthChecks.Insert(project, region, p).Do) + if err != nil { + return err + } + if err := c.i.regionOperationsWait(project, region, op.Name); err != nil { + return err + } + var createdRegionHealthCheck *compute.HealthCheck + if createdRegionHealthCheck, err = c.i.GetRegionHealthCheck(project, region, p.Name); err != nil { + return err + } + *p = *createdRegionHealthCheck + return nil +} + +// GetRegionHealthCheck gets a GCE RegionHealthCheck. +func (c *client) GetRegionHealthCheck(project, region, name string) (*compute.HealthCheck, error) { + i, err := c.raw.RegionHealthChecks.Get(project, region, name).Do() + if shouldRetryWithWait(c.hc.Transport, err, 2) { + return c.raw.RegionHealthChecks.Get(project, region, name).Do() + } + return i, err +} + +// ListRegionHealthChecks lists GCE RegionHealthChecks. +func (c *client) ListRegionHealthChecks(project, region, name string, opts ...ListCallOption) ([]*compute.HealthCheck, error) { + var is []*compute.HealthCheck + var pt string + call := c.raw.RegionHealthChecks.List(project, region) + for _, opt := range opts { + call = opt.listCallOptionApply(call).(*compute.RegionHealthChecksListCall) + } + for il, err := call.PageToken(pt).Do(); ; il, err = call.PageToken(pt).Do() { + if shouldRetryWithWait(c.hc.Transport, err, 2) { + il, err = call.PageToken(pt).Do() + } + if err != nil { + return nil, err + } + is = append(is, il.Items...) + + if il.NextPageToken == "" { + return is, nil + } + pt = il.NextPageToken + } +} + +// DeleteRegionNetworkEndpointGroup deletes a GCE RegionNetworkEndpointGroup. +func (c *client) DeleteRegionNetworkEndpointGroup(project, region, name string) error { + op, err := c.Retry(c.raw.RegionNetworkEndpointGroups.Delete(project, region, name).Do) + if err != nil { + return err + } + return c.i.regionOperationsWait(project, region, op.Name) +} + +// CreateRegionNetworkEndpointGroup creates a GCE RegionNetworkEndpointGroup. +func (c *client) CreateRegionNetworkEndpointGroup(project, region string, p *compute.NetworkEndpointGroup) error { + op, err := c.Retry(c.raw.RegionNetworkEndpointGroups.Insert(project, region, p).Do) + if err != nil { + return err + } + if err := c.i.regionOperationsWait(project, region, op.Name); err != nil { + return err + } + var createdRegionNetworkEndpointGroup *compute.NetworkEndpointGroup + if createdRegionNetworkEndpointGroup, err = c.i.GetRegionNetworkEndpointGroup(project, region, p.Name); err != nil { + return err + } + *p = *createdRegionNetworkEndpointGroup + return nil +} + +// GetRegionNetworkEndpointGroup gets a GCE RegionNetworkEndpointGroup. +func (c *client) GetRegionNetworkEndpointGroup(project, region, name string) (*compute.NetworkEndpointGroup, error) { + i, err := c.raw.RegionNetworkEndpointGroups.Get(project, region, name).Do() + if shouldRetryWithWait(c.hc.Transport, err, 2) { + return c.raw.RegionNetworkEndpointGroups.Get(project, region, name).Do() + } + return i, err +} + +// ListRegionNetworkEndpointGroups lists GCE RegionNetworkEndpointGroups. +func (c *client) ListRegionNetworkEndpointGroups(project, region, name string, opts ...ListCallOption) ([]*compute.NetworkEndpointGroup, error) { + var is []*compute.NetworkEndpointGroup + var pt string + call := c.raw.RegionNetworkEndpointGroups.List(project, region) + for _, opt := range opts { + call = opt.listCallOptionApply(call).(*compute.RegionNetworkEndpointGroupsListCall) + } + for il, err := call.PageToken(pt).Do(); ; il, err = call.PageToken(pt).Do() { + if shouldRetryWithWait(c.hc.Transport, err, 2) { + il, err = call.PageToken(pt).Do() + } + if err != nil { + return nil, err + } + is = append(is, il.Items...) + + if il.NextPageToken == "" { + return is, nil + } + pt = il.NextPageToken + } +} + func (c *client) CreateInstance(project, zone string, i *compute.Instance) error { op, err := c.Retry(c.raw.Instances.Insert(project, zone, i).Do) if err != nil { @@ -1126,6 +1442,31 @@ func (c *client) GetForwardingRule(project, region, name string) (*compute.Forwa return n, err } +// AggregatedListForwardingRules gets an aggregated list of GCE ForwardingRules. +func (c *client) AggregatedListForwardingRules(project string, opts ...ListCallOption) ([]*compute.ForwardingRule, error) { + var frs []*compute.ForwardingRule + var pt string + call := c.raw.ForwardingRules.AggregatedList(project) + for _, opt := range opts { + call = opt.listCallOptionApply(call).(*compute.ForwardingRulesAggregatedListCall) + } + for ail, err := call.PageToken(pt).Do(); ; ail, err = call.PageToken(pt).Do() { + if shouldRetryWithWait(c.hc.Transport, err, 2) { + ail, err = call.PageToken(pt).Do() + } + if err != nil { + return nil, err + } + for _, frl := range ail.Items { + frs = append(frs, frl.ForwardingRules...) + } + if ail.NextPageToken == "" { + return frs, nil + } + pt = ail.NextPageToken + } +} + // ListForwardingRules gets a list of GCE ForwardingRules. func (c *client) ListForwardingRules(project, region string, opts ...ListCallOption) ([]*compute.ForwardingRule, error) { var frs []*compute.ForwardingRule diff --git a/compute/compute_test.go b/compute/compute_test.go index 7f05a99a..33a506b1 100644 --- a/compute/compute_test.go +++ b/compute/compute_test.go @@ -31,24 +31,29 @@ import ( ) var ( - testProject = "test-project" - testZone = "test-zone" - testRegion = "test-region" - testDisk = "test-disk" - testDisk2 = "test-disk2" - testResize int64 = 128 - testForwardingRule = "test-forwarding-rule" - testFirewallRule = "test-firewall-rule" - testImage = "test-image" - testImageAlpha = "test-image-alpha" - testImageBeta = "test-image-beta" - testMachineImage = "test-machine-image" - testInstance = "test-instance" - testInstanceAlpha = "test-instance-alpha" - testInstanceBeta = "test-instance-beta" - testNetwork = "test-network" - testSubnetwork = "test-subnetwork" - testTargetInstance = "test-target-instance" + testProject = "test-project" + testZone = "test-zone" + testRegion = "test-region" + testDisk = "test-disk" + testDisk2 = "test-disk2" + testResize int64 = 128 + testForwardingRule = "test-forwarding-rule" + testFirewallRule = "test-firewall-rule" + testImage = "test-image" + testImageAlpha = "test-image-alpha" + testImageBeta = "test-image-beta" + testMachineImage = "test-machine-image" + testInstance = "test-instance" + testInstanceAlpha = "test-instance-alpha" + testInstanceBeta = "test-instance-beta" + testNetwork = "test-network" + testSubnetwork = "test-subnetwork" + testTargetInstance = "test-target-instance" + testTargetHTTPProxy = "test-target-http-proxy" + testURLMap = "test-url-map" + testBackendService = "test-backend-service" + testHealthCheck = "test-health-check" + testNetworkEndpointGroup = "test-network-endpoint-group" ) func TestShouldRetryWithWait(t *testing.T) { @@ -135,6 +140,11 @@ func TestCreates(t *testing.T) { n := &compute.Network{Name: testNetwork} sn := &compute.Subnetwork{Name: testSubnetwork} ti := &compute.TargetInstance{Name: testTargetInstance} + hp := &compute.TargetHttpProxy{Name: testTargetHTTPProxy} + um := &compute.UrlMap{Name: testURLMap} + bs := &compute.BackendService{Name: testBackendService} + hc := &compute.HealthCheck{Name: testHealthCheck} + neg := &compute.NetworkEndpointGroup{Name: testNetworkEndpointGroup} creates := []struct { name string do func() error @@ -245,6 +255,46 @@ func TestCreates(t *testing.T) { &compute.TargetInstance{Name: testTargetInstance}, ti, }, + { + "regionTargetHttpProxies", + func() error { return c.CreateRegionTargetHTTPProxy(testProject, testRegion, hp) }, + fmt.Sprintf("/%s/regions/%s/targetHttpProxies/%s?alt=json&prettyPrint=false", testProject, testRegion, testTargetHTTPProxy), + fmt.Sprintf("/%s/regions/%s/targetHttpProxies?alt=json&prettyPrint=false", testProject, testRegion), + &compute.TargetHttpProxy{Name: testTargetHTTPProxy}, + hp, + }, + { + "regionUrlMaps", + func() error { return c.CreateRegionURLMap(testProject, testZone, um) }, + fmt.Sprintf("/%s/zones/%s/urlMaps/%s?alt=json&prettyPrint=false", testProject, testZone, testURLMap), + fmt.Sprintf("/%s/zones/%s/urlMaps?alt=json&prettyPrint=false", testProject, testZone), + &compute.UrlMap{Name: testURLMap}, + um, + }, + { + "regionBackendServices", + func() error { return c.CreateRegionBackendService(testProject, testZone, bs) }, + fmt.Sprintf("/%s/zones/%s/backendServices/%s?alt=json&prettyPrint=false", testProject, testZone, testBackendService), + fmt.Sprintf("/%s/zones/%s/backendServices?alt=json&prettyPrint=false", testProject, testZone), + &compute.BackendService{Name: testBackendService}, + bs, + }, + { + "regionHealthChecks", + func() error { return c.CreateRegionHealthCheck(testProject, testZone, hc) }, + fmt.Sprintf("/%s/zones/%s/healthChecks/%s?alt=json&prettyPrint=false", testProject, testZone, testHealthCheck), + fmt.Sprintf("/%s/zones/%s/healthChecks?alt=json&prettyPrint=false", testProject, testZone), + &compute.HealthCheck{Name: testHealthCheck}, + hc, + }, + { + "regionNetworkEndpointGroups", + func() error { return c.CreateRegionNetworkEndpointGroup(testProject, testRegion, neg) }, + fmt.Sprintf("/%s/regions/%s/networkEndpointGroups/%s?alt=json&prettyPrint=false", testProject, testRegion, testNetworkEndpointGroup), + fmt.Sprintf("/%s/regions/%s/networkEndpointGroups?alt=json&prettyPrint=false", testProject, testRegion), + &compute.NetworkEndpointGroup{Name: testNetworkEndpointGroup}, + neg, + }, } for _, create := range creates { @@ -392,6 +442,38 @@ func TestDeletes(t *testing.T) { fmt.Sprintf("/projects/%s/zones/%s/targetInstances/%s?alt=json&prettyPrint=false", testProject, testZone, testTargetInstance), fmt.Sprintf("/projects/%s/zones/%s/operations//wait?alt=json&prettyPrint=false", testProject, testZone), }, + { + "regionTargetHttpProxies", + func() error { return c.DeleteRegionTargetHTTPProxy(testProject, testRegion, testTargetHTTPProxy) }, + fmt.Sprintf("/projects/%s/regions/%s/targetHttpProxies/%s?alt=json&prettyPrint=false", testProject, testRegion, testTargetHTTPProxy), + fmt.Sprintf("/projects/%s/regions/%s/operations//wait?alt=json&prettyPrint=false", testProject, testRegion), + }, + { + "regionUrlMaps", + func() error { return c.DeleteRegionURLMap(testProject, testRegion, testURLMap) }, + fmt.Sprintf("/projects/%s/regions/%s/urlMaps/%s?alt=json&prettyPrint=false", testProject, testRegion, testURLMap), + fmt.Sprintf("/projects/%s/regions/%s/operations//wait?alt=json&prettyPrint=false", testProject, testRegion), + }, + { + "regionBackendServices", + func() error { return c.DeleteRegionBackendService(testProject, testRegion, testBackendService) }, + fmt.Sprintf("/projects/%s/regions/%s/backendServices/%s?alt=json&prettyPrint=false", testProject, testRegion, testBackendService), + fmt.Sprintf("/projects/%s/regions/%s/operations//wait?alt=json&prettyPrint=false", testProject, testRegion), + }, + { + "regionHealthChecks", + func() error { return c.DeleteRegionHealthCheck(testProject, testRegion, testHealthCheck) }, + fmt.Sprintf("/projects/%s/regions/%s/healthChecks/%s?alt=json&prettyPrint=false", testProject, testRegion, testHealthCheck), + fmt.Sprintf("/projects/%s/regions/%s/operations//wait?alt=json&prettyPrint=false", testProject, testRegion), + }, + { + "regionNetworkEndpointGroups", + func() error { + return c.DeleteRegionNetworkEndpointGroup(testProject, testRegion, testNetworkEndpointGroup) + }, + fmt.Sprintf("/projects/%s/regions/%s/networkEndpointGroups/%s?alt=json&prettyPrint=false", testProject, testRegion, testNetworkEndpointGroup), + fmt.Sprintf("/projects/%s/regions/%s/operations//wait?alt=json&prettyPrint=false", testProject, testRegion), + }, } for _, d := range deletes { diff --git a/compute/test_client.go b/compute/test_client.go index 5ef5b996..afb105dd 100644 --- a/compute/test_client.go +++ b/compute/test_client.go @@ -49,71 +49,92 @@ func NewTestClient(handleFunc http.HandlerFunc) (*httptest.Server, *TestClient, type TestClient struct { client - AttachDiskFn func(project, zone, instance string, d *compute.AttachedDisk) error - DetachDiskFn func(project, zone, instance, disk string) error - CreateDiskFn func(project, zone string, d *compute.Disk) error - CreateForwardingRuleFn func(project, region string, fr *compute.ForwardingRule) error - CreateFirewallRuleFn func(project string, i *compute.Firewall) error - CreateImageFn func(project string, i *compute.Image) error - CreateInstanceFn func(project, zone string, i *compute.Instance) error - CreateNetworkFn func(project string, n *compute.Network) error - CreateSnapshotFn func(project, zone, disk string, s *compute.Snapshot) error - CreateSubnetworkFn func(project, region string, n *compute.Subnetwork) error - CreateTargetInstanceFn func(project, zone string, ti *compute.TargetInstance) error - StartInstanceFn func(project, zone, name string) error - StopInstanceFn func(project, zone, name string) error - DeleteDiskFn func(project, zone, name string) error - DeleteForwardingRuleFn func(project, region, name string) error - DeleteFirewallRuleFn func(project, name string) error - DeleteImageFn func(project, name string) error - DeleteInstanceFn func(project, zone, name string) error - DeleteNetworkFn func(project, name string) error - DeleteSubnetworkFn func(project, region, name string) error - DeleteTargetInstanceFn func(project, zone, name string) error - DeprecateImageFn func(project, name string, deprecationstatus *compute.DeprecationStatus) error - GetMachineTypeFn func(project, zone, machineType string) (*compute.MachineType, error) - ListMachineTypesFn func(project, zone string, opts ...ListCallOption) ([]*compute.MachineType, error) - GetProjectFn func(project string) (*compute.Project, error) - GetSerialPortOutputFn func(project, zone, name string, port, start int64) (*compute.SerialPortOutput, error) - GetGuestAttributesFn func(project, zone, name, queryPath, variableKey string) (*compute.GuestAttributes, error) - GetZoneFn func(project, zone string) (*compute.Zone, error) - ListZonesFn func(project string, opts ...ListCallOption) ([]*compute.Zone, error) - GetInstanceFn func(project, zone, name string) (*compute.Instance, error) - AggregatedListInstancesFn func(project string, opts ...ListCallOption) ([]*compute.Instance, error) - ListInstancesFn func(project, zone string, opts ...ListCallOption) ([]*compute.Instance, error) - ListSnapshotsFn func(project string, opts ...ListCallOption) ([]*compute.Snapshot, error) - GetSnapshotFn func(project, name string) (*compute.Snapshot, error) - DeleteSnapshotFn func(project, name string) error - GetDiskFn func(project, zone, name string) (*compute.Disk, error) - AggregatedListDisksFn func(project string, opts ...ListCallOption) ([]*compute.Disk, error) - ListDisksFn func(project, zone string, opts ...ListCallOption) ([]*compute.Disk, error) - GetForwardingRuleFn func(project, region, name string) (*compute.ForwardingRule, error) - ListForwardingRulesFn func(project, region string, opts ...ListCallOption) ([]*compute.ForwardingRule, error) - GetFirewallRuleFn func(project, name string) (*compute.Firewall, error) - ListFirewallRulesFn func(project string, opts ...ListCallOption) ([]*compute.Firewall, error) - GetImageFn func(project, name string) (*compute.Image, error) - GetImageFromFamilyFn func(project, family string) (*compute.Image, error) - ListImagesFn func(project string, opts ...ListCallOption) ([]*compute.Image, error) - GetLicenseFn func(project, name string) (*compute.License, error) - ListLicensesFn func(project string, opts ...ListCallOption) ([]*compute.License, error) - GetNetworkFn func(project, name string) (*compute.Network, error) - GetRegionFn func(project, name string) (*compute.Region, error) - AggregatedListSubnetworksFn func(project string, opts ...ListCallOption) ([]*compute.Subnetwork, error) - ListNetworksFn func(project string, opts ...ListCallOption) ([]*compute.Network, error) - GetSubnetworkFn func(project, region, name string) (*compute.Subnetwork, error) - ListSubnetworksFn func(project, region string, opts ...ListCallOption) ([]*compute.Subnetwork, error) - GetTargetInstanceFn func(project, zone, name string) (*compute.TargetInstance, error) - ListTargetInstancesFn func(project, zone string, opts ...ListCallOption) ([]*compute.TargetInstance, error) - InstanceStatusFn func(project, zone, name string) (string, error) - InstanceStoppedFn func(project, zone, name string) (bool, error) - ResizeDiskFn func(project, zone, disk string, drr *compute.DisksResizeRequest) error - SetInstanceMetadataFn func(project, zone, name string, md *compute.Metadata) error - SetCommonInstanceMetadataFn func(project string, md *compute.Metadata) error - ListMachineImagesFn func(project string, opts ...ListCallOption) ([]*compute.MachineImage, error) - DeleteMachineImageFn func(project, name string) error - CreateMachineImageFn func(project string, i *compute.MachineImage) error - GetMachineImageFn func(project, name string) (*compute.MachineImage, error) - RetryFn func(f func(opts ...googleapi.CallOption) (*compute.Operation, error), opts ...googleapi.CallOption) (op *compute.Operation, err error) + AttachDiskFn func(project, zone, instance string, d *compute.AttachedDisk) error + DetachDiskFn func(project, zone, instance, disk string) error + CreateDiskFn func(project, zone string, d *compute.Disk) error + CreateForwardingRuleFn func(project, region string, fr *compute.ForwardingRule) error + CreateFirewallRuleFn func(project string, i *compute.Firewall) error + CreateImageFn func(project string, i *compute.Image) error + CreateInstanceFn func(project, zone string, i *compute.Instance) error + CreateNetworkFn func(project string, n *compute.Network) error + CreateSnapshotFn func(project, zone, disk string, s *compute.Snapshot) error + CreateSubnetworkFn func(project, region string, n *compute.Subnetwork) error + CreateTargetInstanceFn func(project, zone string, ti *compute.TargetInstance) error + StartInstanceFn func(project, zone, name string) error + StopInstanceFn func(project, zone, name string) error + DeleteDiskFn func(project, zone, name string) error + DeleteForwardingRuleFn func(project, region, name string) error + DeleteFirewallRuleFn func(project, name string) error + DeleteImageFn func(project, name string) error + DeleteInstanceFn func(project, zone, name string) error + DeleteNetworkFn func(project, name string) error + DeleteSubnetworkFn func(project, region, name string) error + DeleteTargetInstanceFn func(project, zone, name string) error + DeprecateImageFn func(project, name string, deprecationstatus *compute.DeprecationStatus) error + GetMachineTypeFn func(project, zone, machineType string) (*compute.MachineType, error) + ListMachineTypesFn func(project, zone string, opts ...ListCallOption) ([]*compute.MachineType, error) + GetProjectFn func(project string) (*compute.Project, error) + GetSerialPortOutputFn func(project, zone, name string, port, start int64) (*compute.SerialPortOutput, error) + GetGuestAttributesFn func(project, zone, name, queryPath, variableKey string) (*compute.GuestAttributes, error) + GetZoneFn func(project, zone string) (*compute.Zone, error) + ListZonesFn func(project string, opts ...ListCallOption) ([]*compute.Zone, error) + GetInstanceFn func(project, zone, name string) (*compute.Instance, error) + AggregatedListInstancesFn func(project string, opts ...ListCallOption) ([]*compute.Instance, error) + ListInstancesFn func(project, zone string, opts ...ListCallOption) ([]*compute.Instance, error) + ListSnapshotsFn func(project string, opts ...ListCallOption) ([]*compute.Snapshot, error) + GetSnapshotFn func(project, name string) (*compute.Snapshot, error) + DeleteSnapshotFn func(project, name string) error + GetDiskFn func(project, zone, name string) (*compute.Disk, error) + AggregatedListDisksFn func(project string, opts ...ListCallOption) ([]*compute.Disk, error) + ListDisksFn func(project, zone string, opts ...ListCallOption) ([]*compute.Disk, error) + GetForwardingRuleFn func(project, region, name string) (*compute.ForwardingRule, error) + AggregatedListForwardingRulesFn func(project string, opts ...ListCallOption) ([]*compute.ForwardingRule, error) + ListForwardingRulesFn func(project, region string, opts ...ListCallOption) ([]*compute.ForwardingRule, error) + GetFirewallRuleFn func(project, name string) (*compute.Firewall, error) + ListFirewallRulesFn func(project string, opts ...ListCallOption) ([]*compute.Firewall, error) + GetImageFn func(project, name string) (*compute.Image, error) + GetImageFromFamilyFn func(project, family string) (*compute.Image, error) + ListImagesFn func(project string, opts ...ListCallOption) ([]*compute.Image, error) + GetLicenseFn func(project, name string) (*compute.License, error) + ListLicensesFn func(project string, opts ...ListCallOption) ([]*compute.License, error) + GetNetworkFn func(project, name string) (*compute.Network, error) + GetRegionFn func(project, name string) (*compute.Region, error) + AggregatedListSubnetworksFn func(project string, opts ...ListCallOption) ([]*compute.Subnetwork, error) + ListNetworksFn func(project string, opts ...ListCallOption) ([]*compute.Network, error) + GetSubnetworkFn func(project, region, name string) (*compute.Subnetwork, error) + ListSubnetworksFn func(project, region string, opts ...ListCallOption) ([]*compute.Subnetwork, error) + GetTargetInstanceFn func(project, zone, name string) (*compute.TargetInstance, error) + ListTargetInstancesFn func(project, zone string, opts ...ListCallOption) ([]*compute.TargetInstance, error) + InstanceStatusFn func(project, zone, name string) (string, error) + InstanceStoppedFn func(project, zone, name string) (bool, error) + ResizeDiskFn func(project, zone, disk string, drr *compute.DisksResizeRequest) error + SetInstanceMetadataFn func(project, zone, name string, md *compute.Metadata) error + SetCommonInstanceMetadataFn func(project string, md *compute.Metadata) error + ListMachineImagesFn func(project string, opts ...ListCallOption) ([]*compute.MachineImage, error) + DeleteMachineImageFn func(project, name string) error + CreateMachineImageFn func(project string, i *compute.MachineImage) error + GetMachineImageFn func(project, name string) (*compute.MachineImage, error) + RetryFn func(f func(opts ...googleapi.CallOption) (*compute.Operation, error), opts ...googleapi.CallOption) (op *compute.Operation, err error) + DeleteRegionTargetHTTPProxyFn func(project, region, name string) error + CreateRegionTargetHTTPProxyFn func(project, region string, p *compute.TargetHttpProxy) error + ListRegionTargetHTTPProxiesFn func(project, region, name string, opts ...ListCallOption) ([]*compute.TargetHttpProxy, error) + GetRegionTargetHTTPProxyFn func(project, region, name string) (*compute.TargetHttpProxy, error) + DeleteRegionURLMapFn func(project, region, name string) error + CreateRegionURLMapFn func(project, region string, u *compute.UrlMap) error + ListRegionURLMapsFn func(project, region, name string, opts ...ListCallOption) ([]*compute.UrlMap, error) + GetRegionURLMapFn func(project, region, name string) (*compute.UrlMap, error) + DeleteRegionBackendServiceFn func(project, region, name string) error + CreateRegionBackendServiceFn func(project, region string, b *compute.BackendService) error + ListRegionBackendServicesFn func(project, region, name string, opts ...ListCallOption) ([]*compute.BackendService, error) + GetRegionBackendServiceFn func(project, region, name string) (*compute.BackendService, error) + DeleteRegionHealthCheckFn func(project, region, name string) error + CreateRegionHealthCheckFn func(project, region string, h *compute.HealthCheck) error + ListRegionHealthChecksFn func(project, region, name string, opts ...ListCallOption) ([]*compute.HealthCheck, error) + GetRegionHealthCheckFn func(project, region, name string) (*compute.HealthCheck, error) + DeleteRegionNetworkEndpointGroupFn func(project, region, name string) error + CreateRegionNetworkEndpointGroupFn func(project, region string, n *compute.NetworkEndpointGroup) error + ListRegionNetworkEndpointGroupsFn func(project, region, name string, opts ...ListCallOption) ([]*compute.NetworkEndpointGroup, error) + GetRegionNetworkEndpointGroupFn func(project, region, name string) (*compute.NetworkEndpointGroup, error) // Alpha API calls CreateInstanceAlphaFn func(project, zone string, i *computeAlpha.Instance) error @@ -438,6 +459,14 @@ func (c *TestClient) ListForwardingRules(project, region string, opts ...ListCal return c.client.ListForwardingRules(project, region, opts...) } +// AggregatedListForwardingRules uses the override method ListForwardingRulesFn or the real implementation. +func (c *TestClient) AggregatedListForwardingRules(project string, opts ...ListCallOption) ([]*compute.ForwardingRule, error) { + if c.AggregatedListForwardingRulesFn != nil { + return c.AggregatedListForwardingRulesFn(project, opts...) + } + return c.client.AggregatedListForwardingRules(project, opts...) +} + // GetFirewallRule uses the override method GetFirewallRuleFn or the real implementation. func (c *TestClient) GetFirewallRule(project, name string) (*compute.Firewall, error) { if c.GetFirewallRuleFn != nil { @@ -685,3 +714,163 @@ func (c *TestClient) CreateInstanceAlpha(project, zone string, i *computeAlpha.I } return c.client.CreateInstanceAlpha(project, zone, i) } + +// DeleteRegionTargetHTTPProxy uses the override method DeleteRegionTargetHttpProxiesFn or the real implementation. +func (c *TestClient) DeleteRegionTargetHTTPProxy(project, region, name string) error { + if c.DeleteRegionTargetHTTPProxyFn != nil { + return c.DeleteRegionTargetHTTPProxyFn(project, region, name) + } + return c.client.DeleteRegionTargetHTTPProxy(project, region, name) +} + +// CreateRegionTargetHTTPProxy uses the override method CreateRegionTargetHTTPProxyFn or the real implementation. +func (c *TestClient) CreateRegionTargetHTTPProxy(project, region string, p *compute.TargetHttpProxy) error { + if c.CreateRegionTargetHTTPProxyFn != nil { + return c.CreateRegionTargetHTTPProxyFn(project, region, p) + } + return c.client.CreateRegionTargetHTTPProxy(project, region, p) +} + +// ListRegionTargetHTTPProxies uses the override method ListRegionTargetHttpProxiesFn or the real implementation. +func (c *TestClient) ListRegionTargetHTTPProxies(project, region, name string, opts ...ListCallOption) ([]*compute.TargetHttpProxy, error) { + if c.ListRegionTargetHTTPProxiesFn != nil { + return c.ListRegionTargetHTTPProxiesFn(project, region, name, opts...) + } + return c.client.ListRegionTargetHTTPProxies(project, region, name, opts...) +} + +// GetRegionTargetHTTPProxy uses the override method GetRegionTargetHTTPProxyFn or the real implementation. +func (c *TestClient) GetRegionTargetHTTPProxy(project, region, name string) (*compute.TargetHttpProxy, error) { + if c.GetRegionTargetHTTPProxyFn != nil { + return c.GetRegionTargetHTTPProxyFn(project, region, name) + } + return c.client.GetRegionTargetHTTPProxy(project, region, name) +} + +// DeleteRegionURLMap uses the override method DeleteRegionURLMapsFn or the real implementation. +func (c *TestClient) DeleteRegionURLMap(project, region, name string) error { + if c.DeleteRegionURLMapFn != nil { + return c.DeleteRegionURLMapFn(project, region, name) + } + return c.client.DeleteRegionURLMap(project, region, name) +} + +// CreateRegionURLMap uses the override method CreateRegionURLMapFn or the real implementation. +func (c *TestClient) CreateRegionURLMap(project, region string, p *compute.UrlMap) error { + if c.CreateRegionURLMapFn != nil { + return c.CreateRegionURLMapFn(project, region, p) + } + return c.client.CreateRegionURLMap(project, region, p) +} + +// ListRegionURLMaps uses the override method ListRegionURLMapsFn or the real implementation. +func (c *TestClient) ListRegionURLMaps(project, region, name string, opts ...ListCallOption) ([]*compute.UrlMap, error) { + if c.ListRegionURLMapsFn != nil { + return c.ListRegionURLMapsFn(project, region, name, opts...) + } + return c.client.ListRegionURLMaps(project, region, name, opts...) +} + +// GetRegionURLMap uses the override method GetRegionURLMapFn or the real implementation. +func (c *TestClient) GetRegionURLMap(project, region, name string) (*compute.UrlMap, error) { + if c.GetRegionURLMapFn != nil { + return c.GetRegionURLMapFn(project, region, name) + } + return c.client.GetRegionURLMap(project, region, name) +} + +// DeleteRegionBackendService uses the override method DeleteRegionBackendServicesFn or the real implementation. +func (c *TestClient) DeleteRegionBackendService(project, region, name string) error { + if c.DeleteRegionBackendServiceFn != nil { + return c.DeleteRegionBackendServiceFn(project, region, name) + } + return c.client.DeleteRegionBackendService(project, region, name) +} + +// CreateRegionBackendService uses the override method CreateRegionBackendServiceFn or the real implementation. +func (c *TestClient) CreateRegionBackendService(project, region string, b *compute.BackendService) error { + if c.CreateRegionBackendServiceFn != nil { + return c.CreateRegionBackendServiceFn(project, region, b) + } + return c.client.CreateRegionBackendService(project, region, b) +} + +// ListRegionBackendServices uses the override method ListRegionBackendServicesFn or the real implementation. +func (c *TestClient) ListRegionBackendServices(project, region, name string, opts ...ListCallOption) ([]*compute.BackendService, error) { + if c.ListRegionBackendServicesFn != nil { + return c.ListRegionBackendServicesFn(project, region, name, opts...) + } + return c.client.ListRegionBackendServices(project, region, name, opts...) +} + +// GetRegionBackendService uses the override method GetRegionBackendServiceFn or the real implementation. +func (c *TestClient) GetRegionBackendService(project, region, name string) (*compute.BackendService, error) { + if c.GetRegionBackendServiceFn != nil { + return c.GetRegionBackendServiceFn(project, region, name) + } + return c.client.GetRegionBackendService(project, region, name) +} + +// DeleteRegionHealthCheck uses the override method DeleteRegionHealthCheckFn or the real implementation. +func (c *TestClient) DeleteRegionHealthCheck(project, region, name string) error { + if c.DeleteRegionHealthCheckFn != nil { + return c.DeleteRegionHealthCheckFn(project, region, name) + } + return c.client.DeleteRegionHealthCheck(project, region, name) +} + +// CreateRegionHealthCheck uses the override method CreateRegionHealthCheckFn or the real implementation. +func (c *TestClient) CreateRegionHealthCheck(project, region string, h *compute.HealthCheck) error { + if c.CreateRegionHealthCheckFn != nil { + return c.CreateRegionHealthCheckFn(project, region, h) + } + return c.client.CreateRegionHealthCheck(project, region, h) +} + +// ListRegionHealthChecks uses the override method ListRegionHealthChecksFn or the real implementation. +func (c *TestClient) ListRegionHealthChecks(project, region, name string, opts ...ListCallOption) ([]*compute.HealthCheck, error) { + if c.ListRegionHealthChecksFn != nil { + return c.ListRegionHealthChecksFn(project, region, name, opts...) + } + return c.client.ListRegionHealthChecks(project, region, name, opts...) +} + +// GetRegionHealthCheck uses the override method GetRegionHealthCheckFn or the real implementation. +func (c *TestClient) GetRegionHealthCheck(project, region, name string) (*compute.HealthCheck, error) { + if c.GetRegionHealthCheckFn != nil { + return c.GetRegionHealthCheckFn(project, region, name) + } + return c.client.GetRegionHealthCheck(project, region, name) +} + +// DeleteRegionNetworkEndpointGroup uses the override method DeleteRegionNetworkEndpointGroupsFn or the real implementation. +func (c *TestClient) DeleteRegionNetworkEndpointGroup(project, region, name string) error { + if c.DeleteRegionNetworkEndpointGroupFn != nil { + return c.DeleteRegionNetworkEndpointGroupFn(project, region, name) + } + return c.client.DeleteRegionNetworkEndpointGroup(project, region, name) +} + +// CreateRegionNetworkEndpointGroup uses the override method CreateRegionNetworkEndpointGroupFn or the real implementation. +func (c *TestClient) CreateRegionNetworkEndpointGroup(project, region string, p *compute.NetworkEndpointGroup) error { + if c.CreateRegionNetworkEndpointGroupFn != nil { + return c.CreateRegionNetworkEndpointGroupFn(project, region, p) + } + return c.client.CreateRegionNetworkEndpointGroup(project, region, p) +} + +// ListRegionNetworkEndpointGroups uses the override method ListRegionNetworkEndpointGroupsFn or the real implementation. +func (c *TestClient) ListRegionNetworkEndpointGroups(project, region, name string, opts ...ListCallOption) ([]*compute.NetworkEndpointGroup, error) { + if c.ListRegionNetworkEndpointGroupsFn != nil { + return c.ListRegionNetworkEndpointGroupsFn(project, region, name, opts...) + } + return c.client.ListRegionNetworkEndpointGroups(project, region, name, opts...) +} + +// GetRegionNetworkEndpointGroup uses the override method GetRegionNetworkEndpointGroupFn or the real implementation. +func (c *TestClient) GetRegionNetworkEndpointGroup(project, region, name string) (*compute.NetworkEndpointGroup, error) { + if c.GetRegionNetworkEndpointGroupFn != nil { + return c.GetRegionNetworkEndpointGroupFn(project, region, name) + } + return c.client.GetRegionNetworkEndpointGroup(project, region, name) +} diff --git a/compute/test_client_test.go b/compute/test_client_test.go index b03f1e54..e4478266 100644 --- a/compute/test_client_test.go +++ b/compute/test_client_test.go @@ -97,6 +97,8 @@ func TestTestClient(t *testing.T) { {"get machine image", func() { c.GetMachineImage("a", "b") }, "/projects/a/global/machineImages/b?alt=json&prettyPrint=false"}, {"list machine images", func() { c.ListMachineImages("a", listOpts...) }, "/projects/a/global/machineImages?alt=json&filter=foo&orderBy=foo&pageToken=&prettyPrint=false"}, {"delete machine image", func() { c.DeleteMachineImage("a", "b") }, "/projects/a/global/machineImages/b?alt=json&prettyPrint=false"}, + {"aggregated list forwarding rule", func() { c.AggregatedListForwardingRules("a", listOpts...) }, "/projects/a/aggregated/forwardingRules?alt=json&pageToken=&prettyPrint=false"}, + {"delete network", func() { c.DeleteNetwork("a", "b") }, "/projects/a/global/networks/b?alt=json&prettyPrint=false"}, } runTests := func() { @@ -218,6 +220,10 @@ func TestTestClient(t *testing.T) { fakeCalled = true return nil, nil } + c.AggregatedListForwardingRulesFn = func(_ string, _ ...ListCallOption) ([]*compute.ForwardingRule, error) { + fakeCalled = true + return nil, nil + } c.DeleteMachineImageFn = func(_, _ string) error { fakeCalled = true; return nil } wantFakeCalled = true wantRealCalled = false