I’m playing with variable validations and came across a couple of doubts:
In case of having a dictionary, and based on providers that default some values, for example, for vmware virtual machines, for the disk if the controller type is null, not defined or scsi the range of unit number can be 0 to 14, but if it is null or not defined it will default to 0.
so the only way I got it working was:
condition = alltrue([for disk_key,disk_value in var.inline_disks:
try(
!contains(keys(disk_value), "controller_type")
? disk_value["unit_number"] >= 0 && disk_value["unit_number"] <= 14
: disk_value["controller_type"] == null
? disk_value["unit_number"] >= 0 && disk_value["unit_number"] <= 14
: disk_value["controller_type"] == "scsi" || disk_value["controller_type"] == "nvme"
? disk_value["unit_number"] >= 0 && disk_value["unit_number"] <= 14
: disk_value["controller_type"] == "ide"
? disk_value["unit_number"] >= 0 && disk_value["unit_number"] <= 1
: disk_value["controller_type"] == "sata"
? disk_value["unit_number"] >= 0 && disk_value["unit_number"] <= 29
: false,
false
)
])
is there any other shorter way to do it, when testing it, with ors won’t work. I found a couple of forums proposing using the ? conditional. Is this really the only way?
I already tried something like and it works, but again wondering if there is a shorter alternative:
condition = alltrue([for disk_key,disk_value in var.inline_disks:
can(!(contains(keys(disk_value), "controller_type") && (disk_value["unit_number"] >= 0 && disk_value["unit_number"] <= 14))) ||
can((disk_value["controller_type"] == null && (disk_value["unit_number"] >= 0 && disk_value["unit_number"] <= 14))) ||
can((disk_value["controller_type"] == "scsi" || disk_value["controller_type"] == "nvme") && (disk_value["unit_number"] >= 0 && disk_value["unit_number"] <= 14)) ||
can((disk_value["controller_type"] == "ide" && (disk_value["unit_number"] >= 0 && disk_value["unit_number"] <= 1))) ||
can((disk_value["controller_type"] == "sata" && (disk_value["unit_number"] >= 0 && disk_value["unit_number"] <= 29)))
])
And second, is there any best practice for writting long conditionals as in this case?
I found different proposals but I was not able to find any official recomendation.
Thanks!