Skip to content

Terraform plan/apply fails when setting a float field to a float value #29861

@eveld

Description

@eveld

Module version

Terraform v1.0.9
on linux_amd64

github.com/hashicorp/terraform-plugin-framework v0.4.2
github.com/hashicorp/terraform-plugin-go v0.4.0

Relevant provider source code

func (r resourceCubeType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
	return tfsdk.Schema{
		Attributes: map[string]tfsdk.Attribute{
			"id": {
				Type:     types.StringType,
				Computed: true,
			},
			"last_updated": {
				Type:     types.StringType,
				Computed: true,
			},
			"name": {
				Type:     types.StringType,
				Required: true,
			},
			"position": {
				Required: true,
				Attributes: tfsdk.SingleNestedAttributes(map[string]tfsdk.Attribute{
					"x": {
						Type:     types.Float64Type,
						Required: true,
					},
					"y": {
						Type:     types.Float64Type,
						Required: true,
					},
					"z": {
						Type:     types.Float64Type,
						Required: true,
					},
				}),
			},
		},
	}, nil
}

func (r resourceCube) Create(ctx context.Context, req tfsdk.CreateResourceRequest, resp *tfsdk.CreateResourceResponse) {
	if !r.p.configured {
		resp.Diagnostics.AddError(
			"Provider not configured",
			"The provider hasn't been configured before apply, likely because it depends on an unknown value from another resource. This leads to weird stuff happening, so we'd prefer if you didn't do that. Thanks!",
		)
		return
	}

	var plan Cube
	diags := req.Plan.Get(ctx, &plan)
	resp.Diagnostics.Append(diags...)
	if resp.Diagnostics.HasError() {
		return
	}

	cube := game.Cube{
		Name: plan.Name.Value,
		Position: game.Position{
			X: plan.Position.X.Value,
			Y: plan.Position.Y.Value,
			Z: plan.Position.Z.Value,
		},
	}

	result, err := r.p.client.CreateCube(cube)
	if err != nil {
		resp.Diagnostics.AddError(
			"Error creating cube",
			"Could not create cube, unexpected error: "+err.Error(),
		)
		return
	}

	// update more fields once they can differ between result and plan.

	plan.ID = types.String{Value: result.ID}
	plan.LastUpdated = types.String{Value: string(time.Now().Format(time.RFC850))}

	diags = resp.State.Set(ctx, plan)
	resp.Diagnostics.Append(diags...)
	if resp.Diagnostics.HasError() {
		return
	}
}

Terraform Configuration Files

resource "game_cube" "player_1" {
	name = "player_1"

	position = {
		x = -11.28
		y = 0
		z = -38.15
	}
}

Expected Behavior

Expect the resource to be created at the given coordinates.

Actual Behavior

Instead of succeeding, it gives me the following error for each of the float fields that actually contain a float value:

Provider "local/hashicraft/game" planned an invalid value for game_cube.player_1.position.x: planned value
│ cty.NumberFloatVal(-11.23) does not match config value cty.MustParseNumberVal("-11.23").

When I change the x and z positions to an integer, the apply actually succeeds.

resource "game_cube" "player_1" {
	name = "player_1"

	position = {
		x = -11
		y = 0
		z = -38
	}
}

Steps to Reproduce

  1. terraform init
  2. terraform apply

Metadata

Metadata

Assignees

Labels

bugconfigconfirmeda Terraform Core team member has reproduced this issueupstream

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions