diff --git a/.changelog/4156.txt b/.changelog/4156.txt new file mode 100644 index 00000000000..4d723575df2 --- /dev/null +++ b/.changelog/4156.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +bigquery: added `email_preferences ` field to `google_bigquery_data_transfer_config` resource +``` diff --git a/google/resource_bigquery_data_transfer_config.go b/google/resource_bigquery_data_transfer_config.go index f7f11519b7c..5f3c46a961c 100644 --- a/google/resource_bigquery_data_transfer_config.go +++ b/google/resource_bigquery_data_transfer_config.go @@ -96,6 +96,22 @@ Set the value to 0 to use the default value.`, Optional: true, Description: `When set to true, no runs are scheduled for a given transfer.`, }, + "email_preferences": { + Type: schema.TypeList, + Optional: true, + Description: `Email notifications will be sent according to these preferences to the +email address of the user who owns this transfer config.`, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "enable_failure_email": { + Type: schema.TypeBool, + Required: true, + Description: `If true, email notifications will be sent on transfer run failures.`, + }, + }, + }, + }, "location": { Type: schema.TypeString, Optional: true, @@ -248,6 +264,12 @@ func resourceBigqueryDataTransferConfigCreate(d *schema.ResourceData, meta inter } else if v, ok := d.GetOkExists("schedule_options"); !isEmptyValue(reflect.ValueOf(scheduleOptionsProp)) && (ok || !reflect.DeepEqual(v, scheduleOptionsProp)) { obj["scheduleOptions"] = scheduleOptionsProp } + emailPreferencesProp, err := expandBigqueryDataTransferConfigEmailPreferences(d.Get("email_preferences"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("email_preferences"); !isEmptyValue(reflect.ValueOf(emailPreferencesProp)) && (ok || !reflect.DeepEqual(v, emailPreferencesProp)) { + obj["emailPreferences"] = emailPreferencesProp + } notificationPubsubTopicProp, err := expandBigqueryDataTransferConfigNotificationPubsubTopic(d.Get("notification_pubsub_topic"), d, config) if err != nil { return err @@ -399,6 +421,9 @@ func resourceBigqueryDataTransferConfigRead(d *schema.ResourceData, meta interfa if err := d.Set("schedule_options", flattenBigqueryDataTransferConfigScheduleOptions(res["scheduleOptions"], d, config)); err != nil { return fmt.Errorf("Error reading Config: %s", err) } + if err := d.Set("email_preferences", flattenBigqueryDataTransferConfigEmailPreferences(res["emailPreferences"], d, config)); err != nil { + return fmt.Errorf("Error reading Config: %s", err) + } if err := d.Set("notification_pubsub_topic", flattenBigqueryDataTransferConfigNotificationPubsubTopic(res["notificationPubsubTopic"], d, config)); err != nil { return fmt.Errorf("Error reading Config: %s", err) } @@ -449,6 +474,12 @@ func resourceBigqueryDataTransferConfigUpdate(d *schema.ResourceData, meta inter } else if v, ok := d.GetOkExists("schedule_options"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, scheduleOptionsProp)) { obj["scheduleOptions"] = scheduleOptionsProp } + emailPreferencesProp, err := expandBigqueryDataTransferConfigEmailPreferences(d.Get("email_preferences"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("email_preferences"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, emailPreferencesProp)) { + obj["emailPreferences"] = emailPreferencesProp + } notificationPubsubTopicProp, err := expandBigqueryDataTransferConfigNotificationPubsubTopic(d.Get("notification_pubsub_topic"), d, config) if err != nil { return err @@ -499,6 +530,10 @@ func resourceBigqueryDataTransferConfigUpdate(d *schema.ResourceData, meta inter updateMask = append(updateMask, "scheduleOptions") } + if d.HasChange("email_preferences") { + updateMask = append(updateMask, "emailPreferences") + } + if d.HasChange("notification_pubsub_topic") { updateMask = append(updateMask, "notificationPubsubTopic") } @@ -635,6 +670,23 @@ func flattenBigqueryDataTransferConfigScheduleOptionsEndTime(v interface{}, d *s return v } +func flattenBigqueryDataTransferConfigEmailPreferences(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["enable_failure_email"] = + flattenBigqueryDataTransferConfigEmailPreferencesEnableFailureEmail(original["enableFailureEmail"], d, config) + return []interface{}{transformed} +} +func flattenBigqueryDataTransferConfigEmailPreferencesEnableFailureEmail(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + func flattenBigqueryDataTransferConfigNotificationPubsubTopic(v interface{}, d *schema.ResourceData, config *Config) interface{} { return v } @@ -735,6 +787,29 @@ func expandBigqueryDataTransferConfigScheduleOptionsEndTime(v interface{}, d Ter return v, nil } +func expandBigqueryDataTransferConfigEmailPreferences(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{}) + + transformedEnableFailureEmail, err := expandBigqueryDataTransferConfigEmailPreferencesEnableFailureEmail(original["enable_failure_email"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedEnableFailureEmail); val.IsValid() && !isEmptyValue(val) { + transformed["enableFailureEmail"] = transformedEnableFailureEmail + } + + return transformed, nil +} + +func expandBigqueryDataTransferConfigEmailPreferencesEnableFailureEmail(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + func expandBigqueryDataTransferConfigNotificationPubsubTopic(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { return v, nil } diff --git a/google/resource_bigquery_data_transfer_config_test.go b/google/resource_bigquery_data_transfer_config_test.go index 80e3cb75cd1..a9b2a7b8b5a 100644 --- a/google/resource_bigquery_data_transfer_config_test.go +++ b/google/resource_bigquery_data_transfer_config_test.go @@ -191,6 +191,9 @@ resource "google_bigquery_data_transfer_config" "query_config" { } destination_dataset_id = google_bigquery_dataset.my_dataset.dataset_id notification_pubsub_topic = google_pubsub_topic.my_topic.id + email_preferences = { + enable_failure_email = true + } params = { destination_table_name_template = "my_table" write_disposition = "WRITE_APPEND" diff --git a/website/docs/r/bigquery_data_transfer_config.html.markdown b/website/docs/r/bigquery_data_transfer_config.html.markdown index a721b15ebb7..d5ce3f241c8 100644 --- a/website/docs/r/bigquery_data_transfer_config.html.markdown +++ b/website/docs/r/bigquery_data_transfer_config.html.markdown @@ -113,6 +113,12 @@ The following arguments are supported: Options customizing the data transfer schedule. Structure is documented below. +* `email_preferences` - + (Optional) + Email notifications will be sent according to these preferences to the + email address of the user who owns this transfer config. + Structure is documented below. + * `notification_pubsub_topic` - (Optional) Pub/Sub topic where notifications will be sent after transfer runs @@ -180,6 +186,12 @@ The `schedule_options` block supports: moment. The time when a data transfer can be triggered manually is not limited by this option. +The `email_preferences` block supports: + +* `enable_failure_email` - + (Required) + If true, email notifications will be sent on transfer run failures. + The `sensitive_params` block supports: * `secret_access_key` -