Provider Development : Instantiate types.Object

Hello,

We are currently developing a ForgeRock provider with the objective of creating OAuth Clients. However, we are encountering difficulties with the implementation of the import method.

In the midst of this process, we aim to exhibit the data that needs to be incorporated in the main.tf file.

Below is the struct we have at the moment:

type OAuth2ClientTF struct {
	LastUpdated         types.String `tfsdk:"last_updated" json:"lastUpdated"`
	Name                types.String `tfsdk:"name" json:"name"`
	UserPasswordVersion types.Int64  `tfsdk:"user_password_version" json:"userPasswordVersion"`

	AdvancedOAuth2ClientConfigTF types.Object `tfsdk:"advanced_oauth2_client_config" json:"advancedOAuth2ClientConfigTF"`

	CoreOAuth2ClientConfigTF types.Object `tfsdk:"core_oauth2_client_config" json:"coreOpenIDClientConfigTF"`

	CoreOpenIDClientConfigTF types.Object `tfsdk:"core_open_id_client_config" json:"coreOAuth2ClientConfigTF"`
}

In this struct, AdvancedOAuth2ClientConfigTF, CoreOAuth2ClientConfigTF, and CoreOpenIDClientConfigTF are sub-structs, each comprising fields that are TF Types. Currently, we are facing a challenge in instantiating an OAuth2ClientTF object with initialized sub-structs as they are being initialized with a nil value.

Given that we have the TF Schema (extracted from the resource.ImportStateResponse parameter of the import method), we are curious to know if there is a feasible way to create an object directly from this schema, or if there is any alternative method to accomplish this. (We have seen the TF reflect.Into method but the package is private)

We appreciate your assistance and guidance on this matter.

Thank you.

Hey there @julien-pal :wave: , welcome to the discuss forum.

We recently added some documentation that describes the data handling for all of the different types that you may find useful:

There is a reflection-based creation method like types.ObjectValueFrom (that I believe internally calls reflect.Into) and a creation method based off a map of attr.Value with types.ObjectValue. Both will require the type information of the “sub struct” defined as map[string]attr.Type.

You can see examples of how to use both at the link provided, hopefully this helps! :wave:

More info after re-reading this:

Given that we have the TF Schema (extracted from the resource.ImportStateResponse parameter of the import method), we are curious to know if there is a feasible way to create an object directly from this schema

For importing a resource, you should focus on only populating the attributes necessary for your Read to work (as it will be called after import), in some cases that be just be an ID/Name field that is used to retrieve the resource data in Read.

We recommend using the (ImportStateResponse).State.SetAttribute function for that so you can skip setting all the other attributes that will eventually be populated by the Read.

Here are the related docs: Plugin Development - Framework: Resource Import | Terraform | HashiCorp Developer