Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

googleapi: Error 400: Invalid value for field 'region': 'global'. Unknown region., invalid when running import google without regions flag #1956

Open
Shion1305 opened this issue Dec 9, 2024 · 0 comments

Comments

@Shion1305
Copy link

Note

This issue has already been reported on #1928, however, it was closed due to no activity. This issue has pull_request #1930 so I recreated the issue here.

When running terraformer import google without the regions flag, the following error appears in the log:

2024/09/29 03:03:13 googleapi: Error 400: Invalid value for field 'region': 'global'. Unknown region., invalid

Although this error doesn't stop the command from completing successfully, it's misleading and could cause confusion for users.

Cause:

The issue arises because, if the regions flag is not set, global is used as the default value for the region. However, global is not a valid region for this API call:

regionsGetCall := computeService.Regions.Get(project, regionName).Fields("name", "zones")

This call triggers the following gRPC API request:

GET https://compute.googleapis.com/compute/v1/projects/{project}/regions/{region}

Since global is not a recognized region, the API responds with an error.

Proposed Solution:

To fix this, the code should handle the case when the region is set to global by skipping the API call. This could be implemented by adding a conditional check in the getRegion function:

func getRegion(project, regionName string) *compute.Region {
	if regionName == "global" {
		return &compute.Region{}
	}
	computeService, err := compute.NewService(context.Background())
	if err != nil {
		log.Println(err)
		return &compute.Region{}
	}
	regionsGetCall := computeService.Regions.Get(project, regionName).Fields("name", "zones")
	region, err := regionsGetCall.Do()
	if err != nil {
		log.Println(err)
		return &compute.Region{}
	}
	return region
}

By making this change, the error message will no longer appear when global is used as the default region, and it will prevent unnecessary API requests.

Additional Information:

  • This issue only occurs when the regions flag is not provided.
  • The error message does not affect the overall functionality of the command.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant