Skip to content

Commit 56eaf42

Browse files
Add scheduleOptions to BigQuery Data Transfer (#3895) (#2641)
* Add scheduleOptions to BigQuery Data Transfer * Fix typo * Update set the timezone to UTC * Update API type to Time Signed-off-by: Modular Magician <[email protected]>
1 parent 83703c2 commit 56eaf42

File tree

4 files changed

+184
-5
lines changed

4 files changed

+184
-5
lines changed

.changelog/3895.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
bigquery: added `schedule_options` field to `google_bigquery_data_transfer_config` resource
3+
```

google-beta/resource_bigquery_data_transfer_config.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,45 @@ about the format here:
122122
https://cloud.google.com/appengine/docs/flexible/python/scheduling-jobs-with-cron-yaml#the_schedule_format
123123
NOTE: the granularity should be at least 8 hours, or less frequent.`,
124124
},
125+
"schedule_options": {
126+
Type: schema.TypeList,
127+
Optional: true,
128+
Description: `Options customizing the data transfer schedule.`,
129+
MaxItems: 1,
130+
Elem: &schema.Resource{
131+
Schema: map[string]*schema.Schema{
132+
"disable_auto_scheduling": {
133+
Type: schema.TypeBool,
134+
Optional: true,
135+
Description: `If true, automatic scheduling of data transfer runs for this
136+
configuration will be disabled. The runs can be started on ad-hoc
137+
basis using transferConfigs.startManualRuns API. When automatic
138+
scheduling is disabled, the TransferConfig.schedule field will
139+
be ignored.`,
140+
AtLeastOneOf: []string{"schedule_options.0.disable_auto_scheduling", "schedule_options.0.start_time", "schedule_options.0.end_time"},
141+
},
142+
"end_time": {
143+
Type: schema.TypeString,
144+
Optional: true,
145+
Description: `Defines time to stop scheduling transfer runs. A transfer run cannot be
146+
scheduled at or after the end time. The end time can be changed at any
147+
moment. The time when a data transfer can be triggered manually is not
148+
limited by this option.`,
149+
AtLeastOneOf: []string{"schedule_options.0.disable_auto_scheduling", "schedule_options.0.start_time", "schedule_options.0.end_time"},
150+
},
151+
"start_time": {
152+
Type: schema.TypeString,
153+
Optional: true,
154+
Description: `Specifies time to start scheduling transfer runs. The first run will be
155+
scheduled at or after the start time according to a recurrence pattern
156+
defined in the schedule string. The start time can be changed at any
157+
moment. The time when a data transfer can be triggered manually is not
158+
limited by this option.`,
159+
AtLeastOneOf: []string{"schedule_options.0.disable_auto_scheduling", "schedule_options.0.start_time", "schedule_options.0.end_time"},
160+
},
161+
},
162+
},
163+
},
125164
"sensitive_params": {
126165
Type: schema.TypeList,
127166
Optional: true,
@@ -203,6 +242,12 @@ func resourceBigqueryDataTransferConfigCreate(d *schema.ResourceData, meta inter
203242
} else if v, ok := d.GetOkExists("schedule"); !isEmptyValue(reflect.ValueOf(scheduleProp)) && (ok || !reflect.DeepEqual(v, scheduleProp)) {
204243
obj["schedule"] = scheduleProp
205244
}
245+
scheduleOptionsProp, err := expandBigqueryDataTransferConfigScheduleOptions(d.Get("schedule_options"), d, config)
246+
if err != nil {
247+
return err
248+
} else if v, ok := d.GetOkExists("schedule_options"); !isEmptyValue(reflect.ValueOf(scheduleOptionsProp)) && (ok || !reflect.DeepEqual(v, scheduleOptionsProp)) {
249+
obj["scheduleOptions"] = scheduleOptionsProp
250+
}
206251
notificationPubsubTopicProp, err := expandBigqueryDataTransferConfigNotificationPubsubTopic(d.Get("notification_pubsub_topic"), d, config)
207252
if err != nil {
208253
return err
@@ -351,6 +396,9 @@ func resourceBigqueryDataTransferConfigRead(d *schema.ResourceData, meta interfa
351396
if err := d.Set("schedule", flattenBigqueryDataTransferConfigSchedule(res["schedule"], d, config)); err != nil {
352397
return fmt.Errorf("Error reading Config: %s", err)
353398
}
399+
if err := d.Set("schedule_options", flattenBigqueryDataTransferConfigScheduleOptions(res["scheduleOptions"], d, config)); err != nil {
400+
return fmt.Errorf("Error reading Config: %s", err)
401+
}
354402
if err := d.Set("notification_pubsub_topic", flattenBigqueryDataTransferConfigNotificationPubsubTopic(res["notificationPubsubTopic"], d, config)); err != nil {
355403
return fmt.Errorf("Error reading Config: %s", err)
356404
}
@@ -395,6 +443,12 @@ func resourceBigqueryDataTransferConfigUpdate(d *schema.ResourceData, meta inter
395443
} else if v, ok := d.GetOkExists("schedule"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, scheduleProp)) {
396444
obj["schedule"] = scheduleProp
397445
}
446+
scheduleOptionsProp, err := expandBigqueryDataTransferConfigScheduleOptions(d.Get("schedule_options"), d, config)
447+
if err != nil {
448+
return err
449+
} else if v, ok := d.GetOkExists("schedule_options"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, scheduleOptionsProp)) {
450+
obj["scheduleOptions"] = scheduleOptionsProp
451+
}
398452
notificationPubsubTopicProp, err := expandBigqueryDataTransferConfigNotificationPubsubTopic(d.Get("notification_pubsub_topic"), d, config)
399453
if err != nil {
400454
return err
@@ -441,6 +495,10 @@ func resourceBigqueryDataTransferConfigUpdate(d *schema.ResourceData, meta inter
441495
updateMask = append(updateMask, "schedule")
442496
}
443497

498+
if d.HasChange("schedule_options") {
499+
updateMask = append(updateMask, "scheduleOptions")
500+
}
501+
444502
if d.HasChange("notification_pubsub_topic") {
445503
updateMask = append(updateMask, "notificationPubsubTopic")
446504
}
@@ -548,6 +606,35 @@ func flattenBigqueryDataTransferConfigSchedule(v interface{}, d *schema.Resource
548606
return v
549607
}
550608

609+
func flattenBigqueryDataTransferConfigScheduleOptions(v interface{}, d *schema.ResourceData, config *Config) interface{} {
610+
if v == nil {
611+
return nil
612+
}
613+
original := v.(map[string]interface{})
614+
if len(original) == 0 {
615+
return nil
616+
}
617+
transformed := make(map[string]interface{})
618+
transformed["disable_auto_scheduling"] =
619+
flattenBigqueryDataTransferConfigScheduleOptionsDisableAutoScheduling(original["disableAutoScheduling"], d, config)
620+
transformed["start_time"] =
621+
flattenBigqueryDataTransferConfigScheduleOptionsStartTime(original["startTime"], d, config)
622+
transformed["end_time"] =
623+
flattenBigqueryDataTransferConfigScheduleOptionsEndTime(original["endTime"], d, config)
624+
return []interface{}{transformed}
625+
}
626+
func flattenBigqueryDataTransferConfigScheduleOptionsDisableAutoScheduling(v interface{}, d *schema.ResourceData, config *Config) interface{} {
627+
return v
628+
}
629+
630+
func flattenBigqueryDataTransferConfigScheduleOptionsStartTime(v interface{}, d *schema.ResourceData, config *Config) interface{} {
631+
return v
632+
}
633+
634+
func flattenBigqueryDataTransferConfigScheduleOptionsEndTime(v interface{}, d *schema.ResourceData, config *Config) interface{} {
635+
return v
636+
}
637+
551638
func flattenBigqueryDataTransferConfigNotificationPubsubTopic(v interface{}, d *schema.ResourceData, config *Config) interface{} {
552639
return v
553640
}
@@ -603,6 +690,51 @@ func expandBigqueryDataTransferConfigSchedule(v interface{}, d TerraformResource
603690
return v, nil
604691
}
605692

693+
func expandBigqueryDataTransferConfigScheduleOptions(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
694+
l := v.([]interface{})
695+
if len(l) == 0 || l[0] == nil {
696+
return nil, nil
697+
}
698+
raw := l[0]
699+
original := raw.(map[string]interface{})
700+
transformed := make(map[string]interface{})
701+
702+
transformedDisableAutoScheduling, err := expandBigqueryDataTransferConfigScheduleOptionsDisableAutoScheduling(original["disable_auto_scheduling"], d, config)
703+
if err != nil {
704+
return nil, err
705+
} else if val := reflect.ValueOf(transformedDisableAutoScheduling); val.IsValid() && !isEmptyValue(val) {
706+
transformed["disableAutoScheduling"] = transformedDisableAutoScheduling
707+
}
708+
709+
transformedStartTime, err := expandBigqueryDataTransferConfigScheduleOptionsStartTime(original["start_time"], d, config)
710+
if err != nil {
711+
return nil, err
712+
} else if val := reflect.ValueOf(transformedStartTime); val.IsValid() && !isEmptyValue(val) {
713+
transformed["startTime"] = transformedStartTime
714+
}
715+
716+
transformedEndTime, err := expandBigqueryDataTransferConfigScheduleOptionsEndTime(original["end_time"], d, config)
717+
if err != nil {
718+
return nil, err
719+
} else if val := reflect.ValueOf(transformedEndTime); val.IsValid() && !isEmptyValue(val) {
720+
transformed["endTime"] = transformedEndTime
721+
}
722+
723+
return transformed, nil
724+
}
725+
726+
func expandBigqueryDataTransferConfigScheduleOptionsDisableAutoScheduling(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
727+
return v, nil
728+
}
729+
730+
func expandBigqueryDataTransferConfigScheduleOptionsStartTime(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
731+
return v, nil
732+
}
733+
734+
func expandBigqueryDataTransferConfigScheduleOptionsEndTime(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
735+
return v, nil
736+
}
737+
606738
func expandBigqueryDataTransferConfigNotificationPubsubTopic(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
607739
return v, nil
608740
}

google-beta/resource_bigquery_data_transfer_config_test.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strings"
66
"testing"
7+
"time"
78

89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
@@ -33,14 +34,17 @@ func TestAccBigqueryDataTransferConfig(t *testing.T) {
3334

3435
func testAccBigqueryDataTransferConfig_scheduledQuery_basic(t *testing.T) {
3536
random_suffix := randString(t, 10)
37+
now := time.Now().UTC()
38+
start_time := now.Add(1 * time.Hour).Format(time.RFC3339)
39+
end_time := now.AddDate(0, 1, 0).Format(time.RFC3339)
3640

3741
vcrTest(t, resource.TestCase{
3842
PreCheck: func() { testAccPreCheck(t) },
3943
Providers: testAccProviders,
4044
CheckDestroy: testAccCheckBigqueryDataTransferConfigDestroyProducer(t),
4145
Steps: []resource.TestStep{
4246
{
43-
Config: testAccBigqueryDataTransferConfig_scheduledQuery(random_suffix, "third", "y"),
47+
Config: testAccBigqueryDataTransferConfig_scheduledQuery(random_suffix, "third", start_time, end_time, "y"),
4448
},
4549
{
4650
ResourceName: "google_bigquery_data_transfer_config.query_config",
@@ -54,17 +58,22 @@ func testAccBigqueryDataTransferConfig_scheduledQuery_basic(t *testing.T) {
5458

5559
func testAccBigqueryDataTransferConfig_scheduledQuery_update(t *testing.T) {
5660
random_suffix := randString(t, 10)
61+
now := time.Now().UTC()
62+
first_start_time := now.Add(1 * time.Hour).Format(time.RFC3339)
63+
first_end_time := now.AddDate(0, 1, 0).Format(time.RFC3339)
64+
second_start_time := now.Add(2 * time.Hour).Format(time.RFC3339)
65+
second_end_time := now.AddDate(0, 2, 0).Format(time.RFC3339)
5766

5867
vcrTest(t, resource.TestCase{
5968
PreCheck: func() { testAccPreCheck(t) },
6069
Providers: testAccProviders,
6170
CheckDestroy: testAccCheckBigqueryDataTransferConfigDestroyProducer(t),
6271
Steps: []resource.TestStep{
6372
{
64-
Config: testAccBigqueryDataTransferConfig_scheduledQuery(random_suffix, "first", "y"),
73+
Config: testAccBigqueryDataTransferConfig_scheduledQuery(random_suffix, "first", first_start_time, first_end_time, "y"),
6574
},
6675
{
67-
Config: testAccBigqueryDataTransferConfig_scheduledQuery(random_suffix, "second", "z"),
76+
Config: testAccBigqueryDataTransferConfig_scheduledQuery(random_suffix, "second", second_start_time, second_end_time, "z"),
6877
},
6978
{
7079
ResourceName: "google_bigquery_data_transfer_config.query_config",
@@ -145,7 +154,7 @@ func testAccCheckBigqueryDataTransferConfigDestroyProducer(t *testing.T) func(s
145154
}
146155
}
147156

148-
func testAccBigqueryDataTransferConfig_scheduledQuery(random_suffix, schedule, letter string) string {
157+
func testAccBigqueryDataTransferConfig_scheduledQuery(random_suffix, schedule, start_time, end_time, letter string) string {
149158
return fmt.Sprintf(`
150159
data "google_project" "project" {}
151160
@@ -175,6 +184,11 @@ resource "google_bigquery_data_transfer_config" "query_config" {
175184
location = "asia-northeast1"
176185
data_source_id = "scheduled_query"
177186
schedule = "%s sunday of quarter 00:00"
187+
schedule_options {
188+
disable_auto_scheduling = false
189+
start_time = "%s"
190+
end_time = "%s"
191+
}
178192
destination_dataset_id = google_bigquery_dataset.my_dataset.dataset_id
179193
notification_pubsub_topic = google_pubsub_topic.my_topic.id
180194
params = {
@@ -183,7 +197,7 @@ resource "google_bigquery_data_transfer_config" "query_config" {
183197
query = "SELECT name FROM tabl WHERE x = '%s'"
184198
}
185199
}
186-
`, random_suffix, random_suffix, random_suffix, schedule, letter)
200+
`, random_suffix, random_suffix, random_suffix, schedule, start_time, end_time, letter)
187201
}
188202

189203
func testAccBigqueryDataTransferConfig_scheduledQuery_service_account(random_suffix string) string {

website/docs/r/bigquery_data_transfer_config.html.markdown

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ The following arguments are supported:
108108
https://cloud.google.com/appengine/docs/flexible/python/scheduling-jobs-with-cron-yaml#the_schedule_format
109109
NOTE: the granularity should be at least 8 hours, or less frequent.
110110

111+
* `schedule_options` -
112+
(Optional)
113+
Options customizing the data transfer schedule.
114+
Structure is documented below.
115+
111116
* `notification_pubsub_topic` -
112117
(Optional)
113118
Pub/Sub topic where notifications will be sent after transfer runs
@@ -150,6 +155,31 @@ The following arguments are supported:
150155
If it is not provided, the provider project is used.
151156

152157

158+
The `schedule_options` block supports:
159+
160+
* `disable_auto_scheduling` -
161+
(Optional)
162+
If true, automatic scheduling of data transfer runs for this
163+
configuration will be disabled. The runs can be started on ad-hoc
164+
basis using transferConfigs.startManualRuns API. When automatic
165+
scheduling is disabled, the TransferConfig.schedule field will
166+
be ignored.
167+
168+
* `start_time` -
169+
(Optional)
170+
Specifies time to start scheduling transfer runs. The first run will be
171+
scheduled at or after the start time according to a recurrence pattern
172+
defined in the schedule string. The start time can be changed at any
173+
moment. The time when a data transfer can be triggered manually is not
174+
limited by this option.
175+
176+
* `end_time` -
177+
(Optional)
178+
Defines time to stop scheduling transfer runs. A transfer run cannot be
179+
scheduled at or after the end time. The end time can be changed at any
180+
moment. The time when a data transfer can be triggered manually is not
181+
limited by this option.
182+
153183
The `sensitive_params` block supports:
154184

155185
* `secret_access_key` -

0 commit comments

Comments
 (0)