Repository navigation
Conversation
The external name of mongodbatlas_project_api_key needs a top-level
project_id. The schema has no such argument, because the project IDs
are in project_assignment. So every APIKey fails in Connect with
"forProvider is missing [project_id]", for a create and for an import.
The Terraform state ID of this resource holds only api_key_id, and its
import ID is {project_id}-{api_key_id}. projectAPIKeyImportJoinedID
builds the state ID from the external name only. It takes the import
project from project_assignment.
Signed-off-by: Sam Mobach <sam.mobach@rtl.nl>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
|
@fernandezcuesta do you have time to look into this :)? |
Collaborator
|
@nilpntr thanks for the detailed analysis, the root cause is right, I think the fix in this case should be way easier (just a one liner): config/resources/mongodbatlas.go p.AddResourceConfigurator("mongodbatlas_project_api_key", func(r *config.Resource) {
- r.ExternalName = importJoinedIDAssigned([]string{refs.ProjectID, refs.APIKeyID}, "-", refs.APIKeyID)
+ r.ExternalName = importJoinedIDAssigned([]string{refs.APIKeyID}, "-", refs.APIKeyID)
})but this would break objects created on v1.1.x. We're folding the fix into a new PR, which fixes the same class of bug in several other resources. |
2 tasks done
Contributor
|
Should be resolved in #206, I've e2e tested the |
Collaborator
|
Closing as superseded by #206 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of your changes
A project
APIKeyfails in Connect, for a new key and for an import of an existing key:The example in
examples/namespaced/project/v1alpha1/apikey.yamlgives the same error.Cause. Since v1.2.0 (#128), the
GetIDFnofmongodbatlas_project_api_keyneeds a top-levelproject_id. This resource has no such argument, because its project IDs are inproject_assignment. So the function always uses its fallback, which accepts only an encoded state ID in the external name. For a create, the external name is empty. For an import, it is the plainapi_key_id.Terraform behavior. In v2.17.0 (
internal/service/projectapikey), the resource sets its ID toEncodeStateID({"api_key_id": ...}). Read, Update and Delete decode onlyapi_key_id. The import ID is{project_id}-{api_key_id}, and the import ignores the project part.Change.
projectAPIKeyImportJoinedIDreplacesimportJoinedIDAssignedfor this resource:GetIDFnencodes only theapi_key_idfrom the external name, which gives the ID that Terraform writes.GetImportIDFnreturns{project_id}-{api_key_id}with the first project inproject_assignment.GetExternalNameFnandDisableNameInitializerdo not change, so the generated code stays the same.The table in
docs/import.mdnow givesapiKeyIdas the external name of this resource, with no identity parameters.I also called the
GetIDFnof all resources with a non-empty external name and all top-level string arguments set. Onlymongodbatlas_project_api_keyfailed.I have:
make reviewable testto ensure this PR is ready for review.How has this code been tested
go test ./config/...passes, with nine new cases inTestProjectAPIKeyImportJoinedID.make lintreports 0 issues.I gave the parameters of the namespaced
APIKeyexample to the configuredGetIDFn. Before this change, it returned the error above. After this change, it returns an empty ID for a create and the encoded ID for an external name.A local test ran the Terraform resource from v2.17.0 against a mock Atlas API. Create, read, import and delete passed.
GetIDFngave the same ID that Terraform wrote at create. This test is not in the PR, but I can add it.I did not test against a real Atlas project.
🤖 Generated with Claude Code