diff --git a/cli/gcp/client/network.go b/cli/gcp/client/network.go index e07ca1f89..ab4c2f4f0 100644 --- a/cli/gcp/client/network.go +++ b/cli/gcp/client/network.go @@ -124,7 +124,7 @@ func (c *Client) TerminateVPCs(ctx context.Context) error { return errors.New("client has firewalls, which must be deleted first") } - if err := c.terminateSubnets(ctx); err != nil { + if err := c.terminateSubnet(ctx); err != nil { return err } @@ -183,23 +183,18 @@ func (c *Client) createSubnet(ctx context.Context, name, cidr, network, secondar return c.subnetworksAPI.Insert(ctx, req) } -func (c *Client) terminateSubnets(ctx context.Context) error { - var op Operation - var err error - if c.subnetwork != "" { - op, err = c.terminateSubnet(ctx, c.subnetwork) - if err != nil { - return err - } +func (c *Client) terminateSubnet(ctx context.Context) error { + if c.subnetwork == "" { + return nil } - return c.waitForOperations(ctx, []Operation{op}) -} - -func (c *Client) terminateSubnet(ctx context.Context, name string) (Operation, error) { req := &computepb.DeleteSubnetworkRequest{ Project: c.project, Region: c.region, - Subnetwork: name, + Subnetwork: c.subnetwork, } - return c.subnetworksAPI.Delete(ctx, req) + op, err := c.subnetworksAPI.Delete(ctx, req) + if err != nil { + return err + } + return c.waitForOperations(ctx, []Operation{op}) } diff --git a/cli/gcp/client/network_test.go b/cli/gcp/client/network_test.go index e32f6c238..303704718 100644 --- a/cli/gcp/client/network_test.go +++ b/cli/gcp/client/network_test.go @@ -97,6 +97,7 @@ func TestTerminateVPCs(t *testing.T) { networksAPI networksAPI subnetworksAPI subnetworksAPI firewalls []string + subnetwork string wantErr bool }{ "successful terminate": { @@ -104,6 +105,14 @@ func TestTerminateVPCs(t *testing.T) { operationRegionAPI: stubOperationRegionAPI{}, networksAPI: stubNetworksAPI{}, subnetworksAPI: stubSubnetworksAPI{}, + subnetwork: "subnetwork-id-1", + }, + "subnetwork empty": { + operationGlobalAPI: stubOperationGlobalAPI{}, + operationRegionAPI: stubOperationRegionAPI{}, + networksAPI: stubNetworksAPI{}, + subnetworksAPI: stubSubnetworksAPI{}, + subnetwork: "", }, "failed wait global op": { operationGlobalAPI: stubOperationGlobalAPI{waitErr: someErr}, @@ -111,6 +120,7 @@ func TestTerminateVPCs(t *testing.T) { networksAPI: stubNetworksAPI{}, subnetworksAPI: stubSubnetworksAPI{}, wantErr: true, + subnetwork: "subnetwork-id-1", }, "failed delete networks": { operationGlobalAPI: stubOperationGlobalAPI{}, @@ -118,6 +128,7 @@ func TestTerminateVPCs(t *testing.T) { networksAPI: stubNetworksAPI{deleteErr: someErr}, subnetworksAPI: stubSubnetworksAPI{}, wantErr: true, + subnetwork: "subnetwork-id-1", }, "failed delete subnetworks": { operationGlobalAPI: stubOperationGlobalAPI{}, @@ -125,6 +136,7 @@ func TestTerminateVPCs(t *testing.T) { networksAPI: stubNetworksAPI{}, subnetworksAPI: stubSubnetworksAPI{deleteErr: someErr}, wantErr: true, + subnetwork: "subnetwork-id-1", }, "must delete firewalls first": { firewalls: []string{"firewall-1", "firewall-2"}, @@ -133,6 +145,7 @@ func TestTerminateVPCs(t *testing.T) { networksAPI: stubNetworksAPI{}, subnetworksAPI: stubSubnetworksAPI{}, wantErr: true, + subnetwork: "subnetwork-id-1", }, } @@ -152,7 +165,7 @@ func TestTerminateVPCs(t *testing.T) { subnetworksAPI: tc.subnetworksAPI, firewalls: tc.firewalls, network: "network-id-1", - subnetwork: "subnetwork-id-1", + subnetwork: tc.subnetwork, } if tc.wantErr { diff --git a/cli/gcp/client/operation.go b/cli/gcp/client/operation.go index fcef268a1..7cb3b7210 100644 --- a/cli/gcp/client/operation.go +++ b/cli/gcp/client/operation.go @@ -2,6 +2,7 @@ package client import ( "context" + "errors" "fmt" computepb "google.golang.org/genproto/googleapis/cloud/compute/v1" @@ -12,6 +13,8 @@ import ( func (c *Client) waitForOperations(ctx context.Context, ops []Operation) error { for _, op := range ops { switch { + case op.Proto() == nil: + return errors.New("proto of operation is nil") case op.Proto().Zone != nil: if err := c.waitForZoneOperation(ctx, op); err != nil { return err