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.32k stars 1.73k forks source link

google_bigquery_dataset import failure #16961

Open antoinedeschenes opened 9 months ago

antoinedeschenes commented 9 months ago

Community Note

Terraform Version

Terraform v1.6.6
on darwin_amd64
+ provider registry.terraform.io/hashicorp/google v5.11.0

Affected Resource(s)

Terraform Configuration Files

# imports.tf
import {
  id = "projects/myproject/datasets/mydataset"
  to = google_bigquery_dataset.main
}
# generated.tf
# __generated__ by Terraform
resource "google_bigquery_dataset" "main" {
  dataset_id                      = "DATASET"
  default_collation               = null
  default_partition_expiration_ms = null
  default_table_expiration_ms     = null
  delete_contents_on_destroy      = false
  description                     = null
  friendly_name                   = null
  is_case_insensitive             = false
  labels                          = {}
  location                        = "US"
  max_time_travel_hours           = null
  project                         = "PROJECT"
  storage_billing_model           = null
  access {
    domain         = null
    group_by_email = null
    iam_member     = null
    role           = "OWNER"
    special_group  = null
    user_by_email  = "ACCOUNT@PROJECT.iam.gserviceaccount.com"
  }
  access {
    domain         = null
    group_by_email = null
    iam_member     = null
    role           = "WRITER"
    special_group  = null
    user_by_email  = "ACCOUNT@PROJECT.iam.gserviceaccount.com"
  }
  timeouts {
    create = null
    delete = null
    update = null
  }
}

Debug Output

https://gist.github.com/antoinedeschenes/7b380ed9c0a38b86b8ebd369bc8edcb5

Panic Output

Expected Behavior

Plan command should exit successfully

╷
│ Warning: Config generation is experimental
│ 
│ Generating configuration during import is currently experimental, and the generated configuration format may change in future versions.
╵

Actual Behavior

Plan crashes with


Planning failed. Terraform encountered an error while generating this plan.

╷
│ Warning: Config generation is experimental
│ 
│ Generating configuration during import is currently experimental, and the generated configuration format may change in future versions.
╵
╷
│ Error: "default_table_expiration_ms" cannot be shorter than 3600000 milliseconds (one hour)
│ 
│   with google_bigquery_dataset.main,
│   on generated.tf line 4:
│   (source code not available)
│ 
╵

Steps to Reproduce

  1. create above imports.tf terraform file only.
  2. terraform init
  3. terraform plan -generate-config-out=generated.tf

Important Factoids

References

b/319825217

edwardmedia commented 9 months ago

@antoinedeschenes it works for me. Can you try similar?

resource "google_bigquery_dataset" "main" {
    dataset_id = 1234
}

import {
  id = "projects/myprojects/datasets/imydataset"
  to = google_bigquery_dataset.main
}
resource "google_bigquery_dataset" "main" {
    dataset_id = 1234
}

import {
  id = "myprojects/mydataset"
  to = google_bigquery_dataset.main
}
antoinedeschenes commented 9 months ago

@edwardmedia don't define a resource in the terraform file, terraform is supposed to generate it with the following command: terraform plan -generate-config-out=generated.tf


  1. Create blank folder
  2. add only the following code:
    import {
     id = "myprojects/mydataset"
     to = google_bigquery_dataset.main
    }
  3. terraform init && terraform plan -generate-config-out=generated.tf
  4. generated.tf is created successfully, but doesn't pass validation if the dataset's default_table_expiration_ms is set to 0/disabled
edwardmedia commented 9 months ago

@antoinedeschenes can you share the debug log for your import?

antoinedeschenes commented 9 months ago

@edwardmedia there's the log: https://gist.github.com/antoinedeschenes/7b380ed9c0a38b86b8ebd369bc8edcb5

My issue is that the generated terraform contains default_table_expiration_ms = 0 in the resource attributes. It's a valid value for the API, but not the validation code. The input validation code expects null and translates it to a 0 in the resource state

edwardmedia commented 9 months ago

Yes, I can repro it and see the error.

Questionable function is here

antoinedeschenes commented 9 months ago

Looking here https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets#Dataset it looks like 0 is a valid value