Skip to content

fix(project): build the APIKey Terraform ID from api_key_id - #199

Closed
nilpntr wants to merge 1 commit into
crossplane-contrib:mainfrom
nilpntr:fix/project-api-key-external-name
Closed

nilpntr wants to merge 1 commit into
crossplane-contrib:mainfrom
nilpntr:fix/project-api-key-external-name

Conversation

@nilpntr

@nilpntr nilpntr commented Oct 2, 2026

Copy link
Copy Markdown

Description of your changes

A project APIKey fails in Connect, for a new key and for an import of an existing key:

connect failed: cannot initialize the Terraform plugin SDK async external client: failed to get the extended parameters for resource "subscribe/subscribe-previews-users": cannot get ID: cannot determine Terraform ID: forProvider is missing [project_id] and crossplane.io/external-name is empty or not a valid encoded state ID

The example in examples/namespaced/project/v1alpha1/apikey.yaml gives the same error.

Cause. Since v1.2.0 (#128), the GetIDFn of mongodbatlas_project_api_key needs a top-level project_id. This resource has no such argument, because its project IDs are in project_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 plain api_key_id.

Terraform behavior. In v2.17.0 (internal/service/projectapikey), the resource sets its ID to EncodeStateID({"api_key_id": ...}). Read, Update and Delete decode only api_key_id. The import ID is {project_id}-{api_key_id}, and the import ignores the project part.

Change. projectAPIKeyImportJoinedID replaces importJoinedIDAssigned for this resource:

  • GetIDFn encodes only the api_key_id from the external name, which gives the ID that Terraform writes.
  • An empty external name gives an empty ID, so Crossplane creates the key.
  • An encoded state ID in the external name also works. The v1.1 releases wrote that format.
  • GetImportIDFn returns {project_id}-{api_key_id} with the first project in project_assignment.
  • GetExternalNameFn and DisableNameInitializer do not change, so the generated code stays the same.

The table in docs/import.md now gives apiKeyId as the external name of this resource, with no identity parameters.

I also called the GetIDFn of all resources with a non-empty external name and all top-level string arguments set. Only mongodbatlas_project_api_key failed.

I have:

  • Read and followed Crossplane's contribution process.
  • Run make reviewable test to ensure this PR is ready for review.

I ran the checks in the next section, not the full make reviewable test.

How has this code been tested

  • go test ./config/... passes, with nine new cases in TestProjectAPIKeyImportJoinedID.
  • make lint reports 0 issues.
  • The upjet generator gives the same output with and without this change.

I gave the parameters of the namespaced APIKey example to the configured GetIDFn. 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. GetIDFn gave 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

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>
@nilpntr

nilpntr commented Oct 2, 2026

Copy link
Copy Markdown
Author

@fernandezcuesta do you have time to look into this :)?

@fernandezcuesta

Copy link
Copy Markdown
Collaborator

@nilpntr thanks for the detailed analysis, the root cause is right, project_id only exists inside project_assignment, and Terraform reads only api_key_id from the state ID.

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.

@jonasz-lasut

Copy link
Copy Markdown
Contributor

Should be resolved in #206, I've e2e tested the mongodbatlas_project_api_key resource and it passed full create/import/delete cycle

@fernandezcuesta

Copy link
Copy Markdown
Collaborator

Closing as superseded by #206

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

Successfully merging this pull request may close these issues.

3 participants