Skip to content

Commit

Permalink
bq analyticshub listing - add support for restrictedExportConfig (#96…
Browse files Browse the repository at this point in the history
…61) (#6784)

* bq analyticshub listing - add support for restrictedExportConfig

* lint

* Update mmv1/products/bigqueryanalyticshub/Listing.yaml

---------


[upstream:9baa61fae74633eaa57e63c532a0bef70c12aaa6]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Dec 20, 2023
1 parent 1e04d77 commit d50dd59
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/9661.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
bigqueryanalyticshub: added `restricted_export_config` field to `google_bigquery_analytics_hub_listing ` resource
```
5 changes: 5 additions & 0 deletions .teamcity/components/generated/packages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ var packages = mapOf(
"displayName" to "Environment Variables",
"path" to "./google-beta/envvar"
),
"fwmodels" to mapOf(
"name" to "fwmodels",
"displayName" to "Framework Models",
"path" to "./google-beta/fwmodels"
),
"fwprovider" to mapOf(
"name" to "fwprovider",
"displayName" to "Framework Provider",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,26 @@ func ResourceBigqueryAnalyticsHubListing() *schema.Resource {
Optional: true,
Description: `Email or URL of the request access of the listing. Subscribers can use this reference to request access.`,
},
"restricted_export_config": {
Type: schema.TypeList,
Optional: true,
Description: `If set, restricted export configuration will be propagated and enforced on the linked dataset.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
Description: `If true, enable restricted export.`,
},
"restrict_query_result": {
Type: schema.TypeBool,
Optional: true,
Description: `If true, restrict export of query result derived from restricted linked dataset table.`,
},
},
},
},
"name": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -249,6 +269,12 @@ func resourceBigqueryAnalyticsHubListingCreate(d *schema.ResourceData, meta inte
} else if v, ok := d.GetOkExists("bigquery_dataset"); !tpgresource.IsEmptyValue(reflect.ValueOf(bigqueryDatasetProp)) && (ok || !reflect.DeepEqual(v, bigqueryDatasetProp)) {
obj["bigqueryDataset"] = bigqueryDatasetProp
}
restrictedExportConfigProp, err := expandBigqueryAnalyticsHubListingRestrictedExportConfig(d.Get("restricted_export_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("restricted_export_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(restrictedExportConfigProp)) && (ok || !reflect.DeepEqual(v, restrictedExportConfigProp)) {
obj["restrictedExportConfig"] = restrictedExportConfigProp
}

url, err := tpgresource.ReplaceVars(d, config, "{{BigqueryAnalyticsHubBasePath}}projects/{{project}}/locations/{{location}}/dataExchanges/{{data_exchange_id}}/listings?listing_id={{listing_id}}")
if err != nil {
Expand Down Expand Up @@ -370,6 +396,9 @@ func resourceBigqueryAnalyticsHubListingRead(d *schema.ResourceData, meta interf
if err := d.Set("bigquery_dataset", flattenBigqueryAnalyticsHubListingBigqueryDataset(res["bigqueryDataset"], d, config)); err != nil {
return fmt.Errorf("Error reading Listing: %s", err)
}
if err := d.Set("restricted_export_config", flattenBigqueryAnalyticsHubListingRestrictedExportConfig(res["restrictedExportConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading Listing: %s", err)
}

return nil
}
Expand Down Expand Up @@ -450,6 +479,12 @@ func resourceBigqueryAnalyticsHubListingUpdate(d *schema.ResourceData, meta inte
} else if v, ok := d.GetOkExists("bigquery_dataset"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, bigqueryDatasetProp)) {
obj["bigqueryDataset"] = bigqueryDatasetProp
}
restrictedExportConfigProp, err := expandBigqueryAnalyticsHubListingRestrictedExportConfig(d.Get("restricted_export_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("restricted_export_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, restrictedExportConfigProp)) {
obj["restrictedExportConfig"] = restrictedExportConfigProp
}

url, err := tpgresource.ReplaceVars(d, config, "{{BigqueryAnalyticsHubBasePath}}projects/{{project}}/locations/{{location}}/dataExchanges/{{data_exchange_id}}/listings/{{listing_id}}")
if err != nil {
Expand Down Expand Up @@ -498,6 +533,10 @@ func resourceBigqueryAnalyticsHubListingUpdate(d *schema.ResourceData, meta inte
if d.HasChange("bigquery_dataset") {
updateMask = append(updateMask, "bigqueryDataset")
}

if d.HasChange("restricted_export_config") {
updateMask = append(updateMask, "restrictedExportConfig")
}
// updateMask is a URL parameter but not present in the schema, so ReplaceVars
// won't set it
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
Expand Down Expand Up @@ -693,6 +732,29 @@ func flattenBigqueryAnalyticsHubListingBigqueryDatasetDataset(v interface{}, d *
return v
}

func flattenBigqueryAnalyticsHubListingRestrictedExportConfig(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["enabled"] =
flattenBigqueryAnalyticsHubListingRestrictedExportConfigEnabled(original["enabled"], d, config)
transformed["restrict_query_result"] =
flattenBigqueryAnalyticsHubListingRestrictedExportConfigRestrictQueryResult(original["restrictQueryResult"], d, config)
return []interface{}{transformed}
}
func flattenBigqueryAnalyticsHubListingRestrictedExportConfigEnabled(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

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

func expandBigqueryAnalyticsHubListingDisplayName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -811,3 +873,37 @@ func expandBigqueryAnalyticsHubListingBigqueryDataset(v interface{}, d tpgresour
func expandBigqueryAnalyticsHubListingBigqueryDatasetDataset(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandBigqueryAnalyticsHubListingRestrictedExportConfig(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{})

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

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

return transformed, nil
}

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

func expandBigqueryAnalyticsHubListingRestrictedExportConfigRestrictQueryResult(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,66 @@ resource "google_bigquery_dataset" "listing" {
`, context)
}

func TestAccBigqueryAnalyticsHubListing_bigqueryAnalyticshubListingRestrictedExample(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: testAccCheckBigqueryAnalyticsHubListingDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigqueryAnalyticsHubListing_bigqueryAnalyticshubListingRestrictedExample(context),
},
{
ResourceName: "google_bigquery_analytics_hub_listing.listing",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"data_exchange_id", "listing_id", "location"},
},
},
})
}

func testAccBigqueryAnalyticsHubListing_bigqueryAnalyticshubListingRestrictedExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_bigquery_analytics_hub_data_exchange" "listing" {
location = "US"
data_exchange_id = "tf_test_my_data_exchange%{random_suffix}"
display_name = "tf_test_my_data_exchange%{random_suffix}"
description = "example data exchange%{random_suffix}"
}
resource "google_bigquery_analytics_hub_listing" "listing" {
location = "US"
data_exchange_id = google_bigquery_analytics_hub_data_exchange.listing.data_exchange_id
listing_id = "tf_test_my_listing%{random_suffix}"
display_name = "tf_test_my_listing%{random_suffix}"
description = "example data exchange%{random_suffix}"
bigquery_dataset {
dataset = google_bigquery_dataset.listing.id
}
restricted_export_config {
enabled = true
restrict_query_result = true
}
}
resource "google_bigquery_dataset" "listing" {
dataset_id = "tf_test_my_listing%{random_suffix}"
friendly_name = "tf_test_my_listing%{random_suffix}"
description = "example data exchange%{random_suffix}"
location = "US"
}
`, context)
}

func testAccCheckBigqueryAnalyticsHubListingDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
55 changes: 55 additions & 0 deletions website/docs/r/bigquery_analytics_hub_listing.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,46 @@ resource "google_bigquery_analytics_hub_listing" "listing" {
}
}
resource "google_bigquery_dataset" "listing" {
dataset_id = "my_listing"
friendly_name = "my_listing"
description = "example data exchange"
location = "US"
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=bigquery_analyticshub_listing_restricted&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Bigquery Analyticshub Listing Restricted


```hcl
resource "google_bigquery_analytics_hub_data_exchange" "listing" {
location = "US"
data_exchange_id = "my_data_exchange"
display_name = "my_data_exchange"
description = "example data exchange"
}
resource "google_bigquery_analytics_hub_listing" "listing" {
location = "US"
data_exchange_id = google_bigquery_analytics_hub_data_exchange.listing.data_exchange_id
listing_id = "my_listing"
display_name = "my_listing"
description = "example data exchange"
bigquery_dataset {
dataset = google_bigquery_dataset.listing.id
}
restricted_export_config {
enabled = true
restrict_query_result = true
}
}
resource "google_bigquery_dataset" "listing" {
dataset_id = "my_listing"
friendly_name = "my_listing"
Expand Down Expand Up @@ -134,6 +174,11 @@ The following arguments are supported:
(Optional)
Categories of the listing. Up to two categories are allowed.

* `restricted_export_config` -
(Optional)
If set, restricted export configuration will be propagated and enforced on the linked dataset.
Structure is [documented below](#nested_restricted_export_config).

* `project` - (Optional) The ID of the project in which the resource belongs.
If it is not provided, the provider project is used.

Expand All @@ -158,6 +203,16 @@ The following arguments are supported:
(Optional)
Email or URL of the listing publisher.

<a name="nested_restricted_export_config"></a>The `restricted_export_config` block supports:

* `enabled` -
(Optional)
If true, enable restricted export.

* `restrict_query_result` -
(Optional)
If true, restrict export of query result derived from restricted linked dataset table.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:
Expand Down

0 comments on commit d50dd59

Please sign in to comment.