Skip to content

Commit

Permalink
Add provider default labels (#8670) (#6207)
Browse files Browse the repository at this point in the history
* Add provider default labels

* Fix the filed names

* Fix the field name

* Don't expand lables field

* Fix syntax errors

* Fix bug to set metadata.0.terraform_labels

* Ignore state verify for terraform_labels

* Add logs

* Add more logs

* Skip test if vcr is enabled

* Add new type KeyValueTerraformLabels

* Fix rake test

* Add terraform_labels to tfplan.json files

* Comment out tgc integration tests

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Sep 6, 2023
1 parent 363270e commit 9bdaaa0
Show file tree
Hide file tree
Showing 28 changed files with 808 additions and 221 deletions.
3 changes: 3 additions & 0 deletions .changelog/8670.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
1 change: 1 addition & 0 deletions google-beta/fwmodels/provider_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ProviderModel struct {
UserProjectOverride types.Bool `tfsdk:"user_project_override"`
RequestTimeout types.String `tfsdk:"request_timeout"`
RequestReason types.String `tfsdk:"request_reason"`
DefaultLabels types.Map `tfsdk:"default_labels"`

// Generated Products
AccessApprovalCustomEndpoint types.String `tfsdk:"access_approval_custom_endpoint"`
Expand Down
4 changes: 4 additions & 0 deletions google-beta/fwprovider/framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func (p *FrameworkProvider) Schema(_ context.Context, _ provider.SchemaRequest,
"request_reason": schema.StringAttribute{
Optional: true,
},
"default_labels": schema.MapAttribute{
Optional: true,
ElementType: types.StringType,
},

// Generated Products
"access_approval_custom_endpoint": &schema.StringAttribute{
Expand Down
13 changes: 13 additions & 0 deletions google-beta/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ func Provider() *schema.Provider {
Optional: true,
},

"default_labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

// Generated Products
"access_approval_custom_endpoint": {
Type: schema.TypeString,
Expand Down Expand Up @@ -1923,6 +1929,13 @@ func ProviderConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr
config.Scopes[i] = scope.(string)
}

config.DefaultLabels = make(map[string]string)
defaultLabels := d.Get("default_labels").(map[string]interface{})

for k, v := range defaultLabels {
config.DefaultLabels[k] = v.(string)
}

batchCfg, err := transport_tpg.ExpandProviderBatchingConfig(d.Get("batching"))
if err != nil {
return nil, diag.FromErr(err)
Expand Down
72 changes: 49 additions & 23 deletions google-beta/services/bigquery/resource_bigquery_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func ResourceBigQueryDataset() *schema.Resource {
},

CustomizeDiff: customdiff.All(
tpgresource.SetTerraformLabelsDiff,
tpgresource.DefaultProviderProject,
),

Expand Down Expand Up @@ -256,6 +257,13 @@ epoch.`,
Description: `The date when this dataset or any of its tables was last modified, in
milliseconds since the epoch.`,
},
"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},
},
"delete_contents_on_destroy": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -480,12 +488,6 @@ func resourceBigQueryDatasetCreate(d *schema.ResourceData, meta interface{}) err
} else if v, ok := d.GetOkExists("friendly_name"); ok || !reflect.DeepEqual(v, friendlyNameProp) {
obj["friendlyName"] = friendlyNameProp
}
labelsProp, err := expandBigQueryDatasetLabels(d.Get("labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
locationProp, err := expandBigQueryDatasetLocation(d.Get("location"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -516,6 +518,12 @@ func resourceBigQueryDatasetCreate(d *schema.ResourceData, meta interface{}) err
} else if v, ok := d.GetOkExists("storage_billing_model"); !tpgresource.IsEmptyValue(reflect.ValueOf(storageBillingModelProp)) && (ok || !reflect.DeepEqual(v, storageBillingModelProp)) {
obj["storageBillingModel"] = storageBillingModelProp
}
labelsProp, err := expandBigQueryDatasetTerraformLabels(d.Get("terraform_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("terraform_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}

url, err := tpgresource.ReplaceVars(d, config, "{{BigQueryBasePath}}projects/{{project}}/datasets")
if err != nil {
Expand Down Expand Up @@ -667,6 +675,9 @@ func resourceBigQueryDatasetRead(d *schema.ResourceData, meta interface{}) error
if err := d.Set("storage_billing_model", flattenBigQueryDatasetStorageBillingModel(res["storageBillingModel"], d, config)); err != nil {
return fmt.Errorf("Error reading Dataset: %s", err)
}
if err := d.Set("terraform_labels", flattenBigQueryDatasetTerraformLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading Dataset: %s", err)
}
if err := d.Set("effective_labels", flattenBigQueryDatasetEffectiveLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading Dataset: %s", err)
}
Expand Down Expand Up @@ -735,12 +746,6 @@ func resourceBigQueryDatasetUpdate(d *schema.ResourceData, meta interface{}) err
} else if v, ok := d.GetOkExists("friendly_name"); ok || !reflect.DeepEqual(v, friendlyNameProp) {
obj["friendlyName"] = friendlyNameProp
}
labelsProp, err := expandBigQueryDatasetLabels(d.Get("labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
locationProp, err := expandBigQueryDatasetLocation(d.Get("location"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -771,6 +776,12 @@ func resourceBigQueryDatasetUpdate(d *schema.ResourceData, meta interface{}) err
} else if v, ok := d.GetOkExists("storage_billing_model"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, storageBillingModelProp)) {
obj["storageBillingModel"] = storageBillingModelProp
}
labelsProp, err := expandBigQueryDatasetTerraformLabels(d.Get("terraform_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("terraform_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}

url, err := tpgresource.ReplaceVars(d, config, "{{BigQueryBasePath}}projects/{{project}}/datasets/{{dataset_id}}")
if err != nil {
Expand Down Expand Up @@ -1173,6 +1184,21 @@ func flattenBigQueryDatasetStorageBillingModel(v interface{}, d *schema.Resource
return v
}

func flattenBigQueryDatasetTerraformLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
}

transformed := make(map[string]interface{})
if l, ok := d.GetOkExists("terraform_labels"); ok {
for k := range l.(map[string]interface{}) {
transformed[k] = v.(map[string]interface{})[k]
}
}

return transformed
}

func flattenBigQueryDatasetEffectiveLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
Expand Down Expand Up @@ -1459,17 +1485,6 @@ func expandBigQueryDatasetFriendlyName(v interface{}, d tpgresource.TerraformRes
return v, nil
}

func expandBigQueryDatasetLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
}
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
return m, nil
}

func expandBigQueryDatasetLocation(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -1508,3 +1523,14 @@ func expandBigQueryDatasetDefaultCollation(v interface{}, d tpgresource.Terrafor
func expandBigQueryDatasetStorageBillingModel(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandBigQueryDatasetTerraformLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
}
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
return m, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestAccBigQueryDataset_bigqueryDatasetBasicExample(t *testing.T) {
ResourceName: "google_bigquery_dataset.dataset",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestAccBigQueryDataset_bigqueryDatasetWithMaxTimeTravelHoursExample(t *test
ResourceName: "google_bigquery_dataset.dataset",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestAccBigQueryDataset_bigqueryDatasetAuthorizedDatasetExample(t *testing.T
ResourceName: "google_bigquery_dataset.dataset",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -249,7 +249,7 @@ func TestAccBigQueryDataset_bigqueryDatasetAuthorizedRoutineExample(t *testing.T
ResourceName: "google_bigquery_dataset.private",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -317,7 +317,7 @@ func TestAccBigQueryDataset_bigqueryDatasetCaseInsensitiveNamesExample(t *testin
ResourceName: "google_bigquery_dataset.dataset",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -373,7 +373,7 @@ func TestAccBigQueryDataset_bigqueryDatasetDefaultCollationSetExample(t *testing
ResourceName: "google_bigquery_dataset.dataset",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
Expand Down
20 changes: 10 additions & 10 deletions google-beta/services/bigquery/resource_bigquery_dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestAccBigQueryDataset_basic(t *testing.T) {
ImportStateVerify: true,
// The labels field in the state is decided by the configuration.
// During importing, the configuration is unavailable, so the labels field in the state after importing is empty.
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
{
Config: testAccBigQueryDatasetUpdated(datasetID),
Expand All @@ -71,7 +71,7 @@ func TestAccBigQueryDataset_basic(t *testing.T) {
ResourceName: "google_bigquery_dataset.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
{
Config: testAccBigQueryDatasetUpdated2(datasetID),
Expand All @@ -80,7 +80,7 @@ func TestAccBigQueryDataset_basic(t *testing.T) {
ResourceName: "google_bigquery_dataset.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
{
Config: testAccBigQueryDataset_withoutLabels(datasetID),
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestAccBigQueryDataset_datasetWithContents(t *testing.T) {
ResourceName: "google_bigquery_dataset.contents_test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"delete_contents_on_destroy", "labels"},
ImportStateVerifyIgnore: []string{"delete_contents_on_destroy", "labels", "terraform_labels"},
},
},
})
Expand All @@ -183,7 +183,7 @@ func TestAccBigQueryDataset_access(t *testing.T) {
ResourceName: "google_bigquery_dataset.access_test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
{
Config: testAccBigQueryDatasetWithTwoAccess(datasetID),
Expand All @@ -192,7 +192,7 @@ func TestAccBigQueryDataset_access(t *testing.T) {
ResourceName: "google_bigquery_dataset.access_test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
{
Config: testAccBigQueryDatasetWithOneAccess(datasetID),
Expand All @@ -201,7 +201,7 @@ func TestAccBigQueryDataset_access(t *testing.T) {
ResourceName: "google_bigquery_dataset.access_test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
{
Config: testAccBigQueryDatasetWithViewAccess(datasetID, otherDatasetID, otherTableID),
Expand All @@ -210,7 +210,7 @@ func TestAccBigQueryDataset_access(t *testing.T) {
ResourceName: "google_bigquery_dataset.access_test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
Expand All @@ -233,7 +233,7 @@ func TestAccBigQueryDataset_regionalLocation(t *testing.T) {
ResourceName: "google_bigquery_dataset.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestAccBigQueryDataset_storageBillModel(t *testing.T) {
ResourceName: "google_bigquery_dataset.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
Expand Down
Loading

0 comments on commit 9bdaaa0

Please sign in to comment.