JetBrains / package-search-gradle-plugins

Gradle plugins for Docker, Terraform and Liquibase.
Apache License 2.0
32 stars 8 forks source link

Unclear how to reference a resource file as a variable in main.tf #7

Closed farrukhnajmi closed 2 years ago

farrukhnajmi commented 2 years ago

Hi @lamba92 and team, thanks for the good plugin.

I am trying to reference a resource within the following resource block:

resource "hsdp_iam_email_template" "account_verification_en-US" {
  // extraneous arguments ommitted
  message = file("${var.resources.path}/email-templates/email-ACCOUNT_VERIFICATION_en-US.html")
}

I am not understanding the documentation at https://github.com/JetBrains/jetbrains-gradle-plugins/blob/master/docs/TERRAFORM.md#resources-references. I think there are some issues with the documentation:

Assuming my resource file is at the following location:

.../build/terraform/main/runtimeExecution/resources/email-templates/email-ACCOUNT_VERIFICATION_en-US.html

What should my path parameter to file function look like?

lamba92 commented 2 years ago

try just message = file(var.resources.email-templates.email-ACCOUNT_VERIFICATION_en-US_html.path)

You can check in the resources file generated in .../build/terraform/main/runtimeExecution/res.tf

farrukhnajmi commented 2 years ago

I did following as suggested:

resource "hsdp_iam_email_template" "account_verification_en-US" {
  // extraneous arguments ommitted
  message = file(var.resources.email-ACCOUNT_VERIFICATION_en-US_html.path)
}

I get an error:

:terraform:terraformMainPlan (Thread[Execution worker for ':',5,main]) completed. Took 1.807 secs.
8 actionable tasks: 4 executed, 4 up-to-date
╷
│ Error: Unsupported attribute
│ 
│   on modules/iam/main.tf line 176, in resource "hsdp_iam_email_template" "account_verification_en-US":
│  176:   message = file(var.resources.email-ACCOUNT_VERIFICATION_en-US_html.path)
│     ├────────────────
│     │ var.resources is object with 3 attributes
│ 
│ This object does not have an attribute named
│ "email-ACCOUNT_VERIFICATION_en-US_html".
╵

Also unclear how this handles two files in resources tree that have same name but different folder paths?

Seems what would be most obvious is that var.resources just resolved to the root of the resources tree and then could have rest of file path appended.

lamba92 commented 2 years ago

All the resources are merged in the same folder but and based on that the res.tf is generated. The folder hierarchy should be respected. Have a look at the file to understand in /build/terraform/main/runtimeExecution/res.tf

farrukhnajmi commented 2 years ago

So here is what my actual resource tree looks like:

 ls -R resources/
resources/:
email-templates

resources/email-templates:
cfz-cosentyx

resources/email-templates/cfz-cosentyx:
email-ACCOUNT_VERIFICATION_de-CH.html         email-EXPIRE_PASSWORD_de-CH.html   email-RESET_PASSWORD_de-CH.html
email-ACCOUNT_VERIFICATION_en-US.html         email-EXPIRE_PASSWORD_en-US.html   email-RESET_PASSWORD_en-US.html
email-EMAIL_VERIFICATION_VIA_CODE_de-CH.html  email-PASSWORD_CHANGED_de-CH.html
email-EMAIL_VERIFICATION_VIA_CODE_en-US.html  email-PASSWORD_CHANGED_en-US.html

My generated res.tf looks like:

variable "resources" {
  default = {
    email-templates-dir = "./resources/email-templates"
    email-templates = {
      cfz-cosentyx-dir = "./resources/cfz-cosentyx"
      cfz-cosentyx = {
        email-RESET-PASSWORD-en-US_html = {
          path = "./resources/email-templates/cfz-cosentyx/email-RESET_PASSWORD_en-US.html"
          name = "email-RESET_PASSWORD_en-US.html"
          name-without-extension = "email-RESET_PASSWORD_en-US"
          ext = "html"
        }
        email-EMAIL-VERIFICATION-VIA-CODE-en-US_html = {
          path = "./resources/email-templates/cfz-cosentyx/email-EMAIL_VERIFICATION_VIA_CODE_en-US.html"
          name = "email-EMAIL_VERIFICATION_VIA_CODE_en-US.html"
          name-without-extension = "email-EMAIL_VERIFICATION_VIA_CODE_en-US"
          ext = "html"
        }
        email-PASSWORD-CHANGED-en-US_html = {
          path = "./resources/email-templates/cfz-cosentyx/email-PASSWORD_CHANGED_en-US.html"
          name = "email-PASSWORD_CHANGED_en-US.html"
          name-without-extension = "email-PASSWORD_CHANGED_en-US"
          ext = "html"
        }
        email-RESET-PASSWORD-de-CH_html = {
          path = "./resources/email-templates/cfz-cosentyx/email-RESET_PASSWORD_de-CH.html"
          name = "email-RESET_PASSWORD_de-CH.html"
          name-without-extension = "email-RESET_PASSWORD_de-CH"
          ext = "html"
        }
        email-EXPIRE-PASSWORD-en-US_html = {
          path = "./resources/email-templates/cfz-cosentyx/email-EXPIRE_PASSWORD_en-US.html"
          name = "email-EXPIRE_PASSWORD_en-US.html"
          name-without-extension = "email-EXPIRE_PASSWORD_en-US"
          ext = "html"
        }
        email-ACCOUNT-VERIFICATION-de-CH_html = {
          path = "./resources/email-templates/cfz-cosentyx/email-ACCOUNT_VERIFICATION_de-CH.html"
          name = "email-ACCOUNT_VERIFICATION_de-CH.html"
          name-without-extension = "email-ACCOUNT_VERIFICATION_de-CH"
          ext = "html"
        }
        email-PASSWORD-CHANGED-de-CH_html = {
          path = "./resources/email-templates/cfz-cosentyx/email-PASSWORD_CHANGED_de-CH.html"
          name = "email-PASSWORD_CHANGED_de-CH.html"
          name-without-extension = "email-PASSWORD_CHANGED_de-CH"
          ext = "html"
        }
        email-EMAIL-VERIFICATION-VIA-CODE-de-CH_html = {
          path = "./resources/email-templates/cfz-cosentyx/email-EMAIL_VERIFICATION_VIA_CODE_de-CH.html"
          name = "email-EMAIL_VERIFICATION_VIA_CODE_de-CH.html"
          name-without-extension = "email-EMAIL_VERIFICATION_VIA_CODE_de-CH"
          ext = "html"
        }
        email-ACCOUNT-VERIFICATION-en-US_html = {
          path = "./resources/email-templates/cfz-cosentyx/email-ACCOUNT_VERIFICATION_en-US.html"
          name = "email-ACCOUNT_VERIFICATION_en-US.html"
          name-without-extension = "email-ACCOUNT_VERIFICATION_en-US"
          ext = "html"
        }
        email-EXPIRE-PASSWORD-de-CH_html = {
          path = "./resources/email-templates/cfz-cosentyx/email-EXPIRE_PASSWORD_de-CH.html"
          name = "email-EXPIRE_PASSWORD_de-CH.html"
          name-without-extension = "email-EXPIRE_PASSWORD_de-CH"
          ext = "html"
        }
      }
    }
  }
}

The error I get is:

:terraform:terraformMainPlan (Thread[Execution worker for ':',5,main]) completed. Took 2.003 secs.
8 actionable tasks: 4 executed, 4 up-to-date
╷
│ Error: Unsupported attribute
│ 
│   on modules/iam/main.tf line 176, in resource "hsdp_iam_email_template" "account_verification_en-US":
│  176:   message = file(var.resources.email-templates.cfz-cosentyx.email-ACCOUNT_VERIFICATION_en-US_html.path)
│     ├────────────────
│     │ var.resources.email-templates.cfz-cosentyx is object with 10 attributes
│ 
│ This object does not have an attribute named
│ "email-ACCOUNT_VERIFICATION_en-US_html".

Much obliged for your help @lamba92

farrukhnajmi commented 2 years ago

Ok I figured it out. My resource declaration needed to be fixed in its var.resources as follows:

resource "hsdp_iam_email_template" "account_verification_en-US" {
  message = file(var.resources.email-templates.cfz-cosentyx.email-ACCOUNT-VERIFICATION-en-US_html.path)
}

Specifically, the '_en-US' in filename email-ACCOUNT_VERIFICATION_en-US.html was modified to -en-US in transformation by plugin. THis was a subtle thing that should be called out in docs that '' char is special for separator between filename and filetype. Any '' in filename will be mapped to '-'.

Above seems to be very brittle behavior since there could be filename classhes after '_' to '-' replacement in filename part. Have you considered just have var.resources resolve to the resources folder and rest be normal path?

Thanks @lamba92