Skip to content

Commit 16a8932

Browse files
pieternnfx
authored andcommitted
Support erasing a schedule that was defined out of band
1 parent e715aad commit 16a8932

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

sqlanalytics/resource_query.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,15 @@ func (q *QueryEntity) fromAPIObject(aq *api.Query, schema map[string]*schema.Sch
283283
q.Tags = append([]string{}, aq.Tags...)
284284

285285
if s := aq.Schedule; s != nil {
286+
// Set `schedule` to non-empty value to ensure it's picked up by `StructToSchema`.
287+
// If it is not yet set in `schema.ResourceData`, then `StructToSchema` mistakingly
288+
// interprets the server side value as a default and skips the field.
289+
// This means, however, that if the schedule is configured out-of-band (e.g. manually),
290+
// running `terraform apply` again won't remove the setting.
291+
data.Set("schedule", []interface{}{
292+
map[string][]interface{}{},
293+
})
294+
286295
q.Schedule = &QuerySchedule{}
287296
switch {
288297
case s.Interval%secondsInWeek == 0:

sqlanalytics/resource_query_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,33 @@ func TestQueryRead(t *testing.T) {
306306
assert.Equal(t, "foo", d.Id())
307307
}
308308

309+
func TestQueryReadWithSchedule(t *testing.T) {
310+
// Note: this tests that if a schedule is returned by the API,
311+
// it will always show up in the resulting resource data.
312+
// If it doesn't, we wouldn't be able to erase a schedule
313+
// that was defined out of band.
314+
d, err := qa.ResourceFixture{
315+
Fixtures: []qa.HTTPFixture{
316+
{
317+
Method: "GET",
318+
Resource: "/api/2.0/preview/sql/queries/foo",
319+
Response: api.Query{
320+
ID: "foo",
321+
Schedule: &api.QuerySchedule{
322+
Interval: 12345,
323+
},
324+
},
325+
},
326+
},
327+
Resource: ResourceQuery(),
328+
Read: true,
329+
ID: "foo",
330+
}.Apply(t)
331+
332+
assert.NoError(t, err, err)
333+
assert.Equal(t, 12345, d.Get("schedule.0.continuous.0.interval_seconds"))
334+
}
335+
309336
func TestQueryUpdate(t *testing.T) {
310337
d, err := qa.ResourceFixture{
311338
Fixtures: []qa.HTTPFixture{

0 commit comments

Comments
 (0)