mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-07 01:26:43 +00:00
![Dana Hoffman](/assets/img/avatar_default.png)
This was done as its own resource as suggested in slack, since we don't have the option of making all fields Computed in google_compute_instance. There's precedent in the aws provider for this sort of thing (see ami_copy, ami_from_instance). When I started working on this I assumed I could do it in the compute_instance resource and so I went ahead and reordered the schema to make it easier to work with in the future. Now it's not quite relevant, but I left it in as its own commit that can be looked at separately from the other changes. Fixes #1582.
110 lines
2.6 KiB
Go
110 lines
2.6 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
compute "google.golang.org/api/compute/v1"
|
|
)
|
|
|
|
func TestAccComputeInstanceFromTemplate_basic(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var instance compute.Instance
|
|
instanceName := fmt.Sprintf("terraform-test-%s", acctest.RandString(10))
|
|
templateName := fmt.Sprintf("terraform-test-%s", acctest.RandString(10))
|
|
resourceName := "google_compute_instance_from_template.foobar"
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckComputeInstanceFromTemplateDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccComputeInstanceFromTemplate_basic(instanceName, templateName),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckComputeInstanceExists(resourceName, &instance),
|
|
|
|
// Check that fields were set based on the template
|
|
resource.TestCheckResourceAttr(resourceName, "machine_type", "n1-standard-1"),
|
|
resource.TestCheckResourceAttr(resourceName, "attached_disk.#", "1"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testAccCheckComputeInstanceFromTemplateDestroy(s *terraform.State) error {
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
if rs.Type != "google_compute_instance_from_template" {
|
|
continue
|
|
}
|
|
|
|
_, err := config.clientCompute.Instances.Get(
|
|
config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
|
|
if err == nil {
|
|
return fmt.Errorf("Instance still exists")
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func testAccComputeInstanceFromTemplate_basic(instance, template string) string {
|
|
return fmt.Sprintf(`
|
|
resource "google_compute_disk" "foobar" {
|
|
name = "%s"
|
|
image = "debian-8-jessie-v20160803"
|
|
size = 10
|
|
type = "pd-ssd"
|
|
zone = "us-central1-a"
|
|
}
|
|
|
|
resource "google_compute_instance_template" "foobar" {
|
|
name = "%s"
|
|
machine_type = "n1-standard-1"
|
|
|
|
disk {
|
|
source_image = "debian-cloud/debian-8"
|
|
auto_delete = true
|
|
disk_size_gb = 100
|
|
boot = true
|
|
}
|
|
|
|
disk {
|
|
source = "${google_compute_disk.foobar.name}"
|
|
auto_delete = false
|
|
boot = false
|
|
}
|
|
|
|
network_interface {
|
|
network = "default"
|
|
}
|
|
|
|
metadata {
|
|
foo = "bar"
|
|
}
|
|
|
|
can_ip_forward = true
|
|
}
|
|
|
|
resource "google_compute_instance_from_template" "foobar" {
|
|
name = "%s"
|
|
zone = "us-central1-a"
|
|
|
|
source_instance_template = "${google_compute_instance_template.foobar.self_link}"
|
|
|
|
// Overrides
|
|
can_ip_forward = false
|
|
labels {
|
|
my_key = "my_value"
|
|
}
|
|
}
|
|
`, template, template, instance)
|
|
}
|