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

Dynamic Pseudo Types inside of Blocks with nested mode of list cant be unmarshalled to tfvalues #267

Open
Nciso opened this issue Feb 28, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@Nciso
Copy link

Nciso commented Feb 28, 2023

terraform-plugin-go version

v0.14.3

Relevant provider source code

// this is making a panic, not even  getting to an error
values, err := req.Config.Unmarshal(tftypes.Object{
   // objTypeDef.AttributeTypes is the result of calling ValueType() on  a  tfprotov5.Schema
    AttributeTypes: objTypeDef.AttributeTypes
})


// this is the tfprotov5 schema

    tfprotov5.Schema{
		Version: 1,
		Block: &tfprotov5.SchemaBlock{
			Version: 1,
			Attributes: []*tfprotov5.SchemaAttribute{
				{
					Name:     "id",
					Type:     tftypes.String,
					Computed: true,
				},
				{
					Name:     "regular_attribute",
					Type:     tftypes.String,
					Optional: true,
				},
				{
					Name:     "dynamic_attribute",
					Type:     tftypes.DynamicPseudoType,
					Optional: true,
				},
			},
			BlockTypes: []*tfprotov5.SchemaNestedBlock{
				{
					TypeName: "dynamic_block",
					Nesting:  tfprotov5.SchemaNestedBlockNestingModeList,
					Block: &tfprotov5.SchemaBlock{
						Attributes: []*tfprotov5.SchemaAttribute{
							{
								Name:     "bar",
								Type:     tftypes.DynamicPseudoType,
								Optional: true,
							},
							{
								Name:     "foo",
								Type:     tftypes.Number,
								Optional: true,
							},
						},
					},
				},
				{
					TypeName: "regular_block",
					Nesting:  tfprotov5.SchemaNestedBlockNestingModeList,
					Block: &tfprotov5.SchemaBlock{
						Attributes: []*tfprotov5.SchemaAttribute{
							{
								Name:     "bar",
								Type:     tftypes.String,
								Optional: true,
							},
							{
								Name:     "foo",
								Type:     tftypes.Number,
								Optional: true,
							},
						},
					},
				},
			},
		},
	}

E2E test https://github.com/Nciso/low_level_provider_example/blob/main/internal/data_dummy/dummy_data_test.go#L17

Terraform Configuration Files

data "provider_dummy" "test" {
  dynamic_attribute = "hello"
  regular_attribute = "bye"
  regular_block {
    bar = "bar"
    foo = 3
  }
  dynamic_block {
    bar = "bar"
    foo = 4
  }
}

Expected Behavior

it should be able to create a statefile, this behavior is happening if your remove the DPT of the dynamic_block schema and change it to a string, (tftypes.String instead of tftypes.DynamicPseudoType )

Actual Behavior

A panic is produced

2023-02-28T17:19:19.961-0600 [ERROR] sdk.proto: Error from downstream: tf_proto_version=5.3 tf_rpc=ReadDataSource tf_data_source_type=provider_dummy tf_req_id=4939bb6e-c465-28c6-470f-5a42012f85e4 error="Cant unmarshall config input, AttributeName(\"dynamic_block\").ElementKeyInt(0): error decoding object length: msgpack: invalid code=c4 decoding map length" tf_provider_addr=registry.terraform.io/hashicorp/provider
    provider_low_example/internal/data_dummy/dummy_data_test.go:21: Step 1/1 error: Error running pre-apply refresh: exit status 1

        Error: Plugin error

          with data.provider_dummy.test,
          on terraform_plugin_test.tf line 3, in data "provider_dummy" "test":
           3:   data "provider_dummy" "test" {

        The plugin returned an unexpected error from
        plugin.(*GRPCProvider).ReadDataSource: rpc error: code = Unknown desc = Cant
        unmarshall config input, AttributeName("dynamic_block").ElementKeyInt(0):
        error decoding object length: msgpack: invalid code=c4 decoding map length

Steps to Reproduce

Run the following test

https://github.com/Nciso/low_level_provider_example/blob/main/internal/data_dummy/dummy_data_test.go#L17

References

Inspecting with the debugger I found the following:

@Nciso Nciso added the bug Something isn't working label Feb 28, 2023
@Nciso Nciso changed the title Dynamic Pseudo Types inside of Blocks with nested mode of list cant be unmarshalled to tftyvalues Dynamic Pseudo Types inside of Blocks with nested mode of list cant be unmarshalled to tfvalues Feb 28, 2023
@tyler-dunkel
Copy link

Hi, encountering a similar issue and wondering if there were any updates here? Thanks!

@frezbo
Copy link

frezbo commented Apr 3, 2023

There seems to be a similar issue with using DynamicPseudoType with tftypes.List

                                                  {
							Name: "config_patches",
							Type: tftypes.List{
								ElementType: tftypes.DynamicPseudoType,
							},
							Optional:    true,
						},

this returns an error way earlier in plan

│ Inappropriate value for attribute "config_patches": all list elements must have the same
│ type.

when using tftypes.Tuple to work around the problem, the doc generation fails https://github.com/hashicorp/terraform-plugin-docs/blob/dc2adaa2368d68ece586a9e49cd4bc75924148cf/schemamd/render.go#L83

frezbo added a commit to siderolabs/terraform-provider-talos that referenced this issue Apr 4, 2023
Example usage of TF mux and plugin go.
There seems to be some limitations around using `DynamicPseudoType` with
lists and the TF docs generation tool lack support for tuples. So for
now it's better to stick to plugin framework only.

Ref: hashicorp/terraform-plugin-go#267

Signed-off-by: Noel Georgi <git@frezbo.dev>
@bflad bflad removed their assignment Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants