github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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/gophercloud/gophercloud"
    12  	"github.com/gophercloud/gophercloud/openstack/blockstorage/v1/volumes"
    13  	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
    14  	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups"
    15  	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach"
    16  	"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
    17  	"github.com/gophercloud/gophercloud/pagination"
    18  )
    19  
    20  func TestAccComputeV2Instance_basic(t *testing.T) {
    21  	var instance servers.Server
    22  	var testAccComputeV2Instance_basic = fmt.Sprintf(`
    23  		resource "openstack_compute_instance_v2" "foo" {
    24  			name = "terraform-test"
    25  			security_groups = ["default"]
    26  			network {
    27  				uuid = "%s"
    28  			}
    29  			metadata {
    30  				foo = "bar"
    31  			}
    32  		}`,
    33  		os.Getenv("OS_NETWORK_ID"))
    34  
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck:     func() { testAccPreCheck(t) },
    37  		Providers:    testAccProviders,
    38  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
    39  		Steps: []resource.TestStep{
    40  			resource.TestStep{
    41  				Config: testAccComputeV2Instance_basic,
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
    44  					testAccCheckComputeV2InstanceMetadata(&instance, "foo", "bar"),
    45  				),
    46  			},
    47  		},
    48  	})
    49  }
    50  
    51  func TestAccComputeV2Instance_volumeAttach(t *testing.T) {
    52  	var instance servers.Server
    53  	var volume volumes.Volume
    54  
    55  	var testAccComputeV2Instance_volumeAttach = fmt.Sprintf(`
    56  		resource "openstack_blockstorage_volume_v1" "myvol" {
    57  			name = "myvol"
    58  			size = 1
    59  		}
    60  
    61  		resource "openstack_compute_instance_v2" "foo" {
    62  			name = "terraform-test"
    63  			security_groups = ["default"]
    64  			volume {
    65  				volume_id = "${openstack_blockstorage_volume_v1.myvol.id}"
    66  			}
    67  		}`)
    68  
    69  	resource.Test(t, resource.TestCase{
    70  		PreCheck:     func() { testAccPreCheck(t) },
    71  		Providers:    testAccProviders,
    72  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
    73  		Steps: []resource.TestStep{
    74  			resource.TestStep{
    75  				Config: testAccComputeV2Instance_volumeAttach,
    76  				Check: resource.ComposeTestCheckFunc(
    77  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume),
    78  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
    79  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume),
    80  				),
    81  			},
    82  		},
    83  	})
    84  }
    85  
    86  func TestAccComputeV2Instance_volumeAttachPostCreation(t *testing.T) {
    87  	var instance servers.Server
    88  	var volume volumes.Volume
    89  
    90  	var testAccComputeV2Instance_volumeAttachPostCreationInstance = fmt.Sprintf(`
    91  		resource "openstack_compute_instance_v2" "foo" {
    92  			name = "terraform-test"
    93  			security_groups = ["default"]
    94  		}`)
    95  
    96  	var testAccComputeV2Instance_volumeAttachPostCreationInstanceAndVolume = fmt.Sprintf(`
    97  		resource "openstack_blockstorage_volume_v1" "myvol" {
    98  			name = "myvol"
    99  			size = 1
   100  		}
   101  
   102  		resource "openstack_compute_instance_v2" "foo" {
   103  			name = "terraform-test"
   104  			security_groups = ["default"]
   105  			volume {
   106  				volume_id = "${openstack_blockstorage_volume_v1.myvol.id}"
   107  			}
   108  		}`)
   109  
   110  	resource.Test(t, resource.TestCase{
   111  		PreCheck:     func() { testAccPreCheck(t) },
   112  		Providers:    testAccProviders,
   113  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   114  		Steps: []resource.TestStep{
   115  			resource.TestStep{
   116  				Config: testAccComputeV2Instance_volumeAttachPostCreationInstance,
   117  				Check: resource.ComposeTestCheckFunc(
   118  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   119  				),
   120  			},
   121  			resource.TestStep{
   122  				Config: testAccComputeV2Instance_volumeAttachPostCreationInstanceAndVolume,
   123  				Check: resource.ComposeTestCheckFunc(
   124  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume),
   125  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   126  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume),
   127  				),
   128  			},
   129  		},
   130  	})
   131  }
   132  
   133  func TestAccComputeV2Instance_volumeDetachPostCreation(t *testing.T) {
   134  	var instance servers.Server
   135  	var volume volumes.Volume
   136  
   137  	var testAccComputeV2Instance_volumeDetachPostCreationInstanceAndVolume = fmt.Sprintf(`
   138  		resource "openstack_blockstorage_volume_v1" "myvol" {
   139  			name = "myvol"
   140  			size = 1
   141  		}
   142  
   143  		resource "openstack_compute_instance_v2" "foo" {
   144  			name = "terraform-test"
   145  			security_groups = ["default"]
   146  			volume {
   147  				volume_id = "${openstack_blockstorage_volume_v1.myvol.id}"
   148  			}
   149  		}`)
   150  
   151  	var testAccComputeV2Instance_volumeDetachPostCreationInstance = fmt.Sprintf(`
   152  		resource "openstack_blockstorage_volume_v1" "myvol" {
   153  			name = "myvol"
   154  			size = 1
   155  		}
   156  
   157  		resource "openstack_compute_instance_v2" "foo" {
   158  			name = "terraform-test"
   159  			security_groups = ["default"]
   160  		}`)
   161  
   162  	resource.Test(t, resource.TestCase{
   163  		PreCheck:     func() { testAccPreCheck(t) },
   164  		Providers:    testAccProviders,
   165  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   166  		Steps: []resource.TestStep{
   167  			resource.TestStep{
   168  				Config: testAccComputeV2Instance_volumeDetachPostCreationInstanceAndVolume,
   169  				Check: resource.ComposeTestCheckFunc(
   170  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume),
   171  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   172  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume),
   173  				),
   174  			},
   175  			resource.TestStep{
   176  				Config: testAccComputeV2Instance_volumeDetachPostCreationInstance,
   177  				Check: resource.ComposeTestCheckFunc(
   178  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume),
   179  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   180  					testAccCheckComputeV2InstanceVolumesDetached(&instance),
   181  				),
   182  			},
   183  		},
   184  	})
   185  }
   186  
   187  func TestAccComputeV2Instance_volumeDetachAdditionalVolumePostCreation(t *testing.T) {
   188  	var instance servers.Server
   189  	var volume_1 volumes.Volume
   190  	var volume_2 volumes.Volume
   191  
   192  	var testAccComputeV2Instance_volumeDetachAdditionalVolumePostCreationInstanceAndVolume = fmt.Sprintf(`
   193  
   194  		resource "openstack_blockstorage_volume_v1" "root_volume" {
   195  			name = "root_volume"
   196  			size = 1
   197  			image_id = "%s"
   198  		}
   199  
   200  		resource "openstack_blockstorage_volume_v1" "additional_volume" {
   201  			name = "additional_volume"
   202  			size = 1
   203  		}
   204  
   205  		resource "openstack_compute_instance_v2" "foo" {
   206  			name = "terraform-test"
   207  			security_groups = ["default"]
   208  
   209  			block_device {
   210  				uuid = "${openstack_blockstorage_volume_v1.root_volume.id}"
   211  				source_type = "volume"
   212  				boot_index = 0
   213  				destination_type = "volume"
   214  				delete_on_termination = false
   215  			}
   216  
   217  			volume {
   218  				volume_id = "${openstack_blockstorage_volume_v1.additional_volume.id}"
   219  			}
   220  		}`,
   221  		os.Getenv("OS_IMAGE_ID"))
   222  
   223  	var testAccComputeV2Instance_volumeDetachPostCreationInstance = fmt.Sprintf(`
   224  
   225  		resource "openstack_blockstorage_volume_v1" "root_volume" {
   226  			name = "root_volume"
   227  			size = 1
   228  			image_id = "%s"
   229  		}
   230  
   231  		resource "openstack_blockstorage_volume_v1" "additional_volume" {
   232  			name = "additional_volume"
   233  			size = 1
   234  		}
   235  
   236  		resource "openstack_compute_instance_v2" "foo" {
   237  			name = "terraform-test"
   238  			security_groups = ["default"]
   239  
   240  			block_device {
   241  				uuid = "${openstack_blockstorage_volume_v1.root_volume.id}"
   242  				source_type = "volume"
   243  				boot_index = 0
   244  				destination_type = "volume"
   245  				delete_on_termination = false
   246  			}
   247  		}`,
   248  		os.Getenv("OS_IMAGE_ID"))
   249  
   250  	resource.Test(t, resource.TestCase{
   251  		PreCheck:     func() { testAccPreCheck(t) },
   252  		Providers:    testAccProviders,
   253  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   254  		Steps: []resource.TestStep{
   255  			resource.TestStep{
   256  				Config: testAccComputeV2Instance_volumeDetachAdditionalVolumePostCreationInstanceAndVolume,
   257  				Check: resource.ComposeTestCheckFunc(
   258  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.root_volume", &volume_1),
   259  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.additional_volume", &volume_2),
   260  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   261  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume_1),
   262  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume_2),
   263  				),
   264  			},
   265  			resource.TestStep{
   266  				Config: testAccComputeV2Instance_volumeDetachPostCreationInstance,
   267  				Check: resource.ComposeTestCheckFunc(
   268  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.root_volume", &volume_1),
   269  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.additional_volume", &volume_2),
   270  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   271  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume_1),
   272  					testAccCheckComputeV2InstanceVolumeDetached(&instance, "openstack_blockstorage_volume_v1.additional_volume"),
   273  				),
   274  			},
   275  		},
   276  	})
   277  }
   278  
   279  func TestAccComputeV2Instance_volumeAttachInstanceDelete(t *testing.T) {
   280  	var instance servers.Server
   281  	var volume_1 volumes.Volume
   282  	var volume_2 volumes.Volume
   283  
   284  	var testAccComputeV2Instance_volumeAttachInstanceDelete_1 = fmt.Sprintf(`
   285  		resource "openstack_blockstorage_volume_v1" "root_volume" {
   286  			name = "root_volume"
   287  			size = 1
   288  			image_id = "%s"
   289  		}
   290  
   291  		resource "openstack_blockstorage_volume_v1" "additional_volume" {
   292  			name = "additional_volume"
   293  			size = 1
   294  		}
   295  
   296  		resource "openstack_compute_instance_v2" "foo" {
   297  			name = "terraform-test"
   298  			security_groups = ["default"]
   299  
   300  			block_device {
   301  				uuid = "${openstack_blockstorage_volume_v1.root_volume.id}"
   302  				source_type = "volume"
   303  				boot_index = 0
   304  				destination_type = "volume"
   305  				delete_on_termination = false
   306  			}
   307  
   308  			volume {
   309  				volume_id = "${openstack_blockstorage_volume_v1.additional_volume.id}"
   310  			}
   311  		}`,
   312  		os.Getenv("OS_IMAGE_ID"))
   313  
   314  	var testAccComputeV2Instance_volumeAttachInstanceDelete_2 = fmt.Sprintf(`
   315  		resource "openstack_blockstorage_volume_v1" "root_volume" {
   316  			name = "root_volume"
   317  			size = 1
   318  			image_id = "%s"
   319  		}
   320  
   321  		resource "openstack_blockstorage_volume_v1" "additional_volume" {
   322  			name = "additional_volume"
   323  			size = 1
   324  		}`,
   325  		os.Getenv("OS_IMAGE_ID"))
   326  
   327  	resource.Test(t, resource.TestCase{
   328  		PreCheck:     func() { testAccPreCheck(t) },
   329  		Providers:    testAccProviders,
   330  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   331  		Steps: []resource.TestStep{
   332  			resource.TestStep{
   333  				Config: testAccComputeV2Instance_volumeAttachInstanceDelete_1,
   334  				Check: resource.ComposeTestCheckFunc(
   335  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.root_volume", &volume_1),
   336  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.additional_volume", &volume_2),
   337  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   338  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume_1),
   339  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume_2),
   340  				),
   341  			},
   342  			resource.TestStep{
   343  				Config: testAccComputeV2Instance_volumeAttachInstanceDelete_2,
   344  				Check: resource.ComposeTestCheckFunc(
   345  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.root_volume", &volume_1),
   346  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.additional_volume", &volume_2),
   347  					testAccCheckComputeV2InstanceDoesNotExist(t, "openstack_compute_instance_v2.foo", &instance),
   348  					testAccCheckComputeV2InstanceVolumeDetached(&instance, "openstack_blockstorage_volume_v1.root_volume"),
   349  					testAccCheckComputeV2InstanceVolumeDetached(&instance, "openstack_blockstorage_volume_v1.additional_volume"),
   350  				),
   351  			},
   352  		},
   353  	})
   354  }
   355  
   356  func TestAccComputeV2Instance_volumeAttachToNewInstance(t *testing.T) {
   357  	var instance_1 servers.Server
   358  	var instance_2 servers.Server
   359  	var volume_1 volumes.Volume
   360  
   361  	var testAccComputeV2Instance_volumeAttachToNewInstance_1 = fmt.Sprintf(`
   362  		resource "openstack_blockstorage_volume_v1" "volume_1" {
   363  			name = "volume_1"
   364  			size = 1
   365  		}
   366  
   367  		resource "openstack_compute_instance_v2" "instance_1" {
   368  			name = "instance_1"
   369  			security_groups = ["default"]
   370  
   371  			volume {
   372  				volume_id = "${openstack_blockstorage_volume_v1.volume_1.id}"
   373  			}
   374  		}
   375  
   376  		resource "openstack_compute_instance_v2" "instance_2" {
   377  			depends_on = ["openstack_compute_instance_v2.instance_1"]
   378  			name = "instance_2"
   379  			security_groups = ["default"]
   380  		}`)
   381  
   382  	var testAccComputeV2Instance_volumeAttachToNewInstance_2 = fmt.Sprintf(`
   383  			resource "openstack_blockstorage_volume_v1" "volume_1" {
   384  				name = "volume_1"
   385  				size = 1
   386  			}
   387  
   388  			resource "openstack_compute_instance_v2" "instance_1" {
   389  				name = "instance_1"
   390  				security_groups = ["default"]
   391  			}
   392  
   393  			resource "openstack_compute_instance_v2" "instance_2" {
   394  				depends_on = ["openstack_compute_instance_v2.instance_1"]
   395  				name = "instance_2"
   396  				security_groups = ["default"]
   397  
   398  				volume {
   399  					volume_id = "${openstack_blockstorage_volume_v1.volume_1.id}"
   400  				}
   401  			}`)
   402  
   403  	resource.Test(t, resource.TestCase{
   404  		PreCheck:     func() { testAccPreCheck(t) },
   405  		Providers:    testAccProviders,
   406  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   407  		Steps: []resource.TestStep{
   408  			resource.TestStep{
   409  				Config: testAccComputeV2Instance_volumeAttachToNewInstance_1,
   410  				Check: resource.ComposeTestCheckFunc(
   411  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.volume_1", &volume_1),
   412  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1),
   413  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_2", &instance_2),
   414  					testAccCheckComputeV2InstanceVolumeAttachment(&instance_1, &volume_1),
   415  					testAccCheckComputeV2InstanceVolumeDetached(&instance_2, "openstack_blockstorage_volume_v1.volume_1"),
   416  				),
   417  			},
   418  			resource.TestStep{
   419  				Config: testAccComputeV2Instance_volumeAttachToNewInstance_2,
   420  				Check: resource.ComposeTestCheckFunc(
   421  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.volume_1", &volume_1),
   422  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1),
   423  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_2", &instance_2),
   424  					testAccCheckComputeV2InstanceVolumeDetached(&instance_1, "openstack_blockstorage_volume_v1.volume_1"),
   425  					testAccCheckComputeV2InstanceVolumeAttachment(&instance_2, &volume_1),
   426  				),
   427  			},
   428  		},
   429  	})
   430  }
   431  
   432  func TestAccComputeV2Instance_floatingIPAttachGlobally(t *testing.T) {
   433  	var instance servers.Server
   434  	var fip floatingips.FloatingIP
   435  	var testAccComputeV2Instance_floatingIPAttachGlobally = fmt.Sprintf(`
   436  		resource "openstack_compute_floatingip_v2" "myip" {
   437  		}
   438  
   439  		resource "openstack_compute_instance_v2" "foo" {
   440  			name = "terraform-test"
   441  			security_groups = ["default"]
   442  			floating_ip = "${openstack_compute_floatingip_v2.myip.address}"
   443  
   444  			network {
   445  				uuid = "%s"
   446  			}
   447  		}`,
   448  		os.Getenv("OS_NETWORK_ID"))
   449  
   450  	resource.Test(t, resource.TestCase{
   451  		PreCheck:     func() { testAccPreCheck(t) },
   452  		Providers:    testAccProviders,
   453  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   454  		Steps: []resource.TestStep{
   455  			resource.TestStep{
   456  				Config: testAccComputeV2Instance_floatingIPAttachGlobally,
   457  				Check: resource.ComposeTestCheckFunc(
   458  					testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip", &fip),
   459  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   460  					testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip),
   461  				),
   462  			},
   463  		},
   464  	})
   465  }
   466  
   467  func TestAccComputeV2Instance_floatingIPAttachToNetwork(t *testing.T) {
   468  	var instance servers.Server
   469  	var fip floatingips.FloatingIP
   470  	var testAccComputeV2Instance_floatingIPAttachToNetwork = fmt.Sprintf(`
   471  		resource "openstack_compute_floatingip_v2" "myip" {
   472  		}
   473  
   474  		resource "openstack_compute_instance_v2" "foo" {
   475  			name = "terraform-test"
   476  			security_groups = ["default"]
   477  
   478  			network {
   479  				uuid = "%s"
   480  				floating_ip = "${openstack_compute_floatingip_v2.myip.address}"
   481  				access_network = true
   482  			}
   483  		}`,
   484  		os.Getenv("OS_NETWORK_ID"))
   485  
   486  	resource.Test(t, resource.TestCase{
   487  		PreCheck:     func() { testAccPreCheck(t) },
   488  		Providers:    testAccProviders,
   489  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   490  		Steps: []resource.TestStep{
   491  			resource.TestStep{
   492  				Config: testAccComputeV2Instance_floatingIPAttachToNetwork,
   493  				Check: resource.ComposeTestCheckFunc(
   494  					testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip", &fip),
   495  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   496  					testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip),
   497  				),
   498  			},
   499  		},
   500  	})
   501  }
   502  
   503  func TestAccComputeV2Instance_floatingIPAttachAndChange(t *testing.T) {
   504  	var instance servers.Server
   505  	var fip floatingips.FloatingIP
   506  	var testAccComputeV2Instance_floatingIPAttachToNetwork_1 = fmt.Sprintf(`
   507  		resource "openstack_compute_floatingip_v2" "myip_1" {
   508  		}
   509  
   510  		resource "openstack_compute_floatingip_v2" "myip_2" {
   511  		}
   512  
   513  		resource "openstack_compute_instance_v2" "foo" {
   514  			name = "terraform-test"
   515  			security_groups = ["default"]
   516  
   517  			network {
   518  				uuid = "%s"
   519  				floating_ip = "${openstack_compute_floatingip_v2.myip_1.address}"
   520  				access_network = true
   521  			}
   522  		}`,
   523  		os.Getenv("OS_NETWORK_ID"))
   524  
   525  	var testAccComputeV2Instance_floatingIPAttachToNetwork_2 = fmt.Sprintf(`
   526  		resource "openstack_compute_floatingip_v2" "myip_1" {
   527  		}
   528  
   529  		resource "openstack_compute_floatingip_v2" "myip_2" {
   530  		}
   531  
   532  		resource "openstack_compute_instance_v2" "foo" {
   533  			name = "terraform-test"
   534  			security_groups = ["default"]
   535  
   536  			network {
   537  				uuid = "%s"
   538  				floating_ip = "${openstack_compute_floatingip_v2.myip_2.address}"
   539  				access_network = true
   540  			}
   541  		}`,
   542  		os.Getenv("OS_NETWORK_ID"))
   543  
   544  	resource.Test(t, resource.TestCase{
   545  		PreCheck:     func() { testAccPreCheck(t) },
   546  		Providers:    testAccProviders,
   547  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   548  		Steps: []resource.TestStep{
   549  			resource.TestStep{
   550  				Config: testAccComputeV2Instance_floatingIPAttachToNetwork_1,
   551  				Check: resource.ComposeTestCheckFunc(
   552  					testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip_1", &fip),
   553  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   554  					testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip),
   555  				),
   556  			},
   557  			resource.TestStep{
   558  				Config: testAccComputeV2Instance_floatingIPAttachToNetwork_2,
   559  				Check: resource.ComposeTestCheckFunc(
   560  					testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip_2", &fip),
   561  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   562  					testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip),
   563  				),
   564  			},
   565  		},
   566  	})
   567  }
   568  
   569  func TestAccComputeV2Instance_multi_secgroups(t *testing.T) {
   570  	var instance_1 servers.Server
   571  	var secgroup_1 secgroups.SecurityGroup
   572  	var testAccComputeV2Instance_multi_secgroups = fmt.Sprintf(`
   573  		resource "openstack_compute_secgroup_v2" "secgroup_1" {
   574  			name = "secgroup_1"
   575  			description = "a security group"
   576  			rule {
   577  				from_port = 22
   578  				to_port = 22
   579  				ip_protocol = "tcp"
   580  				cidr = "0.0.0.0/0"
   581  			}
   582  		}
   583  
   584  		resource "openstack_compute_instance_v2" "instance_1" {
   585  			name = "instance_1"
   586  			security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"]
   587  			network {
   588  				uuid = "%s"
   589  			}
   590  		}`,
   591  		os.Getenv("OS_NETWORK_ID"))
   592  
   593  	resource.Test(t, resource.TestCase{
   594  		PreCheck:     func() { testAccPreCheck(t) },
   595  		Providers:    testAccProviders,
   596  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   597  		Steps: []resource.TestStep{
   598  			resource.TestStep{
   599  				Config: testAccComputeV2Instance_multi_secgroups,
   600  				Check: resource.ComposeTestCheckFunc(
   601  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_1", &secgroup_1),
   602  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1),
   603  				),
   604  			},
   605  		},
   606  	})
   607  }
   608  
   609  func TestAccComputeV2Instance_multi_secgroups_update(t *testing.T) {
   610  	var instance_1 servers.Server
   611  	var secgroup_1, secgroup_2 secgroups.SecurityGroup
   612  	var testAccComputeV2Instance_multi_secgroups_update_1 = fmt.Sprintf(`
   613  		resource "openstack_compute_secgroup_v2" "secgroup_1" {
   614  			name = "secgroup_1"
   615  			description = "a security group"
   616  			rule {
   617  				from_port = 22
   618  				to_port = 22
   619  				ip_protocol = "tcp"
   620  				cidr = "0.0.0.0/0"
   621  			}
   622  		}
   623  
   624  		resource "openstack_compute_secgroup_v2" "secgroup_2" {
   625  			name = "secgroup_2"
   626  			description = "another security group"
   627  			rule {
   628  				from_port = 80
   629  				to_port = 80
   630  				ip_protocol = "tcp"
   631  				cidr = "0.0.0.0/0"
   632  			}
   633  		}
   634  
   635  		resource "openstack_compute_instance_v2" "instance_1" {
   636  			name = "instance_1"
   637  			security_groups = ["default"]
   638  		}`)
   639  
   640  	var testAccComputeV2Instance_multi_secgroups_update_2 = fmt.Sprintf(`
   641  		resource "openstack_compute_secgroup_v2" "secgroup_1" {
   642  			name = "secgroup_1"
   643  			description = "a security group"
   644  			rule {
   645  				from_port = 22
   646  				to_port = 22
   647  				ip_protocol = "tcp"
   648  				cidr = "0.0.0.0/0"
   649  			}
   650  		}
   651  
   652  		resource "openstack_compute_secgroup_v2" "secgroup_2" {
   653  			name = "secgroup_2"
   654  			description = "another security group"
   655  			rule {
   656  				from_port = 80
   657  				to_port = 80
   658  				ip_protocol = "tcp"
   659  				cidr = "0.0.0.0/0"
   660  			}
   661  		}
   662  
   663  		resource "openstack_compute_instance_v2" "instance_1" {
   664  			name = "instance_1"
   665  			security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}", "${openstack_compute_secgroup_v2.secgroup_2.name}"]
   666  		}`)
   667  
   668  	resource.Test(t, resource.TestCase{
   669  		PreCheck:     func() { testAccPreCheck(t) },
   670  		Providers:    testAccProviders,
   671  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   672  		Steps: []resource.TestStep{
   673  			resource.TestStep{
   674  				Config: testAccComputeV2Instance_multi_secgroups_update_1,
   675  				Check: resource.ComposeTestCheckFunc(
   676  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_1", &secgroup_1),
   677  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_2", &secgroup_2),
   678  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1),
   679  				),
   680  			},
   681  			resource.TestStep{
   682  				Config: testAccComputeV2Instance_multi_secgroups_update_2,
   683  				Check: resource.ComposeTestCheckFunc(
   684  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_1", &secgroup_1),
   685  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_2", &secgroup_2),
   686  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1),
   687  				),
   688  			},
   689  		},
   690  	})
   691  }
   692  
   693  func TestAccComputeV2Instance_bootFromVolumeImage(t *testing.T) {
   694  	var instance servers.Server
   695  	var testAccComputeV2Instance_bootFromVolumeImage = fmt.Sprintf(`
   696  		resource "openstack_compute_instance_v2" "foo" {
   697  			name = "terraform-test"
   698  			security_groups = ["default"]
   699  			block_device {
   700  				uuid = "%s"
   701  				source_type = "image"
   702  				volume_size = 5
   703  				boot_index = 0
   704  				destination_type = "volume"
   705  				delete_on_termination = true
   706  			}
   707  		}`,
   708  		os.Getenv("OS_IMAGE_ID"))
   709  
   710  	resource.Test(t, resource.TestCase{
   711  		PreCheck:     func() { testAccPreCheck(t) },
   712  		Providers:    testAccProviders,
   713  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   714  		Steps: []resource.TestStep{
   715  			resource.TestStep{
   716  				Config: testAccComputeV2Instance_bootFromVolumeImage,
   717  				Check: resource.ComposeTestCheckFunc(
   718  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   719  					testAccCheckComputeV2InstanceBootVolumeAttachment(&instance),
   720  				),
   721  			},
   722  		},
   723  	})
   724  }
   725  
   726  func TestAccComputeV2Instance_bootFromVolumeImageWithAttachedVolume(t *testing.T) {
   727  	var instance servers.Server
   728  	var testAccComputeV2Instance_bootFromVolumeImageWithAttachedVolume = fmt.Sprintf(`
   729  		resource "openstack_blockstorage_volume_v1" "volume_1" {
   730    		name = "volume_1"
   731    		size = 1
   732  		}
   733  
   734  		resource "openstack_compute_instance_v2" "instance_1" {
   735  			name = "instance_1"
   736  			security_groups = ["default"]
   737  			block_device {
   738  				uuid = "%s"
   739  				source_type = "image"
   740  				volume_size = 2
   741  				boot_index = 0
   742  				destination_type = "volume"
   743  				delete_on_termination = true
   744  			}
   745  
   746  			volume {
   747  				volume_id = "${openstack_blockstorage_volume_v1.volume_1.id}"
   748  			}
   749  		}`,
   750  		os.Getenv("OS_IMAGE_ID"))
   751  
   752  	resource.Test(t, resource.TestCase{
   753  		PreCheck:     func() { testAccPreCheck(t) },
   754  		Providers:    testAccProviders,
   755  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   756  		Steps: []resource.TestStep{
   757  			resource.TestStep{
   758  				Config: testAccComputeV2Instance_bootFromVolumeImageWithAttachedVolume,
   759  				Check: resource.ComposeTestCheckFunc(
   760  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance),
   761  				),
   762  			},
   763  		},
   764  	})
   765  }
   766  
   767  func TestAccComputeV2Instance_bootFromVolumeVolume(t *testing.T) {
   768  	var instance servers.Server
   769  	var testAccComputeV2Instance_bootFromVolumeVolume = fmt.Sprintf(`
   770  	  resource "openstack_blockstorage_volume_v1" "foo" {
   771  			name = "terraform-test"
   772  			size = 5
   773  			image_id = "%s"
   774  		}
   775  
   776  		resource "openstack_compute_instance_v2" "foo" {
   777  			name = "terraform-test"
   778  			security_groups = ["default"]
   779  			block_device {
   780  				uuid = "${openstack_blockstorage_volume_v1.foo.id}"
   781  				source_type = "volume"
   782  				boot_index = 0
   783  				destination_type = "volume"
   784  				delete_on_termination = true
   785  			}
   786  		}`,
   787  		os.Getenv("OS_IMAGE_ID"))
   788  
   789  	resource.Test(t, resource.TestCase{
   790  		PreCheck:     func() { testAccPreCheck(t) },
   791  		Providers:    testAccProviders,
   792  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   793  		Steps: []resource.TestStep{
   794  			resource.TestStep{
   795  				Config: testAccComputeV2Instance_bootFromVolumeVolume,
   796  				Check: resource.ComposeTestCheckFunc(
   797  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   798  					testAccCheckComputeV2InstanceBootVolumeAttachment(&instance),
   799  				),
   800  			},
   801  		},
   802  	})
   803  }
   804  
   805  func TestAccComputeV2Instance_bootFromVolumeForceNew(t *testing.T) {
   806  	var instance1_1 servers.Server
   807  	var instance1_2 servers.Server
   808  	var testAccComputeV2Instance_bootFromVolumeForceNew_1 = fmt.Sprintf(`
   809  		resource "openstack_compute_instance_v2" "instance_1" {
   810  			name = "instance_1"
   811  			security_groups = ["default"]
   812  			block_device {
   813  				uuid = "%s"
   814  				source_type = "image"
   815  				volume_size = 5
   816  				boot_index = 0
   817  				destination_type = "volume"
   818  				delete_on_termination = true
   819  			}
   820  		}`,
   821  		os.Getenv("OS_IMAGE_ID"))
   822  
   823  	var testAccComputeV2Instance_bootFromVolumeForceNew_2 = fmt.Sprintf(`
   824  		resource "openstack_compute_instance_v2" "instance_1" {
   825  			name = "instance_1"
   826  			security_groups = ["default"]
   827  			block_device {
   828  				uuid = "%s"
   829  				source_type = "image"
   830  				volume_size = 4
   831  				boot_index = 0
   832  				destination_type = "volume"
   833  				delete_on_termination = true
   834  			}
   835  		}`,
   836  		os.Getenv("OS_IMAGE_ID"))
   837  
   838  	resource.Test(t, resource.TestCase{
   839  		PreCheck:     func() { testAccPreCheck(t) },
   840  		Providers:    testAccProviders,
   841  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   842  		Steps: []resource.TestStep{
   843  			resource.TestStep{
   844  				Config: testAccComputeV2Instance_bootFromVolumeForceNew_1,
   845  				Check: resource.ComposeTestCheckFunc(
   846  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_1),
   847  				),
   848  			},
   849  			resource.TestStep{
   850  				Config: testAccComputeV2Instance_bootFromVolumeForceNew_2,
   851  				Check: resource.ComposeTestCheckFunc(
   852  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_2),
   853  					testAccCheckComputeV2InstanceInstanceIDsDoNotMatch(&instance1_1, &instance1_2),
   854  				),
   855  			},
   856  		},
   857  	})
   858  }
   859  
   860  // TODO: verify the personality really exists on the instance.
   861  func TestAccComputeV2Instance_personality(t *testing.T) {
   862  	var instance servers.Server
   863  	var testAccComputeV2Instance_personality = fmt.Sprintf(`
   864  		resource "openstack_compute_instance_v2" "foo" {
   865  			name = "terraform-test"
   866  			security_groups = ["default"]
   867  			personality {
   868  				file = "/tmp/foobar.txt"
   869  				content = "happy"
   870  			}
   871  			personality {
   872  				file = "/tmp/barfoo.txt"
   873  				content = "angry"
   874  			}
   875  		}`)
   876  
   877  	resource.Test(t, resource.TestCase{
   878  		PreCheck:     func() { testAccPreCheck(t) },
   879  		Providers:    testAccProviders,
   880  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   881  		Steps: []resource.TestStep{
   882  			resource.TestStep{
   883  				Config: testAccComputeV2Instance_personality,
   884  				Check: resource.ComposeTestCheckFunc(
   885  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   886  				),
   887  			},
   888  		},
   889  	})
   890  }
   891  
   892  func TestAccComputeV2Instance_multiEphemeral(t *testing.T) {
   893  	var instance servers.Server
   894  	var testAccComputeV2Instance_multiEphemeral = fmt.Sprintf(`
   895  		resource "openstack_compute_instance_v2" "foo" {
   896  			name = "terraform-test"
   897  			security_groups = ["default"]
   898  			block_device {
   899  				boot_index = 0
   900  				delete_on_termination = true
   901  				destination_type = "local"
   902  				source_type = "image"
   903  				uuid = "%s"
   904  			}
   905  			block_device {
   906  				boot_index = -1
   907  				delete_on_termination = true
   908  				destination_type = "local"
   909  				source_type = "blank"
   910  				volume_size = 1
   911  			}
   912  			block_device {
   913  				boot_index = -1
   914  				delete_on_termination = true
   915  				destination_type = "local"
   916  				source_type = "blank"
   917  				volume_size = 1
   918  			}
   919  		}`,
   920  		os.Getenv("OS_IMAGE_ID"))
   921  
   922  	resource.Test(t, resource.TestCase{
   923  		PreCheck:     func() { testAccPreCheck(t) },
   924  		Providers:    testAccProviders,
   925  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   926  		Steps: []resource.TestStep{
   927  			resource.TestStep{
   928  				Config: testAccComputeV2Instance_multiEphemeral,
   929  				Check: resource.ComposeTestCheckFunc(
   930  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   931  				),
   932  			},
   933  		},
   934  	})
   935  }
   936  
   937  func TestAccComputeV2Instance_accessIPv4(t *testing.T) {
   938  	var instance servers.Server
   939  	var testAccComputeV2Instance_accessIPv4 = fmt.Sprintf(`
   940  		resource "openstack_compute_floatingip_v2" "myip" {
   941  		}
   942  
   943  		resource "openstack_networking_network_v2" "network_1" {
   944  			name = "network_1"
   945  		}
   946  
   947  		resource "openstack_networking_subnet_v2" "subnet_1" {
   948  			name = "subnet_1"
   949  			network_id = "${openstack_networking_network_v2.network_1.id}"
   950  			cidr = "192.168.1.0/24"
   951  			ip_version = 4
   952  			enable_dhcp = true
   953  			no_gateway = true
   954  		}
   955  
   956  		resource "openstack_compute_instance_v2" "instance_1" {
   957  			depends_on = ["openstack_networking_subnet_v2.subnet_1"]
   958  
   959  			name = "instance_1"
   960  			security_groups = ["default"]
   961  			floating_ip = "${openstack_compute_floatingip_v2.myip.address}"
   962  
   963  			network {
   964  				uuid = "%s"
   965  			}
   966  
   967  			network {
   968  				uuid = "${openstack_networking_network_v2.network_1.id}"
   969  				fixed_ip_v4 = "192.168.1.100"
   970  				access_network = true
   971  			}
   972  		}`, os.Getenv("OS_NETWORK_ID"))
   973  
   974  	resource.Test(t, resource.TestCase{
   975  		PreCheck:     func() { testAccPreCheck(t) },
   976  		Providers:    testAccProviders,
   977  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   978  		Steps: []resource.TestStep{
   979  			resource.TestStep{
   980  				Config: testAccComputeV2Instance_accessIPv4,
   981  				Check: resource.ComposeTestCheckFunc(
   982  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance),
   983  					resource.TestCheckResourceAttr(
   984  						"openstack_compute_instance_v2.instance_1", "access_ip_v4", "192.168.1.100"),
   985  				),
   986  			},
   987  		},
   988  	})
   989  }
   990  
   991  func TestAccComputeV2Instance_ChangeFixedIP(t *testing.T) {
   992  	var instance1_1 servers.Server
   993  	var instance1_2 servers.Server
   994  	var testAccComputeV2Instance_ChangeFixedIP_1 = fmt.Sprintf(`
   995  		resource "openstack_compute_instance_v2" "instance_1" {
   996  			name = "instance_1"
   997  			security_groups = ["default"]
   998  			network {
   999  				uuid = "%s"
  1000  				fixed_ip_v4 = "10.0.0.24"
  1001  			}
  1002  		}`,
  1003  		os.Getenv("OS_NETWORK_ID"))
  1004  
  1005  	var testAccComputeV2Instance_ChangeFixedIP_2 = fmt.Sprintf(`
  1006  		resource "openstack_compute_instance_v2" "instance_1" {
  1007  			name = "instance_1"
  1008  			security_groups = ["default"]
  1009  			network {
  1010  				uuid = "%s"
  1011  				fixed_ip_v4 = "10.0.0.25"
  1012  			}
  1013  		}`,
  1014  		os.Getenv("OS_NETWORK_ID"))
  1015  
  1016  	resource.Test(t, resource.TestCase{
  1017  		PreCheck:     func() { testAccPreCheck(t) },
  1018  		Providers:    testAccProviders,
  1019  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
  1020  		Steps: []resource.TestStep{
  1021  			resource.TestStep{
  1022  				Config: testAccComputeV2Instance_ChangeFixedIP_1,
  1023  				Check: resource.ComposeTestCheckFunc(
  1024  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_1),
  1025  				),
  1026  			},
  1027  			resource.TestStep{
  1028  				Config: testAccComputeV2Instance_ChangeFixedIP_2,
  1029  				Check: resource.ComposeTestCheckFunc(
  1030  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_2),
  1031  					testAccCheckComputeV2InstanceInstanceIDsDoNotMatch(&instance1_1, &instance1_2),
  1032  				),
  1033  			},
  1034  		},
  1035  	})
  1036  }
  1037  
  1038  func testAccCheckComputeV2InstanceDestroy(s *terraform.State) error {
  1039  	config := testAccProvider.Meta().(*Config)
  1040  	computeClient, err := config.computeV2Client(OS_REGION_NAME)
  1041  	if err != nil {
  1042  		return fmt.Errorf("(testAccCheckComputeV2InstanceDestroy) Error creating OpenStack compute client: %s", err)
  1043  	}
  1044  
  1045  	for _, rs := range s.RootModule().Resources {
  1046  		if rs.Type != "openstack_compute_instance_v2" {
  1047  			continue
  1048  		}
  1049  
  1050  		_, err := servers.Get(computeClient, rs.Primary.ID).Extract()
  1051  		if err == nil {
  1052  			return fmt.Errorf("Instance still exists")
  1053  		}
  1054  	}
  1055  
  1056  	return nil
  1057  }
  1058  
  1059  func testAccCheckComputeV2InstanceExists(t *testing.T, n string, instance *servers.Server) resource.TestCheckFunc {
  1060  	return func(s *terraform.State) error {
  1061  		rs, ok := s.RootModule().Resources[n]
  1062  		if !ok {
  1063  			return fmt.Errorf("Not found: %s", n)
  1064  		}
  1065  
  1066  		if rs.Primary.ID == "" {
  1067  			return fmt.Errorf("No ID is set")
  1068  		}
  1069  
  1070  		config := testAccProvider.Meta().(*Config)
  1071  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
  1072  		if err != nil {
  1073  			return fmt.Errorf("(testAccCheckComputeV2InstanceExists) Error creating OpenStack compute client: %s", err)
  1074  		}
  1075  
  1076  		found, err := servers.Get(computeClient, rs.Primary.ID).Extract()
  1077  		if err != nil {
  1078  			return err
  1079  		}
  1080  
  1081  		if found.ID != rs.Primary.ID {
  1082  			return fmt.Errorf("Instance not found")
  1083  		}
  1084  
  1085  		*instance = *found
  1086  
  1087  		return nil
  1088  	}
  1089  }
  1090  
  1091  func testAccCheckComputeV2InstanceDoesNotExist(t *testing.T, n string, instance *servers.Server) resource.TestCheckFunc {
  1092  	return func(s *terraform.State) error {
  1093  		config := testAccProvider.Meta().(*Config)
  1094  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
  1095  		if err != nil {
  1096  			return fmt.Errorf("(testAccCheckComputeV2InstanceExists) Error creating OpenStack compute client: %s", err)
  1097  		}
  1098  
  1099  		_, err = servers.Get(computeClient, instance.ID).Extract()
  1100  		if err != nil {
  1101  			if _, ok := err.(gophercloud.ErrDefault404); ok {
  1102  				return nil
  1103  			}
  1104  			return err
  1105  		}
  1106  
  1107  		return fmt.Errorf("Instance still exists")
  1108  	}
  1109  }
  1110  
  1111  func testAccCheckComputeV2InstanceMetadata(
  1112  	instance *servers.Server, k string, v string) resource.TestCheckFunc {
  1113  	return func(s *terraform.State) error {
  1114  		if instance.Metadata == nil {
  1115  			return fmt.Errorf("No metadata")
  1116  		}
  1117  
  1118  		for key, value := range instance.Metadata {
  1119  			if k != key {
  1120  				continue
  1121  			}
  1122  
  1123  			if v == value {
  1124  				return nil
  1125  			}
  1126  
  1127  			return fmt.Errorf("Bad value for %s: %s", k, value)
  1128  		}
  1129  
  1130  		return fmt.Errorf("Metadata not found: %s", k)
  1131  	}
  1132  }
  1133  
  1134  func testAccCheckComputeV2InstanceVolumeAttachment(
  1135  	instance *servers.Server, volume *volumes.Volume) resource.TestCheckFunc {
  1136  	return func(s *terraform.State) error {
  1137  		var attachments []volumeattach.VolumeAttachment
  1138  
  1139  		config := testAccProvider.Meta().(*Config)
  1140  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
  1141  		if err != nil {
  1142  			return err
  1143  		}
  1144  		err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) {
  1145  			actual, err := volumeattach.ExtractVolumeAttachments(page)
  1146  			if err != nil {
  1147  				return false, fmt.Errorf("Unable to lookup attachment: %s", err)
  1148  			}
  1149  
  1150  			attachments = actual
  1151  			return true, nil
  1152  		})
  1153  
  1154  		for _, attachment := range attachments {
  1155  			if attachment.VolumeID == volume.ID {
  1156  				return nil
  1157  			}
  1158  		}
  1159  
  1160  		return fmt.Errorf("Volume not found: %s", volume.ID)
  1161  	}
  1162  }
  1163  
  1164  func testAccCheckComputeV2InstanceVolumesDetached(instance *servers.Server) resource.TestCheckFunc {
  1165  	return func(s *terraform.State) error {
  1166  		var attachments []volumeattach.VolumeAttachment
  1167  
  1168  		config := testAccProvider.Meta().(*Config)
  1169  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
  1170  		if err != nil {
  1171  			return err
  1172  		}
  1173  		err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) {
  1174  			actual, err := volumeattach.ExtractVolumeAttachments(page)
  1175  			if err != nil {
  1176  				return false, fmt.Errorf("Unable to lookup attachment: %s", err)
  1177  			}
  1178  
  1179  			attachments = actual
  1180  			return true, nil
  1181  		})
  1182  
  1183  		if len(attachments) > 0 {
  1184  			return fmt.Errorf("Volumes are still attached.")
  1185  		}
  1186  
  1187  		return nil
  1188  	}
  1189  }
  1190  
  1191  func testAccCheckComputeV2InstanceBootVolumeAttachment(
  1192  	instance *servers.Server) resource.TestCheckFunc {
  1193  	return func(s *terraform.State) error {
  1194  		var attachments []volumeattach.VolumeAttachment
  1195  
  1196  		config := testAccProvider.Meta().(*Config)
  1197  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
  1198  		if err != nil {
  1199  			return err
  1200  		}
  1201  		err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) {
  1202  			actual, err := volumeattach.ExtractVolumeAttachments(page)
  1203  			if err != nil {
  1204  				return false, fmt.Errorf("Unable to lookup attachment: %s", err)
  1205  			}
  1206  
  1207  			attachments = actual
  1208  			return true, nil
  1209  		})
  1210  
  1211  		if len(attachments) == 1 {
  1212  			return nil
  1213  		}
  1214  
  1215  		return fmt.Errorf("No attached volume found.")
  1216  	}
  1217  }
  1218  
  1219  func testAccCheckComputeV2InstanceFloatingIPAttach(
  1220  	instance *servers.Server, fip *floatingips.FloatingIP) resource.TestCheckFunc {
  1221  	return func(s *terraform.State) error {
  1222  		if fip.InstanceID == instance.ID {
  1223  			return nil
  1224  		}
  1225  
  1226  		return fmt.Errorf("Floating IP %s was not attached to instance %s", fip.ID, instance.ID)
  1227  	}
  1228  }
  1229  func testAccCheckComputeV2InstanceInstanceIDsDoNotMatch(
  1230  	instance1, instance2 *servers.Server) resource.TestCheckFunc {
  1231  	return func(s *terraform.State) error {
  1232  		if instance1.ID == instance2.ID {
  1233  			return fmt.Errorf("Instance was not recreated.")
  1234  		}
  1235  
  1236  		return nil
  1237  	}
  1238  }
  1239  
  1240  func TestAccComputeV2Instance_stop_before_destroy(t *testing.T) {
  1241  	var instance servers.Server
  1242  	var testAccComputeV2Instance_stop_before_destroy = fmt.Sprintf(`
  1243  		resource "openstack_compute_instance_v2" "foo" {
  1244  			name = "terraform-test"
  1245  			security_groups = ["default"]
  1246  			network {
  1247  				uuid = "%s"
  1248  			}
  1249  			stop_before_destroy = true
  1250  		}`,
  1251  		os.Getenv("OS_NETWORK_ID"))
  1252  
  1253  	resource.Test(t, resource.TestCase{
  1254  		PreCheck:     func() { testAccPreCheck(t) },
  1255  		Providers:    testAccProviders,
  1256  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
  1257  		Steps: []resource.TestStep{
  1258  			resource.TestStep{
  1259  				Config: testAccComputeV2Instance_stop_before_destroy,
  1260  				Check: resource.ComposeTestCheckFunc(
  1261  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
  1262  				),
  1263  			},
  1264  		},
  1265  	})
  1266  }
  1267  
  1268  func testAccCheckComputeV2InstanceVolumeDetached(instance *servers.Server, volume_id string) resource.TestCheckFunc {
  1269  	return func(s *terraform.State) error {
  1270  		var attachments []volumeattach.VolumeAttachment
  1271  
  1272  		rs, ok := s.RootModule().Resources[volume_id]
  1273  		if !ok {
  1274  			return fmt.Errorf("Not found: %s", volume_id)
  1275  		}
  1276  
  1277  		if rs.Primary.ID == "" {
  1278  			return fmt.Errorf("No ID is set")
  1279  		}
  1280  
  1281  		config := testAccProvider.Meta().(*Config)
  1282  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
  1283  		if err != nil {
  1284  			return err
  1285  		}
  1286  		err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) {
  1287  			actual, err := volumeattach.ExtractVolumeAttachments(page)
  1288  			if err != nil {
  1289  				return false, fmt.Errorf("Unable to lookup attachment: %s", err)
  1290  			}
  1291  
  1292  			attachments = actual
  1293  			return true, nil
  1294  		})
  1295  
  1296  		for _, attachment := range attachments {
  1297  			if attachment.VolumeID == rs.Primary.ID {
  1298  				return fmt.Errorf("Volume is still attached.")
  1299  			}
  1300  		}
  1301  
  1302  		return nil
  1303  	}
  1304  }