@@ -149,7 +149,7 @@ func TestAccContainerCluster_misc(t *testing.T) {
149
149
ResourceName: "google_container_cluster.primary",
150
150
ImportState: true,
151
151
ImportStateVerify: true,
152
- ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection"},
152
+ ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels" },
153
153
},
154
154
{
155
155
Config: testAccContainerCluster_misc_update(clusterName, networkName, subnetworkName),
@@ -158,7 +158,7 @@ func TestAccContainerCluster_misc(t *testing.T) {
158
158
ResourceName: "google_container_cluster.primary",
159
159
ImportState: true,
160
160
ImportStateVerify: true,
161
- ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection"},
161
+ ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels" },
162
162
},
163
163
},
164
164
})
@@ -11070,3 +11070,158 @@ resource "google_container_cluster" "primary" {
11070
11070
}
11071
11071
`, secretID, clusterName, networkName, subnetworkName)
11072
11072
}
11073
+
11074
+ func TestAccContainerCluster_withProviderDefaultLabels(t *testing.T) {
11075
+ // The test failed if VCR testing is enabled, because the cached provider config is used.
11076
+ // With the cached provider config, any changes in the provider default labels will not be applied.
11077
+ acctest.SkipIfVcr(t)
11078
+ t.Parallel()
11079
+
11080
+ clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
11081
+ networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
11082
+ subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)
11083
+
11084
+ acctest.VcrTest(t, resource.TestCase{
11085
+ PreCheck: func() { acctest.AccTestPreCheck(t) },
11086
+ ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
11087
+ CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
11088
+ Steps: []resource.TestStep{
11089
+ {
11090
+ Config: testAccContainerCluster_withProviderDefaultLabels(clusterName, networkName, subnetworkName),
11091
+ Check: resource.ComposeTestCheckFunc(
11092
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "1"),
11093
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.created-by", "terraform"),
11094
+
11095
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "2"),
11096
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "default_value1"),
11097
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.created-by", "terraform"),
11098
+
11099
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "2"),
11100
+ ),
11101
+ },
11102
+ {
11103
+ ResourceName: "google_container_cluster.primary",
11104
+ ImportState: true,
11105
+ ImportStateVerify: true,
11106
+ ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
11107
+ },
11108
+ {
11109
+ Config: testAccContainerCluster_resourceLabelsOverridesProviderDefaultLabels(clusterName, networkName, subnetworkName),
11110
+ Check: resource.ComposeTestCheckFunc(
11111
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "2"),
11112
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.created-by", "terraform"),
11113
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "value1"),
11114
+
11115
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "2"),
11116
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "value1"),
11117
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.created-by", "terraform"),
11118
+
11119
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "2"),
11120
+ ),
11121
+ },
11122
+ {
11123
+ ResourceName: "google_container_cluster.primary",
11124
+ ImportState: true,
11125
+ ImportStateVerify: true,
11126
+ ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
11127
+ },
11128
+ {
11129
+ Config: testAccContainerCluster_moveResourceLabelToProviderDefaultLabels(clusterName, networkName, subnetworkName),
11130
+ Check: resource.ComposeTestCheckFunc(
11131
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "0"),
11132
+
11133
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "2"),
11134
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "default_value1"),
11135
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.created-by", "terraform"),
11136
+
11137
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "2"),
11138
+ ),
11139
+ },
11140
+ {
11141
+ ResourceName: "google_container_cluster.primary",
11142
+ ImportState: true,
11143
+ ImportStateVerify: true,
11144
+ ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
11145
+ },
11146
+ {
11147
+ Config: testAccContainerCluster_basic(clusterName, networkName, subnetworkName),
11148
+ Check: resource.ComposeTestCheckFunc(
11149
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "0"),
11150
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "0"),
11151
+ resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "0"),
11152
+ ),
11153
+ },
11154
+ {
11155
+ ResourceName: "google_container_cluster.primary",
11156
+ ImportState: true,
11157
+ ImportStateVerify: true,
11158
+ ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
11159
+ },
11160
+ },
11161
+ })
11162
+ }
11163
+
11164
+ func testAccContainerCluster_withProviderDefaultLabels(name, networkName, subnetworkName string) string {
11165
+ return fmt.Sprintf(`
11166
+ provider "google" {
11167
+ default_labels = {
11168
+ default_key1 = "default_value1"
11169
+ }
11170
+ }
11171
+
11172
+ resource "google_container_cluster" "primary" {
11173
+ name = "%s"
11174
+ location = "us-central1-a"
11175
+ initial_node_count = 1
11176
+ deletion_protection = false
11177
+ network = "%s"
11178
+ subnetwork = "%s"
11179
+ resource_labels = {
11180
+ created-by = "terraform"
11181
+ }
11182
+ }
11183
+ `, name, networkName, subnetworkName)
11184
+ }
11185
+
11186
+ func testAccContainerCluster_resourceLabelsOverridesProviderDefaultLabels(name, networkName, subnetworkName string) string {
11187
+ return fmt.Sprintf(`
11188
+ provider "google" {
11189
+ default_labels = {
11190
+ default_key1 = "default_value1"
11191
+ }
11192
+ }
11193
+
11194
+ resource "google_container_cluster" "primary" {
11195
+ name = "%s"
11196
+ location = "us-central1-a"
11197
+ initial_node_count = 1
11198
+ deletion_protection = false
11199
+ network = "%s"
11200
+ subnetwork = "%s"
11201
+ resource_labels = {
11202
+ created-by = "terraform"
11203
+ default_key1 = "value1"
11204
+ }
11205
+ }
11206
+ `, name, networkName, subnetworkName)
11207
+ }
11208
+
11209
+ func testAccContainerCluster_moveResourceLabelToProviderDefaultLabels(name, networkName, subnetworkName string) string {
11210
+ return fmt.Sprintf(`
11211
+ provider "google" {
11212
+ default_labels = {
11213
+ default_key1 = "default_value1"
11214
+ created-by = "terraform"
11215
+ }
11216
+ }
11217
+
11218
+ resource "google_container_cluster" "primary" {
11219
+ name = "%s"
11220
+ location = "us-central1-a"
11221
+ initial_node_count = 1
11222
+ deletion_protection = false
11223
+ network = "%s"
11224
+ subnetwork = "%s"
11225
+ }
11226
+ `, name, networkName, subnetworkName)
11227
+ }
0 commit comments