-
Notifications
You must be signed in to change notification settings - Fork 0
/
asserts.tf
21 lines (20 loc) · 1.53 KB
/
asserts.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#######################################################################################################
#
# Terraform does not have a easy way to check if the input parameters are in the correct format.
# On top of that, terraform will sometimes produce a valid plan but then fail during apply.
# To handle these errors beforehad, we're using the 'file' hack to throw errors on known mistakes.
#
#######################################################################################################
locals {
# Regular expressions
regex_bucket_name = "(([a-z0-9]|[a-z0-9][a-z0-9\\-]*[a-z0-9])\\.)*([a-z0-9]|[a-z0-9][a-z0-9\\-]*[a-z0-9])" # See https://cloud.google.com/storage/docs/naming-buckets
# Terraform assertion hack
assert_head = "\n\n-------------------------- /!\\ ASSERTION FAILED /!\\ --------------------------\n\n"
assert_foot = "\n\n-------------------------- /!\\ ^^^^^^^^^^^^^^^^ /!\\ --------------------------\n"
asserts = {
for bucket, settings in local.buckets : bucket => merge({
bucketname_too_long = length(settings.bucket_name) > 63 ? file(format("%sbucket [%s]'s generated name is too long:\n%s\n%s > 63 chars!%s", local.assert_head, bucket, settings.bucket_name, length(settings.bucket_name), local.assert_foot)) : "ok"
bucketname_regex = length(regexall("^${local.regex_bucket_name}$", settings.bucket_name)) == 0 ? file(format("%sbucket [%s]'s generated name [%s] does not match regex ^%s$%s", local.assert_head, bucket, settings.bucket_name, local.regex_bucket_name, local.assert_foot)) : "ok"
})
}
}