@@ -36,6 +36,8 @@ import (
3636 sqladmin " google.golang.org/api/sqladmin/v1beta4"
3737 backupdr " google.golang.org/api/backupdr/v1"
3838 " cloud.google.com/go/bigquery"
39+ " google.golang.org/api/option"
40+ " google.golang.org/api/googleapi"
3941)
4042
4143var SharedKeyRing = " tftest-shared-keyring-1"
@@ -2400,10 +2402,14 @@ func BootstrapIntegrationsClient(t *testing.T, locationID string) BootstrapClien
24002402 IsGMEK: true,
24012403 }
24022404}
2403- func AddBigQueryDatasetReplica(projectID string, datasetID string, primaryLocation string, replicaLocation string) (string, error) {
2405+ func AddBigQueryDatasetReplica(t *testing .T , projectID string, datasetID string, primaryLocation string, replicaLocation string) (string, error) {
24042406 ctx := context.Background ()
2407+ config := BootstrapConfig(t)
2408+ if config == nil {
2409+ return " " , fmt.Errorf (" failed to bootstrap config" )
2410+ }
24052411
2406- client, err := bigquery.NewClient (ctx, projectID)
2412+ client, err := bigquery.NewClient (ctx, projectID, option .WithTokenSource (config .TokenSource ), option .WithUserAgent (config .UserAgent ) )
24072413 if err != nil {
24082414 return " " , fmt.Errorf (" failed to create BigQuery client: %w " , err)
24092415 }
@@ -2417,7 +2423,11 @@ func AddBigQueryDatasetReplica(projectID string, datasetID string, primaryLocati
24172423
24182424 err = datasetRef.Create (ctx, datasetMetadata)
24192425 if err != nil {
2420- log.Printf (" failed to create BigQuery dataset '%s '. Dataset already exists." , datasetID)
2426+ if ge , ok := err. (*googleapi.Error ); ok && ge .Code == 409 {
2427+ log.Printf (" INFO: BigQuery dataset '%s ' already exists in project '%s ' at location '%s '. Continuing." , datasetID, projectID, primaryLocation)
2428+ } else {
2429+ return " " , fmt.Errorf (" failed to create BigQuery dataset '%s ': %w " , datasetID, err)
2430+ }
24212431 } else {
24222432 log.Printf (" INFO: Successfully created BigQuery dataset '%s ' at location '%s '." , datasetID, primaryLocation)
24232433 }
@@ -2435,29 +2445,46 @@ func AddBigQueryDatasetReplica(projectID string, datasetID string, primaryLocati
24352445
24362446 job, err := query.Run (ctx)
24372447 if err != nil {
2438- return " " , fmt.Errorf (" failed to submit BigQuery DDL job: %w " , err)
2439- }
2440-
2441- status, err := job.Wait (ctx)
2442- if err != nil {
2443- log.Printf (" failed to wait for BigQuery job completion: %v " , err)
2444- }
2445-
2446- if status.Err () != nil {
2447- log.Printf (" BigQuery job completed with an error: %v " , status.Err ())
2448+ // Check if the error is an " Already Exists" error on submission
2449+ if ge , ok := err. (*googleapi.Error ); ok && ge .Code == 409 && (strings.Contains (ge .Message , " Duplicate" ) || strings.Contains (ge .Message , " Already Exists" )) {
2450+ log.Printf (" INFO: Replica '%s ' already exists for dataset '%s ' (error on job submission). Continuing." , replicaLocation, datasetID)
2451+ } else {
2452+ return " " , fmt.Errorf (" failed to submit BigQuery DDL job for adding replica: %w " , err)
2453+ }
2454+ } else {
2455+ status, waitErr := job.Wait (ctx)
2456+ if waitErr != nil {
2457+ // Check if the error is an " Already Exists" error after waiting
2458+ if ge , ok := waitErr. (*googleapi.Error ); ok && ge .Code == 409 && (strings.Contains (ge .Message , " Duplicate" ) || strings.Contains (ge .Message , " Already Exists" )) {
2459+ log.Printf (" INFO: Replica '%s ' already exists for dataset '%s ' (error on job wait). Continuing." , replicaLocation, datasetID)
2460+ } else {
2461+ return " " , fmt.Errorf (" failed to wait for BigQuery job completion for adding replica: %w " , waitErr)
2462+ }
2463+ } else if status != nil && status.Err () != nil {
2464+ // Check if the status error is an " Already Exists" error
2465+ if ge , ok := status.Err (). (*googleapi.Error ); ok && ge .Code == 409 && (strings.Contains (ge .Message , " Duplicate" ) || strings.Contains (ge .Message , " Already Exists" )) {
2466+ log.Printf (" INFO: Replica '%s ' already exists for dataset '%s ' (error in job status). Continuing." , replicaLocation, datasetID)
2467+ } else {
2468+ return " " , fmt.Errorf (" BigQuery job for adding replica completed with an error: %w " , status.Err ())
2469+ }
2470+ } else {
2471+ log.Printf (" INFO: Successfully added BigQuery dataset replica '%s ' for dataset '%s '." , replicaLocation, datasetID)
2472+ }
24482473 }
24492474
2450- log.Println (" Successfully added BigQuery dataset replica using the Go client library." )
2451-
24522475 fullDatasetLocation := fmt.Sprintf (" projects/%s /datasets/%s " , projectID, datasetID)
24532476 return fullDatasetLocation, nil
24542477}
24552478
2456- func CleanupBigQueryDatasetAndReplica(projectID, datasetID, replicaLocation string) {
2479+ func CleanupBigQueryDatasetAndReplica(t *testing .T , projectID, datasetID, replicaLocation string) {
24572480 log.Printf (" [DEBUG] Cleanup: Starting cleanup for BigQuery dataset: projects/%s /datasets/%s " , projectID, datasetID)
24582481 cleanupCtx := context.Background ()
2482+ config := BootstrapConfig(t)
2483+ if config == nil {
2484+ return
2485+ }
24592486
2460- client, cerr := bigquery.NewClient (cleanupCtx, projectID)
2487+ client, cerr := bigquery.NewClient (cleanupCtx, projectID, option .WithTokenSource (config .TokenSource ), option .WithUserAgent (config .UserAgent ) )
24612488 if cerr != nil {
24622489 log.Printf (" [ERROR] Cleanup: Failed to create BigQuery client for dataset %s : %v " , datasetID, cerr)
24632490 return
0 commit comments