'Can I read from main.tf when importing a resource in Terraform?

context: I'm implementing a custom import method in Terraform provider (SDK v2).

In order to send a GET request I need 2 query parameters and ID of the resource, for example:

resource "foo" "example" {
  // implicit id = "abc"
  bar = "123"
  zoo = "456"
}

GET request URL: example.com/api/bar/123/zoo/456/examples/abc

Is there a way to implement import to access both bar and zoo in main.tf file (barValue = d.Get("bar"))? Or the only option for me is to pass a complex ID like 123/456/abc and decode it as {BAR}/{ZOO}/{ID}?

I guess here it's an option to use terraform import foo.example {BAR}/{ZOO}/{ID} but there's one more resource that contain a map[string]string with random elements so it might be a stretch to encode them all in a string.

I'm concerned about It is given a ResourceData with only ID set.:

// StateContextFunc is the function called to import a resource into the
// Terraform state. It is given a ResourceData with only ID set. This
// ID is going to be an arbitrary value given by the user and may not map
// directly to the ID format that the resource expects, so that should
// be validated.
//
// This should return a slice of ResourceData that turn into the state
// that was imported. This might be as simple as returning only the argument
// that was given to the function. In other cases (such as AWS security groups),
// an import may fan out to multiple resources and this will have to return
// multiple.
//
// To create the ResourceData structures for other resource types (if
// you have to), instantiate your resource and call the Data function.
type StateContextFunc func(context.Context, *ResourceData, interface{}) ([]*ResourceData, error)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source