11// Copyright (c) HashiCorp, Inc.
22// SPDX-License-Identifier: MPL-2.0
3-
4- // ----------------------------------------------------------------------------
5- //
6- // *** AUTO GENERATED CODE *** Type: DCL ***
7- //
8- // ----------------------------------------------------------------------------
9- //
10- // This file is managed by Magic Modules (https://github.com/GoogleCloudPlatform/magic-modules)
11- // and is based on the DCL (https://github.com/GoogleCloudPlatform/declarative-resource-client-library).
12- // Changes will need to be made to the DCL or Magic Modules instead of here.
13- //
14- // We are not currently able to accept contributions to this file. If changes
15- // are required, please file an issue at https://github.com/hashicorp/terraform-provider-google/issues/new/choose
16- //
17- // ----------------------------------------------------------------------------
18-
193package networkconnectivity
204
215import (
226 "context"
237 "log"
8+ "strings"
249 "testing"
2510
26- networkconnectivity "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/networkconnectivity/beta"
2711 "github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar"
2812 "github.com/hashicorp/terraform-provider-google-beta/google-beta/sweeper"
13+ "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
2914 transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
3015)
3116
3217func init () {
3318 sweeper .AddTestSweepers ("NetworkConnectivitySpoke" , testSweepNetworkConnectivitySpoke )
3419}
3520
21+ // At the time of writing, the CI only passes us-central1 as the region
3622func testSweepNetworkConnectivitySpoke (region string ) error {
37- log .Print ("[INFO][SWEEPER_LOG] Starting sweeper for NetworkConnectivitySpoke" )
23+ resourceName := "NetworkConnectivitySpoke"
24+ log .Printf ("[INFO][SWEEPER_LOG] Starting sweeper for %s" , resourceName )
3825
3926 config , err := sweeper .SharedConfigForRegion (region )
4027 if err != nil {
@@ -51,23 +38,87 @@ func testSweepNetworkConnectivitySpoke(region string) error {
5138 t := & testing.T {}
5239 billingId := envvar .GetTestBillingAccountFromEnv (t )
5340
54- // Setup variables to be used for Delete arguments.
55- d := map [string ]string {
56- "project" : config .Project ,
57- "region" : region ,
58- "location" : region ,
59- "zone" : "-" ,
60- "billing_account" : billingId ,
41+ // Setup variables to replace in list template
42+ d := & tpgresource.ResourceDataMock {
43+ FieldsInSchema : map [string ]interface {}{
44+ "project" : config .Project ,
45+ "region" : region ,
46+ "location" : region ,
47+ "zone" : "-" ,
48+ "billing_account" : billingId ,
49+ },
6150 }
6251
63- client := transport_tpg . NewDCLNetworkConnectivityClient ( config , config . UserAgent , "" , 0 )
64- err = client . DeleteAllSpoke ( context . Background (), d [ "project" ], d [ "location" ], isDeletableNetworkConnectivitySpoke )
52+ listTemplate := strings . Split ( "https://networkconnectivity.googleapis.com/v1/projects/{{project}}/locations/global/spokes" , "?" )[ 0 ]
53+ listUrl , err := tpgresource . ReplaceVars ( d , config , listTemplate )
6554 if err != nil {
66- return err
55+ log .Printf ("[INFO][SWEEPER_LOG] error preparing sweeper list url: %s" , err )
56+ return nil
57+ }
58+
59+ res , err := transport_tpg .SendRequest (transport_tpg.SendRequestOptions {
60+ Config : config ,
61+ Method : "GET" ,
62+ Project : config .Project ,
63+ RawURL : listUrl ,
64+ UserAgent : config .UserAgent ,
65+ })
66+ if err != nil {
67+ log .Printf ("[INFO][SWEEPER_LOG] Error in response from request %s: %s" , listUrl , err )
68+ return nil
69+ }
70+
71+ resourceList , ok := res ["spokes" ]
72+ if ! ok {
73+ log .Printf ("[INFO][SWEEPER_LOG] Nothing found in response." )
74+ return nil
6775 }
68- return nil
69- }
7076
71- func isDeletableNetworkConnectivitySpoke (r * networkconnectivity.Spoke ) bool {
72- return sweeper .IsSweepableTestResource (* r .Name )
77+ rl := resourceList .([]interface {})
78+
79+ log .Printf ("[INFO][SWEEPER_LOG] Found %d items in %s list response." , len (rl ), resourceName )
80+ // Keep count of items that aren't sweepable for logging.
81+ nonPrefixCount := 0
82+ for _ , ri := range rl {
83+ obj := ri .(map [string ]interface {})
84+ if obj ["name" ] == nil {
85+ log .Printf ("[INFO][SWEEPER_LOG] %s resource name was nil" , resourceName )
86+ return nil
87+ }
88+
89+ name := tpgresource .GetResourceNameFromSelfLink (obj ["name" ].(string ))
90+ // Skip resources that shouldn't be sweeped
91+ if ! sweeper .IsSweepableTestResource (name ) {
92+ nonPrefixCount ++
93+ continue
94+ }
95+
96+ deleteTemplate := "https://networkconnectivity.googleapis.com/v1/projects/{{project}}/locations/global/spokes/{{name}}"
97+ deleteUrl , err := tpgresource .ReplaceVars (d , config , deleteTemplate )
98+ if err != nil {
99+ log .Printf ("[INFO][SWEEPER_LOG] error preparing delete url: %s" , err )
100+ return nil
101+ }
102+ deleteUrl = deleteUrl + name
103+
104+ // Don't wait on operations as we may have a lot to delete
105+ _ , err = transport_tpg .SendRequest (transport_tpg.SendRequestOptions {
106+ Config : config ,
107+ Method : "DELETE" ,
108+ Project : config .Project ,
109+ RawURL : deleteUrl ,
110+ UserAgent : config .UserAgent ,
111+ })
112+ if err != nil {
113+ log .Printf ("[INFO][SWEEPER_LOG] Error deleting for url %s : %s" , deleteUrl , err )
114+ } else {
115+ log .Printf ("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s" , resourceName , name )
116+ }
117+ }
118+
119+ if nonPrefixCount > 0 {
120+ log .Printf ("[INFO][SWEEPER_LOG] %d items were non-sweepable and skipped." , nonPrefixCount )
121+ }
122+
123+ return nil
73124}
0 commit comments