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

clone from template - Having more disk sub-resources defined than exist in the template results in "index out of range" #285

Closed
jason-azze opened this issue Dec 7, 2017 · 2 comments

Comments

@jason-azze
Copy link
Contributor

Terraform Version

Terraform v0.11.1

vSphere Provider Version

  • provider.vsphere v1.0.3

Affected Resource(s)

  • vsphere_virtual_machine > disk

Terraform Configuration Files

A chunk of my "called" module.

resource "vsphere_virtual_machine" "generic" {
  name             = "${var.name_prefix}${format("%02d", count.index + var.name_starting_val)}"
  resource_pool_id = "${data.vsphere_resource_pool.target-resource-pool.id}"
  datastore_id     = "${data.vsphere_datastore.target-datastore.id}"

  count                = "${var.how_many}"
  folder               = "${var.vm_folder_name}"
  num_cpus             = "${var.vcpu_count}"
  num_cores_per_socket = "${var.cores_per_socket}" 
  memory               = "${var.memory_amount}"
  guest_id             = "${data.vsphere_virtual_machine.source-template.guest_id}"
  scsi_type            = "${data.vsphere_virtual_machine.source-template.scsi_type}"

  /* We assume here that a template won't have more than two disks. The documentation 
            says that "You must specify at least the same number of disk sub-resources as there
            are disks that exist in the template." which with "at least" suggests that we can 
            have more disk sub-resources than we need but not fewer.*/
  disk {
    name             = "${var.name_prefix}${format("%02d", count.index + var.name_starting_val)}disk01.vmdk"
    size             = "${data.vsphere_virtual_machine.source-template.disk_sizes[0]}"
    thin_provisioned = "true"
  }

  disk {
    name             = "${var.name_prefix}${format("%02d", count.index + var.name_starting_val)}disk02.vmdk"
    size             = "${data.vsphere_virtual_machine.source-template.disk_sizes[1]}"
    thin_provisioned = "true"
  }

...

Debug Output

Error: Error running plan: 5 error(s) occurred:

* module.utility-agents.vsphere_virtual_machine.generic: 1 error(s) occurred:

* module.utility-agents.vsphere_virtual_machine.generic: index 1 out of range for list data.vsphere_virtual_machine.source-template.disk_sizes (max 1) in:

${data.vsphere_virtual_machine.source-template.disk_sizes[1]}

Expected Behavior

In the vsphere_virtual_machine resource documentation, under "Additional requirements and notes for cloning" it says:

You must specify at least the same number of disk sub-resources as there are disks that exist in the template. These sub-resources are ordered and lined up by the unit_number attribute. Additional disks can be added past this.

This suggests that I can specify more disk sub-resources than I need but not fewer.
Why would I want to do this? Because I don't know in advance how many disks a template might have.
In this case, my template has one disk, but I have "left room" for two in my module.
I want the documentation's "at least" to be true, but it seems it's not. (Or, more likely, I'm just doing something wrong.)

Actual Behavior

Error: Error running plan: 5 error(s) occurred:

* module.utility-agents.vsphere_virtual_machine.generic: 1 error(s) occurred:

* module.utility-agents.vsphere_virtual_machine.generic: index 1 out of range for list data.vsphere_virtual_machine.source-template.disk_sizes (max 1) in:

${data.vsphere_virtual_machine.source-template.disk_sizes[1]}

Steps to Reproduce

  1. Specify more disks than your VM template has.
  2. terraform apply

Important Factoids

I love all the options the new provider gives me. I just have to learn how to use them. I hope this is just a case of me having to adjust my expectations, or, and easy fix. Thank you for all of your work, Chris.

References

Not really #264. I just want to prove I searched.

@vancluever
Copy link
Contributor

Hey @jason-azze, thanks for kind words! Hopefully I can clarify here:

The error you are seeing is due to there not being 2 elements in the disk_sizes attribute of your vsphere_virtual_machine data source - since there are not two disks in the template as you mentioned, there is only one element in disk_sizes, hence the "index out of range" error when trying to reference the missing element.

As to why we enforce this: a template - which is supposed to be an essentially finished and sealed product ready to use as made - will more than likely fail if it gets created with less disks that it contained (say in the example of a template made with LVM). Conversely, one may want to, either immediately or on a later date, add extra disks to facilitate things like extra data storage to satisfy a specific use case. This is why we allow someone to add more disks, but not less.

When working with modules, things can get a little tricky here. Unfortunately sub-resources don't necessarily scale - this has been a historic issue with Terraform and not necessarily the vSphere provider itself though. We are working towards possibly having the data source being a pretty much 1-1 pass-in of options as #283 moves disk_sizes to the disks sub-resource and adds attributes for eagerly_scrub and thin_provisioned, so at some point in time you may actually be able to do disk = ${data.vsphere_virtual_machine.source-template.disks}, but we currently don't add unit_number there, and this would need to be fully tested to make sure it works. We do this in other providers without issue, however, such as the Azure provider, which is encouraging. We also would still need to determine how we want to assist with generating the name attributes for these disks as well, as if we just read the disks raw they would bear the names from the source virtual machine.

So in summary, to correct what you're seeing here, you will need to (for now at least) change the resource to only have one disk sub-resource. Unfortunately, this makes things a little less flexible in terms of what your template can have for disks, but we are not closing the book on this being scalable in the future.

Hope this helps!

@jason-azze
Copy link
Contributor Author

Thanks, @vancluever. That is very helpful guidance. In general, I'm trying to discover if I'm misusing the tool or if I've made bad architectural decisions (or both). :-)

Might I suggest adjusting the language in the documentation to change "at least" to "exactly"?

@ghost ghost locked and limited conversation to collaborators Apr 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants