hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.27k stars 1.72k forks source link

Missing plan-time validation for the `name` attribute of the `google_compute_instance` #18265

Open AlexPykavy opened 2 months ago

AlexPykavy commented 2 months ago

Community Note

Terraform Version & Provider Version(s)

Terraform v1.7.1 on windows_amd64

Affected Resource(s)

google_compute_instance

Terraform Configuration

resource "google_compute_instance" "main" {
  name         = "super-long-machine-name-that-does-not-meet-the-google-compute-instance-naming-restrictions"
  machine_type = "n2-standard-2"
  zone         = "us-central1-a"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-12"
    }
  }
  network_interface {
    network = "default"

    access_config {
      // Ephemeral public IP
    }
  }
}

Debug Output

No response

Expected Behavior

terraform plan fails with a meaningful message

Actual Behavior

terraform plan passes

Steps to reproduce

  1. terraform plan
  2. terraform apply

Important Factoids

No response

References

I've tried to fix it in the https://github.com/GoogleCloudPlatform/magic-modules/pull/10775.

b/343730710

ggtisc commented 2 months ago

Hi @AlexPykavy I tried to replicate this scenario with the shared terraform version and Google provider version, and with your code and the terraform registry code. But the result was correct, returning the next error:

Error creating instance: googleapi: Error 400: Invalid value for field 'resource.name' "super-long-machine-name-that-does-not-meet-the-google-compute-instance-naming-restrictions". Must be a match of regex '(?:[a-z](?:[a-z0-9]{0,61}[a-z0-9])?)', invalid

I suggest you refresh your environment and make basic troubleshooting to release memory and cache and try again. A terraform init or terraform init -upgrade could also work.

AlexPykavy commented 2 months ago

Hi @ggtisc ,

Thank you for the quick response. Did you receive this error during the terraform plan or the terraform apply?

ggtisc commented 2 months ago

After a double check terraform apply works with an expected behavior, but terraform plan doesn't recognize the error for the long name.

Based on this I'm forwarding this as an enhancement proposal.

SarahFrench commented 2 months ago

Note from triage: We'd want to check for any history of compute instance names being validated in the provider and discovering edge cases before adding this