Skip to content

Commit 75ce1f6

Browse files
authored
Apply new labels model to container cluster and edgenetwork resources (#11320)
1 parent 4f39da5 commit 75ce1f6

11 files changed

+270
-22
lines changed

mmv1/api/type.rb

-6
Original file line numberDiff line numberDiff line change
@@ -762,12 +762,6 @@ def validate
762762
# The "labels" field has type Array, so skip this resource
763763
!(product_name == 'DeploymentManager' && resource_name == 'Deployment') &&
764764

765-
# https://github.com/hashicorp/terraform-provider-google/issues/16219
766-
!(product_name == 'Edgenetwork' && resource_name == 'Network') &&
767-
768-
# https://github.com/hashicorp/terraform-provider-google/issues/16219
769-
!(product_name == 'Edgenetwork' && resource_name == 'Subnet') &&
770-
771765
# "userLabels" is the resource labels field
772766
!(product_name == 'Monitoring' && resource_name == 'NotificationChannel') &&
773767

mmv1/products/edgenetwork/Network.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ properties:
6666
description: |
6767
The canonical name of this resource, with format
6868
`projects/{{project}}/locations/{{location}}/zones/{{zone}}/networks/{{network_id}}`
69-
- !ruby/object:Api::Type::KeyValuePairs
69+
- !ruby/object:Api::Type::KeyValueLabels
7070
name: 'labels'
7171
required: false
7272
description: |

mmv1/products/edgenetwork/Subnet.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ properties:
7777
description: |
7878
The canonical name of this resource, with format
7979
`projects/{{project}}/locations/{{location}}/zones/{{zone}}/subnets/{{subnet_id}}`
80-
- !ruby/object:Api::Type::KeyValuePairs
80+
- !ruby/object:Api::Type::KeyValueLabels
8181
name: 'labels'
8282
required: false
8383
description: |

mmv1/third_party/terraform/services/container/data_source_google_container_cluster.go

+9
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ func datasourceContainerClusterRead(d *schema.ResourceData, meta interface{}) er
4747
return err
4848
}
4949

50+
// Sets the "resource_labels" field and "terraform_labels" with the value of the field "effective_labels".
51+
effectiveLabels := d.Get("effective_labels")
52+
if err := d.Set("resource_labels", effectiveLabels); err != nil {
53+
return fmt.Errorf("Error setting labels in data source: %s", err)
54+
}
55+
if err := d.Set("terraform_labels", effectiveLabels); err != nil {
56+
return fmt.Errorf("Error setting terraform_labels in data source: %s", err)
57+
}
58+
5059
if d.Id() == "" {
5160
return fmt.Errorf("%s not found", id)
5261
}

mmv1/third_party/terraform/services/container/data_source_google_container_cluster_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ resource "google_container_cluster" "kubes" {
9696
deletion_protection = false
9797
network = "%s"
9898
subnetwork = "%s"
99-
99+
resource_labels = {
100+
created-by = "terraform"
101+
}
100102
}
101103
102104
data "google_container_cluster" "kubes" {

mmv1/third_party/terraform/services/container/go/resource_container_cluster_test.go.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func TestAccContainerCluster_misc(t *testing.T) {
148148
ResourceName: "google_container_cluster.primary",
149149
ImportState: true,
150150
ImportStateVerify: true,
151-
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection"},
151+
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
152152
},
153153
{
154154
Config: testAccContainerCluster_misc_update(clusterName, networkName, subnetworkName),
@@ -157,7 +157,7 @@ func TestAccContainerCluster_misc(t *testing.T) {
157157
ResourceName: "google_container_cluster.primary",
158158
ImportState: true,
159159
ImportStateVerify: true,
160-
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection"},
160+
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
161161
},
162162
},
163163
})

mmv1/third_party/terraform/services/container/resource_container_cluster.go.erb

+28-6
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ func ResourceContainerCluster() *schema.Resource {
218218
containerClusterSurgeSettingsCustomizeDiff,
219219
containerClusterEnableK8sBetaApisCustomizeDiff,
220220
containerClusterNodeVersionCustomizeDiff,
221+
tpgresource.SetDiffForLabelsWithCustomizedName("resource_labels"),
221222
),
222223

223224
Timeouts: &schema.ResourceTimeout{
@@ -1811,7 +1812,22 @@ func ResourceContainerCluster() *schema.Resource {
18111812
Type: schema.TypeMap,
18121813
Optional: true,
18131814
Elem: &schema.Schema{Type: schema.TypeString},
1814-
Description: `The GCE resource labels (a map of key/value pairs) to be applied to the cluster.`,
1815+
Description: `The GCE resource labels (a map of key/value pairs) to be applied to the cluster.
1816+
1817+
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
1818+
Please refer to the field 'effective_labels' for all of the labels present on the resource.`,
1819+
},
1820+
"terraform_labels": {
1821+
Type: schema.TypeMap,
1822+
Computed: true,
1823+
Description: `The combination of labels configured directly on the resource and default labels configured on the provider.`,
1824+
Elem: &schema.Schema{Type: schema.TypeString},
1825+
},
1826+
"effective_labels": {
1827+
Type: schema.TypeMap,
1828+
Computed: true,
1829+
Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`,
1830+
Elem: &schema.Schema{Type: schema.TypeString},
18151831
},
18161832

18171833
"label_fingerprint": {
@@ -2376,7 +2392,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
23762392
MasterAuth: expandMasterAuth(d.Get("master_auth")),
23772393
NotificationConfig: expandNotificationConfig(d.Get("notification_config")),
23782394
ConfidentialNodes: expandConfidentialNodes(d.Get("confidential_nodes")),
2379-
ResourceLabels: tpgresource.ExpandStringMap(d, "resource_labels"),
2395+
ResourceLabels: tpgresource.ExpandStringMap(d, "effective_labels"),
23802396
NodePoolAutoConfig: expandNodePoolAutoConfig(d.Get("node_pool_auto_config")),
23812397
<% unless version == 'ga' -%>
23822398
ProtectConfig: expandProtectConfig(d.Get("protect_config")),
@@ -2994,8 +3010,14 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
29943010
}
29953011
<% end -%>
29963012

2997-
if err := d.Set("resource_labels", cluster.ResourceLabels); err != nil {
2998-
return fmt.Errorf("Error setting resource_labels: %s", err)
3013+
if err := tpgresource.SetLabels(cluster.ResourceLabels, d, "resource_labels"); err != nil {
3014+
return fmt.Errorf("Error setting labels: %s", err)
3015+
}
3016+
if err := tpgresource.SetLabels(cluster.ResourceLabels, d, "terraform_labels"); err != nil {
3017+
return fmt.Errorf("Error setting terraform_labels: %s", err)
3018+
}
3019+
if err := d.Set("effective_labels", cluster.ResourceLabels); err != nil {
3020+
return fmt.Errorf("Error setting effective_labels: %s", err)
29993021
}
30003022
if err := d.Set("label_fingerprint", cluster.LabelFingerprint); err != nil {
30013023
return fmt.Errorf("Error setting label_fingerprint: %s", err)
@@ -4087,8 +4109,8 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
40874109
log.Printf("[INFO] GKE cluster %s monitoring config has been updated", d.Id())
40884110
}
40894111

4090-
if d.HasChange("resource_labels") {
4091-
resourceLabels := d.Get("resource_labels").(map[string]interface{})
4112+
if d.HasChange("effective_labels") {
4113+
resourceLabels := d.Get("effective_labels").(map[string]interface{})
40924114
labelFingerprint := d.Get("label_fingerprint").(string)
40934115
req := &container.SetLabelsRequest{
40944116
ResourceLabels: tpgresource.ConvertStringMap(resourceLabels),

mmv1/third_party/terraform/services/container/resource_container_cluster_test.go.erb

+157-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func TestAccContainerCluster_misc(t *testing.T) {
149149
ResourceName: "google_container_cluster.primary",
150150
ImportState: true,
151151
ImportStateVerify: true,
152-
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection"},
152+
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
153153
},
154154
{
155155
Config: testAccContainerCluster_misc_update(clusterName, networkName, subnetworkName),
@@ -158,7 +158,7 @@ func TestAccContainerCluster_misc(t *testing.T) {
158158
ResourceName: "google_container_cluster.primary",
159159
ImportState: true,
160160
ImportStateVerify: true,
161-
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection"},
161+
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
162162
},
163163
},
164164
})
@@ -11070,3 +11070,158 @@ resource "google_container_cluster" "primary" {
1107011070
}
1107111071
`, secretID, clusterName, networkName, subnetworkName)
1107211072
}
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+
}

mmv1/third_party/terraform/tpgresource/labels.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ func SetDataSourceLabels(d *schema.ResourceData) error {
5555
return nil
5656
}
5757

58-
func SetLabelsDiff(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
59-
raw := d.Get("labels")
58+
// Sets the values of terraform_labels and effective_labels fields when labels field is in root level
59+
func setLabelsFields(labelsField string, d *schema.ResourceDiff, meta interface{}) error {
60+
raw := d.Get(labelsField)
6061
if raw == nil {
6162
return nil
6263
}
@@ -71,7 +72,7 @@ func SetLabelsDiff(_ context.Context, d *schema.ResourceDiff, meta interface{})
7172

7273
// If "labels" field is computed, set "terraform_labels" and "effective_labels" to computed.
7374
// https://github.com/hashicorp/terraform-provider-google/issues/16217
74-
if !d.GetRawPlan().GetAttr("labels").IsWhollyKnown() {
75+
if !d.GetRawPlan().GetAttr(labelsField).IsWhollyKnown() {
7576
if err := d.SetNewComputed("terraform_labels"); err != nil {
7677
return fmt.Errorf("error setting terraform_labels to computed: %w", err)
7778
}
@@ -131,6 +132,20 @@ func SetLabelsDiff(_ context.Context, d *schema.ResourceDiff, meta interface{})
131132
return nil
132133
}
133134

135+
// The CustomizeDiff func to set the values of terraform_labels and effective_labels fields
136+
// when labels field is at the root level and named "labels".
137+
func SetLabelsDiff(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
138+
return setLabelsFields("labels", d, meta)
139+
}
140+
141+
// The CustomizeDiff func to set the values of terraform_labels and effective_labels fields
142+
// when labels field is at the root level and has a diffent name (e.g. resource_labels) than "labels"
143+
func SetDiffForLabelsWithCustomizedName(labelsField string) func(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
144+
return func(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
145+
return setLabelsFields(labelsField, d, meta)
146+
}
147+
}
148+
134149
func SetMetadataLabelsDiff(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
135150
l := d.Get("metadata").([]interface{})
136151
if len(l) == 0 || l[0] == nil {

0 commit comments

Comments
 (0)