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.29k stars 1.72k forks source link

google_monitoring_alert_policy imports to wrong project #10639

Open stellirin opened 2 years ago

stellirin commented 2 years ago

Community Note

Terraform Version

Terraform v1.0.10 on darwin_amd64

Affected Resource(s)

Terraform Configuration Files

provider "google" {
  project = "my-master-project"
  region  = "us-central1"
}

resource "google_monitoring_alert_policy" "my_policy" {
  project = "my-other-project"
...
}

Expected Behavior

terraform import google_monitoring_alert_policy.my_policy projects/my-other-project/alertPolicies/1234567890987654321
terraform apply

No changes. Your infrastructure matches the configuration.

Actual Behavior

Terraform will perform the following actions:

  # google_monitoring_alert_policy. my_policy must be replaced
-/+ resource "google_monitoring_alert_policy" "my_policy" {
      ~ id                    = "projects/my-other-project/alertPolicies/1234567890987654321" -> (known after apply)
      ~ name                  = "projects/my-other-project/alertPolicies/1234567890987654321" -> (known after apply)
      ~ project               = "my-master-project" -> "my-other-project" # forces replacement
      - user_labels           = {} -> null
        # (4 unchanged attributes hidden)

      ~ conditions {
          ~ name         = "projects/my-other-project/alertPolicies/1234567890987654321/conditions/1234567890987654321" -> (known after apply)
            # (1 unchanged attribute hidden)

          ~ condition_threshold {
                # (4 unchanged attributes hidden)

              ~ aggregations {
                    # (3 unchanged attributes hidden)
                }

              ~ trigger {
                  - percent = 0 -> null
                    # (1 unchanged attribute hidden)
                }
            }
        }

      - timeouts {}
    }

Plan: 1 to add, 0 to change, 1 to destroy.

Steps to Reproduce

  1. Google provider with project set
  2. Import policy from second project
  3. terraform apply

Important Factoids

This is not an issue on other resource types (we do this regularly for a wide range of resource on GCP).

A valid workaround is to temporarily change the project value on the provider.

References

b/275110457

hype-klaus commented 9 months ago

This bug is still present in v5.5.0 for the resource google_monitoring_alert_policy. The resource google_monitoring_uptime_check_config is showing the same behavior after import. The mentioned workaround setting temporarily the project like GOOGLE_PROJECT=my-other-project terraform import ... does work.