From f86ffc075c48dd568c9ce8b046e5989108d99297 Mon Sep 17 00:00:00 2001
From: Vincent Roseberry <roseberryv@gmail.com>
Date: Wed, 30 Aug 2017 17:25:31 -0400
Subject: [PATCH]  Add support for min_cpu_platform in google_compute_instance.
 (#349)

---
 google/resource_compute_instance.go           | 18 +++++++
 google/resource_compute_instance_test.go      | 52 +++++++++++++++++++
 website/docs/r/compute_instance.html.markdown |  5 ++
 3 files changed, 75 insertions(+)

diff --git a/google/resource_compute_instance.go b/google/resource_compute_instance.go
index c1bdca05857..c154120fbbb 100644
--- a/google/resource_compute_instance.go
+++ b/google/resource_compute_instance.go
@@ -19,6 +19,10 @@ var InstanceVersionedFeatures = []Feature{
 		Version: v0beta,
 		Item:    "guest_accelerator",
 	},
+	{
+		Version: v0beta,
+		Item:    "min_cpu_platform",
+	},
 }
 
 func stringScopeHashcode(v interface{}) int {
@@ -465,6 +469,17 @@ func resourceComputeInstance() *schema.Resource {
 				},
 			},
 
+			"cpu_platform": &schema.Schema{
+				Type:     schema.TypeString,
+				Computed: true,
+			},
+
+			"min_cpu_platform": &schema.Schema{
+				Type:     schema.TypeString,
+				Optional: true,
+				ForceNew: true,
+			},
+
 			"tags": &schema.Schema{
 				Type:     schema.TypeSet,
 				Optional: true,
@@ -858,6 +873,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
 		Labels:            expandLabels(d),
 		ServiceAccounts:   serviceAccounts,
 		GuestAccelerators: expandGuestAccelerators(zone.Name, d.Get("guest_accelerator").([]interface{})),
+		MinCpuPlatform:    d.Get("min_cpu_platform").(string),
 		Scheduling:        scheduling,
 	}
 
@@ -1122,6 +1138,8 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
 	d.Set("scratch_disk", scratchDisks)
 	d.Set("scheduling", flattenBetaScheduling(instance.Scheduling))
 	d.Set("guest_accelerator", flattenGuestAccelerators(instance.Zone, instance.GuestAccelerators))
+	d.Set("cpu_platform", instance.CpuPlatform)
+	d.Set("min_cpu_platform", instance.MinCpuPlatform)
 	d.Set("self_link", ConvertSelfLinkToV1(instance.SelfLink))
 	d.SetId(instance.Name)
 
diff --git a/google/resource_compute_instance_test.go b/google/resource_compute_instance_test.go
index 07f31dfa25d..88eea4404a0 100644
--- a/google/resource_compute_instance_test.go
+++ b/google/resource_compute_instance_test.go
@@ -717,6 +717,27 @@ func TestAccComputeInstance_guestAccelerator(t *testing.T) {
 
 }
 
+func TestAccComputeInstance_minCpuPlatform(t *testing.T) {
+	var instance computeBeta.Instance
+	instanceName := fmt.Sprintf("terraform-test-%s", acctest.RandString(10))
+
+	resource.Test(t, resource.TestCase{
+		PreCheck:     func() { testAccPreCheck(t) },
+		Providers:    testAccProviders,
+		CheckDestroy: testAccCheckComputeInstanceDestroy,
+		Steps: []resource.TestStep{
+			resource.TestStep{
+				Config: testAccComputeInstance_minCpuPlatform(instanceName),
+				Check: resource.ComposeTestCheckFunc(
+					testAccCheckComputeBetaInstanceExists("google_compute_instance.foobar", &instance),
+					testAccCheckComputeInstanceHasMinCpuPlatform(&instance, "Intel Haswell"),
+				),
+			},
+		},
+	})
+
+}
+
 func testAccCheckComputeInstanceUpdateMachineType(n string) resource.TestCheckFunc {
 	return func(s *terraform.State) error {
 		rs, ok := s.RootModule().Resources[n]
@@ -1099,6 +1120,16 @@ func testAccCheckComputeInstanceHasGuestAccelerator(instance *computeBeta.Instan
 	}
 }
 
+func testAccCheckComputeInstanceHasMinCpuPlatform(instance *computeBeta.Instance, minCpuPlatform string) resource.TestCheckFunc {
+	return func(s *terraform.State) error {
+		if instance.MinCpuPlatform != minCpuPlatform {
+			return fmt.Errorf("Wrong minimum CPU platform: expected %s, got %s", minCpuPlatform, instance.MinCpuPlatform)
+		}
+
+		return nil
+	}
+}
+
 func testAccComputeInstance_basic_deprecated_network(instance string) string {
 	return fmt.Sprintf(`
 resource "google_compute_instance" "foobar" {
@@ -1950,3 +1981,24 @@ resource "google_compute_instance" "foobar" {
   }
 }`, instance)
 }
+
+func testAccComputeInstance_minCpuPlatform(instance string) string {
+	return fmt.Sprintf(`
+resource "google_compute_instance" "foobar" {
+  name = "%s"
+  machine_type = "n1-standard-1"
+  zone = "us-east1-d"
+
+  boot_disk {
+    initialize_params {
+      image = "debian-8-jessie-v20160803"
+    }
+  }
+
+  network_interface {
+    network = "default"
+  }
+
+  min_cpu_platform = "Intel Haswell"
+}`, instance)
+}
diff --git a/website/docs/r/compute_instance.html.markdown b/website/docs/r/compute_instance.html.markdown
index d73fb603092..8e31b8d5c66 100644
--- a/website/docs/r/compute_instance.html.markdown
+++ b/website/docs/r/compute_instance.html.markdown
@@ -264,6 +264,9 @@ The `scheduling` block supports:
 
 * `guest_accelerator` - (Optional, [Beta](/docs/providers/google/index.html#beta-features)) List of the type and count of accelerator cards attached to the instance. Structure documented below.
 
+* `min_cpu_platform` - (Optional, [Beta](/docs/providers/google/index.html#beta-features)) Specifies a minimum CPU platform for the VM instance. Applicable values are the friendly names of CPU platforms, such as
+`Intel Haswell` or `Intel Skylake`. See the complete list [here](https://cloud.google.com/compute/docs/instances/specify-min-cpu-platform).
+
 The `guest_accelerator` block supports:
 
 * `type` (Required) - The accelerator type resource to expose to this instance. E.g. `nvidia-tesla-k80`.
@@ -283,6 +286,8 @@ exported:
 
 * `label_fingerprint` - The unique fingerprint of the labels.
 
+* `cpu_platform` - The CPU platform used by this instance.
+
 * `network_interface.0.address` - The internal ip address of the instance, either manually or dynamically assigned.
 
 * `network_interface.0.access_config.0.assigned_nat_ip` - If the instance has an access config, either the given external ip (in the `nat_ip` field) or the ephemeral (generated) ip (if you didn't provide one).