Skip to content

fix: align external names with Terraform state IDs and remove unused import IDs - #201

Closed
fernandezcuesta wants to merge 1 commit into
mainfrom
fix/resource-policy-ear-endpoint-external-name
Closed

fernandezcuesta wants to merge 1 commit into
mainfrom
fix/resource-policy-ear-endpoint-external-name

Conversation

@fernandezcuesta

@fernandezcuesta fernandezcuesta commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

Description of your changes

This PR fixes the external name configuration of 12 resources. In each one, the configured ID does not match the Terraform schema or the ID that Terraform Read expects. The PR also removes the import-ID code, because the no-fork connectors never call it.

All controllers in this provider use the no-fork connectors, which never run terraform import.
Only GetIDFn (external name to Terraform state ID) and GetExternalNameFn (state to external name) have an effect, so the import formats in the Terraform docs (for example {ORG_ID}-{POLICY_ID}) do not apply.

Resources where create fails

  • mongodbatlas_project_api_key: GetIDFn needs a top-level project_id, but this resource has project IDs only in project_assignment. Create and import fail with forProvider is missing [project_id]. Terraform reads only api_key_id from the state ID, so the config now uses api_key_id only. Supersedes fix(project): build the APIKey Terraform ID from api_key_id #199.

  • mongodbatlas_cloud_user_team_assignment: the template uses username, which is computed. The required argument is user_id. The template now uses user_id, and userId can reference a UserOrgAssignment (status.atProvider.userId).

  • mongodbatlas_resource_policy and mongodbatlas_encryption_at_rest_private_endpoint: GetIDFn sends <orgId>-<policyId> (or <projectId>-<cloudProvider>-<endpointId>) to Read, which expects the plain provider-assigned ID. GetExternalNameFn also fails on the real state ID with expected at least 1 left segments. Both resources now use the default identifierFromProvider().

  • mongodbatlas_ai_model_api_key, mongodbatlas_cloud_backup_collection_restore_job, mongodbatlas_metric_integration, mongodbatlas_stream_connection_failover and mongodbatlas_log_integration are plugin-framework resources with no id attribute. GetExternalNameFn reads id, so it fails after create with id not found in Terraform state. Before create, the Read path parameter is empty, and an import with only the annotation cannot work. This is the same root cause as ServiceAccount resources (both mongodbatlas.crossplane.io and project.mongodbatlas.crossplane.io) report Ready=True one second after they are applied, never reach Synced=True, and never create anything in Atlas. #189. log_integration also used type as external name, which does not identify one integration. A new constructor, computedKeyID(key), reads the external name from the computed key, copies the annotation into the key for imports, and does not overwrite a key that is already in the state. Each of these resources also sets StateEmptyWhenAttributeUnset(key), so that Create runs.

  • mongodbatlas_ai_model_rate_limit has no id attribute either, and its GetExternalNameFn failed the same way. It now reads the external name from model_group_name.

Resources where observed state is incomplete

mongodbatlas_x509_authentication_database_user: the state ID had no username, so Terraform Read skipped the user certificates. The state ID now includes username when it is set.

mongodbatlas_cluster: the state ID had no provider_name, which Terraform Read uses to read containers. The state ID now includes it.

External names from v1.1.x

In v1.1.x, the external name of resources that use EncodeStateID was the full encoded state ID. encodedStateGetIDFn encoded that value a second time, and Read did not find the resource. encodedStateGetIDFn now decodes the external name first, for all importJoinedID* resources.

Code cleanup

  • GetImportIDFn, plainImportGetIDFn and collectImportValues. Upjet reads GetImportIDFn only in the fork workspace path (pkg/terraform/store.go). No controller in this provider uses that path.
  • The separator parameter of importJoinedID and importJoinedIDAssigned. Only the import ID used it. The state ID always uses the EncodeStateID format.
  • refs.ExtractParamPath: the second parameter is now isObservation (was sensitive). Upjet uses it to select status.atProvider or spec.forProvider. No caller or generated code changes.

Missing references

  • UserTeamAssignment: new optional userIdRef and userIdSelector fields.
  • LogIntegration controllers: the name initializer is removed (computedKeyID disables it).

Points for reviewers

  • Existing LogIntegration objects change behavior. On the first reconcile after the upgrade, the external name changes from the integration type (for example S3) to the integration ID. The real integration_id comes from status.atProvider, and the guard in computedKeyID keeps it. Tools that read this annotation will see the new value.
  • docs/import.md is updated for all changed resources. New rows are added for the resources that can now be imported.
  • project_service_account_secret and service_account_secret are not changed. The secret value is only available at create, so an import is not useful.

Refs #113, #189, #199

I have:

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

How has this code been tested

  • New tests in config/external_name_test.go call the configured GetIDFn, GetExternalNameFn, SetIdentifierArgumentFn and TerraformPluginFrameworkIsStateEmptyFn of each changed resource. Each test failed before its fix.
  • A comparison of GetIDFn output for all resources, before and after the change, shows differences only in the resources above. DisableNameInitializer changes only for log_integration.
  • The expected state IDs come from the Terraform provider source (EncodeStateID calls, Read and ImportState functions).
  • go test ./config/..., make lint (0 issues), go build ./... and make generate pass.
  • Not tested against a live Atlas organization.

…import IDs

- project_api_key: use api_key_id only (project IDs are only in project_assignment)
- cloud_user_team_assignment: template uses user_id; add userId reference to UserOrgAssignment
- resource_policy, encryption_at_rest_private_endpoint: use the plain provider-assigned ID
- ai_model_api_key, cloud_backup_collection_restore_job, metric_integration,
  stream_connection_failover, log_integration: new computedKeyID constructor for
  plugin-framework resources without an id attribute
- ai_model_rate_limit: read the external name from model_group_name
- x509_authentication_database_user: add username to the state ID
- cluster: add provider_name to the state ID
- encodedStateGetIDFn: decode external names written as full state IDs by v1.1.x
- remove GetImportIDFn and the separator argument; the no-fork connectors never run terraform import
- refs.ExtractParamPath: rename the second parameter to isObservation

Signed-off-by: Jesús Fernández <7312236+fernandezcuesta@users.noreply.github.com>
@fernandezcuesta
fernandezcuesta force-pushed the fix/resource-policy-ear-endpoint-external-name branch from e276c59 to 89194cc Compare October 5, 2026 23:15
@fernandezcuesta
fernandezcuesta marked this pull request as draft October 5, 2026 23:16
@fernandezcuesta

Copy link
Copy Markdown
Collaborator Author

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.

1 participant