Skip to content

Commit 2086f82

Browse files
use scheduling from template rather than defaults (#3352) (#1939)
Signed-off-by: Modular Magician <[email protected]>
1 parent 2f48370 commit 2086f82

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

.changelog/3352.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
compute: fixed bug where `google_compute_instance_from_template` instance defaults were overriding `scheduling`
3+
```

google-beta/resource_compute_instance_from_template.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ func resourceComputeInstanceFromTemplateCreate(d *schema.ResourceData, meta inte
126126
return err
127127
}
128128

129+
// when we make the original call to expandComputeInstance expandScheduling is called, which sets default values.
130+
// However, we want the values to be read from the template instead.
131+
if _, hasSchedule := d.GetOk("scheduling"); !hasSchedule {
132+
instance.Scheduling = it.Properties.Scheduling
133+
}
134+
129135
// Force send all top-level fields that have been set in case they're overridden to zero values.
130136
// Initialize ForceSendFields to empty so we don't get things that the instance resource
131137
// always force-sends.

google-beta/resource_compute_instance_from_template_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,30 @@ func TestAccComputeInstanceFromTemplate_overrideScratchDisk(t *testing.T) {
126126
})
127127
}
128128

129+
func TestAccComputeInstanceFromTemplate_overrideScheduling(t *testing.T) {
130+
t.Parallel()
131+
132+
var instance compute.Instance
133+
instanceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
134+
templateName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
135+
templateDisk := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
136+
resourceName := "google_compute_instance_from_template.inst"
137+
138+
resource.Test(t, resource.TestCase{
139+
PreCheck: func() { testAccPreCheck(t) },
140+
Providers: testAccProviders,
141+
CheckDestroy: testAccCheckComputeInstanceFromTemplateDestroy,
142+
Steps: []resource.TestStep{
143+
{
144+
Config: testAccComputeInstanceFromTemplate_overrideScheduling(templateDisk, templateName, instanceName),
145+
Check: resource.ComposeTestCheckFunc(
146+
testAccCheckComputeInstanceExists(resourceName, &instance),
147+
),
148+
},
149+
},
150+
})
151+
}
152+
129153
func TestAccComputeInstanceFromTemplate_012_removableFields(t *testing.T) {
130154
t.Parallel()
131155

@@ -476,6 +500,56 @@ resource "google_compute_instance_from_template" "inst" {
476500
`, templateDisk, overrideDisk, template, instance)
477501
}
478502

503+
func testAccComputeInstanceFromTemplate_overrideScheduling(templateDisk, template, instance string) string {
504+
return fmt.Sprintf(`
505+
data "google_compute_image" "my_image" {
506+
family = "debian-9"
507+
project = "debian-cloud"
508+
}
509+
510+
resource "google_compute_disk" "foobar" {
511+
name = "%s"
512+
image = data.google_compute_image.my_image.self_link
513+
size = 10
514+
type = "pd-ssd"
515+
zone = "us-central1-a"
516+
}
517+
518+
resource "google_compute_instance_template" "foobar" {
519+
name = "%s"
520+
machine_type = "n1-standard-1"
521+
522+
disk {
523+
source = google_compute_disk.foobar.name
524+
auto_delete = false
525+
boot = true
526+
}
527+
528+
network_interface {
529+
network = "default"
530+
}
531+
532+
metadata = {
533+
foo = "bar"
534+
}
535+
536+
scheduling {
537+
automatic_restart = false
538+
preemptible = true
539+
}
540+
541+
can_ip_forward = true
542+
}
543+
544+
resource "google_compute_instance_from_template" "inst" {
545+
name = "%s"
546+
zone = "us-central1-a"
547+
548+
source_instance_template = google_compute_instance_template.foobar.self_link
549+
}
550+
`, templateDisk, template, instance)
551+
}
552+
479553
func testAccComputeInstanceFromTemplate_012_removableFieldsTpl(template string) string {
480554

481555
return fmt.Sprintf(`

0 commit comments

Comments
 (0)