Skip to content

Commit

Permalink
Apply provider default labels to handwritten resources
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 committed Sep 17, 2023
1 parent dbd00e3 commit 0d400e8
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 52 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 := d.Set("labels", tpgresource.FlattenLabels(res.Labels, d, "labels")); err != nil {
return fmt.Errorf("Error setting labels: %s", err)
}
if err := d.Set("terraform_labels", tpgresource.FlattenLabels(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 @@ -1031,7 +1031,7 @@ func TestAccBigQueryDataTable_jsonEquivalency(t *testing.T) {
ResourceName: "google_bigquery_table.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"etag", "last_modified_time", "deletion_protection", "labels"},
ImportStateVerifyIgnore: []string{"etag", "last_modified_time", "deletion_protection", "labels", "terraform_labels"},
},
{
Config: testAccBigQueryTable_jsonEqModeRemoved(datasetID, tableID),
Expand All @@ -1040,7 +1040,7 @@ func TestAccBigQueryDataTable_jsonEquivalency(t *testing.T) {
ResourceName: "google_bigquery_table.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"etag", "last_modified_time", "deletion_protection", "labels"},
ImportStateVerifyIgnore: []string{"etag", "last_modified_time", "deletion_protection", "labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -1090,7 +1090,7 @@ func TestAccBigQueryDataTable_expandArray(t *testing.T) {
ResourceName: "google_bigquery_table.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"etag", "last_modified_time", "deletion_protection", "labels"},
ImportStateVerifyIgnore: []string{"etag", "last_modified_time", "deletion_protection", "labels", "terraform_labels"},
},
{
Config: testAccBigQueryTable_arrayExpanded(datasetID, tableID),
Expand All @@ -1099,7 +1099,7 @@ func TestAccBigQueryDataTable_expandArray(t *testing.T) {
ResourceName: "google_bigquery_table.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"etag", "last_modified_time", "deletion_protection", "labels"},
ImportStateVerifyIgnore: []string{"etag", "last_modified_time", "deletion_protection", "labels", "terraform_labels"},
},
},
})
Expand All @@ -1123,7 +1123,7 @@ func TestAccBigQueryTable_allowDestroy(t *testing.T) {
ResourceName: "google_bigquery_table.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection", "labels"},
ImportStateVerifyIgnore: []string{"deletion_protection", "labels", "terraform_labels"},
},
{
Config: testAccBigQueryTable_noAllowDestroy(datasetID, tableID),
Expand Down 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 := d.Set("labels", tpgresource.FlattenLabels(instance.Labels, d, "labels")); err != nil {
return fmt.Errorf("Error setting labels: %s", err)
}
if err := d.Set("terraform_labels", tpgresource.FlattenLabels(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 := d.Set("labels", tpgresource.FlattenLabels(function.Labels, d, "labels")); err != nil {
return fmt.Errorf("Error setting labels: %s", err)
}
if err := d.Set("terraform_labels", tpgresource.FlattenLabels(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 @@ -80,7 +80,7 @@ func TestAccCloudFunctionsFunction_basic(t *testing.T) {
ResourceName: funcResourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"build_environment_variables", "labels"},
ImportStateVerifyIgnore: []string{"build_environment_variables", "labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestAccCloudFunctionsFunction_update(t *testing.T) {
ResourceName: funcResourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"build_environment_variables", "labels"},
ImportStateVerifyIgnore: []string{"build_environment_variables", "labels", "terraform_labels"},
},
{
Config: testAccCloudFunctionsFunction_updated(functionName, bucketName, zipFileUpdatePath, random_suffix),
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestAccCloudFunctionsFunction_update(t *testing.T) {
ResourceName: funcResourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"build_environment_variables", "labels"},
ImportStateVerifyIgnore: []string{"build_environment_variables", "labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -425,7 +425,7 @@ func TestAccCloudFunctionsFunction_vpcConnector(t *testing.T) {
ResourceName: funcResourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"build_environment_variables", "labels"},
ImportStateVerifyIgnore: []string{"build_environment_variables", "labels", "terraform_labels"},
},
{
Config: testAccCloudFunctionsFunction_vpcConnector(projectNumber, networkName, functionName, bucketName, zipFilePath, "10.20.0.0/28", vpcConnectorName+"-update"),
Expand All @@ -434,7 +434,7 @@ func TestAccCloudFunctionsFunction_vpcConnector(t *testing.T) {
ResourceName: funcResourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"build_environment_variables", "labels"},
ImportStateVerifyIgnore: []string{"build_environment_variables", "labels", "terraform_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 := d.Set("labels", tpgresource.FlattenLabels(res.Labels, d, "labels")); err != nil {
return fmt.Errorf("Error setting Environment labels: %s", err)
}
if err := d.Set("terraform_labels", tpgresource.FlattenLabels(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
Loading

0 comments on commit 0d400e8

Please sign in to comment.