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

provider/google: Added scheduling block to compute_instance #3643

Merged
merged 1 commit into from
Oct 26, 2015
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
71 changes: 71 additions & 0 deletions builtin/providers/google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions builtin/providers/google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
}
}`
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ The `service_account` block supports:
* `scopes` - (Required) A list of service scopes. Both OAuth2 URLs and gcloud
short names are supported.

The `scheduling` block supports:

* `preemptible` - (Optional) Is the instance preemptible.

* `on_host_maintenance` - (Optional) Describes maintenance behavior for
the instance. Can be MIGRATE or TERMINATE, for more info, read
[here](https://cloud.google.com/compute/docs/instances/setting-instance-scheduling-options)

* `automatic_restart` - (Optional) Specifies if the instance should be
restarted if it was terminated by Compute Engine (not a user).

## Attributes Reference

The following attributes are exported:
Expand Down