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

Add error messages for failing interpolations in project-factory #2917

Merged
merged 1 commit into from
Feb 21, 2025

Conversation

wiktorn
Copy link
Collaborator

@wiktorn wiktorn commented Feb 21, 2025

Currently, failed interpolations fail with the error message like this:

Error: invalid value for member (IAM members must have one of the values outlined here: https://cloud.google.com/billing/docs/reference/rest/v1/Policy#Binding)

  with module.projects-iam["service1"].google_project_iam_member.shared_vpc_host_iam["rw"],
  on ../project/shared-vpc.tf line 131, in resource "google_project_iam_member" "shared_vpc_host_iam":
 131:   member     = each.value

This is not helpful, when looking for a source of the issue. With this PR, error messages look like this:

Error: Error in function call

  on main.tf line 173, in module "projects-iam":
 173:         try(
 174:           # project service accounts
 175:           module.service-accounts["${each.key}/${v}"].iam_email,
 176:           # automation service account
 177:           local.context.iam_principals["${each.key}/${v}"],
 178:           # other projects service accounts
 179:           module.service-accounts[v].iam_email,
 180:           # other automation service account
 181:           local.context.iam_principals[v],
 182:           # passthrough + error handling using tonumber until Terraform gets fail/raise function
 183:           strcontains(v, ":") ? v : tonumber("[Error] Invalid member: '${v}' in project '${each.key}'")
 184:         )
    ├────────────────
    │ each.key is "service1"
    │ local.context.iam_principals is object with 3 attributes
    │ module.service-accounts is object with 4 attributes

Call to function "try" failed: no expression succeeded:
- Invalid index (at main.tf:175,34-54)
  The given key does not identify an element in this collection value.
- Invalid index (at main.tf:177,39-59)
  The given key does not identify an element in this collection value.
- Invalid index (at main.tf:179,34-37)
  The given key does not identify an element in this collection value.
- Invalid index (at main.tf:181,39-42)
  The given key does not identify an element in this collection value.
- Invalid function argument (at main.tf:183,47-72)
  Invalid value for "v" parameter: cannot convert "[Error] Invalid member: 'rw' in project 'service1'" to number; given string must be a decimal representation of a number.

At least one expression must produce a successful result.

Which provides the user with the information that allows them to narrow down the search area.


Checklist

I applicable, I acknowledge that I have:

  • Read the contributing guide
  • Ran terraform fmt on all modified files
  • Regenerated the relevant README.md files using tools/tfdoc.py
  • Made sure all relevant tests pass

@wiktorn wiktorn enabled auto-merge (rebase) February 21, 2025 08:16
@wiktorn wiktorn merged commit 2427b2e into fast-dev Feb 21, 2025
19 checks passed
@wiktorn wiktorn deleted the wiktorn-pf-multi-iam branch February 21, 2025 08:20
@wiktorn wiktorn changed the title Add error messages for failing interpolations Add error messages for failing interpolations in project-factory Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants