Skip to content

Commit

Permalink
Fix the bug for computed labels and annotations (#10182)
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 authored Mar 13, 2024
1 parent 5114e38 commit ca2bf3f
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,55 @@ func TestAccBigQueryDataset_basic(t *testing.T) {
})
}

func TestAccBigQueryDataset_withComputedLabels(t *testing.T) {
// Skip it in VCR test because of the randomness of uuid in "labels" field
// which causes the replaying mode after recording mode failing in VCR test
acctest.SkipIfVcr(t)
t.Parallel()

datasetID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
ExternalProviders: map[string]resource.ExternalProvider{
"random": {},
},
CheckDestroy: testAccCheckBigQueryDatasetDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigQueryDataset(datasetID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_bigquery_dataset.test", "labels.%", "2"),
resource.TestCheckResourceAttr("google_bigquery_dataset.test", "labels.env", "foo"),
resource.TestCheckResourceAttr("google_bigquery_dataset.test", "labels.default_table_expiration_ms", "3600000"),

resource.TestCheckResourceAttr("google_bigquery_dataset.test", "effective_labels.%", "2"),
resource.TestCheckResourceAttr("google_bigquery_dataset.test", "effective_labels.env", "foo"),
resource.TestCheckResourceAttr("google_bigquery_dataset.test", "effective_labels.default_table_expiration_ms", "3600000"),
),
},
{
ResourceName: "google_bigquery_dataset.test",
ImportState: true,
ImportStateVerify: true,
// The labels field in the state is decided by the configuration.
// During importing, the configuration is unavailable, so the labels field in the state after importing is empty.
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
{
Config: testAccBigQueryDatasetUpdated_withComputedLabels(datasetID),
},
{
ResourceName: "google_bigquery_dataset.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
}

func TestAccBigQueryDataset_withProvider5(t *testing.T) {
acctest.SkipIfVcr(t)
t.Parallel()
Expand Down Expand Up @@ -493,6 +542,27 @@ resource "google_bigquery_dataset" "test" {
`, datasetID)
}

func testAccBigQueryDatasetUpdated_withComputedLabels(datasetID string) string {
return fmt.Sprintf(`
resource "random_uuid" "test" {
}
resource "google_bigquery_dataset" "test" {
dataset_id = "%s"
# friendly_name = "bar"
description = "This is a bar description"
location = "EU"
default_partition_expiration_ms = 7200000
default_table_expiration_ms = 7200000
labels = {
env = "${random_uuid.test.result}"
default_table_expiration_ms = 7200000
}
}
`, datasetID)
}

func testAccBigQueryDatasetDeleteContents(datasetID string) string {
return fmt.Sprintf(`
resource "google_bigquery_dataset" "contents_test" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,47 @@ import (
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)

func TestAccCloudbuildWorkerPool_withComputedAnnotations(t *testing.T) {
// Skip it in VCR test because of the randomness of uuid in "annotations" field
// which causes the replaying mode after recording mode failing in VCR test
acctest.SkipIfVcr(t)
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
"project": envvar.GetTestProjectFromEnv(),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
ExternalProviders: map[string]resource.ExternalProvider{
"random": {},
},
CheckDestroy: funcAccTestCloudbuildWorkerPoolCheckDestroy(t),
Steps: []resource.TestStep{
{
Config: testAccCloudbuildWorkerPool_updated(context),
},
{
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"annotations"},
ResourceName: "google_cloudbuild_worker_pool.pool",
},
{
Config: testAccCloudbuildWorkerPool_withComputedAnnotations(context),
},
{
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"annotations"},
ResourceName: "google_cloudbuild_worker_pool.pool",
},
},
})
}

func TestAccCloudbuildWorkerPool_basic(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -89,6 +130,28 @@ resource "google_cloudbuild_worker_pool" "pool" {
`, context)
}

func testAccCloudbuildWorkerPool_withComputedAnnotations(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "random_uuid" "test" {
}

resource "google_cloudbuild_worker_pool" "pool" {
name = "pool%{random_suffix}"
location = "europe-west1"
worker_config {
disk_size_gb = 101
machine_type = "e2-standard-4"
no_external_ip = false
}

annotations = {
env = "${random_uuid.test.result}"
default_expiration_ms = 3600000
}
}
`, context)
}

func testAccCloudbuildWorkerPool_noWorkerConfig(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_cloudbuild_worker_pool" "pool" {
Expand Down
9 changes: 9 additions & 0 deletions mmv1/third_party/terraform/tpgresource/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ func SetAnnotationsDiff(_ context.Context, d *schema.ResourceDiff, meta interfac
return fmt.Errorf("`effective_annotations` field is not present in the resource schema.")
}

// If "annotations" field is computed, set "effective_annotations" to computed.
// https://github.com/hashicorp/terraform-provider-google/issues/16217
if !d.GetRawPlan().GetAttr("annotations").IsWhollyKnown() {
if err := d.SetNewComputed("effective_annotations"); err != nil {
return fmt.Errorf("error setting effective_annotations to computed: %w", err)
}
return nil
}

o, n := d.GetChange("annotations")
effectiveAnnotations := d.Get("effective_annotations").(map[string]interface{})

Expand Down
13 changes: 13 additions & 0 deletions mmv1/third_party/terraform/tpgresource/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ func SetLabelsDiff(_ context.Context, d *schema.ResourceDiff, meta interface{})
return fmt.Errorf("`effective_labels` field is not present in the resource schema.")
}

// If "labels" field is computed, set "terraform_labels" and "effective_labels" to computed.
// https://github.com/hashicorp/terraform-provider-google/issues/16217
if !d.GetRawPlan().GetAttr("labels").IsWhollyKnown() {
if err := d.SetNewComputed("terraform_labels"); err != nil {
return fmt.Errorf("error setting terraform_labels to computed: %w", err)
}

if err := d.SetNewComputed("effective_labels"); err != nil {
return fmt.Errorf("error setting effective_labels to computed: %w", err)
}
return nil
}

config := meta.(*transport_tpg.Config)

// Merge provider default labels with the user defined labels in the resource to get terraform managed labels
Expand Down

0 comments on commit ca2bf3f

Please sign in to comment.