Skip to content

Commit

Permalink
use scheduling from template rather than defaults (GoogleCloudPlatfor…
Browse files Browse the repository at this point in the history
  • Loading branch information
megan07 authored and Nathan Klish committed May 18, 2020
1 parent 1d4e76f commit ef0371e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ func resourceComputeInstanceFromTemplateCreate(d *schema.ResourceData, meta inte
return err
}

// when we make the original call to expandComputeInstance expandScheduling is called, which sets default values.
// However, we want the values to be read from the template instead.
if _, hasSchedule := d.GetOk("scheduling"); !hasSchedule {
instance.Scheduling = it.Properties.Scheduling
}

// Force send all top-level fields that have been set in case they're overridden to zero values.
// Initialize ForceSendFields to empty so we don't get things that the instance resource
// always force-sends.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,30 @@ func TestAccComputeInstanceFromTemplate_overrideScratchDisk(t *testing.T) {
})
}

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

var instance compute.Instance
instanceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
templateName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
templateDisk := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
resourceName := "google_compute_instance_from_template.inst"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceFromTemplateDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeInstanceFromTemplate_overrideScheduling(templateDisk, templateName, instanceName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(resourceName, &instance),
),
},
},
})
}

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

Expand Down Expand Up @@ -476,6 +500,56 @@ resource "google_compute_instance_from_template" "inst" {
`, templateDisk, overrideDisk, template, instance)
}

func testAccComputeInstanceFromTemplate_overrideScheduling(templateDisk, template, instance string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-9"
project = "debian-cloud"
}
resource "google_compute_disk" "foobar" {
name = "%s"
image = data.google_compute_image.my_image.self_link
size = 10
type = "pd-ssd"
zone = "us-central1-a"
}
resource "google_compute_instance_template" "foobar" {
name = "%s"
machine_type = "n1-standard-1"
disk {
source = google_compute_disk.foobar.name
auto_delete = false
boot = true
}
network_interface {
network = "default"
}
metadata = {
foo = "bar"
}
scheduling {
automatic_restart = false
preemptible = true
}
can_ip_forward = true
}
resource "google_compute_instance_from_template" "inst" {
name = "%s"
zone = "us-central1-a"
source_instance_template = google_compute_instance_template.foobar.self_link
}
`, templateDisk, template, instance)
}

func testAccComputeInstanceFromTemplate_012_removableFieldsTpl(template string) string {

return fmt.Sprintf(`
Expand Down

0 comments on commit ef0371e

Please sign in to comment.