hashicorp / vscode-terraform

HashiCorp Terraform VSCode extension
https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform
Mozilla Public License 2.0
922 stars 179 forks source link

RFE: ide suggestions for $cloud_region in modules #1235

Open todd-dsm opened 2 years ago

todd-dsm commented 2 years ago

Versions

Terraform

% tf version 
Terraform v1.2.9
on darwin_arm64

macOS

% sw_vers 
ProductName:    macOS
ProductVersion: 12.6
BuildVersion:   21G115

Extension

Name: HashiCorp Terraform
Id: hashicorp.terraform
Description: Syntax highlighting and autocompletion for Terraform
Version: 2.24.2
Publisher: HashiCorp
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform

Problem Statement

Would be cool if there were suggestions for $cloud_region, like:

module "nomad" {
  source = "hashicorp/nomad/google"
  gcp_project = "my-project-id"
  gcp_region = "us-west2"          <- providing a list would be brilliant
}

Might be helpful for definitions of modules/providers/terraform.tfvars, etc..

Expected User Experience

During the assignment:

  1. enter the parameter name region + [TAB]
  2. a [SPACE] = [SPACE] is automatically dropped into the line for convenience, then the cursor lands between 2 double quotes.
  3. The user begins tying and IntelliSense takes over to suggest a list of live (available) regions.
  4. Typing the first few letters of the location will narrow the list Example: (Los Angeles): typing 'los' or 'west', should narrow the list of options
  5. Hit the up/down arrow to make a selection, possible k or j if using vim mode(?!)
  6. Hit the [ENTER] key and "us-west2" is populated inside the double quotes.

This would be somewhat helpful to those of us helping companies with global ambitions; I always have to look this stuff up.

radeksimko commented 2 years ago

Hi @todd-dsm I agree that completion of regions and similar would be a great feature.

It seems relatively realistic to offer completion in this context:

provider "google" {
  region      = # <- HERE
}

Even that would however require some additions to the provider protocol and most importantly to providers themselves, such that e.g. google provider can provide completion candidates for the region field. i.e. it will require cooperation with a few teams and basically the whole provider maintainer ecosystem - it's not impossible, just clarifying that this is more than just a VS Code extension feature.


With a module (as in your example), the additional difficulty is understanding the relationship between the module input and a resource field. In order for this to work I expect that we'd have to introduce some changes to the variable block, which module authors would use to draw such relationships.

For example:

variable "gcp_region" {
  options = [
    "us-west2",
    "eu-west1",
    "us-east1",
  ]
}

or

variable "gcp_region" {
  options = google_compute_regions.available.names
}

data "google_compute_regions" "available" { }

These are all very hypothetical and don't account for any Terraform Core internals, which could may it difficult to implement either of the two approaches above.

Is there a particular UX you would expect on the module author side here?

todd-dsm commented 2 years ago

Understood. Let's throw it on the books - even as a low priority. Someone may have time some day?!

🤷

radeksimko commented 2 years ago

@todd-dsm Is there a particular UX you would expect on the module author side here?

todd-dsm commented 2 years ago

nothing I can think of beyond what was noted above.