When using TypeSet, Terraform indicates the hash indexes are calculated based on the values of the set. I have a Terraform Provider written in Go using the Terraform Provider Go SDK where I currently need to hard code these index values in the tests. Can hardcoding the values in the Terraform Provider tests be avoided?
Terraform docs say the following, but don't provide information on how to calculate the hash index values or avoid calculating them.
TypeSetitems are stored in state with an index value calculated by the hash of the attributes of the set.
Ref: Terraform docs: https://www.terraform.io/plugin/sdkv2/schemas/schema-types
Here's the example provided in the documentation where 1061987227 and 493694946 are auto-generated hash index values.
"ingress.#": "2",
"ingress.1061987227.cidr_blocks.#": "1",
"ingress.1061987227.cidr_blocks.0": "10.0.0.0/8",
"ingress.1061987227.description": "",
"ingress.1061987227.from_port": "80",
"ingress.1061987227.ipv6_cidr_blocks.#": "0",
"ingress.1061987227.protocol": "tcp",
"ingress.1061987227.security_groups.#": "0",
"ingress.1061987227.self": "false",
"ingress.1061987227.to_port": "9000",
"ingress.493694946.cidr_blocks.#": "2",
"ingress.493694946.cidr_blocks.0": "0.0.0.0/0",
"ingress.493694946.cidr_blocks.1": "10.0.0.0/8",
"ingress.493694946.description": "",
"ingress.493694946.from_port": "80",
"ingress.493694946.ipv6_cidr_blocks.#": "0",
"ingress.493694946.protocol": "tcp",
"ingress.493694946.security_groups.#": "0",
"ingress.493694946.self": "false",
"ingress.493694946.to_port": "8000",
Currently, in my Go tests, I hardcode the hash index values which I'd like to avoid. Here's an excerpt with 3 hard coded index values: 368698502, 1552086545, and 3172309932:
resource.TestCheckResourceAttr(resourceName, "workspace_configs.368698502.client_ip_header", "Fastly-Client-IP"),
resource.TestCheckResourceAttr(resourceName, "workspace_configs.368698502.instance_location", "advanced"),
resource.TestCheckResourceAttr(resourceName, "workspace_configs.368698502.listener_protocols.#", "1"),
resource.TestCheckResourceAttr(resourceName, "workspace_configs.368698502.listener_protocols.1552086545", "https"),
resource.TestCheckResourceAttr(resourceName, "workspace_configs.368698502.routes.#", "1"),
resource.TestCheckResourceAttr(resourceName, "workspace_configs.368698502.routes.3172309932.certificate_ids.#", "0"),
resource.TestCheckResourceAttr(resourceName, "workspace_configs.368698502.routes.3172309932.connection_pooling", "true"),
The full code is here: https://github.com/signalsciences/terraform-provider-sigsci/blob/main/provider/resource_corp_cloudwaf_instance_test.go
Is there a way to avoid hard coding the index values? For example, is it possible to create a function that fulfills the CheckResourceAttrWithFunc interface used with TestCheckResourceAttrWith?