Skip to content

Commit 3241faf

Browse files
committed
Apply new labels model
1 parent d49eb28 commit 3241faf

File tree

9 files changed

+249
-21
lines changed

9 files changed

+249
-21
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/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
@@ -217,6 +217,7 @@ func ResourceContainerCluster() *schema.Resource {
217217
containerClusterSurgeSettingsCustomizeDiff,
218218
containerClusterEnableK8sBetaApisCustomizeDiff,
219219
containerClusterNodeVersionCustomizeDiff,
220+
tpgresource.SetDiffForLabelsWithCustomizedName("resource_labels"),
220221
),
221222

222223
Timeouts: &schema.ResourceTimeout{
@@ -1768,7 +1769,22 @@ func ResourceContainerCluster() *schema.Resource {
17681769
Type: schema.TypeMap,
17691770
Optional: true,
17701771
Elem: &schema.Schema{Type: schema.TypeString},
1771-
Description: `The GCE resource labels (a map of key/value pairs) to be applied to the cluster.`,
1772+
Description: `The GCE resource labels (a map of key/value pairs) to be applied to the cluster.
1773+
1774+
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
1775+
Please refer to the field 'effective_labels' for all of the labels present on the resource.`,
1776+
},
1777+
"terraform_labels": {
1778+
Type: schema.TypeMap,
1779+
Computed: true,
1780+
Description: `The combination of labels configured directly on the resource and default labels configured on the provider.`,
1781+
Elem: &schema.Schema{Type: schema.TypeString},
1782+
},
1783+
"effective_labels": {
1784+
Type: schema.TypeMap,
1785+
Computed: true,
1786+
Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`,
1787+
Elem: &schema.Schema{Type: schema.TypeString},
17721788
},
17731789

17741790
"label_fingerprint": {
@@ -2333,7 +2349,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
23332349
MasterAuth: expandMasterAuth(d.Get("master_auth")),
23342350
NotificationConfig: expandNotificationConfig(d.Get("notification_config")),
23352351
ConfidentialNodes: expandConfidentialNodes(d.Get("confidential_nodes")),
2336-
ResourceLabels: tpgresource.ExpandStringMap(d, "resource_labels"),
2352+
ResourceLabels: tpgresource.ExpandStringMap(d, "effective_labels"),
23372353
NodePoolAutoConfig: expandNodePoolAutoConfig(d.Get("node_pool_auto_config")),
23382354
<% unless version == 'ga' -%>
23392355
ProtectConfig: expandProtectConfig(d.Get("protect_config")),
@@ -2951,8 +2967,14 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
29512967
}
29522968
<% end -%>
29532969

2954-
if err := d.Set("resource_labels", cluster.ResourceLabels); err != nil {
2955-
return fmt.Errorf("Error setting resource_labels: %s", err)
2970+
if err := tpgresource.SetLabels(cluster.ResourceLabels, d, "resource_labels"); err != nil {
2971+
return fmt.Errorf("Error setting labels: %s", err)
2972+
}
2973+
if err := tpgresource.SetLabels(cluster.ResourceLabels, d, "terraform_labels"); err != nil {
2974+
return fmt.Errorf("Error setting terraform_labels: %s", err)
2975+
}
2976+
if err := d.Set("effective_labels", cluster.ResourceLabels); err != nil {
2977+
return fmt.Errorf("Error setting effective_labels: %s", err)
29562978
}
29572979
if err := d.Set("label_fingerprint", cluster.LabelFingerprint); err != nil {
29582980
return fmt.Errorf("Error setting label_fingerprint: %s", err)
@@ -4044,8 +4066,8 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
40444066
log.Printf("[INFO] GKE cluster %s monitoring config has been updated", d.Id())
40454067
}
40464068

4047-
if d.HasChange("resource_labels") {
4048-
resourceLabels := d.Get("resource_labels").(map[string]interface{})
4069+
if d.HasChange("effective_labels") {
4070+
resourceLabels := d.Get("effective_labels").(map[string]interface{})
40494071
labelFingerprint := d.Get("label_fingerprint").(string)
40504072
req := &container.SetLabelsRequest{
40514073
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
})
@@ -11080,3 +11080,158 @@ resource "google_container_cluster" "primary" {
1108011080
}
1108111081
`, secretID, clusterName, networkName, subnetworkName)
1108211082
}
11083+
11084+
func TestAccContainerCluster_withProviderDefaultLabels(t *testing.T) {
11085+
// The test failed if VCR testing is enabled, because the cached provider config is used.
11086+
// With the cached provider config, any changes in the provider default labels will not be applied.
11087+
acctest.SkipIfVcr(t)
11088+
t.Parallel()
11089+
11090+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
11091+
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
11092+
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)
11093+
11094+
acctest.VcrTest(t, resource.TestCase{
11095+
PreCheck: func() { acctest.AccTestPreCheck(t) },
11096+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
11097+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
11098+
Steps: []resource.TestStep{
11099+
{
11100+
Config: testAccContainerCluster_withProviderDefaultLabels(clusterName, networkName, subnetworkName),
11101+
Check: resource.ComposeTestCheckFunc(
11102+
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "1"),
11103+
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.created-by", "terraform"),
11104+
11105+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "2"),
11106+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "default_value1"),
11107+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.created-by", "terraform"),
11108+
11109+
resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "2"),
11110+
),
11111+
},
11112+
{
11113+
ResourceName: "google_container_cluster.primary",
11114+
ImportState: true,
11115+
ImportStateVerify: true,
11116+
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
11117+
},
11118+
{
11119+
Config: testAccContainerCluster_resourceLabelsOverridesProviderDefaultLabels(clusterName, networkName, subnetworkName),
11120+
Check: resource.ComposeTestCheckFunc(
11121+
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "2"),
11122+
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.created-by", "terraform"),
11123+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "value1"),
11124+
11125+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "2"),
11126+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "value1"),
11127+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.created-by", "terraform"),
11128+
11129+
resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "2"),
11130+
),
11131+
},
11132+
{
11133+
ResourceName: "google_container_cluster.primary",
11134+
ImportState: true,
11135+
ImportStateVerify: true,
11136+
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
11137+
},
11138+
{
11139+
Config: testAccContainerCluster_moveResourceLabelToProviderDefaultLabels(clusterName, networkName, subnetworkName),
11140+
Check: resource.ComposeTestCheckFunc(
11141+
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "0"),
11142+
11143+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "2"),
11144+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "default_value1"),
11145+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.created-by", "terraform"),
11146+
11147+
resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "2"),
11148+
),
11149+
},
11150+
{
11151+
ResourceName: "google_container_cluster.primary",
11152+
ImportState: true,
11153+
ImportStateVerify: true,
11154+
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
11155+
},
11156+
{
11157+
Config: testAccContainerCluster_basic(clusterName, networkName, subnetworkName),
11158+
Check: resource.ComposeTestCheckFunc(
11159+
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "0"),
11160+
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "0"),
11161+
resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "0"),
11162+
),
11163+
},
11164+
{
11165+
ResourceName: "google_container_cluster.primary",
11166+
ImportState: true,
11167+
ImportStateVerify: true,
11168+
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
11169+
},
11170+
},
11171+
})
11172+
}
11173+
11174+
func testAccContainerCluster_withProviderDefaultLabels(name, networkName, subnetworkName string) string {
11175+
return fmt.Sprintf(`
11176+
provider "google" {
11177+
default_labels = {
11178+
default_key1 = "default_value1"
11179+
}
11180+
}
11181+
11182+
resource "google_container_cluster" "primary" {
11183+
name = "%s"
11184+
location = "us-central1-a"
11185+
initial_node_count = 1
11186+
deletion_protection = false
11187+
network = "%s"
11188+
subnetwork = "%s"
11189+
resource_labels = {
11190+
created-by = "terraform"
11191+
}
11192+
}
11193+
`, name, networkName, subnetworkName)
11194+
}
11195+
11196+
func testAccContainerCluster_resourceLabelsOverridesProviderDefaultLabels(name, networkName, subnetworkName string) string {
11197+
return fmt.Sprintf(`
11198+
provider "google" {
11199+
default_labels = {
11200+
default_key1 = "default_value1"
11201+
}
11202+
}
11203+
11204+
resource "google_container_cluster" "primary" {
11205+
name = "%s"
11206+
location = "us-central1-a"
11207+
initial_node_count = 1
11208+
deletion_protection = false
11209+
network = "%s"
11210+
subnetwork = "%s"
11211+
resource_labels = {
11212+
created-by = "terraform"
11213+
default_key1 = "value1"
11214+
}
11215+
}
11216+
`, name, networkName, subnetworkName)
11217+
}
11218+
11219+
func testAccContainerCluster_moveResourceLabelToProviderDefaultLabels(name, networkName, subnetworkName string) string {
11220+
return fmt.Sprintf(`
11221+
provider "google" {
11222+
default_labels = {
11223+
default_key1 = "default_value1"
11224+
created-by = "terraform"
11225+
}
11226+
}
11227+
11228+
resource "google_container_cluster" "primary" {
11229+
name = "%s"
11230+
location = "us-central1-a"
11231+
initial_node_count = 1
11232+
deletion_protection = false
11233+
network = "%s"
11234+
subnetwork = "%s"
11235+
}
11236+
`, name, networkName, subnetworkName)
11237+
}

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 {

mmv1/third_party/terraform/website/docs/guides/version_6_upgrade.html.markdown

+33
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,36 @@ Users will need to check their configuration for any `google_vpc_access_connecto
172172
resource blocks that contain both fields in a conflicting pair, and remove one of those fields.
173173
The fields that are removed from the configuration will still have Computed values,
174174
that are derived from the API.
175+
176+
## Resource: `google_container_cluster`
177+
178+
### Three label-related fields are now present
179+
180+
* `resource_labels` field is non-authoritative and only manages the labels defined by
181+
the users on the resource through Terraform.
182+
* The new output-only `terraform_labels` field merges the labels defined by the users
183+
on the resource through Terraform and the default labels configured on the provider.
184+
* The new output-only `effective_labels` field lists all of labels present on the resource
185+
in GCP, including the labels configured through Terraform, the system, and other clients.
186+
187+
## Resource: `google_edgenetwork_network`
188+
189+
### Three label-related fields are now present
190+
191+
* `labels` field is non-authoritative and only manages the labels defined by
192+
the users on the resource through Terraform.
193+
* The new output-only `terraform_labels` field merges the labels defined by the users
194+
on the resource through Terraform and the default labels configured on the provider.
195+
* The new output-only `effective_labels` field lists all of labels present on the resource
196+
in GCP, including the labels configured through Terraform, the system, and other clients.
197+
198+
## Resource: `google_edgenetwork_subnet`
199+
200+
### Three label-related fields are now present
201+
202+
* `labels` field is non-authoritative and only manages the labels defined by
203+
the users on the resource through Terraform.
204+
* The new output-only `terraform_labels` field merges the labels defined by the users
205+
on the resource through Terraform and the default labels configured on the provider.
206+
* The new output-only `effective_labels` field lists all of labels present on the resource
207+
in GCP, including the labels configured through Terraform, the system, and other clients.

mmv1/third_party/terraform/website/docs/r/container_cluster.html.markdown

+9
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,15 @@ channel. Structure is [documented below](#nested_release_channel).
328328

329329
* `resource_labels` - (Optional) The GCE resource labels (a map of key/value pairs) to be applied to the cluster.
330330

331+
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
332+
Please refer to the field 'effective_labels' for all of the labels present on the resource.
333+
334+
* `terraform_labels` -
335+
The combination of labels configured directly on the resource and default labels configured on the provider.
336+
337+
* `effective_labels` -
338+
All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.
339+
331340
* `cost_management_config` - (Optional) Configuration for the
332341
[Cost Allocation](https://cloud.google.com/kubernetes-engine/docs/how-to/cost-allocations) feature.
333342
Structure is [documented below](#nested_cost_management_config).

0 commit comments

Comments
 (0)