Skip to content

Commit

Permalink
Promote external_dataset_reference in bigquery_dataset to GA (#10516
Browse files Browse the repository at this point in the history
) (#17944)

[upstream:6ea6f333801d112cb53ba29b8341aff403b25c2c]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Apr 24, 2024
1 parent b496ec6 commit fc84d7e
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 3 deletions.
96 changes: 96 additions & 0 deletions google/services/bigquery/resource_bigquery_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,30 @@ expiration time indicated by this property.`,
Optional: true,
Description: `A user-friendly description of the dataset`,
},
"external_dataset_reference": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Information about the external metadata storage where the dataset is defined.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"connection": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The connection id that is used to access the externalSource.
Format: projects/{projectId}/locations/{locationId}/connections/{connectionId}`,
},
"external_source": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `External source that backs this dataset.`,
},
},
},
},
"friendly_name": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -490,6 +514,12 @@ func resourceBigQueryDatasetCreate(d *schema.ResourceData, meta interface{}) err
} else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}
externalDatasetReferenceProp, err := expandBigQueryDatasetExternalDatasetReference(d.Get("external_dataset_reference"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("external_dataset_reference"); !tpgresource.IsEmptyValue(reflect.ValueOf(externalDatasetReferenceProp)) && (ok || !reflect.DeepEqual(v, externalDatasetReferenceProp)) {
obj["externalDatasetReference"] = externalDatasetReferenceProp
}
friendlyNameProp, err := expandBigQueryDatasetFriendlyName(d.Get("friendly_name"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -663,6 +693,9 @@ func resourceBigQueryDatasetRead(d *schema.ResourceData, meta interface{}) error
if err := d.Set("etag", flattenBigQueryDatasetEtag(res["etag"], d, config)); err != nil {
return fmt.Errorf("Error reading Dataset: %s", err)
}
if err := d.Set("external_dataset_reference", flattenBigQueryDatasetExternalDatasetReference(res["externalDatasetReference"], d, config)); err != nil {
return fmt.Errorf("Error reading Dataset: %s", err)
}
if err := d.Set("friendly_name", flattenBigQueryDatasetFriendlyName(res["friendlyName"], d, config)); err != nil {
return fmt.Errorf("Error reading Dataset: %s", err)
}
Expand Down Expand Up @@ -752,6 +785,12 @@ func resourceBigQueryDatasetUpdate(d *schema.ResourceData, meta interface{}) err
} else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}
externalDatasetReferenceProp, err := expandBigQueryDatasetExternalDatasetReference(d.Get("external_dataset_reference"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("external_dataset_reference"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, externalDatasetReferenceProp)) {
obj["externalDatasetReference"] = externalDatasetReferenceProp
}
friendlyNameProp, err := expandBigQueryDatasetFriendlyName(d.Get("friendly_name"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -1131,6 +1170,29 @@ func flattenBigQueryDatasetEtag(v interface{}, d *schema.ResourceData, config *t
return v
}

func flattenBigQueryDatasetExternalDatasetReference(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["external_source"] =
flattenBigQueryDatasetExternalDatasetReferenceExternalSource(original["externalSource"], d, config)
transformed["connection"] =
flattenBigQueryDatasetExternalDatasetReferenceConnection(original["connection"], d, config)
return []interface{}{transformed}
}
func flattenBigQueryDatasetExternalDatasetReferenceExternalSource(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenBigQueryDatasetExternalDatasetReferenceConnection(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenBigQueryDatasetFriendlyName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
Expand Down Expand Up @@ -1514,6 +1576,40 @@ func expandBigQueryDatasetDescription(v interface{}, d tpgresource.TerraformReso
return v, nil
}

func expandBigQueryDatasetExternalDatasetReference(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedExternalSource, err := expandBigQueryDatasetExternalDatasetReferenceExternalSource(original["external_source"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedExternalSource); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["externalSource"] = transformedExternalSource
}

transformedConnection, err := expandBigQueryDatasetExternalDatasetReferenceConnection(original["connection"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedConnection); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["connection"] = transformedConnection
}

return transformed, nil
}

func expandBigQueryDatasetExternalDatasetReferenceExternalSource(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandBigQueryDatasetExternalDatasetReferenceConnection(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandBigQueryDatasetFriendlyName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,47 @@ resource "google_service_account" "bqowner" {
`, context)
}

func TestAccBigQueryDataset_bigqueryDatasetExternalReferenceAwsTestExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckBigQueryDatasetDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigQueryDataset_bigqueryDatasetExternalReferenceAwsTestExample(context),
},
{
ResourceName: "google_bigquery_dataset.dataset",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
},
})
}

func testAccBigQueryDataset_bigqueryDatasetExternalReferenceAwsTestExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_bigquery_dataset" "dataset" {
dataset_id = "tf_test_example_dataset%{random_suffix}"
friendly_name = "test"
description = "This is a test description"
location = "aws-us-east-1"
external_dataset_reference {
external_source = "aws-glue://arn:aws:glue:us-east-1:772042918353:database/db_other_formats_external"
connection = "projects/bigquerytestdefault/locations/aws-us-east-1/connections/external_test-connection"
}
}
`, context)
}

func testAccCheckBigQueryDatasetDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
5 changes: 2 additions & 3 deletions website/docs/r/bigquery_dataset.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,11 @@ resource "google_bigquery_dataset" "private" {
}
}
```
## Example Usage - Bigquery Dataset External Reference Aws Docs
## Example Usage - Bigquery Dataset External Reference Aws


```hcl
resource "google_bigquery_dataset" "dataset" {
provider = google-beta
dataset_id = "example_dataset"
friendly_name = "test"
description = "This is a test description"
Expand Down Expand Up @@ -282,7 +281,7 @@ The following arguments are supported:
A user-friendly description of the dataset

* `external_dataset_reference` -
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
(Optional)
Information about the external metadata storage where the dataset is defined.
Structure is [documented below](#nested_external_dataset_reference).

Expand Down

0 comments on commit fc84d7e

Please sign in to comment.