Defining resource schema and struct model

I tried to create a resource with the next schema

func (e *Events2MetricResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
	resp.Schema = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"id": schema.StringAttribute{
				Computed: true,
			"name": schema.StringAttribute{
				Required: true,
				Validators: []validator.String{
					stringvalidator.RegexMatches(regexp.MustCompile(`^[A-Za-z\d_:-]*$`), "Invalid metric name, name may only contain ASCII letters and digits, as well as underscores and colons."),
				MarkdownDescription: "Events2Metric name. Events2Metric names have to be unique per account.",

I defined the next correlated struct model (which I don’t even think it related to this issue) -

type Events2MetricResourceModel struct {
	ID   types.String `tfsdk:"id"`
	Name types.String `tfsdk:"name"`

When I tried to run a plan with next config -

resource "coralogix_events2metric" "logs2metric" {
  name        = "logs2metricExample"

I got the next error -

> 2023-06-14T14:16:53.011+0300 [ERROR] vertex "coralogix_events2metric.logs2metric" error: Value Conversion Error
> ╷
> │ Error: Value Conversion Error
> │ 
> │   with coralogix_events2metric.logs2metric,
> │ An unexpected error was encountered trying to convert tftypes.Value into coralogix.Events2MetricResource. This is always an error in the provider. Please report the following to the provider developer:
> │ 
> │ mismatch between struct and object: Object defines fields not found in struct: id and name.

Any idea what can cause that?

You have not shown us the Go code that attempts to decode into your Go struct.

I feel that is likely to be relevant.

This is the beginning of the Create function, but I tried to debug, and it seems the error happens before this step.

func (e *Events2MetricResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
	// Retrieve values from plan
	var plan Events2MetricResourceModel
	diags := req.Plan.Get(ctx, &plan)
	if resp.Diagnostics.HasError() {

That isn’t what would happen, unless a different method of the resource was being called instead.

The code you’ve actually shown us does not reproduce the error message. I copy/pasted it into my own IDE and confirmed this.

One thing you can do to confirm which Terraform RPC is being invoked against the provider and where the error is being generated is enable logging, e.g. adding the TF_LOG=TRACE environment variable while invoking the terraform command or go test command, such as TF_LOG=TRACE terraform apply or TF_LOG=TRACE TF_ACC=1 go test -count=1 -v ./... when performing provider acceptance testing.

For a vanilla terraform apply command a summary of the following calls are made along with the following framework functionality that gets invoked (if defined):

  • ValidateProviderConfig (PrepareProviderConfig in protocol version 5) - provider.Provider implementation Schema method validators, ConfigValidators method, ValidateConfig method
  • ConfigureProvider - provider.Provider implementation Configure method
  • ValidateResourceConfig - resource.Resource implementation Configure method, Schema method validators, ConfigValidators method, ValidateConfig method
  • UpgradeResourceState - (Only advanced use cases would ever invoke provider-defined code for this) resource.Resource implementation Configure method, StateUpgraders method
  • PlanResourceChange - resource.Resource implementation Configure method, Read method (if resource previously existed in state), Schema method plan modifiers, ModifyPlan method
  • ApplyResourceChange - resource.Resource implementation Configure method, Create/Delete/Update method (as appropriate)

Without seeing the full resource implementation its hard to immediately tell why that error is being generated, but it silly as it might be, it might also be good to confirm that the Events2MetricResourceModel type definition is correctly saved on disk.