Skip to content

Commit

Permalink
Support healthcare dicom bq stream configure (#4717) (#3190)
Browse files Browse the repository at this point in the history
* Change healthcare consent API to GA support

* rebase update

* fix import format

* add fully quantified reference to healthcare resource

* fix import tests

* add dicom bq stream support

* resolve comments

* update example

* fix bq table name

* fix bq ds name

* change bq ds name

Co-authored-by: Scott Suarez <ScottMSuarez@gmail.com>
Signed-off-by: Modular Magician <magic-modules@google.com>

Co-authored-by: Scott Suarez <ScottMSuarez@gmail.com>
  • Loading branch information
modular-magician and ScottSuarez committed Apr 23, 2021
1 parent 762f528 commit db20835
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/4717.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
healthcare: support bq stream configure in google_healthcare_dicom_store
```
124 changes: 124 additions & 0 deletions google-beta/resource_healthcare_dicom_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,31 @@ Cloud Pub/Sub topic. Not having adequate permissions will cause the calls that s
},
},
},
"stream_configs": {
Type: schema.TypeList,
Optional: true,
Description: `To enable streaming to BigQuery, configure the streamConfigs object in your DICOM store.
streamConfigs is an array, so you can specify multiple BigQuery destinations. You can stream metadata from a single DICOM store to up to five BigQuery tables in a BigQuery dataset.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"bigquery_destination": {
Type: schema.TypeList,
Required: true,
Description: `BigQueryDestination to include a fully qualified BigQuery table URI where DICOM instance metadata will be streamed.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"table_uri": {
Type: schema.TypeString,
Required: true,
Description: `a fully qualified BigQuery table URI where DICOM instance metadata will be streamed.`,
},
},
},
},
},
},
},
"self_link": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -131,6 +156,12 @@ func resourceHealthcareDicomStoreCreate(d *schema.ResourceData, meta interface{}
} else if v, ok := d.GetOkExists("notification_config"); !isEmptyValue(reflect.ValueOf(notificationConfigProp)) && (ok || !reflect.DeepEqual(v, notificationConfigProp)) {
obj["notificationConfig"] = notificationConfigProp
}
streamConfigsProp, err := expandHealthcareDicomStoreStreamConfigs(d.Get("stream_configs"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("stream_configs"); !isEmptyValue(reflect.ValueOf(streamConfigsProp)) && (ok || !reflect.DeepEqual(v, streamConfigsProp)) {
obj["streamConfigs"] = streamConfigsProp
}

url, err := replaceVars(d, config, "{{HealthcareBasePath}}{{dataset}}/dicomStores?dicomStoreId={{name}}")
if err != nil {
Expand Down Expand Up @@ -207,6 +238,9 @@ func resourceHealthcareDicomStoreRead(d *schema.ResourceData, meta interface{})
if err := d.Set("notification_config", flattenHealthcareDicomStoreNotificationConfig(res["notificationConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading DicomStore: %s", err)
}
if err := d.Set("stream_configs", flattenHealthcareDicomStoreStreamConfigs(res["streamConfigs"], d, config)); err != nil {
return fmt.Errorf("Error reading DicomStore: %s", err)
}

return nil
}
Expand All @@ -233,6 +267,12 @@ func resourceHealthcareDicomStoreUpdate(d *schema.ResourceData, meta interface{}
} else if v, ok := d.GetOkExists("notification_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, notificationConfigProp)) {
obj["notificationConfig"] = notificationConfigProp
}
streamConfigsProp, err := expandHealthcareDicomStoreStreamConfigs(d.Get("stream_configs"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("stream_configs"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, streamConfigsProp)) {
obj["streamConfigs"] = streamConfigsProp
}

url, err := replaceVars(d, config, "{{HealthcareBasePath}}{{dataset}}/dicomStores/{{name}}")
if err != nil {
Expand All @@ -249,6 +289,10 @@ func resourceHealthcareDicomStoreUpdate(d *schema.ResourceData, meta interface{}
if d.HasChange("notification_config") {
updateMask = append(updateMask, "notificationConfig")
}

if d.HasChange("stream_configs") {
updateMask = append(updateMask, "streamConfigs")
}
// updateMask is a URL parameter but not present in the schema, so replaceVars
// won't set it
url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
Expand Down Expand Up @@ -347,6 +391,41 @@ func flattenHealthcareDicomStoreNotificationConfigPubsubTopic(v interface{}, d *
return v
}

func flattenHealthcareDicomStoreStreamConfigs(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return v
}
l := v.([]interface{})
transformed := make([]interface{}, 0, len(l))
for _, raw := range l {
original := raw.(map[string]interface{})
if len(original) < 1 {
// Do not include empty json objects coming back from the api
continue
}
transformed = append(transformed, map[string]interface{}{
"bigquery_destination": flattenHealthcareDicomStoreStreamConfigsBigqueryDestination(original["bigqueryDestination"], d, config),
})
}
return transformed
}
func flattenHealthcareDicomStoreStreamConfigsBigqueryDestination(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["table_uri"] =
flattenHealthcareDicomStoreStreamConfigsBigqueryDestinationTableUri(original["tableUri"], d, config)
return []interface{}{transformed}
}
func flattenHealthcareDicomStoreStreamConfigsBigqueryDestinationTableUri(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func expandHealthcareDicomStoreName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -385,6 +464,51 @@ func expandHealthcareDicomStoreNotificationConfigPubsubTopic(v interface{}, d Te
return v, nil
}

func expandHealthcareDicomStoreStreamConfigs(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
for _, raw := range l {
if raw == nil {
continue
}
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedBigqueryDestination, err := expandHealthcareDicomStoreStreamConfigsBigqueryDestination(original["bigquery_destination"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedBigqueryDestination); val.IsValid() && !isEmptyValue(val) {
transformed["bigqueryDestination"] = transformedBigqueryDestination
}

req = append(req, transformed)
}
return req, nil
}

func expandHealthcareDicomStoreStreamConfigsBigqueryDestination(v interface{}, d TerraformResourceData, config *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{})

transformedTableUri, err := expandHealthcareDicomStoreStreamConfigsBigqueryDestinationTableUri(original["table_uri"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedTableUri); val.IsValid() && !isEmptyValue(val) {
transformed["tableUri"] = transformedTableUri
}

return transformed, nil
}

func expandHealthcareDicomStoreStreamConfigsBigqueryDestinationTableUri(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func resourceHealthcareDicomStoreDecoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) {
// Take the returned long form of the name and use it as `self_link`.
// Then modify the name to be the user specified form.
Expand Down
75 changes: 75 additions & 0 deletions google-beta/resource_healthcare_dicom_store_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,81 @@ resource "google_healthcare_dataset" "dataset" {
`, context)
}

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

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

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProvidersOiCS,
CheckDestroy: testAccCheckHealthcareDicomStoreDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccHealthcareDicomStore_healthcareDicomStoreBqStreamExample(context),
},
},
})
}

func testAccHealthcareDicomStore_healthcareDicomStoreBqStreamExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_healthcare_dicom_store" "default" {
provider = google-beta
name = "tf-test-example-dicom-store%{random_suffix}"
dataset = google_healthcare_dataset.dataset.id
notification_config {
pubsub_topic = google_pubsub_topic.topic.id
}
labels = {
label1 = "labelvalue1"
}
stream_configs {
bigquery_destination {
table_uri = "bq://${google_bigquery_dataset.bq_dataset.project}.${google_bigquery_dataset.bq_dataset.dataset_id}.${google_bigquery_table.bq_table.table_id}"
}
}
}
resource "google_pubsub_topic" "topic" {
provider = google-beta
name = "tf-test-dicom-notifications%{random_suffix}"
}
resource "google_healthcare_dataset" "dataset" {
provider = google-beta
name = "tf-test-example-dataset%{random_suffix}"
location = "us-central1"
}
resource "google_bigquery_dataset" "bq_dataset" {
provider = google-beta
dataset_id = "tf_test_dicom_bq_ds%{random_suffix}"
friendly_name = "test"
description = "This is a test description"
location = "US"
delete_contents_on_destroy = true
}
resource "google_bigquery_table" "bq_table" {
provider = google-beta
deletion_protection = false
dataset_id = google_bigquery_dataset.bq_dataset.dataset_id
table_id = "tf_test_dicom_bq_tb%{random_suffix}"
}
`, context)
}

func testAccCheckHealthcareDicomStoreDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
81 changes: 81 additions & 0 deletions website/docs/r/healthcare_dicom_store.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,67 @@ resource "google_healthcare_dataset" "dataset" {
location = "us-central1"
}
```
<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=healthcare_dicom_store_bq_stream&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%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 - Healthcare Dicom Store Bq Stream


```hcl
resource "google_healthcare_dicom_store" "default" {
provider = google-beta
name = "example-dicom-store"
dataset = google_healthcare_dataset.dataset.id
notification_config {
pubsub_topic = google_pubsub_topic.topic.id
}
labels = {
label1 = "labelvalue1"
}
stream_configs {
bigquery_destination {
table_uri = "bq://${google_bigquery_dataset.bq_dataset.project}.${google_bigquery_dataset.bq_dataset.dataset_id}.${google_bigquery_table.bq_table.table_id}"
}
}
}
resource "google_pubsub_topic" "topic" {
provider = google-beta
name = "dicom-notifications"
}
resource "google_healthcare_dataset" "dataset" {
provider = google-beta
name = "example-dataset"
location = "us-central1"
}
resource "google_bigquery_dataset" "bq_dataset" {
provider = google-beta
dataset_id = "dicom_bq_ds"
friendly_name = "test"
description = "This is a test description"
location = "US"
delete_contents_on_destroy = true
}
resource "google_bigquery_table" "bq_table" {
provider = google-beta
deletion_protection = false
dataset_id = google_bigquery_dataset.bq_dataset.dataset_id
table_id = "dicom_bq_tb"
}
```

## Argument Reference

Expand Down Expand Up @@ -100,6 +161,12 @@ The following arguments are supported:
A nested object resource
Structure is documented below.

* `stream_configs` -
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
To enable streaming to BigQuery, configure the streamConfigs object in your DICOM store.
streamConfigs is an array, so you can specify multiple BigQuery destinations. You can stream metadata from a single DICOM store to up to five BigQuery tables in a BigQuery dataset.
Structure is documented below.


The `notification_config` block supports:

Expand All @@ -112,6 +179,20 @@ The `notification_config` block supports:
project. service-PROJECT_NUMBER@gcp-sa-healthcare.iam.gserviceaccount.com must have publisher permissions on the given
Cloud Pub/Sub topic. Not having adequate permissions will cause the calls that send notifications to fail.

The `stream_configs` block supports:

* `bigquery_destination` -
(Required)
BigQueryDestination to include a fully qualified BigQuery table URI where DICOM instance metadata will be streamed.
Structure is documented below.


The `bigquery_destination` block supports:

* `table_uri` -
(Required)
a fully qualified BigQuery table URI where DICOM instance metadata will be streamed.

## Attributes Reference

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

0 comments on commit db20835

Please sign in to comment.