Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use scheduling from template rather than instance defaults #1939

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/3352.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
compute: fixed bug where `google_compute_instance_from_template` instance defaults were overriding `scheduling`
```
6 changes: 6 additions & 0 deletions google-beta/resource_compute_instance_from_template.go
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
74 changes: 74 additions & 0 deletions google-beta/resource_compute_instance_from_template_test.go
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