Skip to content

Commit

Permalink
Apply provider default labels to handwritten resources (#8966)
Browse files Browse the repository at this point in the history
* Apply provider default labels to handwritten resources

* Add terraform_labels and effective_labels to doc

* New fun SetLabels instead of FlattenLabels

* Add comment for func SetLabels
  • Loading branch information
zli82016 authored Sep 18, 2023
1 parent d6b01a2 commit 34cf135
Show file tree
Hide file tree
Showing 22 changed files with 176 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ func ResourceBigQueryTable() *schema.Resource {
CustomizeDiff: customdiff.All(
tpgresource.DefaultProviderProject,
resourceBigQueryTableSchemaCustomizeDiff,
tpgresource.SetLabelsDiff,
),
Schema: map[string]*schema.Schema{
// TableId: [Required] The ID of the table. The ID must contain only
Expand Down Expand Up @@ -796,6 +797,12 @@ func ResourceBigQueryTable() *schema.Resource {
**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,
Expand Down Expand Up @@ -1283,7 +1290,7 @@ func resourceTable(d *schema.ResourceData, meta interface{}) (*bigquery.Table, e
}
}

if v, ok := d.GetOk("labels"); ok {
if v, ok := d.GetOk("effective_labels"); ok {
labels := map[string]string{}

for k, v := range v.(map[string]interface{}) {
Expand Down Expand Up @@ -1405,9 +1412,12 @@ func resourceBigQueryTableRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("max_staleness", res.MaxStaleness); err != nil {
return fmt.Errorf("Error setting max_staleness: %s", err)
}
if err := d.Set("labels", tpgresource.FlattenLabels(res.Labels, d)); err != nil {
if err := tpgresource.SetLabels(res.Labels, d, "labels"); err != nil {
return fmt.Errorf("Error setting labels: %s", err)
}
if err := tpgresource.SetLabels(res.Labels, d, "terraform_labels"); err != nil {
return fmt.Errorf("Error setting terraform_labels: %s", err)
}
if err := d.Set("effective_labels", res.Labels); err != nil {
return fmt.Errorf("Error setting effective_labels: %s", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3098,7 +3098,7 @@ resource "google_bigquery_table" "test" {
friendly_name = "bigquerytest"
labels = {
"terrafrom_managed" = "true"
"terrafrom_managed" = "false"
}
schema = jsonencode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func ResourceBigtableInstance() *schema.Resource {
tpgresource.DefaultProviderProject,
resourceBigtableInstanceClusterReorderTypeList,
resourceBigtableInstanceUniqueClusterID,
tpgresource.SetLabelsDiff,
),

SchemaVersion: 1,
Expand Down Expand Up @@ -163,6 +164,13 @@ func ResourceBigtableInstance() *schema.Resource {
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,
Expand Down Expand Up @@ -206,8 +214,8 @@ func resourceBigtableInstanceCreate(d *schema.ResourceData, meta interface{}) er
}
conf.DisplayName = displayName.(string)

if _, ok := d.GetOk("labels"); ok {
conf.Labels = tpgresource.ExpandLabels(d)
if _, ok := d.GetOk("effective_labels"); ok {
conf.Labels = tpgresource.ExpandEffectiveLabels(d)
}

switch d.Get("instance_type").(string) {
Expand Down Expand Up @@ -312,9 +320,12 @@ func resourceBigtableInstanceRead(d *schema.ResourceData, meta interface{}) erro
if err := d.Set("display_name", instance.DisplayName); err != nil {
return fmt.Errorf("Error setting display_name: %s", err)
}
if err := d.Set("labels", tpgresource.FlattenLabels(instance.Labels, d)); err != nil {
if err := tpgresource.SetLabels(instance.Labels, d, "labels"); err != nil {
return fmt.Errorf("Error setting labels: %s", err)
}
if err := tpgresource.SetLabels(instance.Labels, d, "terraform_labels"); err != nil {
return fmt.Errorf("Error setting terraform_labels: %s", err)
}
if err := d.Set("effective_labels", instance.Labels); err != nil {
return fmt.Errorf("Error setting effective_labels: %s", err)
}
Expand Down Expand Up @@ -353,8 +364,8 @@ func resourceBigtableInstanceUpdate(d *schema.ResourceData, meta interface{}) er
}
conf.DisplayName = displayName.(string)

if d.HasChange("labels") {
conf.Labels = tpgresource.ExpandLabels(d)
if d.HasChange("effective_labels") {
conf.Labels = tpgresource.ExpandEffectiveLabels(d)
}

switch d.Get("instance_type").(string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestAccBigtableInstance_cluster(t *testing.T) {
ResourceName: "google_bigtable_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection", "instance_type", "cluster", "labels"}, // we don't read instance type back
ImportStateVerifyIgnore: []string{"deletion_protection", "instance_type", "cluster", "labels", "terraform_labels"}, // we don't read instance type back
},
{
Config: testAccBigtableInstance_clusterReordered(instanceName, 5),
Expand All @@ -108,7 +108,7 @@ func TestAccBigtableInstance_cluster(t *testing.T) {
ResourceName: "google_bigtable_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection", "instance_type", "cluster", "labels"}, // we don't read instance type back
ImportStateVerifyIgnore: []string{"deletion_protection", "instance_type", "cluster", "labels", "terraform_labels"}, // we don't read instance type back
},
},
})
Expand Down Expand Up @@ -428,7 +428,7 @@ func TestAccBigtableInstance_MultipleClustersSameID(t *testing.T) {
ResourceName: "google_bigtable_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection", "instance_type", "labels"}, // we don't read instance type back
ImportStateVerifyIgnore: []string{"deletion_protection", "instance_type", "labels", "terraform_labels"}, // we don't read instance type back
},
{
Config: testAccBigtableInstance_multipleClustersSameID(instanceName),
Expand Down Expand Up @@ -644,7 +644,7 @@ resource "google_bigtable_instance" "instance" {
deletion_protection = false
labels = {
env = "default"
env = "test"
}
}
`, instanceName, instanceName, numNodes, instanceName, numNodes, instanceName, numNodes, instanceName, numNodes, instanceName, numNodes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func ResourceCloudFunctionsFunction() *schema.Resource {
CustomizeDiff: customdiff.All(
tpgresource.DefaultProviderProject,
tpgresource.DefaultProviderRegion,
tpgresource.SetLabelsDiff,
),

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -269,6 +270,13 @@ func ResourceCloudFunctionsFunction() *schema.Resource {
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,
Expand Down Expand Up @@ -574,8 +582,8 @@ func resourceCloudFunctionsCreate(d *schema.ResourceData, meta interface{}) erro
function.IngressSettings = v.(string)
}

if _, ok := d.GetOk("labels"); ok {
function.Labels = tpgresource.ExpandLabels(d)
if _, ok := d.GetOk("effective_labels"); ok {
function.Labels = tpgresource.ExpandEffectiveLabels(d)
}

if _, ok := d.GetOk("environment_variables"); ok {
Expand Down Expand Up @@ -686,9 +694,12 @@ func resourceCloudFunctionsRead(d *schema.ResourceData, meta interface{}) error
if err := d.Set("ingress_settings", function.IngressSettings); err != nil {
return fmt.Errorf("Error setting ingress_settings: %s", err)
}
if err := d.Set("labels", tpgresource.FlattenLabels(function.Labels, d)); err != nil {
if err := tpgresource.SetLabels(function.Labels, d, "labels"); err != nil {
return fmt.Errorf("Error setting labels: %s", err)
}
if err := tpgresource.SetLabels(function.Labels, d, "terraform_labels"); err != nil {
return fmt.Errorf("Error setting terraform_labels: %s", err)
}
if err := d.Set("effective_labels", function.Labels); err != nil {
return fmt.Errorf("Error setting effective_labels: %s", err)
}
Expand Down Expand Up @@ -858,8 +869,8 @@ func resourceCloudFunctionsUpdate(d *schema.ResourceData, meta interface{}) erro
updateMaskArr = append(updateMaskArr, "ingressSettings")
}

if d.HasChange("labels") {
function.Labels = tpgresource.ExpandLabels(d)
if d.HasChange("effective_labels") {
function.Labels = tpgresource.ExpandEffectiveLabels(d)
updateMaskArr = append(updateMaskArr, "labels")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func ResourceComposerEnvironment() *schema.Resource {
CustomizeDiff: customdiff.All(
tpgresource.DefaultProviderProject,
tpgresource.DefaultProviderRegion,
tpgresource.SetLabelsDiff,
),

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -886,6 +887,13 @@ func ResourceComposerEnvironment() *schema.Resource {
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,
Expand Down Expand Up @@ -916,7 +924,7 @@ func resourceComposerEnvironmentCreate(d *schema.ResourceData, meta interface{})

env := &composer.Environment{
Name: envName.ResourceName(),
Labels: tpgresource.ExpandLabels(d),
Labels: tpgresource.ExpandEffectiveLabels(d),
Config: transformedConfig,
}

Expand Down Expand Up @@ -994,11 +1002,14 @@ func resourceComposerEnvironmentRead(d *schema.ResourceData, meta interface{}) e
if err := d.Set("config", flattenComposerEnvironmentConfig(res.Config)); err != nil {
return fmt.Errorf("Error setting Environment: %s", err)
}
if err := d.Set("labels", tpgresource.FlattenLabels(res.Labels, d)); err != nil {
return fmt.Errorf("Error setting Environment: %s", err)
if err := tpgresource.SetLabels(res.Labels, d, "labels"); err != nil {
return fmt.Errorf("Error setting Environment labels: %s", err)
}
if err := tpgresource.SetLabels(res.Labels, d, "terraform_labels"); err != nil {
return fmt.Errorf("Error setting terraform_labels: %s", err)
}
if err := d.Set("effective_labels", res.Labels); err != nil {
return fmt.Errorf("Error setting Environment: %s", err)
return fmt.Errorf("Error setting Environment effective_labels: %s", err)
}
return nil
}
Expand Down Expand Up @@ -1241,8 +1252,8 @@ func resourceComposerEnvironmentUpdate(d *schema.ResourceData, meta interface{})
}
}

if d.HasChange("labels") {
patchEnv := &composer.Environment{Labels: tpgresource.ExpandLabels(d)}
if d.HasChange("effective_labels") {
patchEnv := &composer.Environment{Labels: tpgresource.ExpandEffectiveLabels(d)}
err := resourceComposerEnvironmentPatchField("labels", userAgent, patchEnv, d, tfConfig)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestAccComposerEnvironment_update(t *testing.T) {
ResourceName: "google_composer_environment.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
// This is a terrible clean-up step in order to get destroy to succeed,
// due to dangling firewall rules left by the Composer Environment blocking network deletion.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,13 @@ func ResourceComputeInstance() *schema.Resource {
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,
Expand Down Expand Up @@ -1069,6 +1076,7 @@ be from 0 to 999,999,999 inclusive.`,
),
desiredStatusDiff,
forceNewIfNetworkIPNotUpdatable,
tpgresource.SetLabelsDiff,
),
UseJSONNumber: true,
}
Expand Down Expand Up @@ -1202,7 +1210,7 @@ func expandComputeInstance(project string, d *schema.ResourceData, config *trans
NetworkPerformanceConfig: networkPerformanceConfig,
Tags: resourceInstanceTags(d),
Params: params,
Labels: tpgresource.ExpandLabels(d),
Labels: tpgresource.ExpandEffectiveLabels(d),
ServiceAccounts: expandServiceAccounts(d.Get("service_account").([]interface{})),
GuestAccelerators: accels,
MinCpuPlatform: d.Get("min_cpu_platform").(string),
Expand Down Expand Up @@ -1402,7 +1410,11 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
}
}

if err := d.Set("labels", tpgresource.FlattenLabels(instance.Labels, d)); err != nil {
if err := tpgresource.SetLabels(instance.Labels, d, "labels"); err != nil {
return err
}

if err := tpgresource.SetLabels(instance.Labels, d, "terraform_labels"); err != nil {
return err
}

Expand Down Expand Up @@ -1685,8 +1697,8 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
}
}

if d.HasChange("labels") {
labels := tpgresource.ExpandLabels(d)
if d.HasChange("effective_labels") {
labels := tpgresource.ExpandEffectiveLabels(d)
labelFingerprint := d.Get("label_fingerprint").(string)
req := compute.InstancesSetLabelsRequest{Labels: labels, LabelFingerprint: labelFingerprint}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,7 @@ func resourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interface{
}
}
if instanceTemplate.Properties.Labels != nil {
if err := d.Set("labels", tpgresource.FlattenLabels(instanceTemplate.Properties.Labels, d)); err != nil {
if err := tpgresource.SetLabels(instanceTemplate.Properties.Labels, d, "labels"); err != nil {
return fmt.Errorf("Error setting labels: %s", err)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestAccComputeInstance_basic1(t *testing.T) {
testAccCheckComputeInstanceHasConfiguredDeletionProtection(&instance, false),
),
},
computeInstanceImportStep("us-central1-a", instanceName, []string{"metadata.baz", "metadata.foo", "desired_status", "current_status", "labels"}),
computeInstanceImportStep("us-central1-a", instanceName, []string{"metadata.baz", "metadata.foo", "desired_status", "current_status", "labels", "terraform_labels"}),
},
})
}
Expand Down Expand Up @@ -1426,7 +1426,7 @@ func TestAccComputeInstance_forceChangeMachineTypeManually(t *testing.T) {
),
ExpectNonEmptyPlan: true,
},
computeInstanceImportStep("us-central1-a", instanceName, []string{"metadata.baz", "metadata.foo", "desired_status", "current_status", "labels"}),
computeInstanceImportStep("us-central1-a", instanceName, []string{"metadata.baz", "metadata.foo", "desired_status", "current_status", "labels", "terraform_labels"}),
},
})
}
Expand Down
Loading

0 comments on commit 34cf135

Please sign in to comment.