|
| 1 | +// Copyright IBM Corp. 2014, 2026 |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +// ---------------------------------------------------------------------------- |
| 4 | +// |
| 5 | +// *** AUTO GENERATED CODE *** Type: Handwritten *** |
| 6 | +// |
| 7 | +// ---------------------------------------------------------------------------- |
| 8 | +// |
| 9 | +// This code is generated by Magic Modules using the following: |
| 10 | +// |
| 11 | +// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/services/chronicle/resource_chronicle_data_table_deletion_policy_test.go.tmpl |
| 12 | +// |
| 13 | +// DO NOT EDIT this file directly. Any changes made to this file will be |
| 14 | +// overwritten during the next generation cycle. |
| 15 | +// |
| 16 | +// ---------------------------------------------------------------------------- |
| 17 | +package chronicle_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "regexp" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 25 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 26 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" |
| 27 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar" |
| 28 | +) |
| 29 | + |
| 30 | +func TestAccChronicleDataTable_DeletionPolicy_DefaultFail(t *testing.T) { |
| 31 | + t.Parallel() |
| 32 | + |
| 33 | + context := map[string]interface{}{ |
| 34 | + "random_suffix": acctest.RandString(t, 10), |
| 35 | + "instance_id": envvar.GetTestChronicleInstanceIdFromEnv(t), |
| 36 | + } |
| 37 | + |
| 38 | + acctest.VcrTest(t, resource.TestCase{ |
| 39 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 40 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t), |
| 41 | + Steps: []resource.TestStep{ |
| 42 | + { |
| 43 | + Config: testAccChronicleDataTable_DefaultDeletion(context), |
| 44 | + Check: resource.ComposeTestCheckFunc( |
| 45 | + resource.TestCheckResourceAttrSet("google_chronicle_data_table.test_delete_default_fail", "id"), |
| 46 | + resource.TestCheckResourceAttrSet("google_chronicle_data_table_row.default_fail_row", "id"), |
| 47 | + ), |
| 48 | + }, |
| 49 | + { |
| 50 | + Config: testAccChronicleDataTable_DefaultDeletion_TableOnly(context), |
| 51 | + Destroy: true, |
| 52 | + ExpectError: regexp.MustCompile("Unable to delete table as table is non empty"), |
| 53 | + }, |
| 54 | + }, |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +func TestAccChronicleDataTable_DeletionPolicy_Force(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + |
| 61 | + context := map[string]interface{}{ |
| 62 | + "random_suffix": acctest.RandString(t, 10), |
| 63 | + "instance_id": envvar.GetTestChronicleInstanceIdFromEnv(t), |
| 64 | + } |
| 65 | + |
| 66 | + acctest.VcrTest(t, resource.TestCase{ |
| 67 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 68 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t), |
| 69 | + Steps: []resource.TestStep{ |
| 70 | + // Step 1: Create table and a row, with Deletion policy FORCE. |
| 71 | + { |
| 72 | + Config: testAccChronicleDataTable_ForceDeletion(context), |
| 73 | + Check: resource.ComposeTestCheckFunc( |
| 74 | + resource.TestCheckResourceAttrSet("google_chronicle_data_table.test_delete_force", "id"), |
| 75 | + resource.TestCheckResourceAttrSet("google_chronicle_data_table_row.force_row", "id"), |
| 76 | + ), |
| 77 | + }, |
| 78 | + { |
| 79 | + Config: testAccChronicleDataTable_ForceDeletion_TableOnly(context), // Config with only the row |
| 80 | + Check: resource.ComposeTestCheckFunc( |
| 81 | + checkResourceDestroyed("google_chronicle_data_table.test_delete_force"), |
| 82 | + ), |
| 83 | + //Expect a non-empty plan because Terraform's refresh found |
| 84 | + // the row missing and wants to recreate it. |
| 85 | + ExpectNonEmptyPlan: true, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +func testAccChronicleDataTable_DefaultDeletion(context map[string]interface{}) string { |
| 92 | + return acctest.Nprintf(` |
| 93 | +resource "google_chronicle_data_table" "test_delete_default_fail" { |
| 94 | + provider = google-beta |
| 95 | + location = "us" |
| 96 | + instance = "%{instance_id}" |
| 97 | + data_table_id = "tf_test_def_fail_%{random_suffix}" |
| 98 | + description = "Test deletion_policy DEFAULT with rows" |
| 99 | + deletion_policy = "DEFAULT" |
| 100 | +
|
| 101 | + column_info { |
| 102 | + column_index = 0 |
| 103 | + original_column = "username" |
| 104 | + column_type = "STRING" |
| 105 | + } |
| 106 | +} |
| 107 | +
|
| 108 | +resource "google_chronicle_data_table_row" "default_fail_row" { |
| 109 | + provider = google-beta |
| 110 | + location = google_chronicle_data_table.test_delete_default_fail.location |
| 111 | + instance = google_chronicle_data_table.test_delete_default_fail.instance |
| 112 | + data_table_id = google_chronicle_data_table.test_delete_default_fail.data_table_id |
| 113 | + values = ["testuser"] |
| 114 | + depends_on = [google_chronicle_data_table.test_delete_default_fail] |
| 115 | +} |
| 116 | +`, context) |
| 117 | +} |
| 118 | + |
| 119 | +func testAccChronicleDataTable_ForceDeletion(context map[string]interface{}) string { |
| 120 | + return acctest.Nprintf(` |
| 121 | +resource "google_chronicle_data_table" "test_delete_force" { |
| 122 | + provider = google-beta |
| 123 | + location = "us" |
| 124 | + instance = "%{instance_id}" |
| 125 | + data_table_id = "tf_test_force_%{random_suffix}" |
| 126 | + description = "Test deletion_policy FORCE" |
| 127 | + deletion_policy = "FORCE" |
| 128 | +
|
| 129 | + column_info { |
| 130 | + column_index = 0 |
| 131 | + original_column = "username" |
| 132 | + column_type = "STRING" |
| 133 | + } |
| 134 | +} |
| 135 | +
|
| 136 | +resource "google_chronicle_data_table_row" "force_row" { |
| 137 | + provider = google-beta |
| 138 | + location = google_chronicle_data_table.test_delete_force.location |
| 139 | + instance = google_chronicle_data_table.test_delete_force.instance |
| 140 | + data_table_id = google_chronicle_data_table.test_delete_force.data_table_id |
| 141 | + values = ["testuser"] |
| 142 | + depends_on = [google_chronicle_data_table.test_delete_force] |
| 143 | +} |
| 144 | +`, context) |
| 145 | +} |
| 146 | + |
| 147 | +func testAccChronicleDataTable_DefaultDeletion_TableOnly(context map[string]interface{}) string { |
| 148 | + return acctest.Nprintf(` |
| 149 | +resource "google_chronicle_data_table_row" "default_fail_row" { |
| 150 | + provider = google-beta |
| 151 | + location = "us" # Assuming 'us' |
| 152 | + instance = "%{instance_id}" |
| 153 | + data_table_id = "tf_test_def_fail_%{random_suffix}" # Needs to match the created table ID |
| 154 | + values = ["testuser"] |
| 155 | +} |
| 156 | +`, context) |
| 157 | +} |
| 158 | + |
| 159 | +// Includes only the row, to attempt table deletion for FORCE test |
| 160 | +func testAccChronicleDataTable_ForceDeletion_TableOnly(context map[string]interface{}) string { |
| 161 | + return acctest.Nprintf(` |
| 162 | +resource "google_chronicle_data_table_row" "force_row" { |
| 163 | + provider = google-beta |
| 164 | + location = "us" # Assuming 'us' |
| 165 | + instance = "%{instance_id}" |
| 166 | + data_table_id = "tf_test_force_%{random_suffix}" # Needs to match the created table ID |
| 167 | + values = ["testuser"] |
| 168 | +} |
| 169 | +`, context) |
| 170 | +} |
| 171 | + |
| 172 | +// Helper function to check if a resource is destroyed |
| 173 | +func checkResourceDestroyed(name string) resource.TestCheckFunc { |
| 174 | + return func(s *terraform.State) error { |
| 175 | + _, ok := s.RootModule().Resources[name] |
| 176 | + if ok { |
| 177 | + return fmt.Errorf("Resource %s still exists in state when it should have been destroyed", name) |
| 178 | + } |
| 179 | + return nil |
| 180 | + } |
| 181 | +} |
0 commit comments