github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_instance_template_test.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "google.golang.org/api/compute/v1" 10 ) 11 12 func TestAccComputeInstanceTemplate_basic(t *testing.T) { 13 var instanceTemplate compute.InstanceTemplate 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckComputeInstanceTemplateDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccComputeInstanceTemplate_basic, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckComputeInstanceTemplateExists( 24 "google_compute_instance_template.foobar", &instanceTemplate), 25 testAccCheckComputeInstanceTemplateTag(&instanceTemplate, "foo"), 26 testAccCheckComputeInstanceTemplateMetadata(&instanceTemplate, "foo", "bar"), 27 testAccCheckComputeInstanceTemplateDisk(&instanceTemplate, "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140814", true, true), 28 ), 29 }, 30 }, 31 }) 32 } 33 34 func TestAccComputeInstanceTemplate_IP(t *testing.T) { 35 var instanceTemplate compute.InstanceTemplate 36 37 resource.Test(t, resource.TestCase{ 38 PreCheck: func() { testAccPreCheck(t) }, 39 Providers: testAccProviders, 40 CheckDestroy: testAccCheckComputeInstanceTemplateDestroy, 41 Steps: []resource.TestStep{ 42 resource.TestStep{ 43 Config: testAccComputeInstanceTemplate_ip, 44 Check: resource.ComposeTestCheckFunc( 45 testAccCheckComputeInstanceTemplateExists( 46 "google_compute_instance_template.foobar", &instanceTemplate), 47 testAccCheckComputeInstanceTemplateNetwork(&instanceTemplate), 48 ), 49 }, 50 }, 51 }) 52 } 53 54 func TestAccComputeInstanceTemplate_disks(t *testing.T) { 55 var instanceTemplate compute.InstanceTemplate 56 57 resource.Test(t, resource.TestCase{ 58 PreCheck: func() { testAccPreCheck(t) }, 59 Providers: testAccProviders, 60 CheckDestroy: testAccCheckComputeInstanceTemplateDestroy, 61 Steps: []resource.TestStep{ 62 resource.TestStep{ 63 Config: testAccComputeInstanceTemplate_disks, 64 Check: resource.ComposeTestCheckFunc( 65 testAccCheckComputeInstanceTemplateExists( 66 "google_compute_instance_template.foobar", &instanceTemplate), 67 testAccCheckComputeInstanceTemplateDisk(&instanceTemplate, "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140814", true, true), 68 testAccCheckComputeInstanceTemplateDisk(&instanceTemplate, "terraform-test-foobar", false, false), 69 ), 70 }, 71 }, 72 }) 73 } 74 75 func testAccCheckComputeInstanceTemplateDestroy(s *terraform.State) error { 76 config := testAccProvider.Meta().(*Config) 77 78 for _, rs := range s.RootModule().Resources { 79 if rs.Type != "google_compute_instance_template" { 80 continue 81 } 82 83 _, err := config.clientCompute.InstanceTemplates.Get( 84 config.Project, rs.Primary.ID).Do() 85 if err == nil { 86 return fmt.Errorf("Instance template still exists") 87 } 88 } 89 90 return nil 91 } 92 93 func testAccCheckComputeInstanceTemplateExists(n string, instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc { 94 return func(s *terraform.State) error { 95 rs, ok := s.RootModule().Resources[n] 96 if !ok { 97 return fmt.Errorf("Not found: %s", n) 98 } 99 100 if rs.Primary.ID == "" { 101 return fmt.Errorf("No ID is set") 102 } 103 104 config := testAccProvider.Meta().(*Config) 105 106 found, err := config.clientCompute.InstanceTemplates.Get( 107 config.Project, rs.Primary.ID).Do() 108 if err != nil { 109 return err 110 } 111 112 if found.Name != rs.Primary.ID { 113 return fmt.Errorf("Instance template not found") 114 } 115 116 *instanceTemplate = *found 117 118 return nil 119 } 120 } 121 122 func testAccCheckComputeInstanceTemplateMetadata( 123 instanceTemplate *compute.InstanceTemplate, 124 k string, v string) resource.TestCheckFunc { 125 return func(s *terraform.State) error { 126 if instanceTemplate.Properties.Metadata == nil { 127 return fmt.Errorf("no metadata") 128 } 129 130 for _, item := range instanceTemplate.Properties.Metadata.Items { 131 if k != item.Key { 132 continue 133 } 134 135 if item.Value != nil && v == *item.Value { 136 return nil 137 } 138 139 return fmt.Errorf("bad value for %s: %s", k, *item.Value) 140 } 141 142 return fmt.Errorf("metadata not found: %s", k) 143 } 144 } 145 146 func testAccCheckComputeInstanceTemplateNetwork(instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc { 147 return func(s *terraform.State) error { 148 for _, i := range instanceTemplate.Properties.NetworkInterfaces { 149 for _, c := range i.AccessConfigs { 150 if c.NatIP == "" { 151 return fmt.Errorf("no NAT IP") 152 } 153 } 154 } 155 156 return nil 157 } 158 } 159 160 func testAccCheckComputeInstanceTemplateDisk(instanceTemplate *compute.InstanceTemplate, source string, delete bool, boot bool) resource.TestCheckFunc { 161 return func(s *terraform.State) error { 162 if instanceTemplate.Properties.Disks == nil { 163 return fmt.Errorf("no disks") 164 } 165 166 for _, disk := range instanceTemplate.Properties.Disks { 167 if disk.InitializeParams == nil { 168 // Check disk source 169 if disk.Source == source { 170 if disk.AutoDelete == delete && disk.Boot == boot { 171 return nil 172 } 173 } 174 } else { 175 // Check source image 176 if disk.InitializeParams.SourceImage == source { 177 if disk.AutoDelete == delete && disk.Boot == boot { 178 return nil 179 } 180 } 181 } 182 } 183 184 return fmt.Errorf("Disk not found: %s", source) 185 } 186 } 187 188 func testAccCheckComputeInstanceTemplateTag(instanceTemplate *compute.InstanceTemplate, n string) resource.TestCheckFunc { 189 return func(s *terraform.State) error { 190 if instanceTemplate.Properties.Tags == nil { 191 return fmt.Errorf("no tags") 192 } 193 194 for _, k := range instanceTemplate.Properties.Tags.Items { 195 if k == n { 196 return nil 197 } 198 } 199 200 return fmt.Errorf("tag not found: %s", n) 201 } 202 } 203 204 const testAccComputeInstanceTemplate_basic = ` 205 resource "google_compute_instance_template" "foobar" { 206 name = "terraform-test" 207 machine_type = "n1-standard-1" 208 can_ip_forward = false 209 tags = ["foo", "bar"] 210 211 disk { 212 source_image = "debian-7-wheezy-v20140814" 213 auto_delete = true 214 boot = true 215 } 216 217 network_interface { 218 network = "default" 219 } 220 221 scheduling { 222 preemptible = false 223 automatic_restart = true 224 } 225 226 metadata { 227 foo = "bar" 228 } 229 230 service_account { 231 scopes = ["userinfo-email", "compute-ro", "storage-ro"] 232 } 233 }` 234 235 const testAccComputeInstanceTemplate_ip = ` 236 resource "google_compute_address" "foo" { 237 name = "foo" 238 } 239 240 resource "google_compute_instance_template" "foobar" { 241 name = "terraform-test" 242 machine_type = "n1-standard-1" 243 tags = ["foo", "bar"] 244 245 disk { 246 source_image = "debian-7-wheezy-v20140814" 247 } 248 249 network_interface { 250 network = "default" 251 access_config { 252 nat_ip = "${google_compute_address.foo.address}" 253 } 254 } 255 256 metadata { 257 foo = "bar" 258 } 259 }` 260 261 const testAccComputeInstanceTemplate_disks = ` 262 resource "google_compute_disk" "foobar" { 263 name = "terraform-test-foobar" 264 image = "debian-7-wheezy-v20140814" 265 size = 10 266 type = "pd-ssd" 267 zone = "us-central1-a" 268 } 269 270 resource "google_compute_instance_template" "foobar" { 271 name = "terraform-test" 272 machine_type = "n1-standard-1" 273 274 disk { 275 source_image = "debian-7-wheezy-v20140814" 276 auto_delete = true 277 disk_size_gb = 100 278 boot = true 279 } 280 281 disk { 282 source = "terraform-test-foobar" 283 auto_delete = false 284 boot = false 285 } 286 287 network_interface { 288 network = "default" 289 } 290 291 metadata { 292 foo = "bar" 293 } 294 }`