HCL2 JSON Syntax: network_adapters and storage blocks

I’m trying to build a VM using the HCL2 JSON syntax. It’s not a deal breaker if this is impossible; I just prefer to use the JSON syntax as I find it easier and cleaner to manipulate programmatically. I keep running into the issue of adding storage and network_adapters complex types. Here is what I have currently:

var-defs.pkr.json:

{
    "variable": {
        "network_adapters": {
            "description": "List of network adapters to add to the VM.",
            "type": "list(object({ network=string, network_card=string }))",
            "default": [
                {
                    "network": "Infra",
                    "network_card": "vmxnet3"
                }
            ]
        },

        "storage": {
            "description": "List of virtual disks to add to the VM.",
            "type": "list(object({ disk_controller_index=number, disk_size=number, disk_thin_provisioned=bool }))",
            "default": [
                {
                    "disk_controller_index": 0,
                    "disk_size": 65536,
                    "disk_thin_provisioned": true
                }
            ]
        }
    }
}

I also tried setting the type as "list(map(string))".

var.auto.pkrvars.json:

{
    "network_adapters" : [
        {
            "network": "Infra",
            "network_card": "vmxnet3"
        }
    ],

    "storage" : [
        {
            "disk_controller_index" : 0,
            "disk_size" : 65536,
            "disk_thin_provisioned" : true
        },

        {
            "disk_controller_index" : 0,
            "disk_size" : 65536,
            "disk_thin_provisioned" : true
        }
    ]
}

I’ve tried the following approaches with no success:

source.pkr.json (direct assignment):

{
    "source" : {
        "vsphere-iso" : {
            "hcl2-json-build-vm" : {
                "network_adapters" : "${var.network_adapters}",
                "storage" : "${var.storage}"
            }
        }
    }
}

source.pkr.json (splatting):

{
    "source" : {
        "vsphere-iso" : {
            "hcl2-json-build-vm" : {
                "network_adapters" : [
                    {
                        "network" : "${var.network_adapters[*].network}",
                        "network_card" : "${var.network_adapters[*].network_card}"
                    }
                ],
                
                "storage" : [
                    {
                        "disk_controller_index" : "${var.storage[*].disk_controller_index}",
                        "disk_size" : "${var.storage[*].disk_size}",
                        "disk_thin_provisioned" : "${var.storage[*].disk_thin_provisioned}"
                    }
                ]
            }
        }
    }
}

source.pkr.json (dynamic blocks):

{
    "source" : {
        "vsphere-iso" : {
            "hcl2-json-build-vm" : {
                "dynamic" : {
                    "network_adapters" : {
                        "for_each" : "${var.network_adapters}",
                        "content"  : {
                            "network" : "${network_adapters.network}",
                            "network_card" : "${network_adapters.network_card}"
                        }
                    },

                    "storage" : {
                        "for_each" : "${var.storage}",
                        "content"  : {
                            "disk_controller_index" : "${storage.disk_controller_index}",
                            "disk_size" : "${storage.disk_size}",
                            "disk_thin_provisioned" : "${storage.disk_thin_provisioned}"
                        }
                    }
                }
            }
        }
    }
}

Can anyone help point out my error, or let me know if this is even possible currently? Much appreciated!

To document better (and give the thread a bump, let’s be honest) the errors I get when trying to define network_adapters and storage in user variables and using the splatting method are:

Error: Incorrect attribute value type
  on source.pkr.json line 39:
  (source code not available)
with var.storage as tuple with 2 elements.
Inappropriate value for attribute "disk_controller_index": number required.

Error: Incorrect attribute value type
  on source.pkr.json line 40:
  (source code not available)
with var.storage as tuple with 2 elements.
Inappropriate value for attribute "disk_size": number required.

Error: Incorrect attribute value type
  on source.pkr.json line 41:
  (source code not available)
with var.storage as tuple with 2 elements.
Inappropriate value for attribute "disk_thin_provisioned": bool required.

Error: Incorrect attribute value type
  on source.pkr.json line 32:
  (source code not available)
with var.network_adapters as tuple with 1 element.
Inappropriate value for attribute "network": string required.

Error: Incorrect attribute value type
  on source.pkr.json line 33:
  (source code not available)
with var.network_adapters as tuple with 1 element.
Inappropriate value for attribute "network_card": string required.

The string interpolation doesn’t seem to work as expected. Am I doing it wrong?

“storage is a tuple with 2 elements” because it has a key and a value, and the pieces are stored in the value. Try

                            "disk_thin_provisioned" : "${storage.value.disk_thin_provisioned}"

etc instead.