dmacvicar / terraform-provider-libvirt

Terraform provider to provision infrastructure with Linux's KVM using libvirt
Apache License 2.0
1.6k stars 458 forks source link

Terraform JSON Disk block issues #728

Open Unfairz opened 4 years ago

Unfairz commented 4 years ago

System Information

Linux distribution

Centos7

Terraform version

Terraform v0.12.24

Provider and libvirt versions

./terraform-provider-libvirt 0.6.2+git.1585292411.8cbe9ad0
Compiled against library: libvirt 4.1.0
Using library: libvirt 4.5.0

Checklist

Description of Issue/Question

Setup

{
  "data": [
    {
      "template_file": [
        {
          "user_data": [
            {
              "template": "$file(\"/home/terraform/config/cloud_init.cfg\")"
            }
          ]
        }
      ]
    },
    {
      "template_file": [
        {
          "meta_data": [
            {
              "template": "file(\"/home/terraform/config/network_config.cfg\")"
            }
          ]
        }
      ]
    }
  ],
  "resource": [
    {
      "libvirt_volume": [
        {
          "vm-001": [
            {
              "format": "qcow2",
              "name": "vm-001.qcow2",
              "pool": "vm-storage",
              "source": "/home/terraform/images/CentOS-7.qcow2"
            }
          ]
        }
      ]
    },
    {
      "libvirt_cloudinit_disk": [
        {
          "cloudinit": [
            {
              "meta_data": "${data.template_file.meta_data.rendered}",
              "name": "cloudinit.iso",
              "pool": "vm-storage",
              "user_data": "${data.template_file.user_data.rendered}"
            }
          ]
        }
      ]
    },
    {
      "libvirt_domain": [
        {
          "vm-001": [
            {
              "autostart": "true",
              "cloudinit": "${libvirt_cloudinit_disk.cloudinit.id}",
              "memory": "2048",
              "name": "vm-001",
              "network_interface": [
                {
                   "bridge": "br0"
                }
              ],
              "disk": [
                {
                 "volume_id": "${libvirt_volume.vm-001.id}"
                }
              ],
              "running": "true",
              "vcpu": "2"
            }
          ]
        }
      ]
    }
  ]
}

Steps to Reproduce Issue

Hello folks! I have been dealing with this problem for quite some time now and am hoping to be missing something really simple at this point!

Basically, when I use the JSON syntax of building the terraform files, I cannot get the disk portition to function correctly and the error given is quite odd.

Error: Incorrect attribute value type

  on vm.tf.json line 69, in resource[2].libvirt_domain[0].vm-001[0]:
  69:               "disk": [
  70:                 {
  71:                  "volume_id": "${libvirt_volume.vm-001.id}"
  72:                 }
  73:               ],

Inappropriate value for attribute "disk": element 0: attributes
"block_device", "file", "scsi", "url", and "wwn" are required.

I thought it would be caused due to the way JSON maps values, however, in my testing I wasn't able to get it working.

Odd part is that if I go ahead and change the "volume_id" to a "block_device", for example, the error message changes to:

Inappropriate value for attribute "disk": element 0: attributes
"volume_id", "file", "scsi", "url", and "wwn" are required.

If I delete the disk block, the rest works perfectly fine, without the disk (to be expected).

Huge thanks for taking your time to take a look into this and sorry if it's something really simple that I might have missed!

Unfairz commented 4 years ago

Following up, I have double-checked and it seems like, in order to get the disk block to function, I would need to apply all of the attributes and set the ones that I don't need to null, as this doesn't set it automatically:

              "disk": [
                {
                 "volume_id": "${libvirt_volume.vps-001.id}",
                 "block_device": null,
                 "file": null,
                 "scsi": null,
                 "url": null,
                 "wwn": null
                }
              ],

This works and seems to be caused by the #665 merge which included the different mapping. Not sure this can be resolved due to the specific JSON syntax and the way it handles mapping.

MalloZup commented 4 years ago

@Unfairz thx for issue.We might needt to check if we can make json happy.

However the json format has less prio then the others since it is less used. But we might check if we can find a solution where there both compatible. it should exists one

RouxAntoine commented 4 years ago

Hello,

I notice same error with hcl format.

--> terraform apply

Error: Incorrect attribute value type

  on computes.tf line 18, in resource "libvirt_domain" "db1":
  18:   disk = [
  19:     {
  20:       volume_id = libvirt_volume.my_root_centos.id
  21:     },
  22:     {
  23:       volume_id = libvirt_volume.my_root_debian.id
  24:     }
  25:   ]

Inappropriate value for attribute "disk": element 0: attributes
"block_device", "file", "scsi", "url", and "wwn" are required.

I try to look at code but here https://github.com/dmacvicar/terraform-provider-libvirt/blob/v0.6.2/libvirt/resource_libvirt_domain.go#L146 attributes are marked as optional. and i fail to find error message Inappropriate value for attribute "disk". Does it had anything to do with non set value Computed: true ?

scabala commented 1 month ago

I think the last error is related to #1032