Skip to content
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

Error decoding attributes/blocks #328

Closed
abergmeier opened this issue May 14, 2022 · 3 comments · Fixed by #371
Closed

Error decoding attributes/blocks #328

abergmeier opened this issue May 14, 2022 · 3 comments · Fixed by #371
Assignees
Labels
bug Something isn't working
Milestone

Comments

@abergmeier
Copy link

Module version

github.com/hashicorp/terraform-plugin-framework v0.8.0

Relevant provider source code

var (
	outputDockerAttributes = map[string]tfsdk.Attribute{
		"dest": {
			Type:     types.StringType,
			Optional: true,
			PlanModifiers: tfsdk.AttributePlanModifiers{
				tfsdk.RequiresReplace(),
			},
		},
		"context": {
			Type:     types.StringType,
			Optional: true,
			PlanModifiers: tfsdk.AttributePlanModifiers{
				tfsdk.RequiresReplace(),
			},
		},
	}
	outputBlocks                             = map[string]tfsdk.Block{
		"docker": {
			Attributes:  outputDockerAttributes,
			MaxItems:    1,
			NestingMode: tfsdk.BlockNestingModeList,
			PlanModifiers: tfsdk.AttributePlanModifiers{
				tfsdk.RequiresReplace(),
			},
		},
		"image": {
			Attributes:  outputImageAttributes,
			MaxItems:    1,
			NestingMode: tfsdk.BlockNestingModeList,
			PlanModifiers: tfsdk.AttributePlanModifiers{
				tfsdk.RequiresReplace(),
			},
		},
		"local": {
			Attributes:  outputLocalAttributes,
			MaxItems:    1,
			NestingMode: tfsdk.BlockNestingModeList,
			PlanModifiers: tfsdk.AttributePlanModifiers{
				tfsdk.RequiresReplace(),
			},
		},
		"oci": {
			Attributes:  outputOciAttributes,
			MaxItems:    1,
			NestingMode: tfsdk.BlockNestingModeList,
			PlanModifiers: tfsdk.AttributePlanModifiers{
				tfsdk.RequiresReplace(),
			},
		},
		"tar": {
			Attributes:  outputTarAttributes,
			MaxItems:    1,
			NestingMode: tfsdk.BlockNestingModeList,
			PlanModifiers: tfsdk.AttributePlanModifiers{
				tfsdk.RequiresReplace(),
			},
		},
	}
)

func (t builtResourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
	return tfsdk.Schema{
		Blocks: map[string]tfsdk.Block{
			"output": {
				MinItems:            1,
				MaxItems:            1,
				Blocks:              outputBlocks,
				NestingMode:         tfsdk.BlockNestingModeList,
				Description:         "Output destination. For entries of type gha, GitHub Action credentials are automatically added to attrs.",
				MarkdownDescription: "Output destination. For entries of `type` `gha`, GitHub Action credentials are automatically added to attrs.",
				PlanModifiers: tfsdk.AttributePlanModifiers{
					tfsdk.RequiresReplace(),
				},
			},
		},
	}
}

Terraform Configuration Files

resource "buildx_built" "foo" {
  output {
    docker {
      dest = "image.docker"
    }
  }
}

Expected Behavior

It should map config to Schema or indicate what is actually the problem.

Actual Behavior

It reported an error about Attributes while there seem to be only Blocks used:

The provider had a problem parsing the config. Report this to the provider
developer:
AttributeName("output").ElementKeyInt(0): error decoding object; expected 0
attributes, got 5

Steps to Reproduce

CGO_ENABLED=0 TF_ACC=1 TF_LOG=trace go test ./internal/resources/builttest -v

References

@abergmeier abergmeier added the bug Something isn't working label May 14, 2022
@bflad
Copy link
Contributor

bflad commented Jun 9, 2022

Hi @abergmeier 👋 Thank you for reporting this and sorry you ran into trouble here. I was able to successfully reproduce the issue.

Good news is that some of our recent refactoring efforts have improved the error messaging here already. On main (likely similar to what will release with v0.9.0 in this regard), the error message now very clearly says the framework messed up and when:

    example_resource_test.go:11: Step 1/1 error: Error running pre-apply refresh: exit status 1

        Error: Unable to Convert Configuration

          with blox_example.test,
          on terraform_plugin_test.tf line 2, in resource "blox_example" "test":
           2: resource "blox_example" "test" {

        An unexpected error was encountered when converting the configuration from
        the protocol type. This is always an issue in the Terraform Provider SDK used
        to implement the provider and should be reported to the provider developers.

        Please report this to the provider developer:

        AttributeName("output").ElementKeyInt(0): error decoding object; expected 0
        attributes, got 5

Bad news is that there certainly is unexpected behavior here in the framework that needs to be fixed to prevent the issue as I'm not immediately seeing anything wrong with that schema definition.

I'm running out of time during my work day today and I'm offline tomorrow, but will keep you posted next week.

@bflad bflad self-assigned this Jun 9, 2022
bflad added a commit that referenced this issue Jun 9, 2022
Reference: #328

Previously the framework could return errors similar to the following:

```
Error: Error parsing config

The provider had a problem parsing the config. Report this to the provider
developer:
AttributeName("output").ElementKeyInt(0): error decoding object; expected 0
attributes, got 5
```

New unit testing failures before fix:

```
--- FAIL: TestBlockAttributeType (0.00s)
    --- FAIL: TestBlockAttributeType/NestingMode-Set (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/tfsdk/block_test.go:103: unexpected difference:   types.SetType(
            - 	s`types.SetType[types.ObjectType["test_attribute":types.StringType]]`,
            + 	s`types.SetType[types.ObjectType["test_attribute":types.StringType, "test_block":types.SetType[types.ObjectType["test_block_attribute":types.StringType]]]]`,
              )
    --- FAIL: TestBlockAttributeType/NestingMode-List (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/tfsdk/block_test.go:103: unexpected difference:   types.ListType(
            - 	s`types.ListType[types.ObjectType["test_attribute":types.StringType]]`,
            + 	s`types.ListType[types.ObjectType["test_attribute":types.StringType, "test_block":types.ListType[types.ObjectType["test_block_attribute":types.StringType]]]]`,
              )

--- FAIL: TestBlockTerraformType (0.00s)
    --- FAIL: TestBlockTerraformType/NestingMode-List (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/tfsdk/block_test.go:199: unexpected difference:   tftypes.List(
            - 	s`tftypes.List[tftypes.Object["test_attribute":tftypes.String]]`,
            + 	s`tftypes.List[tftypes.Object["test_attribute":tftypes.String, "test_block":tftypes.List[tftypes.Object["test_block_attribute":tftypes.String]]]]`,
              )
    --- FAIL: TestBlockTerraformType/NestingMode-Set (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/tfsdk/block_test.go:199: unexpected difference:   tftypes.Set(
            - 	s`tftypes.Set[tftypes.Object["test_attribute":tftypes.String]]`,
            + 	s`tftypes.Set[tftypes.Object["test_attribute":tftypes.String, "test_block":tftypes.Set[tftypes.Object["test_block_attribute":tftypes.String]]]]`,
              )
```
@bflad
Copy link
Contributor

bflad commented Jun 9, 2022

Turns out I was able to find the issue and submit #371 right now, which prevents the error and introduces extra unit testing to cover similar functionality. I was also able to verify the fix in the reproduction provider that previously threw the error.

@bflad bflad added this to the v0.9.0 milestone Jun 9, 2022
bflad added a commit that referenced this issue Jun 13, 2022
…ks (#371)

Reference: #328

Previously the framework could return errors similar to the following:

```
Error: Error parsing config

The provider had a problem parsing the config. Report this to the provider
developer:
AttributeName("output").ElementKeyInt(0): error decoding object; expected 0
attributes, got 5
```

New unit testing failures before fix:

```
--- FAIL: TestBlockAttributeType (0.00s)
    --- FAIL: TestBlockAttributeType/NestingMode-Set (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/tfsdk/block_test.go:103: unexpected difference:   types.SetType(
            - 	s`types.SetType[types.ObjectType["test_attribute":types.StringType]]`,
            + 	s`types.SetType[types.ObjectType["test_attribute":types.StringType, "test_block":types.SetType[types.ObjectType["test_block_attribute":types.StringType]]]]`,
              )
    --- FAIL: TestBlockAttributeType/NestingMode-List (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/tfsdk/block_test.go:103: unexpected difference:   types.ListType(
            - 	s`types.ListType[types.ObjectType["test_attribute":types.StringType]]`,
            + 	s`types.ListType[types.ObjectType["test_attribute":types.StringType, "test_block":types.ListType[types.ObjectType["test_block_attribute":types.StringType]]]]`,
              )

--- FAIL: TestBlockTerraformType (0.00s)
    --- FAIL: TestBlockTerraformType/NestingMode-List (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/tfsdk/block_test.go:199: unexpected difference:   tftypes.List(
            - 	s`tftypes.List[tftypes.Object["test_attribute":tftypes.String]]`,
            + 	s`tftypes.List[tftypes.Object["test_attribute":tftypes.String, "test_block":tftypes.List[tftypes.Object["test_block_attribute":tftypes.String]]]]`,
              )
    --- FAIL: TestBlockTerraformType/NestingMode-Set (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/tfsdk/block_test.go:199: unexpected difference:   tftypes.Set(
            - 	s`tftypes.Set[tftypes.Object["test_attribute":tftypes.String]]`,
            + 	s`tftypes.Set[tftypes.Object["test_attribute":tftypes.String, "test_block":tftypes.Set[tftypes.Object["test_block_attribute":tftypes.String]]]]`,
              )
```
@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
2 participants