Skip to content

Commit

Permalink
Apply new labels model to container cluster and edgenetwork resources (
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 authored Aug 9, 2024
1 parent 4f39da5 commit 75ce1f6
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 22 deletions.
6 changes: 0 additions & 6 deletions mmv1/api/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,6 @@ def validate
# The "labels" field has type Array, so skip this resource
!(product_name == 'DeploymentManager' && resource_name == 'Deployment') &&

# https://github.com/hashicorp/terraform-provider-google/issues/16219
!(product_name == 'Edgenetwork' && resource_name == 'Network') &&

# https://github.com/hashicorp/terraform-provider-google/issues/16219
!(product_name == 'Edgenetwork' && resource_name == 'Subnet') &&

# "userLabels" is the resource labels field
!(product_name == 'Monitoring' && resource_name == 'NotificationChannel') &&

Expand Down
2 changes: 1 addition & 1 deletion mmv1/products/edgenetwork/Network.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ properties:
description: |
The canonical name of this resource, with format
`projects/{{project}}/locations/{{location}}/zones/{{zone}}/networks/{{network_id}}`
- !ruby/object:Api::Type::KeyValuePairs
- !ruby/object:Api::Type::KeyValueLabels
name: 'labels'
required: false
description: |
Expand Down
2 changes: 1 addition & 1 deletion mmv1/products/edgenetwork/Subnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ properties:
description: |
The canonical name of this resource, with format
`projects/{{project}}/locations/{{location}}/zones/{{zone}}/subnets/{{subnet_id}}`
- !ruby/object:Api::Type::KeyValuePairs
- !ruby/object:Api::Type::KeyValueLabels
name: 'labels'
required: false
description: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ func datasourceContainerClusterRead(d *schema.ResourceData, meta interface{}) er
return err
}

// Sets the "resource_labels" field and "terraform_labels" with the value of the field "effective_labels".
effectiveLabels := d.Get("effective_labels")
if err := d.Set("resource_labels", effectiveLabels); err != nil {
return fmt.Errorf("Error setting labels in data source: %s", err)
}
if err := d.Set("terraform_labels", effectiveLabels); err != nil {
return fmt.Errorf("Error setting terraform_labels in data source: %s", err)
}

if d.Id() == "" {
return fmt.Errorf("%s not found", id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ resource "google_container_cluster" "kubes" {
deletion_protection = false
network = "%s"
subnetwork = "%s"
resource_labels = {
created-by = "terraform"
}
}
data "google_container_cluster" "kubes" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestAccContainerCluster_misc(t *testing.T) {
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection"},
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
},
{
Config: testAccContainerCluster_misc_update(clusterName, networkName, subnetworkName),
Expand All @@ -157,7 +157,7 @@ func TestAccContainerCluster_misc(t *testing.T) {
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection"},
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
},
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func ResourceContainerCluster() *schema.Resource {
containerClusterSurgeSettingsCustomizeDiff,
containerClusterEnableK8sBetaApisCustomizeDiff,
containerClusterNodeVersionCustomizeDiff,
tpgresource.SetDiffForLabelsWithCustomizedName("resource_labels"),
),

Timeouts: &schema.ResourceTimeout{
Expand Down Expand Up @@ -1811,7 +1812,22 @@ func ResourceContainerCluster() *schema.Resource {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The GCE resource labels (a map of key/value pairs) to be applied to the cluster.`,
Description: `The GCE resource labels (a map of key/value pairs) to be applied to the cluster.

**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
Please refer to the field 'effective_labels' for all of the labels present on the resource.`,
},
"terraform_labels": {
Type: schema.TypeMap,
Computed: true,
Description: `The combination of labels configured directly on the resource and default labels configured on the provider.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"effective_labels": {
Type: schema.TypeMap,
Computed: true,
Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`,
Elem: &schema.Schema{Type: schema.TypeString},
},

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

if err := d.Set("resource_labels", cluster.ResourceLabels); err != nil {
return fmt.Errorf("Error setting resource_labels: %s", err)
if err := tpgresource.SetLabels(cluster.ResourceLabels, d, "resource_labels"); err != nil {
return fmt.Errorf("Error setting labels: %s", err)
}
if err := tpgresource.SetLabels(cluster.ResourceLabels, d, "terraform_labels"); err != nil {
return fmt.Errorf("Error setting terraform_labels: %s", err)
}
if err := d.Set("effective_labels", cluster.ResourceLabels); err != nil {
return fmt.Errorf("Error setting effective_labels: %s", err)
}
if err := d.Set("label_fingerprint", cluster.LabelFingerprint); err != nil {
return fmt.Errorf("Error setting label_fingerprint: %s", err)
Expand Down Expand Up @@ -4087,8 +4109,8 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
log.Printf("[INFO] GKE cluster %s monitoring config has been updated", d.Id())
}

if d.HasChange("resource_labels") {
resourceLabels := d.Get("resource_labels").(map[string]interface{})
if d.HasChange("effective_labels") {
resourceLabels := d.Get("effective_labels").(map[string]interface{})
labelFingerprint := d.Get("label_fingerprint").(string)
req := &container.SetLabelsRequest{
ResourceLabels: tpgresource.ConvertStringMap(resourceLabels),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestAccContainerCluster_misc(t *testing.T) {
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection"},
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
},
{
Config: testAccContainerCluster_misc_update(clusterName, networkName, subnetworkName),
Expand All @@ -158,7 +158,7 @@ func TestAccContainerCluster_misc(t *testing.T) {
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection"},
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -11070,3 +11070,158 @@ resource "google_container_cluster" "primary" {
}
`, secretID, clusterName, networkName, subnetworkName)
}

func TestAccContainerCluster_withProviderDefaultLabels(t *testing.T) {
// The test failed if VCR testing is enabled, because the cached provider config is used.
// With the cached provider config, any changes in the provider default labels will not be applied.
acctest.SkipIfVcr(t)
t.Parallel()

clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_withProviderDefaultLabels(clusterName, networkName, subnetworkName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "1"),
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.created-by", "terraform"),

resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "2"),
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "default_value1"),
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.created-by", "terraform"),

resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "2"),
),
},
{
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
},
{
Config: testAccContainerCluster_resourceLabelsOverridesProviderDefaultLabels(clusterName, networkName, subnetworkName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "2"),
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.created-by", "terraform"),
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "value1"),

resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "2"),
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "value1"),
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.created-by", "terraform"),

resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "2"),
),
},
{
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
},
{
Config: testAccContainerCluster_moveResourceLabelToProviderDefaultLabels(clusterName, networkName, subnetworkName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "0"),

resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "2"),
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.default_key1", "default_value1"),
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.created-by", "terraform"),

resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "2"),
),
},
{
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
},
{
Config: testAccContainerCluster_basic(clusterName, networkName, subnetworkName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_container_cluster.primary", "resource_labels.%", "0"),
resource.TestCheckResourceAttr("google_container_cluster.primary", "terraform_labels.%", "0"),
resource.TestCheckResourceAttr("google_container_cluster.primary", "effective_labels.%", "0"),
),
},
{
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"remove_default_node_pool", "deletion_protection", "resource_labels", "terraform_labels"},
},
},
})
}

func testAccContainerCluster_withProviderDefaultLabels(name, networkName, subnetworkName string) string {
return fmt.Sprintf(`
provider "google" {
default_labels = {
default_key1 = "default_value1"
}
}

resource "google_container_cluster" "primary" {
name = "%s"
location = "us-central1-a"
initial_node_count = 1
deletion_protection = false
network = "%s"
subnetwork = "%s"
resource_labels = {
created-by = "terraform"
}
}
`, name, networkName, subnetworkName)
}

func testAccContainerCluster_resourceLabelsOverridesProviderDefaultLabels(name, networkName, subnetworkName string) string {
return fmt.Sprintf(`
provider "google" {
default_labels = {
default_key1 = "default_value1"
}
}

resource "google_container_cluster" "primary" {
name = "%s"
location = "us-central1-a"
initial_node_count = 1
deletion_protection = false
network = "%s"
subnetwork = "%s"
resource_labels = {
created-by = "terraform"
default_key1 = "value1"
}
}
`, name, networkName, subnetworkName)
}

func testAccContainerCluster_moveResourceLabelToProviderDefaultLabels(name, networkName, subnetworkName string) string {
return fmt.Sprintf(`
provider "google" {
default_labels = {
default_key1 = "default_value1"
created-by = "terraform"
}
}

resource "google_container_cluster" "primary" {
name = "%s"
location = "us-central1-a"
initial_node_count = 1
deletion_protection = false
network = "%s"
subnetwork = "%s"
}
`, name, networkName, subnetworkName)
}
21 changes: 18 additions & 3 deletions mmv1/third_party/terraform/tpgresource/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ func SetDataSourceLabels(d *schema.ResourceData) error {
return nil
}

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

// 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 !d.GetRawPlan().GetAttr(labelsField).IsWhollyKnown() {
if err := d.SetNewComputed("terraform_labels"); err != nil {
return fmt.Errorf("error setting terraform_labels to computed: %w", err)
}
Expand Down Expand Up @@ -131,6 +132,20 @@ func SetLabelsDiff(_ context.Context, d *schema.ResourceDiff, meta interface{})
return nil
}

// The CustomizeDiff func to set the values of terraform_labels and effective_labels fields
// when labels field is at the root level and named "labels".
func SetLabelsDiff(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
return setLabelsFields("labels", d, meta)
}

// The CustomizeDiff func to set the values of terraform_labels and effective_labels fields
// when labels field is at the root level and has a diffent name (e.g. resource_labels) than "labels"
func SetDiffForLabelsWithCustomizedName(labelsField string) func(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
return func(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
return setLabelsFields(labelsField, d, meta)
}
}

func SetMetadataLabelsDiff(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
l := d.Get("metadata").([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down
Loading

0 comments on commit 75ce1f6

Please sign in to comment.