...
Usually other languages has feature ifdefined(var.key) to check if the variable is defined.
Something like is present in many languages e.g.
if (typeof variable !== 'undefined')
The lookup()
function supports this:
lookup(map, key, default)
how about nested maps?
``` access_points = {
marketing = {
posix_user = {
gid = "1000"
uid = "1000"
secondary_gids = "0755"
},
creation_info = {
gid = "1000"
uid = "1000"
permissions = "0755"
}
}
data = {
posix_user = {
gid = "1000"
uid = "1000"
permissions = "0755"
}
}
}
creation_info {
owner_gid = lookup(var.access_points[each.key], "creation_info", null) == null ? null : lookup(var.access_points[each.key]["creation_info"], "gid", null)
owner_uid = lookup(var.access_points[each.key], "creation_info", null) == null ? null : lookup(var.access_points[each.key]["creation_info"], "uid", null)
permissions = lookup(var.access_points[each.key], "creation_info", null) == null ? null : lookup(var.access_points[each.key]["creation_info"], "permissions", null)
}
```
I am trying to test for 'creation_info' before I attempt to use keys in 'creation_info'. I keep getting errors nesting lookup.
The only option I've found to get a binary result with lookup
looks ugly:
key_exists = lookup(map, key, default) != default
Is there a better way?
not that I know of.
It can be done like this:
key_exists = contains(keys(map), key)
Most helpful comment
It can be done like this: