Skip to content

support importing by resource identity #1463

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

Merged
merged 6 commits into from
Apr 18, 2025
Merged

support importing by resource identity #1463

merged 6 commits into from
Apr 18, 2025

Conversation

ansgarm
Copy link
Member

@ansgarm ansgarm commented Apr 3, 2025

This PR adds a new ImportStateWithIdentity method to Provider which is used in the ImportResourceState RPC handler to optionally pass a resource identity instead of a string id. Both still exist side-by-side and will for backwards compatibility.

Resources can now retrieve and set values in an identity like this to support import by identity:

Importer: &schema.ResourceImporter{
	StateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
		if rd.Id() != "" {
			return []*schema.ResourceData{rd}, nil // just return the resource data, since the string id is used
		}

		identity, err := rd.Identity()
		if err != nil {
			return nil, err
		}

		emailRaw, ok := identity.GetOk("email")
		if !ok {
			return nil, fmt.Errorf("error getting email from identity: %w", err)
		}

		email, ok := emailRaw.(string)
		if !ok {
			return nil, fmt.Errorf("error converting email to string")
		}

		if email == "" {
			return nil, fmt.Errorf("email cannot be empty")
		}

		err = rd.Set("email", email)
		rd.SetId(email) // TODO: document that this is still required with resource identity
		if err != nil {
			return nil, fmt.Errorf("error setting email: %w", err)
		}

		return []*schema.ResourceData{rd}, nil
	},
},

@ansgarm ansgarm marked this pull request as ready for review April 17, 2025 09:40
@ansgarm ansgarm requested a review from a team as a code owner April 17, 2025 09:40
@austinvalle austinvalle added this to the v2.37.0 milestone Apr 17, 2025
Copy link
Member

@austinvalle austinvalle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work Ansgar! I left a note about a potential helper, but that can be in a follow-up PR before stable release 👍🏻

@austinvalle
Copy link
Member

I'm going to merge this now and cut a beta release 👍🏻

@austinvalle austinvalle merged commit b451f6d into main Apr 18, 2025
22 checks passed
@austinvalle austinvalle deleted the identity-import branch April 18, 2025 19:42
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants