github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/builtin/providers/openstack/resource_openstack_compute_instance_v2_test.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  
    11  	"github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
    12  	"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/floatingip"
    13  	"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups"
    14  	"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/volumeattach"
    15  	"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
    16  	"github.com/rackspace/gophercloud/pagination"
    17  )
    18  
    19  func TestAccComputeV2Instance_basic(t *testing.T) {
    20  	var instance servers.Server
    21  	var testAccComputeV2Instance_basic = fmt.Sprintf(`
    22  		resource "openstack_compute_instance_v2" "foo" {
    23  			name = "terraform-test"
    24  			security_groups = ["default"]
    25  			network {
    26  				uuid = "%s"
    27  			}
    28  			metadata {
    29  				foo = "bar"
    30  			}
    31  		}`,
    32  		os.Getenv("OS_NETWORK_ID"))
    33  
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
    38  		Steps: []resource.TestStep{
    39  			resource.TestStep{
    40  				Config: testAccComputeV2Instance_basic,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
    43  					testAccCheckComputeV2InstanceMetadata(&instance, "foo", "bar"),
    44  				),
    45  			},
    46  		},
    47  	})
    48  }
    49  
    50  func TestAccComputeV2Instance_volumeAttach(t *testing.T) {
    51  	var instance servers.Server
    52  	var volume volumes.Volume
    53  
    54  	var testAccComputeV2Instance_volumeAttach = fmt.Sprintf(`
    55  		resource "openstack_blockstorage_volume_v1" "myvol" {
    56  			name = "myvol"
    57  			size = 1
    58  		}
    59  
    60  		resource "openstack_compute_instance_v2" "foo" {
    61  			name = "terraform-test"
    62  			security_groups = ["default"]
    63  			volume {
    64  				volume_id = "${openstack_blockstorage_volume_v1.myvol.id}"
    65  			}
    66  		}`)
    67  
    68  	resource.Test(t, resource.TestCase{
    69  		PreCheck:     func() { testAccPreCheck(t) },
    70  		Providers:    testAccProviders,
    71  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
    72  		Steps: []resource.TestStep{
    73  			resource.TestStep{
    74  				Config: testAccComputeV2Instance_volumeAttach,
    75  				Check: resource.ComposeTestCheckFunc(
    76  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume),
    77  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
    78  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume),
    79  				),
    80  			},
    81  		},
    82  	})
    83  }
    84  
    85  func TestAccComputeV2Instance_volumeAttachPostCreation(t *testing.T) {
    86  	var instance servers.Server
    87  	var volume volumes.Volume
    88  
    89  	var testAccComputeV2Instance_volumeAttachPostCreationInstance = fmt.Sprintf(`
    90  		resource "openstack_compute_instance_v2" "foo" {
    91  			name = "terraform-test"
    92  			security_groups = ["default"]
    93  		}`)
    94  
    95  	var testAccComputeV2Instance_volumeAttachPostCreationInstanceAndVolume = fmt.Sprintf(`
    96  		resource "openstack_blockstorage_volume_v1" "myvol" {
    97  			name = "myvol"
    98  			size = 1
    99  		}
   100  
   101  		resource "openstack_compute_instance_v2" "foo" {
   102  			name = "terraform-test"
   103  			security_groups = ["default"]
   104  			volume {
   105  				volume_id = "${openstack_blockstorage_volume_v1.myvol.id}"
   106  			}
   107  		}`)
   108  
   109  	resource.Test(t, resource.TestCase{
   110  		PreCheck:     func() { testAccPreCheck(t) },
   111  		Providers:    testAccProviders,
   112  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   113  		Steps: []resource.TestStep{
   114  			resource.TestStep{
   115  				Config: testAccComputeV2Instance_volumeAttachPostCreationInstance,
   116  				Check: resource.ComposeTestCheckFunc(
   117  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   118  				),
   119  			},
   120  			resource.TestStep{
   121  				Config: testAccComputeV2Instance_volumeAttachPostCreationInstanceAndVolume,
   122  				Check: resource.ComposeTestCheckFunc(
   123  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume),
   124  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   125  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume),
   126  				),
   127  			},
   128  		},
   129  	})
   130  }
   131  
   132  func TestAccComputeV2Instance_volumeDetachPostCreation(t *testing.T) {
   133  	var instance servers.Server
   134  	var volume volumes.Volume
   135  
   136  	var testAccComputeV2Instance_volumeDetachPostCreationInstanceAndVolume = fmt.Sprintf(`
   137  		resource "openstack_blockstorage_volume_v1" "myvol" {
   138  			name = "myvol"
   139  			size = 1
   140  		}
   141  
   142  		resource "openstack_compute_instance_v2" "foo" {
   143  			name = "terraform-test"
   144  			security_groups = ["default"]
   145  			volume {
   146  				volume_id = "${openstack_blockstorage_volume_v1.myvol.id}"
   147  			}
   148  		}`)
   149  
   150  	var testAccComputeV2Instance_volumeDetachPostCreationInstance = fmt.Sprintf(`
   151  		resource "openstack_compute_instance_v2" "foo" {
   152  			name = "terraform-test"
   153  			security_groups = ["default"]
   154  		}`)
   155  
   156  	resource.Test(t, resource.TestCase{
   157  		PreCheck:     func() { testAccPreCheck(t) },
   158  		Providers:    testAccProviders,
   159  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   160  		Steps: []resource.TestStep{
   161  			resource.TestStep{
   162  				Config: testAccComputeV2Instance_volumeDetachPostCreationInstanceAndVolume,
   163  				Check: resource.ComposeTestCheckFunc(
   164  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume),
   165  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   166  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume),
   167  				),
   168  			},
   169  			resource.TestStep{
   170  				Config: testAccComputeV2Instance_volumeDetachPostCreationInstance,
   171  				Check: resource.ComposeTestCheckFunc(
   172  					testAccCheckBlockStorageV1VolumeDoesNotExist(t, "openstack_blockstorage_volume_v1.myvol", &volume),
   173  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   174  					testAccCheckComputeV2InstanceVolumesDetached(&instance),
   175  				),
   176  			},
   177  		},
   178  	})
   179  }
   180  
   181  func TestAccComputeV2Instance_floatingIPAttachGlobally(t *testing.T) {
   182  	var instance servers.Server
   183  	var fip floatingip.FloatingIP
   184  	var testAccComputeV2Instance_floatingIPAttachGlobally = fmt.Sprintf(`
   185  		resource "openstack_compute_floatingip_v2" "myip" {
   186  		}
   187  
   188  		resource "openstack_compute_instance_v2" "foo" {
   189  			name = "terraform-test"
   190  			security_groups = ["default"]
   191  			floating_ip = "${openstack_compute_floatingip_v2.myip.address}"
   192  
   193  			network {
   194  				uuid = "%s"
   195  			}
   196  		}`,
   197  		os.Getenv("OS_NETWORK_ID"))
   198  
   199  	resource.Test(t, resource.TestCase{
   200  		PreCheck:     func() { testAccPreCheck(t) },
   201  		Providers:    testAccProviders,
   202  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   203  		Steps: []resource.TestStep{
   204  			resource.TestStep{
   205  				Config: testAccComputeV2Instance_floatingIPAttachGlobally,
   206  				Check: resource.ComposeTestCheckFunc(
   207  					testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip", &fip),
   208  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   209  					testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip),
   210  				),
   211  			},
   212  		},
   213  	})
   214  }
   215  
   216  func TestAccComputeV2Instance_floatingIPAttachToNetwork(t *testing.T) {
   217  	var instance servers.Server
   218  	var fip floatingip.FloatingIP
   219  	var testAccComputeV2Instance_floatingIPAttachToNetwork = fmt.Sprintf(`
   220  		resource "openstack_compute_floatingip_v2" "myip" {
   221  		}
   222  
   223  		resource "openstack_compute_instance_v2" "foo" {
   224  			name = "terraform-test"
   225  			security_groups = ["default"]
   226  
   227  			network {
   228  				uuid = "%s"
   229  				floating_ip = "${openstack_compute_floatingip_v2.myip.address}"
   230  				access_network = true
   231  			}
   232  		}`,
   233  		os.Getenv("OS_NETWORK_ID"))
   234  
   235  	resource.Test(t, resource.TestCase{
   236  		PreCheck:     func() { testAccPreCheck(t) },
   237  		Providers:    testAccProviders,
   238  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   239  		Steps: []resource.TestStep{
   240  			resource.TestStep{
   241  				Config: testAccComputeV2Instance_floatingIPAttachToNetwork,
   242  				Check: resource.ComposeTestCheckFunc(
   243  					testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip", &fip),
   244  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   245  					testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip),
   246  				),
   247  			},
   248  		},
   249  	})
   250  }
   251  
   252  func TestAccComputeV2Instance_floatingIPAttachAndChange(t *testing.T) {
   253  	var instance servers.Server
   254  	var fip floatingip.FloatingIP
   255  	var testAccComputeV2Instance_floatingIPAttachToNetwork_1 = fmt.Sprintf(`
   256  		resource "openstack_compute_floatingip_v2" "myip_1" {
   257  		}
   258  
   259  		resource "openstack_compute_floatingip_v2" "myip_2" {
   260  		}
   261  
   262  		resource "openstack_compute_instance_v2" "foo" {
   263  			name = "terraform-test"
   264  			security_groups = ["default"]
   265  
   266  			network {
   267  				uuid = "%s"
   268  				floating_ip = "${openstack_compute_floatingip_v2.myip_1.address}"
   269  				access_network = true
   270  			}
   271  		}`,
   272  		os.Getenv("OS_NETWORK_ID"))
   273  
   274  	var testAccComputeV2Instance_floatingIPAttachToNetwork_2 = fmt.Sprintf(`
   275  		resource "openstack_compute_floatingip_v2" "myip_1" {
   276  		}
   277  
   278  		resource "openstack_compute_floatingip_v2" "myip_2" {
   279  		}
   280  
   281  		resource "openstack_compute_instance_v2" "foo" {
   282  			name = "terraform-test"
   283  			security_groups = ["default"]
   284  
   285  			network {
   286  				uuid = "%s"
   287  				floating_ip = "${openstack_compute_floatingip_v2.myip_2.address}"
   288  				access_network = true
   289  			}
   290  		}`,
   291  		os.Getenv("OS_NETWORK_ID"))
   292  
   293  	resource.Test(t, resource.TestCase{
   294  		PreCheck:     func() { testAccPreCheck(t) },
   295  		Providers:    testAccProviders,
   296  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   297  		Steps: []resource.TestStep{
   298  			resource.TestStep{
   299  				Config: testAccComputeV2Instance_floatingIPAttachToNetwork_1,
   300  				Check: resource.ComposeTestCheckFunc(
   301  					testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip_1", &fip),
   302  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   303  					testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip),
   304  				),
   305  			},
   306  			resource.TestStep{
   307  				Config: testAccComputeV2Instance_floatingIPAttachToNetwork_2,
   308  				Check: resource.ComposeTestCheckFunc(
   309  					testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip_2", &fip),
   310  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   311  					testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip),
   312  				),
   313  			},
   314  		},
   315  	})
   316  }
   317  
   318  func TestAccComputeV2Instance_multi_secgroups(t *testing.T) {
   319  	var instance_1 servers.Server
   320  	var secgroup_1 secgroups.SecurityGroup
   321  	var testAccComputeV2Instance_multi_secgroups = fmt.Sprintf(`
   322  		resource "openstack_compute_secgroup_v2" "secgroup_1" {
   323  			name = "secgroup_1"
   324  			description = "a security group"
   325  			rule {
   326  				from_port = 22
   327  				to_port = 22
   328  				ip_protocol = "tcp"
   329  				cidr = "0.0.0.0/0"
   330  			}
   331  		}
   332  
   333  		resource "openstack_compute_instance_v2" "instance_1" {
   334  			name = "instance_1"
   335  			security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"]
   336  			network {
   337  				uuid = "%s"
   338  			}
   339  		}`,
   340  		os.Getenv("OS_NETWORK_ID"))
   341  
   342  	resource.Test(t, resource.TestCase{
   343  		PreCheck:     func() { testAccPreCheck(t) },
   344  		Providers:    testAccProviders,
   345  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   346  		Steps: []resource.TestStep{
   347  			resource.TestStep{
   348  				Config: testAccComputeV2Instance_multi_secgroups,
   349  				Check: resource.ComposeTestCheckFunc(
   350  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_1", &secgroup_1),
   351  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1),
   352  				),
   353  			},
   354  		},
   355  	})
   356  }
   357  
   358  func TestAccComputeV2Instance_multi_secgroups_update(t *testing.T) {
   359  	var instance_1 servers.Server
   360  	var secgroup_1, secgroup_2 secgroups.SecurityGroup
   361  	var testAccComputeV2Instance_multi_secgroups_update_1 = fmt.Sprintf(`
   362  		resource "openstack_compute_secgroup_v2" "secgroup_1" {
   363  			name = "secgroup_1"
   364  			description = "a security group"
   365  			rule {
   366  				from_port = 22
   367  				to_port = 22
   368  				ip_protocol = "tcp"
   369  				cidr = "0.0.0.0/0"
   370  			}
   371  		}
   372  
   373  		resource "openstack_compute_secgroup_v2" "secgroup_2" {
   374  			name = "secgroup_2"
   375  			description = "another security group"
   376  			rule {
   377  				from_port = 80
   378  				to_port = 80
   379  				ip_protocol = "tcp"
   380  				cidr = "0.0.0.0/0"
   381  			}
   382  		}
   383  
   384  		resource "openstack_compute_instance_v2" "instance_1" {
   385  			name = "instance_1"
   386  			security_groups = ["default"]
   387  		}`)
   388  
   389  	var testAccComputeV2Instance_multi_secgroups_update_2 = fmt.Sprintf(`
   390  		resource "openstack_compute_secgroup_v2" "secgroup_1" {
   391  			name = "secgroup_1"
   392  			description = "a security group"
   393  			rule {
   394  				from_port = 22
   395  				to_port = 22
   396  				ip_protocol = "tcp"
   397  				cidr = "0.0.0.0/0"
   398  			}
   399  		}
   400  
   401  		resource "openstack_compute_secgroup_v2" "secgroup_2" {
   402  			name = "secgroup_2"
   403  			description = "another security group"
   404  			rule {
   405  				from_port = 80
   406  				to_port = 80
   407  				ip_protocol = "tcp"
   408  				cidr = "0.0.0.0/0"
   409  			}
   410  		}
   411  
   412  		resource "openstack_compute_instance_v2" "instance_1" {
   413  			name = "instance_1"
   414  			security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}", "${openstack_compute_secgroup_v2.secgroup_2.name}"]
   415  		}`)
   416  
   417  	resource.Test(t, resource.TestCase{
   418  		PreCheck:     func() { testAccPreCheck(t) },
   419  		Providers:    testAccProviders,
   420  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   421  		Steps: []resource.TestStep{
   422  			resource.TestStep{
   423  				Config: testAccComputeV2Instance_multi_secgroups_update_1,
   424  				Check: resource.ComposeTestCheckFunc(
   425  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_1", &secgroup_1),
   426  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_2", &secgroup_2),
   427  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1),
   428  				),
   429  			},
   430  			resource.TestStep{
   431  				Config: testAccComputeV2Instance_multi_secgroups_update_2,
   432  				Check: resource.ComposeTestCheckFunc(
   433  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_1", &secgroup_1),
   434  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_2", &secgroup_2),
   435  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1),
   436  				),
   437  			},
   438  		},
   439  	})
   440  }
   441  
   442  func TestAccComputeV2Instance_bootFromVolumeImage(t *testing.T) {
   443  	var instance servers.Server
   444  	var testAccComputeV2Instance_bootFromVolumeImage = fmt.Sprintf(`
   445  		resource "openstack_compute_instance_v2" "foo" {
   446  			name = "terraform-test"
   447  			security_groups = ["default"]
   448  			block_device {
   449  				uuid = "%s"
   450  				source_type = "image"
   451  				volume_size = 5
   452  				boot_index = 0
   453  				destination_type = "volume"
   454  				delete_on_termination = true
   455  			}
   456  		}`,
   457  		os.Getenv("OS_IMAGE_ID"))
   458  
   459  	resource.Test(t, resource.TestCase{
   460  		PreCheck:     func() { testAccPreCheck(t) },
   461  		Providers:    testAccProviders,
   462  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   463  		Steps: []resource.TestStep{
   464  			resource.TestStep{
   465  				Config: testAccComputeV2Instance_bootFromVolumeImage,
   466  				Check: resource.ComposeTestCheckFunc(
   467  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   468  					testAccCheckComputeV2InstanceBootVolumeAttachment(&instance),
   469  				),
   470  			},
   471  		},
   472  	})
   473  }
   474  
   475  func TestAccComputeV2Instance_bootFromVolumeVolume(t *testing.T) {
   476  	var instance servers.Server
   477  	var testAccComputeV2Instance_bootFromVolumeVolume = fmt.Sprintf(`
   478  	  resource "openstack_blockstorage_volume_v1" "foo" {
   479  			name = "terraform-test"
   480  			size = 5
   481  			image_id = "%s"
   482  		}
   483  
   484  		resource "openstack_compute_instance_v2" "foo" {
   485  			name = "terraform-test"
   486  			security_groups = ["default"]
   487  			block_device {
   488  				uuid = "${openstack_blockstorage_volume_v1.foo.id}"
   489  				source_type = "volume"
   490  				boot_index = 0
   491  				destination_type = "volume"
   492  				delete_on_termination = true
   493  			}
   494  		}`,
   495  		os.Getenv("OS_IMAGE_ID"))
   496  
   497  	resource.Test(t, resource.TestCase{
   498  		PreCheck:     func() { testAccPreCheck(t) },
   499  		Providers:    testAccProviders,
   500  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   501  		Steps: []resource.TestStep{
   502  			resource.TestStep{
   503  				Config: testAccComputeV2Instance_bootFromVolumeVolume,
   504  				Check: resource.ComposeTestCheckFunc(
   505  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   506  					testAccCheckComputeV2InstanceBootVolumeAttachment(&instance),
   507  				),
   508  			},
   509  		},
   510  	})
   511  }
   512  
   513  func TestAccComputeV2Instance_bootFromVolumeForceNew(t *testing.T) {
   514  	var instance1_1 servers.Server
   515  	var instance1_2 servers.Server
   516  	var testAccComputeV2Instance_bootFromVolumeForceNew_1 = fmt.Sprintf(`
   517  		resource "openstack_compute_instance_v2" "instance_1" {
   518  			name = "instance_1"
   519  			security_groups = ["default"]
   520  			block_device {
   521  				uuid = "%s"
   522  				source_type = "image"
   523  				volume_size = 5
   524  				boot_index = 0
   525  				destination_type = "volume"
   526  				delete_on_termination = true
   527  			}
   528  		}`,
   529  		os.Getenv("OS_IMAGE_ID"))
   530  
   531  	var testAccComputeV2Instance_bootFromVolumeForceNew_2 = fmt.Sprintf(`
   532  		resource "openstack_compute_instance_v2" "instance_1" {
   533  			name = "instance_1"
   534  			security_groups = ["default"]
   535  			block_device {
   536  				uuid = "%s"
   537  				source_type = "image"
   538  				volume_size = 4
   539  				boot_index = 0
   540  				destination_type = "volume"
   541  				delete_on_termination = true
   542  			}
   543  		}`,
   544  		os.Getenv("OS_IMAGE_ID"))
   545  
   546  	resource.Test(t, resource.TestCase{
   547  		PreCheck:     func() { testAccPreCheck(t) },
   548  		Providers:    testAccProviders,
   549  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   550  		Steps: []resource.TestStep{
   551  			resource.TestStep{
   552  				Config: testAccComputeV2Instance_bootFromVolumeForceNew_1,
   553  				Check: resource.ComposeTestCheckFunc(
   554  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_1),
   555  				),
   556  			},
   557  			resource.TestStep{
   558  				Config: testAccComputeV2Instance_bootFromVolumeForceNew_2,
   559  				Check: resource.ComposeTestCheckFunc(
   560  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_2),
   561  					testAccCheckComputeV2InstanceInstanceIDsDoNotMatch(&instance1_1, &instance1_2),
   562  				),
   563  			},
   564  		},
   565  	})
   566  }
   567  
   568  // TODO: verify the personality really exists on the instance.
   569  func TestAccComputeV2Instance_personality(t *testing.T) {
   570  	var instance servers.Server
   571  	var testAccComputeV2Instance_personality = fmt.Sprintf(`
   572  		resource "openstack_compute_instance_v2" "foo" {
   573  			name = "terraform-test"
   574  			security_groups = ["default"]
   575  			personality {
   576  				file = "/tmp/foobar.txt"
   577  				content = "happy"
   578  			}
   579  			personality {
   580  				file = "/tmp/barfoo.txt"
   581  				content = "angry"
   582  			}
   583  		}`)
   584  
   585  	resource.Test(t, resource.TestCase{
   586  		PreCheck:     func() { testAccPreCheck(t) },
   587  		Providers:    testAccProviders,
   588  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   589  		Steps: []resource.TestStep{
   590  			resource.TestStep{
   591  				Config: testAccComputeV2Instance_personality,
   592  				Check: resource.ComposeTestCheckFunc(
   593  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   594  				),
   595  			},
   596  		},
   597  	})
   598  }
   599  
   600  func TestAccComputeV2Instance_multiEphemeral(t *testing.T) {
   601  	var instance servers.Server
   602  	var testAccComputeV2Instance_multiEphemeral = fmt.Sprintf(`
   603  		resource "openstack_compute_instance_v2" "foo" {
   604  			name = "terraform-test"
   605  			security_groups = ["default"]
   606  			block_device {
   607  				boot_index = 0
   608  				delete_on_termination = true
   609  				destination_type = "local"
   610  				source_type = "image"
   611  				uuid = "%s"
   612  			}
   613  			block_device {
   614  				boot_index = -1
   615  				delete_on_termination = true
   616  				destination_type = "local"
   617  				source_type = "blank"
   618  				volume_size = 1
   619  			}
   620  			block_device {
   621  				boot_index = -1
   622  				delete_on_termination = true
   623  				destination_type = "local"
   624  				source_type = "blank"
   625  				volume_size = 1
   626  			}
   627  		}`,
   628  		os.Getenv("OS_IMAGE_ID"))
   629  
   630  	resource.Test(t, resource.TestCase{
   631  		PreCheck:     func() { testAccPreCheck(t) },
   632  		Providers:    testAccProviders,
   633  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   634  		Steps: []resource.TestStep{
   635  			resource.TestStep{
   636  				Config: testAccComputeV2Instance_multiEphemeral,
   637  				Check: resource.ComposeTestCheckFunc(
   638  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   639  				),
   640  			},
   641  		},
   642  	})
   643  }
   644  
   645  func TestAccComputeV2Instance_accessIPv4(t *testing.T) {
   646  	var instance servers.Server
   647  	var testAccComputeV2Instance_accessIPv4 = fmt.Sprintf(`
   648  		resource "openstack_compute_floatingip_v2" "myip" {
   649  		}
   650  
   651  		resource "openstack_networking_network_v2" "network_1" {
   652  			name = "network_1"
   653  		}
   654  
   655  		resource "openstack_networking_subnet_v2" "subnet_1" {
   656  			name = "subnet_1"
   657  			network_id = "${openstack_networking_network_v2.network_1.id}"
   658  			cidr = "192.168.1.0/24"
   659  			ip_version = 4
   660  			enable_dhcp = true
   661  			no_gateway = true
   662  		}
   663  
   664  		resource "openstack_compute_instance_v2" "instance_1" {
   665  			depends_on = ["openstack_networking_subnet_v2.subnet_1"]
   666  
   667  			name = "instance_1"
   668  			security_groups = ["default"]
   669  			floating_ip = "${openstack_compute_floatingip_v2.myip.address}"
   670  
   671  			network {
   672  				uuid = "%s"
   673  			}
   674  
   675  			network {
   676  				uuid = "${openstack_networking_network_v2.network_1.id}"
   677  				fixed_ip_v4 = "192.168.1.100"
   678  				access_network = true
   679  			}
   680  		}`, os.Getenv("OS_NETWORK_ID"))
   681  
   682  	resource.Test(t, resource.TestCase{
   683  		PreCheck:     func() { testAccPreCheck(t) },
   684  		Providers:    testAccProviders,
   685  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   686  		Steps: []resource.TestStep{
   687  			resource.TestStep{
   688  				Config: testAccComputeV2Instance_accessIPv4,
   689  				Check: resource.ComposeTestCheckFunc(
   690  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance),
   691  					resource.TestCheckResourceAttr(
   692  						"openstack_compute_instance_v2.instance_1", "access_ip_v4", "192.168.1.100"),
   693  				),
   694  			},
   695  		},
   696  	})
   697  }
   698  
   699  func TestAccComputeV2Instance_ChangeFixedIP(t *testing.T) {
   700  	var instance1_1 servers.Server
   701  	var instance1_2 servers.Server
   702  	var testAccComputeV2Instance_ChangeFixedIP_1 = fmt.Sprintf(`
   703  		resource "openstack_compute_instance_v2" "instance_1" {
   704  			name = "instance_1"
   705  			security_groups = ["default"]
   706  			network {
   707  				uuid = "%s"
   708  				fixed_ip_v4 = "10.0.0.24"
   709  			}
   710  		}`,
   711  		os.Getenv("OS_NETWORK_ID"))
   712  
   713  	var testAccComputeV2Instance_ChangeFixedIP_2 = fmt.Sprintf(`
   714  		resource "openstack_compute_instance_v2" "instance_1" {
   715  			name = "instance_1"
   716  			security_groups = ["default"]
   717  			network {
   718  				uuid = "%s"
   719  				fixed_ip_v4 = "10.0.0.25"
   720  			}
   721  		}`,
   722  		os.Getenv("OS_NETWORK_ID"))
   723  
   724  	resource.Test(t, resource.TestCase{
   725  		PreCheck:     func() { testAccPreCheck(t) },
   726  		Providers:    testAccProviders,
   727  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   728  		Steps: []resource.TestStep{
   729  			resource.TestStep{
   730  				Config: testAccComputeV2Instance_ChangeFixedIP_1,
   731  				Check: resource.ComposeTestCheckFunc(
   732  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_1),
   733  				),
   734  			},
   735  			resource.TestStep{
   736  				Config: testAccComputeV2Instance_ChangeFixedIP_2,
   737  				Check: resource.ComposeTestCheckFunc(
   738  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_2),
   739  					testAccCheckComputeV2InstanceInstanceIDsDoNotMatch(&instance1_1, &instance1_2),
   740  				),
   741  			},
   742  		},
   743  	})
   744  }
   745  
   746  func testAccCheckComputeV2InstanceDestroy(s *terraform.State) error {
   747  	config := testAccProvider.Meta().(*Config)
   748  	computeClient, err := config.computeV2Client(OS_REGION_NAME)
   749  	if err != nil {
   750  		return fmt.Errorf("(testAccCheckComputeV2InstanceDestroy) Error creating OpenStack compute client: %s", err)
   751  	}
   752  
   753  	for _, rs := range s.RootModule().Resources {
   754  		if rs.Type != "openstack_compute_instance_v2" {
   755  			continue
   756  		}
   757  
   758  		_, err := servers.Get(computeClient, rs.Primary.ID).Extract()
   759  		if err == nil {
   760  			return fmt.Errorf("Instance still exists")
   761  		}
   762  	}
   763  
   764  	return nil
   765  }
   766  
   767  func testAccCheckComputeV2InstanceExists(t *testing.T, n string, instance *servers.Server) resource.TestCheckFunc {
   768  	return func(s *terraform.State) error {
   769  		rs, ok := s.RootModule().Resources[n]
   770  		if !ok {
   771  			return fmt.Errorf("Not found: %s", n)
   772  		}
   773  
   774  		if rs.Primary.ID == "" {
   775  			return fmt.Errorf("No ID is set")
   776  		}
   777  
   778  		config := testAccProvider.Meta().(*Config)
   779  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   780  		if err != nil {
   781  			return fmt.Errorf("(testAccCheckComputeV2InstanceExists) Error creating OpenStack compute client: %s", err)
   782  		}
   783  
   784  		found, err := servers.Get(computeClient, rs.Primary.ID).Extract()
   785  		if err != nil {
   786  			return err
   787  		}
   788  
   789  		if found.ID != rs.Primary.ID {
   790  			return fmt.Errorf("Instance not found")
   791  		}
   792  
   793  		*instance = *found
   794  
   795  		return nil
   796  	}
   797  }
   798  
   799  func testAccCheckComputeV2InstanceMetadata(
   800  	instance *servers.Server, k string, v string) resource.TestCheckFunc {
   801  	return func(s *terraform.State) error {
   802  		if instance.Metadata == nil {
   803  			return fmt.Errorf("No metadata")
   804  		}
   805  
   806  		for key, value := range instance.Metadata {
   807  			if k != key {
   808  				continue
   809  			}
   810  
   811  			if v == value.(string) {
   812  				return nil
   813  			}
   814  
   815  			return fmt.Errorf("Bad value for %s: %s", k, value)
   816  		}
   817  
   818  		return fmt.Errorf("Metadata not found: %s", k)
   819  	}
   820  }
   821  
   822  func testAccCheckComputeV2InstanceVolumeAttachment(
   823  	instance *servers.Server, volume *volumes.Volume) resource.TestCheckFunc {
   824  	return func(s *terraform.State) error {
   825  		var attachments []volumeattach.VolumeAttachment
   826  
   827  		config := testAccProvider.Meta().(*Config)
   828  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   829  		if err != nil {
   830  			return err
   831  		}
   832  		err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) {
   833  			actual, err := volumeattach.ExtractVolumeAttachments(page)
   834  			if err != nil {
   835  				return false, fmt.Errorf("Unable to lookup attachment: %s", err)
   836  			}
   837  
   838  			attachments = actual
   839  			return true, nil
   840  		})
   841  
   842  		for _, attachment := range attachments {
   843  			if attachment.VolumeID == volume.ID {
   844  				return nil
   845  			}
   846  		}
   847  
   848  		return fmt.Errorf("Volume not found: %s", volume.ID)
   849  	}
   850  }
   851  
   852  func testAccCheckComputeV2InstanceVolumesDetached(instance *servers.Server) resource.TestCheckFunc {
   853  	return func(s *terraform.State) error {
   854  		var attachments []volumeattach.VolumeAttachment
   855  
   856  		config := testAccProvider.Meta().(*Config)
   857  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   858  		if err != nil {
   859  			return err
   860  		}
   861  		err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) {
   862  			actual, err := volumeattach.ExtractVolumeAttachments(page)
   863  			if err != nil {
   864  				return false, fmt.Errorf("Unable to lookup attachment: %s", err)
   865  			}
   866  
   867  			attachments = actual
   868  			return true, nil
   869  		})
   870  
   871  		if len(attachments) > 0 {
   872  			return fmt.Errorf("Volumes are still attached.")
   873  		}
   874  
   875  		return nil
   876  	}
   877  }
   878  
   879  func testAccCheckComputeV2InstanceBootVolumeAttachment(
   880  	instance *servers.Server) resource.TestCheckFunc {
   881  	return func(s *terraform.State) error {
   882  		var attachments []volumeattach.VolumeAttachment
   883  
   884  		config := testAccProvider.Meta().(*Config)
   885  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   886  		if err != nil {
   887  			return err
   888  		}
   889  		err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) {
   890  			actual, err := volumeattach.ExtractVolumeAttachments(page)
   891  			if err != nil {
   892  				return false, fmt.Errorf("Unable to lookup attachment: %s", err)
   893  			}
   894  
   895  			attachments = actual
   896  			return true, nil
   897  		})
   898  
   899  		if len(attachments) == 1 {
   900  			return nil
   901  		}
   902  
   903  		return fmt.Errorf("No attached volume found.")
   904  	}
   905  }
   906  
   907  func testAccCheckComputeV2InstanceFloatingIPAttach(
   908  	instance *servers.Server, fip *floatingip.FloatingIP) resource.TestCheckFunc {
   909  	return func(s *terraform.State) error {
   910  		if fip.InstanceID == instance.ID {
   911  			return nil
   912  		}
   913  
   914  		return fmt.Errorf("Floating IP %s was not attached to instance %s", fip.ID, instance.ID)
   915  	}
   916  }
   917  func testAccCheckComputeV2InstanceInstanceIDsDoNotMatch(
   918  	instance1, instance2 *servers.Server) resource.TestCheckFunc {
   919  	return func(s *terraform.State) error {
   920  		if instance1.ID == instance2.ID {
   921  			return fmt.Errorf("Instance was not recreated.")
   922  		}
   923  
   924  		return nil
   925  	}
   926  }
   927  
   928  func TestAccComputeV2Instance_stop_before_destroy(t *testing.T) {
   929  	var instance servers.Server
   930  	var testAccComputeV2Instance_stop_before_destroy = fmt.Sprintf(`
   931  		resource "openstack_compute_instance_v2" "foo" {
   932  			name = "terraform-test"
   933  			security_groups = ["default"]
   934  			network {
   935  				uuid = "%s"
   936  			}
   937  			stop_before_destroy = true
   938  		}`,
   939  		os.Getenv("OS_NETWORK_ID"))
   940  
   941  	resource.Test(t, resource.TestCase{
   942  		PreCheck:     func() { testAccPreCheck(t) },
   943  		Providers:    testAccProviders,
   944  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   945  		Steps: []resource.TestStep{
   946  			resource.TestStep{
   947  				Config: testAccComputeV2Instance_stop_before_destroy,
   948  				Check: resource.ComposeTestCheckFunc(
   949  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   950  				),
   951  			},
   952  		},
   953  	})
   954  }