From 17e36a2b84151990c4f8bc709e5f10165860bcbc Mon Sep 17 00:00:00 2001 From: Lars Wander Date: Mon, 26 Oct 2015 16:16:06 -0400 Subject: [PATCH] provider/google: Added scheduling block to compute_instance --- resource_compute_instance.go | 71 +++++++++++++++++++++++++++++++ resource_compute_instance_test.go | 37 ++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/resource_compute_instance.go b/resource_compute_instance.go index 68b8aed3..e3f00240 100644 --- a/resource_compute_instance.go +++ b/resource_compute_instance.go @@ -231,6 +231,29 @@ func resourceComputeInstance() *schema.Resource { }, }, + "scheduling": &schema.Schema{ + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "on_host_maintenance": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + }, + + "automatic_restart": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + }, + + "preemptible": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + }, + }, + }, + }, + "tags": &schema.Schema{ Type: schema.TypeSet, Optional: true, @@ -466,6 +489,21 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err serviceAccounts = append(serviceAccounts, serviceAccount) } + prefix := "scheduling.0" + scheduling := &compute.Scheduling{} + + if val, ok := d.GetOk(prefix + ".automatic_restart"); ok { + scheduling.AutomaticRestart = val.(bool) + } + + if val, ok := d.GetOk(prefix + ".preemptible"); ok { + scheduling.Preemptible = val.(bool) + } + + if val, ok := d.GetOk(prefix + ".on_host_maintenance"); ok { + scheduling.OnHostMaintenance = val.(string) + } + metadata, err := resourceInstanceMetadata(d) if err != nil { return fmt.Errorf("Error creating metadata: %s", err) @@ -482,6 +520,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err NetworkInterfaces: networkInterfaces, Tags: resourceInstanceTags(d), ServiceAccounts: serviceAccounts, + Scheduling: scheduling, } log.Printf("[INFO] Requesting instance creation") @@ -720,6 +759,38 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err d.SetPartial("tags") } + if d.HasChange("scheduling") { + prefix := "scheduling.0" + scheduling := &compute.Scheduling{} + + if val, ok := d.GetOk(prefix + ".automatic_restart"); ok { + scheduling.AutomaticRestart = val.(bool) + } + + if val, ok := d.GetOk(prefix + ".preemptible"); ok { + scheduling.Preemptible = val.(bool) + } + + if val, ok := d.GetOk(prefix + ".on_host_maintenance"); ok { + scheduling.OnHostMaintenance = val.(string) + } + + op, err := config.clientCompute.Instances.SetScheduling(config.Project, + zone, d.Id(), scheduling).Do() + + if err != nil { + return fmt.Errorf("Error updating scheduling policy: %s", err) + } + + opErr := computeOperationWaitZone(config, op, zone, + "scheduling policy update") + if opErr != nil { + return opErr + } + + d.SetPartial("scheduling"); + } + networkInterfacesCount := d.Get("network_interface.#").(int) if networkInterfacesCount > 0 { // Sanity check diff --git a/resource_compute_instance_test.go b/resource_compute_instance_test.go index f59da73e..4cee16a5 100644 --- a/resource_compute_instance_test.go +++ b/resource_compute_instance_test.go @@ -272,6 +272,25 @@ func TestAccComputeInstance_service_account(t *testing.T) { }) } +func TestAccComputeInstance_scheduling(t *testing.T) { + var instance compute.Instance + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeInstanceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccComputeInstance_scheduling, + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceExists( + "google_compute_instance.foobar", &instance), + ), + }, + }, + }) +} + func testAccCheckComputeInstanceDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) @@ -672,3 +691,21 @@ resource "google_compute_instance" "foobar" { ] } }` + +const testAccComputeInstance_scheduling = ` +resource "google_compute_instance" "foobar" { + name = "terraform-test" + machine_type = "n1-standard-1" + zone = "us-central1-a" + + disk { + image = "debian-7-wheezy-v20140814" + } + + network_interface { + network = "default" + } + + scheduling { + } +}`