Provider Plugin Framework - Data Source with No ID

Trying to utilize the new plugin framework for use in a custom provider and wondering how best to handle a data source that returns a list of items with their own IDs but does not have an ID itself (for the list). Do you normally assign a date/time as ID? Will that cause issues as the ID is refreshed each time it is pulled in?

Initially I didn’t bother setting an ID but when I tried to create some testing around the data source it screamed at me for lack of an ID associated with the data source:

testing_new_config.go:81: no "id" found in attributes
    testing_new.go:53: no "id" found in attributes
1 Like

The testing creating the error:

func TestAccCloudsDataSource(t *testing.T) {
	resource.Test(t, resource.TestCase{
		PreCheck:                 func() { testAccPreCheck(t) },
		ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
		Steps: []resource.TestStep{
			// Read testing
			{
				Config: testAccCloudsDataSourceConfig,
				Check: resource.ComposeAggregateTestCheckFunc(
					resource.TestCheckTypeSetElemAttr("data.myexample_clouds.test", "clouds", "1"),
				),
			},
		},
	})
}

const testAccCloudsDataSourceConfig = `
data "myexample_clouds" "test" {}
`

The error appears before it ever runs the TestCheckTypeSetElemAttr() call.

2 Likes

Good Day,

Curious to know how you’ve managed to resolve this issue?
I’ve referred to this bit of advice, Plugin Development - Framework: Acceptance Tests | Terraform by HashiCorp
but despite adding a computed “id” attribute, my test case still fails with the exact error as yours did.

Thanks in advance!

I just had this issue as well. In my case, it turns out it was a copy-paste error, and my data source’s ReadFunc/ReadContextFunc was actually using the resource’s read function instead of the data source’s one.

I’ve gotten this error when the data source used d.SetId(id) when id was an empty string.