-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Fix confusing error message gcp #3756
base: main
Are you sure you want to change the base?
Fix confusing error message gcp #3756
Conversation
Hey @Excoriate , Thanks for submitting this pull request! I'm excited to get it merged in, as I think improving error messages is a solid way to improve the UX of Terragrunt. I'm seeing an error in one of our integration tests. If you could resolve that, I'd be happy to merge this in:
|
Thanks so much for taking the time @yhakbar —
Scenario 1: When
Scenario 2: When
What I've added, and I considered relevant is the Bucket Existence Check: If After these changes, I've tested them by running: go test ./remote -tags=gcp -run TestValidateGCSConfig -v === RUN TestValidateGCSConfig
=== RUN TestValidateGCSConfig/Valid_config_with_project_and_location
=== RUN TestValidateGCSConfig/Valid_config_with_skip_bucket_creation
=== RUN TestValidateGCSConfig/Missing_bucket
=== RUN TestValidateGCSConfig/Existing_bucket_without_project_and_location_when_bucket_exists
=== RUN TestValidateGCSConfig/Missing_project_when_bucket_exists
=== RUN TestValidateGCSConfig/Missing_project_when_bucket_does_not_exist
=== RUN TestValidateGCSConfig/Missing_location_when_bucket_does_not_exist
=== RUN TestValidateGCSConfig/Existing_bucket_without_project_and_location_when_skip_bucket_creation_is_true
=== RUN TestValidateGCSConfig/Missing_location_when_bucket_exists
--- PASS: TestValidateGCSConfig (0.00s)
--- PASS: TestValidateGCSConfig/Valid_config_with_project_and_location (0.00s)
--- PASS: TestValidateGCSConfig/Valid_config_with_skip_bucket_creation (0.00s)
--- PASS: TestValidateGCSConfig/Missing_bucket (0.00s)
--- PASS: TestValidateGCSConfig/Existing_bucket_without_project_and_location_when_bucket_exists (0.00s)
--- PASS: TestValidateGCSConfig/Missing_project_when_bucket_exists (0.00s)
--- PASS: TestValidateGCSConfig/Missing_project_when_bucket_does_not_exist (0.00s)
--- PASS: TestValidateGCSConfig/Missing_location_when_bucket_does_not_exist (0.00s)
--- PASS: TestValidateGCSConfig/Existing_bucket_without_project_and_location_when_skip_bucket_creation_is_true (0.00s)
--- PASS: TestValidateGCSConfig/Missing_location_when_bucket_exists (0.00s)
PASS Also —thanks again for the great catch 😅 — I run the integration tests that are linked to GCS (let me know if I'm missing something else). export GOOGLE_CLOUD_PROJECT=mysuperprojectingcp && go test -tags=gcp ./test -run TestGcpWorksWithExistingBucket -v
And also passes! |
We're very close! Please take a look at this lint error and address when convenient:
|
Also, @Excoriate , the logic that you're using here isn't optimal. A lot of what Terragrunt does is handle concurrent behavior, and overriding global anonymous functions in tests for mocking can result in problems for concurrent testing. If possible, take one of these two approaches in order of preference:
If this is too complicated to worry about now, feel free to ignore this. We can address it later, assuming tests pass and the lint errors are resolved. |
Oh! I see 😄 , thanks a lot! When I run golangci-lint run --config=.strict.golangci.yml ./remote/remote_state_gcs.go ./remote/remote_state_gcs_test.go I obtained different linter errors (few of them aren't caused by my changes) than the ones highlighted by your comment: golangci-lint run --config=.strict.golangci.yml ./remote/remote_state_gcs.go ./remote/remote_state_gcs_test.go
remote/remote_state_gcs.go:93:68: undefined: RemoteState (typecheck)
func (initializer GCSInitializer) NeedsInitialization(remoteState *RemoteState, existingBackend *TerraformBackend, terragruntOptions *options.TerragruntOptions) (bool, error) {
^
remote/remote_state_gcs.go:131:75: undefined: TerraformBackend (typecheck)
func GCSConfigValuesEqual(config map[string]interface{}, existingBackend *TerraformBackend, terragruntOptions *options.TerragruntOptions) bool {
^
remote/remote_state_gcs.go:179:80: undefined: RemoteState (typecheck)
func (initializer GCSInitializer) Initialize(ctx context.Context, remoteState *RemoteState, terragruntOptions *options.TerragruntOptions) error {
^
remote/remote_state_gcs.go:164:6: undefined: terraformStateConfigEqual (typecheck)
if !terraformStateConfigEqual(existingBackend.Config, comparisonConfig) {
^
remote/remote_state_gcs.go:192:25: undefined: initializedRemoteStateCache (typecheck)
if initialized, hit := initializedRemoteStateCache.Get(ctx, cacheKey); initialized && hit {
^
remote/remote_state_gcs.go:198:9: undefined: stateAccessLock (typecheck)
return stateAccessLock.StateBucketUpdate(gcsConfig.Bucket, func() error {
^
remote/remote_state_gcs.go:200:26: undefined: initializedRemoteStateCache (typecheck)
if initialized, hit := initializedRemoteStateCache.Get(ctx, cacheKey); initialized && hit {
^
remote/remote_state_gcs.go:225:3: undefined: initializedRemoteStateCache (typecheck)
initializedRemoteStateCache.Put(ctx, cacheKey, true)
^
remote/remote_state_gcs.go:344:11: undefined: BucketCreationNotAllowed (typecheck)
return BucketCreationNotAllowed(config.remoteStateConfigGCS.Bucket)
^
remote/remote_state_gcs.go:488:20: undefined: MaxRetriesWaitingForS3BucketExceeded (typecheck)
return errors.New(MaxRetriesWaitingForS3BucketExceeded(config.Bucket))
^ My questions are:
|
The changes in this commit add validation for the `project` and `location` fields in the GCS remote state configuration. If the `skip_bucket_creation` option is not set, the `project` and `location` fields are required. This ensures that the necessary information is provided for creating the GCS bucket if it doesn't already exist.
The changes in this commit focus on adding important considerations for using the GCS remote state backend in Terragrunt. The key changes are: 1. Clarify the requirements for creating a new GCS bucket, including the need for `project` and `location` configuration options. 2. Explain that Terragrunt will automatically create the bucket if it doesn't exist, and that versioning is enabled by default unless `skip_bucket_versioning` is set. 3. Highlight that Terragrunt validates the presence of `bucket`, `project`, and `location` when creating a new bucket, but these are not strictly required if the bucket already exists.
…project and location This change adds support for using an existing GCS bucket for remote state storage, even if the `project` and `location` are not specified in the configuration. The key changes are: 1. Modify the `ValidateGCSConfig` function to check if the bucket exists when `project` or `location` are not specified, and only require them if the bucket does not exist. 2. Add two new test cases to cover the scenarios where the bucket exists or does not exist without `project` and `location`. 3. Mock the `CreateGCSClient` and `DoesGCSBucketExist` functions in the tests to simulate the bucket existence scenarios. This change allows users to use an existing GCS bucket for remote state storage without having to specify the `project` and `location` in the configuration, making it more flexible and easier to use.
…tion This change enhances the validation logic for GCS remote state configuration: - Simplifies the test cases by using a map-based approach instead of a slice. - Introduces a mock function to simulate the existence of the GCS bucket, making the tests more robust. - Clarifies the validation logic by separating the cases where the bucket creation is skipped and when it is not. - Ensures that the bucket is always a required configuration parameter, regardless of the `skip_bucket_creation` setting. - Requires both the project and location to be specified when the bucket creation is not skipped, as these are the minimum required information to create a new bucket. - Adds a comment to indicate that the configuration is valid at the end of the validation process.
- Wrap the error returned by `CreateGCSClient` to provide more context. - Wrap the error returned by `CreateGCSBucket` to provide more context. - Add a deferred function to close the GCS client, and log any errors that occur during the close operation. - Rename the `parseGCSConfig` function to `ParseExtendedGCSConfig` to better reflect its purpose.
…r issues The changes in this commit simplify the validation of the GCS remote state configuration. The main improvements are: - If both `project` and `location` are provided, the configuration is considered valid and no further checks are needed. - If the bucket doesn't exist, the validation now requires both `project` and `location` to be set, instead of checking them separately. - The code for creating and closing the GCS client has been moved outside the conditional blocks, reducing duplication. These changes make the validation logic more straightforward and easier to understand.
d9decd4
to
86f18d0
Compare
Description
Fixes #3730
Additionally, I added a slight enhancement in the relevant docs to explain this validation further. However, I'd love to be directed to the correct place if I haven't added it 😅 .
Problem Statement
The current Terragrunt GCS remote state configuration lacks consistent validation for
project
andlocation
attributes when creating new buckets. This can lead to unclear error messages and potential configuration issues during remote state initialization.Proposed Solution
Enhance the GCS remote state configuration validation to:
project
andlocation
only when bucket creation is not explicitly skippedskip_bucket_creation
configuration optionFeel free to run the tests executing:
TODOs
Read the Gruntwork contribution guidelines.
Release Notes (draft)
Added GCS (Remote State) validation for
location
andproject's
attribute when theskip_bucket_creation
option is set to false.