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

ListAttribute does not allows in Blocks? #885

Closed
duytiennguyen-okta opened this issue Dec 2, 2023 · 3 comments
Closed

ListAttribute does not allows in Blocks? #885

duytiennguyen-okta opened this issue Dec 2, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@duytiennguyen-okta
Copy link

duytiennguyen-okta commented Dec 2, 2023

Module version

1.4.2

Relevant provider source code

func (r *datasource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
        resp.Schema = schema.Schema{
	        Blocks: map[string]schema.Block{
		        "setting": schema.SingleNestedBlock{
			        Description: "",
			        Attributes: map[string]schema.Attribute{
				        "src_list": schema.ListAttribute{
					        Description: "",
					        Computed:    true,
					        ElementType: types.StringType,
				        },
			        },
		        },
	        }
        }
}
func (d *datasource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
        srcList, diags := types.ListValueFrom(ctx, types.StringType, []string{"abc", "xyz"})
        if diags.HasError() {
	        resp.Diagnostics.Append(diags...)
	        return
        }
}

Terraform Configuration Files

This issue is not related to terraform, only the terraform plugin framework

...

Debug Output

Expected Behavior

There should not be an error when running

Actual Behavior

I received the following error

An unexpected error was encountered while verifying an attribute value
matched its expected type to prevent unexpected behavior or panics. This is
always an error in the provider. Please report the following to the provider
developer:
Expected framework type from provider logic:
types.ListType[basetypes.StringType] / underlying type:
tftypes.List[tftypes.String]
Received framework type from provider logic: types.ListType[!!! MISSING TYPE
!!!] / underlying type: tftypes.List[tftypes.DynamicPseudoType]
Path: src_list

Steps to Reproduce

References

Note

I should note that I do not have issue when I was using ListAttribute inside Attributes instead of Blocks

func (r *datasource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
        resp.Schema = schema.Schema{
	        Attributes: map[string]schema.Attribute{
		        "src_list": schema.ListAttribute{
			        Description: "",
			        Computed:    true,
			        ElementType: types.StringType,
		        },
	        },
        }
}

I just want to know if this behaviour is expected or because I am using it wrong

@duytiennguyen-okta duytiennguyen-okta added the bug Something isn't working label Dec 2, 2023
@austinvalle
Copy link
Member

Hey there @duytiennguyen-okta 👋🏻!

It's definitely valid to put a ListAttribute inside of a nested block, you'll just need to be sure you set the ElemType when creating new values, which the error you provided seems to indicate might be missing:

Received framework type from provider logic: types.ListType[!!! MISSING TYPE !!!] / underlying type: tftypes.List[tftypes.DynamicPseudoType]

I didn't see in your example the setting of the value, but here is a full example that gives you an idea of what I mean (I'm using the ObjectValueMust in here, but you'd likely want to handle diagnostics, as your example does 😄 ):

package provider

import (
	"context"

	"github.com/hashicorp/terraform-plugin-framework/attr"
	"github.com/hashicorp/terraform-plugin-framework/datasource"
	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

var _ datasource.DataSource = (*thingDataSource)(nil)

func NewThingDataSource() datasource.DataSource {
	return &thingDataSource{}
}

type thingDataSource struct{}

type thingDataSourceModel struct {
	BlockObj types.Object `tfsdk:"block_obj"` // BlockObject
}

type BlockObject struct {
	NestedList types.List `tfsdk:"nested_list"`
}

func (d *thingDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
	resp.TypeName = req.ProviderTypeName + "_thing"
}

func (d *thingDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
	resp.Schema = schema.Schema{
		Blocks: map[string]schema.Block{
			"block_obj": schema.SingleNestedBlock{
				Attributes: map[string]schema.Attribute{
					"nested_list": schema.ListAttribute{
						Computed:    true,
						ElementType: types.StringType,
					},
				},
			},
		},
	}
}

func (d *thingDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
	var data thingDataSourceModel

	resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
	if resp.Diagnostics.HasError() {
		return
	}

	data.BlockObj = types.ObjectValueMust(map[string]attr.Type{
		"nested_list": types.ListType{
			// This needs to be set, otherwise you might see a "MISSING TYPE" in your diagnostic
			ElemType: types.StringType,
		},
	}, map[string]attr.Value{
		"nested_list": types.ListValueMust(types.StringType, []attr.Value{types.StringValue("hey")}),
	})

	resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
data "examplecloud_thing" "this" {}

output "d" {
  value = data.examplecloud_thing.this.block_obj.nested_list
}
 $ terraform plan

data.examplecloud_thing.this: Reading...
data.examplecloud_thing.this: Read complete after 8s

Changes to Outputs:
  + d = [
      + "hey",
    ]

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.

Let me know if this helps!

@bflad bflad added the waiting-response Issues or pull requests waiting for an external response label Dec 28, 2023
@bflad
Copy link
Member

bflad commented Jan 30, 2024

Hi @duytiennguyen-okta 👋 Since we have not heard back from you in awhile, I'm going to close this issue. There is a time period before this issue will be automatically locked so please let us know about the above if you get a chance and we can reopen/revisit.

@bflad bflad closed this as not planned Won't fix, can't repro, duplicate, stale Jan 30, 2024
@github-actions github-actions bot removed the waiting-response Issues or pull requests waiting for an external response label Jan 30, 2024
Copy link

github-actions bot commented Mar 1, 2024

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 Mar 1, 2024
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
Development

No branches or pull requests

3 participants