github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/openstack/resource_openstack_compute_instance_v2_test.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  
    10  	"github.com/gophercloud/gophercloud"
    11  	"github.com/gophercloud/gophercloud/openstack/blockstorage/v1/volumes"
    12  	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
    13  	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups"
    14  	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach"
    15  	"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
    16  	"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
    17  	"github.com/gophercloud/gophercloud/pagination"
    18  )
    19  
    20  func TestAccComputeV2Instance_basic(t *testing.T) {
    21  	var instance servers.Server
    22  
    23  	resource.Test(t, resource.TestCase{
    24  		PreCheck:     func() { testAccPreCheck(t) },
    25  		Providers:    testAccProviders,
    26  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
    27  		Steps: []resource.TestStep{
    28  			resource.TestStep{
    29  				Config: testAccComputeV2Instance_basic,
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
    32  					testAccCheckComputeV2InstanceMetadata(&instance, "foo", "bar"),
    33  					resource.TestCheckResourceAttr(
    34  						"openstack_compute_instance_v2.instance_1", "all_metadata.foo", "bar"),
    35  					resource.TestCheckResourceAttr(
    36  						"openstack_compute_instance_v2.instance_1", "availability_zone", "nova"),
    37  				),
    38  			},
    39  		},
    40  	})
    41  }
    42  
    43  func TestAccComputeV2Instance_volumeAttach(t *testing.T) {
    44  	var instance servers.Server
    45  	var volume volumes.Volume
    46  
    47  	resource.Test(t, resource.TestCase{
    48  		PreCheck:     func() { testAccPreCheck(t) },
    49  		Providers:    testAccProviders,
    50  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
    51  		Steps: []resource.TestStep{
    52  			resource.TestStep{
    53  				Config: testAccComputeV2Instance_volumeAttach,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testAccCheckBlockStorageV1VolumeExists("openstack_blockstorage_volume_v1.vol_1", &volume),
    56  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
    57  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume),
    58  				),
    59  			},
    60  		},
    61  	})
    62  }
    63  
    64  func TestAccComputeV2Instance_volumeAttachPostCreation(t *testing.T) {
    65  	var instance servers.Server
    66  	var volume volumes.Volume
    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_volumeAttachPostCreation_1,
    75  				Check: resource.ComposeTestCheckFunc(
    76  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
    77  				),
    78  			},
    79  			resource.TestStep{
    80  				Config: testAccComputeV2Instance_volumeAttachPostCreation_2,
    81  				Check: resource.ComposeTestCheckFunc(
    82  					testAccCheckBlockStorageV1VolumeExists("openstack_blockstorage_volume_v1.vol_1", &volume),
    83  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
    84  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume),
    85  				),
    86  			},
    87  		},
    88  	})
    89  }
    90  
    91  func TestAccComputeV2Instance_volumeDetachPostCreation(t *testing.T) {
    92  	var instance servers.Server
    93  	var volume volumes.Volume
    94  
    95  	resource.Test(t, resource.TestCase{
    96  		PreCheck:     func() { testAccPreCheck(t) },
    97  		Providers:    testAccProviders,
    98  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
    99  		Steps: []resource.TestStep{
   100  			resource.TestStep{
   101  				Config: testAccComputeV2Instance_volumeDetachPostCreation_1,
   102  				Check: resource.ComposeTestCheckFunc(
   103  					testAccCheckBlockStorageV1VolumeExists("openstack_blockstorage_volume_v1.vol_1", &volume),
   104  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   105  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume),
   106  				),
   107  			},
   108  			resource.TestStep{
   109  				Config: testAccComputeV2Instance_volumeDetachPostCreation_2,
   110  				Check: resource.ComposeTestCheckFunc(
   111  					testAccCheckBlockStorageV1VolumeExists("openstack_blockstorage_volume_v1.vol_1", &volume),
   112  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   113  					testAccCheckComputeV2InstanceVolumesDetached(&instance),
   114  				),
   115  			},
   116  		},
   117  	})
   118  }
   119  
   120  func TestAccComputeV2Instance_volumeDetachAdditionalVolumePostCreation(t *testing.T) {
   121  	var instance servers.Server
   122  	var volume_1 volumes.Volume
   123  	var volume_2 volumes.Volume
   124  
   125  	resource.Test(t, resource.TestCase{
   126  		PreCheck:     func() { testAccPreCheck(t) },
   127  		Providers:    testAccProviders,
   128  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   129  		Steps: []resource.TestStep{
   130  			resource.TestStep{
   131  				Config: testAccComputeV2Instance_volumeDetachAdditionalVolumePostCreation_1,
   132  				Check: resource.ComposeTestCheckFunc(
   133  					testAccCheckBlockStorageV1VolumeExists(
   134  						"openstack_blockstorage_volume_v1.root_volume", &volume_1),
   135  					testAccCheckBlockStorageV1VolumeExists(
   136  						"openstack_blockstorage_volume_v1.additional_volume", &volume_2),
   137  					testAccCheckComputeV2InstanceExists(
   138  						"openstack_compute_instance_v2.instance_1", &instance),
   139  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume_1),
   140  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume_2),
   141  				),
   142  			},
   143  			resource.TestStep{
   144  				Config: testAccComputeV2Instance_volumeDetachAdditionalVolumePostCreation_2,
   145  				Check: resource.ComposeTestCheckFunc(
   146  					testAccCheckBlockStorageV1VolumeExists(
   147  						"openstack_blockstorage_volume_v1.root_volume", &volume_1),
   148  					testAccCheckBlockStorageV1VolumeExists(
   149  						"openstack_blockstorage_volume_v1.additional_volume", &volume_2),
   150  					testAccCheckComputeV2InstanceExists(
   151  						"openstack_compute_instance_v2.instance_1", &instance),
   152  					testAccCheckComputeV2InstanceVolumeDetached(
   153  						&instance, "openstack_blockstorage_volume_v1.additional_volume"),
   154  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume_1),
   155  				),
   156  			},
   157  		},
   158  	})
   159  }
   160  
   161  func TestAccComputeV2Instance_volumeAttachInstanceDelete(t *testing.T) {
   162  	var instance servers.Server
   163  	var volume_1 volumes.Volume
   164  	var volume_2 volumes.Volume
   165  
   166  	resource.Test(t, resource.TestCase{
   167  		PreCheck:     func() { testAccPreCheck(t) },
   168  		Providers:    testAccProviders,
   169  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   170  		Steps: []resource.TestStep{
   171  			resource.TestStep{
   172  				Config: testAccComputeV2Instance_volumeAttachInstanceDelete_1,
   173  				Check: resource.ComposeTestCheckFunc(
   174  					testAccCheckBlockStorageV1VolumeExists(
   175  						"openstack_blockstorage_volume_v1.root_volume", &volume_1),
   176  					testAccCheckBlockStorageV1VolumeExists(
   177  						"openstack_blockstorage_volume_v1.additional_volume", &volume_2),
   178  					testAccCheckComputeV2InstanceExists(
   179  						"openstack_compute_instance_v2.instance_1", &instance),
   180  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume_1),
   181  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume_2),
   182  				),
   183  			},
   184  			resource.TestStep{
   185  				Config: testAccComputeV2Instance_volumeAttachInstanceDelete_2,
   186  				Check: resource.ComposeTestCheckFunc(
   187  					testAccCheckBlockStorageV1VolumeExists(
   188  						"openstack_blockstorage_volume_v1.root_volume", &volume_1),
   189  					testAccCheckBlockStorageV1VolumeExists(
   190  						"openstack_blockstorage_volume_v1.additional_volume", &volume_2),
   191  					testAccCheckComputeV2InstanceDoesNotExist(
   192  						"openstack_compute_instance_v2.instance_1", &instance),
   193  					testAccCheckComputeV2InstanceVolumeDetached(
   194  						&instance, "openstack_blockstorage_volume_v1.root_volume"),
   195  					testAccCheckComputeV2InstanceVolumeDetached(
   196  						&instance, "openstack_blockstorage_volume_v1.additional_volume"),
   197  				),
   198  			},
   199  		},
   200  	})
   201  }
   202  
   203  func TestAccComputeV2Instance_volumeAttachToNewInstance(t *testing.T) {
   204  	var instance_1 servers.Server
   205  	var instance_2 servers.Server
   206  	var volume_1 volumes.Volume
   207  
   208  	resource.Test(t, resource.TestCase{
   209  		PreCheck:     func() { testAccPreCheck(t) },
   210  		Providers:    testAccProviders,
   211  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   212  		Steps: []resource.TestStep{
   213  			resource.TestStep{
   214  				Config: testAccComputeV2Instance_volumeAttachToNewInstance_1,
   215  				Check: resource.ComposeTestCheckFunc(
   216  					testAccCheckBlockStorageV1VolumeExists(
   217  						"openstack_blockstorage_volume_v1.volume_1", &volume_1),
   218  					testAccCheckComputeV2InstanceExists(
   219  						"openstack_compute_instance_v2.instance_1", &instance_1),
   220  					testAccCheckComputeV2InstanceExists(
   221  						"openstack_compute_instance_v2.instance_2", &instance_2),
   222  					testAccCheckComputeV2InstanceVolumeDetached(
   223  						&instance_2, "openstack_blockstorage_volume_v1.volume_1"),
   224  					testAccCheckComputeV2InstanceVolumeAttachment(&instance_1, &volume_1),
   225  				),
   226  			},
   227  			resource.TestStep{
   228  				Config: testAccComputeV2Instance_volumeAttachToNewInstance_2,
   229  				Check: resource.ComposeTestCheckFunc(
   230  					testAccCheckBlockStorageV1VolumeExists(
   231  						"openstack_blockstorage_volume_v1.volume_1", &volume_1),
   232  					testAccCheckComputeV2InstanceExists(
   233  						"openstack_compute_instance_v2.instance_1", &instance_1),
   234  					testAccCheckComputeV2InstanceExists(
   235  						"openstack_compute_instance_v2.instance_2", &instance_2),
   236  					testAccCheckComputeV2InstanceVolumeDetached(
   237  						&instance_1, "openstack_blockstorage_volume_v1.volume_1"),
   238  					testAccCheckComputeV2InstanceVolumeAttachment(&instance_2, &volume_1),
   239  				),
   240  			},
   241  		},
   242  	})
   243  }
   244  
   245  func TestAccComputeV2Instance_floatingIPAttachGlobally(t *testing.T) {
   246  	var instance servers.Server
   247  	var fip floatingips.FloatingIP
   248  
   249  	resource.Test(t, resource.TestCase{
   250  		PreCheck:     func() { testAccPreCheck(t) },
   251  		Providers:    testAccProviders,
   252  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   253  		Steps: []resource.TestStep{
   254  			resource.TestStep{
   255  				Config: testAccComputeV2Instance_floatingIPAttachGlobally,
   256  				Check: resource.ComposeTestCheckFunc(
   257  					testAccCheckComputeV2FloatingIPExists("openstack_compute_floatingip_v2.fip_1", &fip),
   258  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   259  					testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip),
   260  				),
   261  			},
   262  		},
   263  	})
   264  }
   265  
   266  func TestAccComputeV2Instance_floatingIPAttachToNetwork(t *testing.T) {
   267  	var instance servers.Server
   268  	var fip floatingips.FloatingIP
   269  
   270  	resource.Test(t, resource.TestCase{
   271  		PreCheck:     func() { testAccPreCheck(t) },
   272  		Providers:    testAccProviders,
   273  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   274  		Steps: []resource.TestStep{
   275  			resource.TestStep{
   276  				Config: testAccComputeV2Instance_floatingIPAttachToNetwork,
   277  				Check: resource.ComposeTestCheckFunc(
   278  					testAccCheckComputeV2FloatingIPExists("openstack_compute_floatingip_v2.fip_1", &fip),
   279  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   280  					testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip),
   281  				),
   282  			},
   283  		},
   284  	})
   285  }
   286  
   287  func TestAccComputeV2Instance_floatingIPAttachToNetworkAndChange(t *testing.T) {
   288  	var instance servers.Server
   289  	var fip floatingips.FloatingIP
   290  
   291  	resource.Test(t, resource.TestCase{
   292  		PreCheck:     func() { testAccPreCheck(t) },
   293  		Providers:    testAccProviders,
   294  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   295  		Steps: []resource.TestStep{
   296  			resource.TestStep{
   297  				Config: testAccComputeV2Instance_floatingIPAttachToNetworkAndChange_1,
   298  				Check: resource.ComposeTestCheckFunc(
   299  					testAccCheckComputeV2FloatingIPExists("openstack_compute_floatingip_v2.fip_1", &fip),
   300  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   301  					testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip),
   302  				),
   303  			},
   304  			resource.TestStep{
   305  				Config: testAccComputeV2Instance_floatingIPAttachToNetworkAndChange_2,
   306  				Check: resource.ComposeTestCheckFunc(
   307  					testAccCheckComputeV2FloatingIPExists("openstack_compute_floatingip_v2.fip_2", &fip),
   308  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   309  					testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip),
   310  				),
   311  			},
   312  		},
   313  	})
   314  }
   315  
   316  func TestAccComputeV2Instance_secgroupMulti(t *testing.T) {
   317  	var instance_1 servers.Server
   318  	var secgroup_1 secgroups.SecurityGroup
   319  
   320  	resource.Test(t, resource.TestCase{
   321  		PreCheck:     func() { testAccPreCheck(t) },
   322  		Providers:    testAccProviders,
   323  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   324  		Steps: []resource.TestStep{
   325  			resource.TestStep{
   326  				Config: testAccComputeV2Instance_secgroupMulti,
   327  				Check: resource.ComposeTestCheckFunc(
   328  					testAccCheckComputeV2SecGroupExists(
   329  						"openstack_compute_secgroup_v2.secgroup_1", &secgroup_1),
   330  					testAccCheckComputeV2InstanceExists(
   331  						"openstack_compute_instance_v2.instance_1", &instance_1),
   332  				),
   333  			},
   334  		},
   335  	})
   336  }
   337  
   338  func TestAccComputeV2Instance_secgroupMultiUpdate(t *testing.T) {
   339  	var instance_1 servers.Server
   340  	var secgroup_1, secgroup_2 secgroups.SecurityGroup
   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_secgroupMultiUpdate_1,
   349  				Check: resource.ComposeTestCheckFunc(
   350  					testAccCheckComputeV2SecGroupExists(
   351  						"openstack_compute_secgroup_v2.secgroup_1", &secgroup_1),
   352  					testAccCheckComputeV2SecGroupExists(
   353  						"openstack_compute_secgroup_v2.secgroup_2", &secgroup_2),
   354  					testAccCheckComputeV2InstanceExists(
   355  						"openstack_compute_instance_v2.instance_1", &instance_1),
   356  				),
   357  			},
   358  			resource.TestStep{
   359  				Config: testAccComputeV2Instance_secgroupMultiUpdate_2,
   360  				Check: resource.ComposeTestCheckFunc(
   361  					testAccCheckComputeV2SecGroupExists(
   362  						"openstack_compute_secgroup_v2.secgroup_1", &secgroup_1),
   363  					testAccCheckComputeV2SecGroupExists(
   364  						"openstack_compute_secgroup_v2.secgroup_2", &secgroup_2),
   365  					testAccCheckComputeV2InstanceExists(
   366  						"openstack_compute_instance_v2.instance_1", &instance_1),
   367  				),
   368  			},
   369  		},
   370  	})
   371  }
   372  
   373  func TestAccComputeV2Instance_bootFromVolumeImage(t *testing.T) {
   374  	var instance servers.Server
   375  
   376  	resource.Test(t, resource.TestCase{
   377  		PreCheck:     func() { testAccPreCheck(t) },
   378  		Providers:    testAccProviders,
   379  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   380  		Steps: []resource.TestStep{
   381  			resource.TestStep{
   382  				Config: testAccComputeV2Instance_bootFromVolumeImage,
   383  				Check: resource.ComposeTestCheckFunc(
   384  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   385  					testAccCheckComputeV2InstanceBootVolumeAttachment(&instance),
   386  				),
   387  			},
   388  		},
   389  	})
   390  }
   391  
   392  func TestAccComputeV2Instance_bootFromVolumeImageWithAttachedVolume(t *testing.T) {
   393  	var instance servers.Server
   394  
   395  	resource.Test(t, resource.TestCase{
   396  		PreCheck:     func() { testAccPreCheck(t) },
   397  		Providers:    testAccProviders,
   398  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   399  		Steps: []resource.TestStep{
   400  			resource.TestStep{
   401  				Config: testAccComputeV2Instance_bootFromVolumeImageWithAttachedVolume,
   402  				Check: resource.ComposeTestCheckFunc(
   403  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   404  				),
   405  			},
   406  		},
   407  	})
   408  }
   409  
   410  func TestAccComputeV2Instance_bootFromVolumeVolume(t *testing.T) {
   411  	var instance servers.Server
   412  
   413  	resource.Test(t, resource.TestCase{
   414  		PreCheck:     func() { testAccPreCheck(t) },
   415  		Providers:    testAccProviders,
   416  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   417  		Steps: []resource.TestStep{
   418  			resource.TestStep{
   419  				Config: testAccComputeV2Instance_bootFromVolumeVolume,
   420  				Check: resource.ComposeTestCheckFunc(
   421  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   422  					testAccCheckComputeV2InstanceBootVolumeAttachment(&instance),
   423  				),
   424  			},
   425  		},
   426  	})
   427  }
   428  
   429  func TestAccComputeV2Instance_bootFromVolumeForceNew(t *testing.T) {
   430  	var instance1_1 servers.Server
   431  	var instance1_2 servers.Server
   432  
   433  	resource.Test(t, resource.TestCase{
   434  		PreCheck:     func() { testAccPreCheck(t) },
   435  		Providers:    testAccProviders,
   436  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   437  		Steps: []resource.TestStep{
   438  			resource.TestStep{
   439  				Config: testAccComputeV2Instance_bootFromVolumeForceNew_1,
   440  				Check: resource.ComposeTestCheckFunc(
   441  					testAccCheckComputeV2InstanceExists(
   442  						"openstack_compute_instance_v2.instance_1", &instance1_1),
   443  				),
   444  			},
   445  			resource.TestStep{
   446  				Config: testAccComputeV2Instance_bootFromVolumeForceNew_2,
   447  				Check: resource.ComposeTestCheckFunc(
   448  					testAccCheckComputeV2InstanceExists(
   449  						"openstack_compute_instance_v2.instance_1", &instance1_2),
   450  					testAccCheckComputeV2InstanceInstanceIDsDoNotMatch(&instance1_1, &instance1_2),
   451  				),
   452  			},
   453  		},
   454  	})
   455  }
   456  
   457  func TestAccComputeV2Instance_blockDeviceNewVolume(t *testing.T) {
   458  	var instance servers.Server
   459  
   460  	resource.Test(t, resource.TestCase{
   461  		PreCheck:     func() { testAccPreCheck(t) },
   462  		Providers:    testAccProviders,
   463  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   464  		Steps: []resource.TestStep{
   465  			resource.TestStep{
   466  				Config: testAccComputeV2Instance_blockDeviceNewVolume,
   467  				Check: resource.ComposeTestCheckFunc(
   468  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   469  				),
   470  			},
   471  		},
   472  	})
   473  }
   474  
   475  func TestAccComputeV2Instance_blockDeviceExistingVolume(t *testing.T) {
   476  	var instance servers.Server
   477  	var volume volumes.Volume
   478  
   479  	resource.Test(t, resource.TestCase{
   480  		PreCheck:     func() { testAccPreCheck(t) },
   481  		Providers:    testAccProviders,
   482  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   483  		Steps: []resource.TestStep{
   484  			resource.TestStep{
   485  				Config: testAccComputeV2Instance_blockDeviceExistingVolume,
   486  				Check: resource.ComposeTestCheckFunc(
   487  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   488  					testAccCheckBlockStorageV1VolumeExists(
   489  						"openstack_blockstorage_volume_v1.volume_1", &volume),
   490  				),
   491  			},
   492  		},
   493  	})
   494  }
   495  
   496  // TODO: verify the personality really exists on the instance.
   497  func TestAccComputeV2Instance_personality(t *testing.T) {
   498  	var instance servers.Server
   499  
   500  	resource.Test(t, resource.TestCase{
   501  		PreCheck:     func() { testAccPreCheck(t) },
   502  		Providers:    testAccProviders,
   503  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   504  		Steps: []resource.TestStep{
   505  			resource.TestStep{
   506  				Config: testAccComputeV2Instance_personality,
   507  				Check: resource.ComposeTestCheckFunc(
   508  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   509  				),
   510  			},
   511  		},
   512  	})
   513  }
   514  
   515  func TestAccComputeV2Instance_multiEphemeral(t *testing.T) {
   516  	var instance servers.Server
   517  
   518  	resource.Test(t, resource.TestCase{
   519  		PreCheck:     func() { testAccPreCheck(t) },
   520  		Providers:    testAccProviders,
   521  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   522  		Steps: []resource.TestStep{
   523  			resource.TestStep{
   524  				Config: testAccComputeV2Instance_multiEphemeral,
   525  				Check: resource.ComposeTestCheckFunc(
   526  					testAccCheckComputeV2InstanceExists(
   527  						"openstack_compute_instance_v2.instance_1", &instance),
   528  				),
   529  			},
   530  		},
   531  	})
   532  }
   533  
   534  func TestAccComputeV2Instance_accessIPv4(t *testing.T) {
   535  	var instance servers.Server
   536  
   537  	resource.Test(t, resource.TestCase{
   538  		PreCheck:     func() { testAccPreCheck(t) },
   539  		Providers:    testAccProviders,
   540  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   541  		Steps: []resource.TestStep{
   542  			resource.TestStep{
   543  				Config: testAccComputeV2Instance_accessIPv4,
   544  				Check: resource.ComposeTestCheckFunc(
   545  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   546  					resource.TestCheckResourceAttr(
   547  						"openstack_compute_instance_v2.instance_1", "access_ip_v4", "192.168.1.100"),
   548  				),
   549  			},
   550  		},
   551  	})
   552  }
   553  
   554  func TestAccComputeV2Instance_changeFixedIP(t *testing.T) {
   555  	var instance1_1 servers.Server
   556  	var instance1_2 servers.Server
   557  
   558  	resource.Test(t, resource.TestCase{
   559  		PreCheck:     func() { testAccPreCheck(t) },
   560  		Providers:    testAccProviders,
   561  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   562  		Steps: []resource.TestStep{
   563  			resource.TestStep{
   564  				Config: testAccComputeV2Instance_changeFixedIP_1,
   565  				Check: resource.ComposeTestCheckFunc(
   566  					testAccCheckComputeV2InstanceExists(
   567  						"openstack_compute_instance_v2.instance_1", &instance1_1),
   568  				),
   569  			},
   570  			resource.TestStep{
   571  				Config: testAccComputeV2Instance_changeFixedIP_2,
   572  				Check: resource.ComposeTestCheckFunc(
   573  					testAccCheckComputeV2InstanceExists(
   574  						"openstack_compute_instance_v2.instance_1", &instance1_2),
   575  					testAccCheckComputeV2InstanceInstanceIDsDoNotMatch(&instance1_1, &instance1_2),
   576  				),
   577  			},
   578  		},
   579  	})
   580  }
   581  
   582  func TestAccComputeV2Instance_stopBeforeDestroy(t *testing.T) {
   583  	var instance servers.Server
   584  	resource.Test(t, resource.TestCase{
   585  		PreCheck:     func() { testAccPreCheck(t) },
   586  		Providers:    testAccProviders,
   587  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   588  		Steps: []resource.TestStep{
   589  			resource.TestStep{
   590  				Config: testAccComputeV2Instance_stopBeforeDestroy,
   591  				Check: resource.ComposeTestCheckFunc(
   592  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   593  				),
   594  			},
   595  		},
   596  	})
   597  }
   598  
   599  func TestAccComputeV2Instance_metadataRemove(t *testing.T) {
   600  	var instance servers.Server
   601  
   602  	resource.Test(t, resource.TestCase{
   603  		PreCheck:     func() { testAccPreCheck(t) },
   604  		Providers:    testAccProviders,
   605  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   606  		Steps: []resource.TestStep{
   607  			resource.TestStep{
   608  				Config: testAccComputeV2Instance_metadataRemove_1,
   609  				Check: resource.ComposeTestCheckFunc(
   610  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   611  					testAccCheckComputeV2InstanceMetadata(&instance, "foo", "bar"),
   612  					testAccCheckComputeV2InstanceMetadata(&instance, "abc", "def"),
   613  					resource.TestCheckResourceAttr(
   614  						"openstack_compute_instance_v2.instance_1", "all_metadata.foo", "bar"),
   615  					resource.TestCheckResourceAttr(
   616  						"openstack_compute_instance_v2.instance_1", "all_metadata.abc", "def"),
   617  				),
   618  			},
   619  			resource.TestStep{
   620  				Config: testAccComputeV2Instance_metadataRemove_2,
   621  				Check: resource.ComposeTestCheckFunc(
   622  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   623  					testAccCheckComputeV2InstanceMetadata(&instance, "foo", "bar"),
   624  					testAccCheckComputeV2InstanceMetadata(&instance, "ghi", "jkl"),
   625  					testAccCheckComputeV2InstanceNoMetadataKey(&instance, "abc"),
   626  					resource.TestCheckResourceAttr(
   627  						"openstack_compute_instance_v2.instance_1", "all_metadata.foo", "bar"),
   628  					resource.TestCheckResourceAttr(
   629  						"openstack_compute_instance_v2.instance_1", "all_metadata.ghi", "jkl"),
   630  				),
   631  			},
   632  		},
   633  	})
   634  }
   635  
   636  func TestAccComputeV2Instance_forceDelete(t *testing.T) {
   637  	var instance servers.Server
   638  	resource.Test(t, resource.TestCase{
   639  		PreCheck:     func() { testAccPreCheck(t) },
   640  		Providers:    testAccProviders,
   641  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   642  		Steps: []resource.TestStep{
   643  			resource.TestStep{
   644  				Config: testAccComputeV2Instance_forceDelete,
   645  				Check: resource.ComposeTestCheckFunc(
   646  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   647  				),
   648  			},
   649  		},
   650  	})
   651  }
   652  
   653  func TestAccComputeV2Instance_timeout(t *testing.T) {
   654  	var instance servers.Server
   655  	resource.Test(t, resource.TestCase{
   656  		PreCheck:     func() { testAccPreCheck(t) },
   657  		Providers:    testAccProviders,
   658  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   659  		Steps: []resource.TestStep{
   660  			resource.TestStep{
   661  				Config: testAccComputeV2Instance_timeout,
   662  				Check: resource.ComposeTestCheckFunc(
   663  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   664  				),
   665  			},
   666  		},
   667  	})
   668  }
   669  
   670  func TestAccComputeV2Instance_networkNameToID(t *testing.T) {
   671  	var instance servers.Server
   672  	var network networks.Network
   673  	resource.Test(t, resource.TestCase{
   674  		PreCheck:     func() { testAccPreCheck(t) },
   675  		Providers:    testAccProviders,
   676  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   677  		Steps: []resource.TestStep{
   678  			resource.TestStep{
   679  				Config: testAccComputeV2Instance_networkNameToID,
   680  				Check: resource.ComposeTestCheckFunc(
   681  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   682  					testAccCheckNetworkingV2NetworkExists("openstack_networking_network_v2.network_1", &network),
   683  					resource.TestCheckResourceAttrPtr(
   684  						"openstack_compute_instance_v2.instance_1", "network.1.uuid", &network.ID),
   685  				),
   686  			},
   687  		},
   688  	})
   689  }
   690  
   691  func testAccCheckComputeV2InstanceDestroy(s *terraform.State) error {
   692  	config := testAccProvider.Meta().(*Config)
   693  	computeClient, err := config.computeV2Client(OS_REGION_NAME)
   694  	if err != nil {
   695  		return fmt.Errorf("Error creating OpenStack compute client: %s", err)
   696  	}
   697  
   698  	for _, rs := range s.RootModule().Resources {
   699  		if rs.Type != "openstack_compute_instance_v2" {
   700  			continue
   701  		}
   702  
   703  		server, err := servers.Get(computeClient, rs.Primary.ID).Extract()
   704  		if err == nil {
   705  			if server.Status != "SOFT_DELETED" {
   706  				return fmt.Errorf("Instance still exists")
   707  			}
   708  		}
   709  	}
   710  
   711  	return nil
   712  }
   713  
   714  func testAccCheckComputeV2InstanceExists(n string, instance *servers.Server) resource.TestCheckFunc {
   715  	return func(s *terraform.State) error {
   716  		rs, ok := s.RootModule().Resources[n]
   717  		if !ok {
   718  			return fmt.Errorf("Not found: %s", n)
   719  		}
   720  
   721  		if rs.Primary.ID == "" {
   722  			return fmt.Errorf("No ID is set")
   723  		}
   724  
   725  		config := testAccProvider.Meta().(*Config)
   726  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   727  		if err != nil {
   728  			return fmt.Errorf("Error creating OpenStack compute client: %s", err)
   729  		}
   730  
   731  		found, err := servers.Get(computeClient, rs.Primary.ID).Extract()
   732  		if err != nil {
   733  			return err
   734  		}
   735  
   736  		if found.ID != rs.Primary.ID {
   737  			return fmt.Errorf("Instance not found")
   738  		}
   739  
   740  		*instance = *found
   741  
   742  		return nil
   743  	}
   744  }
   745  
   746  func testAccCheckComputeV2InstanceDoesNotExist(n string, instance *servers.Server) resource.TestCheckFunc {
   747  	return func(s *terraform.State) error {
   748  		config := testAccProvider.Meta().(*Config)
   749  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   750  		if err != nil {
   751  			return fmt.Errorf("Error creating OpenStack compute client: %s", err)
   752  		}
   753  
   754  		_, err = servers.Get(computeClient, instance.ID).Extract()
   755  		if err != nil {
   756  			if _, ok := err.(gophercloud.ErrDefault404); ok {
   757  				return nil
   758  			}
   759  			return err
   760  		}
   761  
   762  		return fmt.Errorf("Instance still exists")
   763  	}
   764  }
   765  
   766  func testAccCheckComputeV2InstanceMetadata(
   767  	instance *servers.Server, k string, v string) resource.TestCheckFunc {
   768  	return func(s *terraform.State) error {
   769  		if instance.Metadata == nil {
   770  			return fmt.Errorf("No metadata")
   771  		}
   772  
   773  		for key, value := range instance.Metadata {
   774  			if k != key {
   775  				continue
   776  			}
   777  
   778  			if v == value {
   779  				return nil
   780  			}
   781  
   782  			return fmt.Errorf("Bad value for %s: %s", k, value)
   783  		}
   784  
   785  		return fmt.Errorf("Metadata not found: %s", k)
   786  	}
   787  }
   788  
   789  func testAccCheckComputeV2InstanceNoMetadataKey(
   790  	instance *servers.Server, k string) resource.TestCheckFunc {
   791  	return func(s *terraform.State) error {
   792  		if instance.Metadata == nil {
   793  			return nil
   794  		}
   795  
   796  		for key, _ := range instance.Metadata {
   797  			if k == key {
   798  				return fmt.Errorf("Metadata found: %s", k)
   799  			}
   800  		}
   801  
   802  		return nil
   803  	}
   804  }
   805  
   806  func testAccCheckComputeV2InstanceVolumeAttachment(
   807  	instance *servers.Server, volume *volumes.Volume) resource.TestCheckFunc {
   808  	return func(s *terraform.State) error {
   809  		var attachments []volumeattach.VolumeAttachment
   810  
   811  		config := testAccProvider.Meta().(*Config)
   812  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   813  		if err != nil {
   814  			return err
   815  		}
   816  
   817  		err = volumeattach.List(computeClient, instance.ID).EachPage(
   818  			func(page pagination.Page) (bool, error) {
   819  
   820  				actual, err := volumeattach.ExtractVolumeAttachments(page)
   821  				if err != nil {
   822  					return false, fmt.Errorf("Unable to lookup attachment: %s", err)
   823  				}
   824  
   825  				attachments = actual
   826  				return true, nil
   827  			})
   828  
   829  		for _, attachment := range attachments {
   830  			if attachment.VolumeID == volume.ID {
   831  				return nil
   832  			}
   833  		}
   834  
   835  		return fmt.Errorf("Volume not found: %s", volume.ID)
   836  	}
   837  }
   838  
   839  func testAccCheckComputeV2InstanceVolumesDetached(instance *servers.Server) resource.TestCheckFunc {
   840  	return func(s *terraform.State) error {
   841  		var attachments []volumeattach.VolumeAttachment
   842  
   843  		config := testAccProvider.Meta().(*Config)
   844  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   845  		if err != nil {
   846  			return err
   847  		}
   848  
   849  		err = volumeattach.List(computeClient, instance.ID).EachPage(
   850  			func(page pagination.Page) (bool, error) {
   851  
   852  				actual, err := volumeattach.ExtractVolumeAttachments(page)
   853  				if err != nil {
   854  					return false, fmt.Errorf("Unable to lookup attachment: %s", err)
   855  				}
   856  
   857  				attachments = actual
   858  				return true, nil
   859  			})
   860  
   861  		if len(attachments) > 0 {
   862  			return fmt.Errorf("Volumes are still attached.")
   863  		}
   864  
   865  		return nil
   866  	}
   867  }
   868  
   869  func testAccCheckComputeV2InstanceBootVolumeAttachment(
   870  	instance *servers.Server) resource.TestCheckFunc {
   871  	return func(s *terraform.State) error {
   872  		var attachments []volumeattach.VolumeAttachment
   873  
   874  		config := testAccProvider.Meta().(*Config)
   875  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   876  		if err != nil {
   877  			return err
   878  		}
   879  
   880  		err = volumeattach.List(computeClient, instance.ID).EachPage(
   881  			func(page pagination.Page) (bool, error) {
   882  
   883  				actual, err := volumeattach.ExtractVolumeAttachments(page)
   884  				if err != nil {
   885  					return false, fmt.Errorf("Unable to lookup attachment: %s", err)
   886  				}
   887  
   888  				attachments = actual
   889  				return true, nil
   890  			})
   891  
   892  		if len(attachments) == 1 {
   893  			return nil
   894  		}
   895  
   896  		return fmt.Errorf("No attached volume found.")
   897  	}
   898  }
   899  
   900  func testAccCheckComputeV2InstanceFloatingIPAttach(
   901  	instance *servers.Server, fip *floatingips.FloatingIP) resource.TestCheckFunc {
   902  	return func(s *terraform.State) error {
   903  		if fip.InstanceID == instance.ID {
   904  			return nil
   905  		}
   906  
   907  		return fmt.Errorf("Floating IP %s was not attached to instance %s", fip.ID, instance.ID)
   908  	}
   909  }
   910  
   911  func testAccCheckComputeV2InstanceInstanceIDsDoNotMatch(
   912  	instance1, instance2 *servers.Server) resource.TestCheckFunc {
   913  	return func(s *terraform.State) error {
   914  		if instance1.ID == instance2.ID {
   915  			return fmt.Errorf("Instance was not recreated.")
   916  		}
   917  
   918  		return nil
   919  	}
   920  }
   921  
   922  func testAccCheckComputeV2InstanceVolumeDetached(instance *servers.Server, volume_id string) resource.TestCheckFunc {
   923  	return func(s *terraform.State) error {
   924  		var attachments []volumeattach.VolumeAttachment
   925  
   926  		rs, ok := s.RootModule().Resources[volume_id]
   927  		if !ok {
   928  			return fmt.Errorf("Not found: %s", volume_id)
   929  		}
   930  
   931  		if rs.Primary.ID == "" {
   932  			return fmt.Errorf("No ID is set")
   933  		}
   934  
   935  		config := testAccProvider.Meta().(*Config)
   936  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   937  		if err != nil {
   938  			return err
   939  		}
   940  
   941  		err = volumeattach.List(computeClient, instance.ID).EachPage(
   942  			func(page pagination.Page) (bool, error) {
   943  				actual, err := volumeattach.ExtractVolumeAttachments(page)
   944  				if err != nil {
   945  					return false, fmt.Errorf("Unable to lookup attachment: %s", err)
   946  				}
   947  
   948  				attachments = actual
   949  				return true, nil
   950  			})
   951  
   952  		for _, attachment := range attachments {
   953  			if attachment.VolumeID == rs.Primary.ID {
   954  				return fmt.Errorf("Volume is still attached.")
   955  			}
   956  		}
   957  
   958  		return nil
   959  	}
   960  }
   961  
   962  const testAccComputeV2Instance_basic = `
   963  resource "openstack_compute_instance_v2" "instance_1" {
   964    name = "instance_1"
   965    security_groups = ["default"]
   966    metadata {
   967      foo = "bar"
   968    }
   969  }
   970  `
   971  
   972  const testAccComputeV2Instance_volumeAttach = `
   973  resource "openstack_blockstorage_volume_v1" "vol_1" {
   974    name = "vol_1"
   975   	size = 1
   976  }
   977  
   978  resource "openstack_compute_instance_v2" "instance_1" {
   979    name = "instance_1"
   980    security_groups = ["default"]
   981    volume {
   982      volume_id = "${openstack_blockstorage_volume_v1.vol_1.id}"
   983    }
   984  }
   985  `
   986  
   987  const testAccComputeV2Instance_volumeAttachPostCreation_1 = `
   988  resource "openstack_compute_instance_v2" "instance_1" {
   989  	name = "instance_1"
   990  	security_groups = ["default"]
   991  }
   992  `
   993  
   994  const testAccComputeV2Instance_volumeAttachPostCreation_2 = `
   995  resource "openstack_blockstorage_volume_v1" "vol_1" {
   996    name = "vol_1"
   997    size = 1
   998  }
   999  
  1000  resource "openstack_compute_instance_v2" "instance_1" {
  1001    name = "instance_1"
  1002    security_groups = ["default"]
  1003    volume {
  1004     volume_id = "${openstack_blockstorage_volume_v1.vol_1.id}"
  1005    }
  1006  }
  1007  `
  1008  
  1009  const testAccComputeV2Instance_volumeDetachPostCreation_1 = `
  1010  resource "openstack_blockstorage_volume_v1" "vol_1" {
  1011    name = "vol_1"
  1012    size = 1
  1013  }
  1014  
  1015  resource "openstack_compute_instance_v2" "instance_1" {
  1016    name = "instance_1"
  1017    security_groups = ["default"]
  1018    volume {
  1019      volume_id = "${openstack_blockstorage_volume_v1.vol_1.id}"
  1020    }
  1021  }
  1022  `
  1023  
  1024  const testAccComputeV2Instance_volumeDetachPostCreation_2 = `
  1025  resource "openstack_blockstorage_volume_v1" "vol_1" {
  1026    name = "vol_1"
  1027    size = 1
  1028  }
  1029  
  1030  resource "openstack_compute_instance_v2" "instance_1" {
  1031    name = "instance_1"
  1032    security_groups = ["default"]
  1033  }
  1034  `
  1035  
  1036  var testAccComputeV2Instance_volumeDetachAdditionalVolumePostCreation_1 = fmt.Sprintf(`
  1037  resource "openstack_blockstorage_volume_v1" "root_volume" {
  1038    name = "root_volume"
  1039    size = 1
  1040    image_id = "%s"
  1041  }
  1042  
  1043  resource "openstack_blockstorage_volume_v1" "additional_volume" {
  1044    name = "additional_volume"
  1045    size = 1
  1046  }
  1047  
  1048  resource "openstack_compute_instance_v2" "instance_1" {
  1049    name = "instance_1"
  1050    security_groups = ["default"]
  1051  
  1052    block_device {
  1053      uuid = "${openstack_blockstorage_volume_v1.root_volume.id}"
  1054      source_type = "volume"
  1055      boot_index = 0
  1056      destination_type = "volume"
  1057      delete_on_termination = false
  1058    }
  1059  
  1060    volume {
  1061      volume_id = "${openstack_blockstorage_volume_v1.additional_volume.id}"
  1062  	}
  1063  }
  1064  `, OS_IMAGE_ID)
  1065  
  1066  var testAccComputeV2Instance_volumeDetachAdditionalVolumePostCreation_2 = fmt.Sprintf(`
  1067  resource "openstack_blockstorage_volume_v1" "root_volume" {
  1068    name = "root_volume"
  1069    size = 1
  1070    image_id = "%s"
  1071  }
  1072  
  1073  resource "openstack_blockstorage_volume_v1" "additional_volume" {
  1074    name = "additional_volume"
  1075    size = 1
  1076  }
  1077  
  1078  resource "openstack_compute_instance_v2" "instance_1" {
  1079    name = "instance_1"
  1080    security_groups = ["default"]
  1081  
  1082    block_device {
  1083      uuid = "${openstack_blockstorage_volume_v1.root_volume.id}"
  1084      source_type = "volume"
  1085      boot_index = 0
  1086      destination_type = "volume"
  1087      delete_on_termination = false
  1088    }
  1089  }
  1090  `, OS_IMAGE_ID)
  1091  
  1092  var testAccComputeV2Instance_volumeAttachInstanceDelete_1 = fmt.Sprintf(`
  1093  resource "openstack_blockstorage_volume_v1" "root_volume" {
  1094    name = "root_volume"
  1095    size = 1
  1096    image_id = "%s"
  1097  }
  1098  
  1099  resource "openstack_blockstorage_volume_v1" "additional_volume" {
  1100    name = "additional_volume"
  1101    size = 1
  1102  }
  1103  
  1104  resource "openstack_compute_instance_v2" "instance_1" {
  1105    name = "instance_1"
  1106    security_groups = ["default"]
  1107  
  1108    block_device {
  1109      uuid = "${openstack_blockstorage_volume_v1.root_volume.id}"
  1110      source_type = "volume"
  1111      boot_index = 0
  1112      destination_type = "volume"
  1113      delete_on_termination = false
  1114    }
  1115  
  1116    volume {
  1117      volume_id = "${openstack_blockstorage_volume_v1.additional_volume.id}"
  1118    }
  1119  }
  1120  `, OS_IMAGE_ID)
  1121  
  1122  var testAccComputeV2Instance_volumeAttachInstanceDelete_2 = fmt.Sprintf(`
  1123  resource "openstack_blockstorage_volume_v1" "root_volume" {
  1124    name = "root_volume"
  1125    size = 1
  1126    image_id = "%s"
  1127  }
  1128  
  1129  resource "openstack_blockstorage_volume_v1" "additional_volume" {
  1130    name = "additional_volume"
  1131    size = 1
  1132  }
  1133  `, OS_IMAGE_ID)
  1134  
  1135  const testAccComputeV2Instance_volumeAttachToNewInstance_1 = `
  1136  resource "openstack_blockstorage_volume_v1" "volume_1" {
  1137    name = "volume_1"
  1138    size = 1
  1139  }
  1140  
  1141  resource "openstack_compute_instance_v2" "instance_1" {
  1142    name = "instance_1"
  1143    security_groups = ["default"]
  1144  
  1145    volume {
  1146      volume_id = "${openstack_blockstorage_volume_v1.volume_1.id}"
  1147    }
  1148  }
  1149  
  1150  resource "openstack_compute_instance_v2" "instance_2" {
  1151    depends_on = ["openstack_compute_instance_v2.instance_1"]
  1152    name = "instance_2"
  1153    security_groups = ["default"]
  1154  }
  1155  `
  1156  
  1157  const testAccComputeV2Instance_volumeAttachToNewInstance_2 = `
  1158  resource "openstack_blockstorage_volume_v1" "volume_1" {
  1159    name = "volume_1"
  1160    size = 1
  1161  }
  1162  
  1163  resource "openstack_compute_instance_v2" "instance_1" {
  1164    name = "instance_1"
  1165    security_groups = ["default"]
  1166  }
  1167  
  1168  resource "openstack_compute_instance_v2" "instance_2" {
  1169    depends_on = ["openstack_compute_instance_v2.instance_1"]
  1170    name = "instance_2"
  1171    security_groups = ["default"]
  1172  
  1173    volume {
  1174      volume_id = "${openstack_blockstorage_volume_v1.volume_1.id}"
  1175    }
  1176  }
  1177  	`
  1178  
  1179  const testAccComputeV2Instance_floatingIPAttachGlobally = `
  1180  resource "openstack_compute_floatingip_v2" "fip_1" {
  1181  }
  1182  
  1183  resource "openstack_compute_instance_v2" "instance_1" {
  1184    name = "instance_1"
  1185    security_groups = ["default"]
  1186    floating_ip = "${openstack_compute_floatingip_v2.fip_1.address}"
  1187  }
  1188  `
  1189  
  1190  var testAccComputeV2Instance_floatingIPAttachToNetwork = fmt.Sprintf(`
  1191  resource "openstack_compute_floatingip_v2" "fip_1" {
  1192  }
  1193  
  1194  resource "openstack_compute_instance_v2" "instance_1" {
  1195    name = "instance_1"
  1196    security_groups = ["default"]
  1197  
  1198    network {
  1199      uuid = "%s"
  1200      floating_ip = "${openstack_compute_floatingip_v2.fip_1.address}"
  1201      access_network = true
  1202    }
  1203  }
  1204  `, OS_NETWORK_ID)
  1205  
  1206  var testAccComputeV2Instance_floatingIPAttachToNetworkAndChange_1 = fmt.Sprintf(`
  1207  resource "openstack_compute_floatingip_v2" "fip_1" {
  1208  }
  1209  
  1210  resource "openstack_compute_floatingip_v2" "fip_2" {
  1211  }
  1212  
  1213  resource "openstack_compute_instance_v2" "instance_1" {
  1214    name = "instance_1"
  1215    security_groups = ["default"]
  1216  
  1217    network {
  1218      uuid = "%s"
  1219      floating_ip = "${openstack_compute_floatingip_v2.fip_1.address}"
  1220      access_network = true
  1221    }
  1222  }
  1223  `, OS_NETWORK_ID)
  1224  
  1225  var testAccComputeV2Instance_floatingIPAttachToNetworkAndChange_2 = fmt.Sprintf(`
  1226  resource "openstack_compute_floatingip_v2" "fip_1" {
  1227  }
  1228  
  1229  resource "openstack_compute_floatingip_v2" "fip_2" {
  1230  }
  1231  
  1232  resource "openstack_compute_instance_v2" "instance_1" {
  1233    name = "instance_1"
  1234    security_groups = ["default"]
  1235  
  1236    network {
  1237      uuid = "%s"
  1238      floating_ip = "${openstack_compute_floatingip_v2.fip_2.address}"
  1239      access_network = true
  1240    }
  1241  }
  1242  `, OS_NETWORK_ID)
  1243  
  1244  const testAccComputeV2Instance_secgroupMulti = `
  1245  resource "openstack_compute_secgroup_v2" "secgroup_1" {
  1246    name = "secgroup_1"
  1247    description = "a security group"
  1248    rule {
  1249      from_port = 22
  1250      to_port = 22
  1251      ip_protocol = "tcp"
  1252      cidr = "0.0.0.0/0"
  1253    }
  1254  }
  1255  
  1256  resource "openstack_compute_instance_v2" "instance_1" {
  1257    name = "instance_1"
  1258    security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"]
  1259  }
  1260  `
  1261  
  1262  const testAccComputeV2Instance_secgroupMultiUpdate_1 = `
  1263  resource "openstack_compute_secgroup_v2" "secgroup_1" {
  1264    name = "secgroup_1"
  1265    description = "a security group"
  1266    rule {
  1267      from_port = 22
  1268      to_port = 22
  1269      ip_protocol = "tcp"
  1270      cidr = "0.0.0.0/0"
  1271    }
  1272  }
  1273  
  1274  resource "openstack_compute_secgroup_v2" "secgroup_2" {
  1275    name = "secgroup_2"
  1276    description = "another security group"
  1277    rule {
  1278      from_port = 80
  1279      to_port = 80
  1280      ip_protocol = "tcp"
  1281      cidr = "0.0.0.0/0"
  1282    }
  1283  }
  1284  
  1285  resource "openstack_compute_instance_v2" "instance_1" {
  1286    name = "instance_1"
  1287    security_groups = ["default"]
  1288  }
  1289  `
  1290  
  1291  const testAccComputeV2Instance_secgroupMultiUpdate_2 = `
  1292  resource "openstack_compute_secgroup_v2" "secgroup_1" {
  1293    name = "secgroup_1"
  1294    description = "a security group"
  1295    rule {
  1296      from_port = 22
  1297      to_port = 22
  1298      ip_protocol = "tcp"
  1299      cidr = "0.0.0.0/0"
  1300    }
  1301  }
  1302  
  1303  resource "openstack_compute_secgroup_v2" "secgroup_2" {
  1304    name = "secgroup_2"
  1305    description = "another security group"
  1306    rule {
  1307      from_port = 80
  1308      to_port = 80
  1309      ip_protocol = "tcp"
  1310      cidr = "0.0.0.0/0"
  1311    }
  1312  }
  1313  
  1314  resource "openstack_compute_instance_v2" "instance_1" {
  1315    name = "instance_1"
  1316    security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}", "${openstack_compute_secgroup_v2.secgroup_2.name}"]
  1317  }
  1318  `
  1319  
  1320  var testAccComputeV2Instance_bootFromVolumeImage = fmt.Sprintf(`
  1321  resource "openstack_compute_instance_v2" "instance_1" {
  1322    name = "instance_1"
  1323    security_groups = ["default"]
  1324    block_device {
  1325      uuid = "%s"
  1326      source_type = "image"
  1327      volume_size = 5
  1328      boot_index = 0
  1329      destination_type = "volume"
  1330      delete_on_termination = true
  1331    }
  1332  }
  1333  `, OS_IMAGE_ID)
  1334  
  1335  var testAccComputeV2Instance_bootFromVolumeImageWithAttachedVolume = fmt.Sprintf(`
  1336  resource "openstack_blockstorage_volume_v1" "volume_1" {
  1337    name = "volume_1"
  1338    size = 1
  1339  }
  1340  
  1341  resource "openstack_compute_instance_v2" "instance_1" {
  1342    name = "instance_1"
  1343    security_groups = ["default"]
  1344    block_device {
  1345      uuid = "%s"
  1346      source_type = "image"
  1347      volume_size = 2
  1348      boot_index = 0
  1349      destination_type = "volume"
  1350      delete_on_termination = true
  1351    }
  1352  
  1353    volume {
  1354      volume_id = "${openstack_blockstorage_volume_v1.volume_1.id}"
  1355    }
  1356  }
  1357  `, OS_IMAGE_ID)
  1358  
  1359  var testAccComputeV2Instance_bootFromVolumeVolume = fmt.Sprintf(`
  1360  resource "openstack_blockstorage_volume_v1" "vol_1" {
  1361    name = "vol_1"
  1362    size = 5
  1363    image_id = "%s"
  1364  }
  1365  
  1366  resource "openstack_compute_instance_v2" "instance_1" {
  1367    name = "instance_1"
  1368    security_groups = ["default"]
  1369    block_device {
  1370      uuid = "${openstack_blockstorage_volume_v1.vol_1.id}"
  1371      source_type = "volume"
  1372      boot_index = 0
  1373      destination_type = "volume"
  1374      delete_on_termination = true
  1375    }
  1376  }
  1377  `, OS_IMAGE_ID)
  1378  
  1379  var testAccComputeV2Instance_bootFromVolumeForceNew_1 = fmt.Sprintf(`
  1380  resource "openstack_compute_instance_v2" "instance_1" {
  1381    name = "instance_1"
  1382    security_groups = ["default"]
  1383    block_device {
  1384      uuid = "%s"
  1385      source_type = "image"
  1386      volume_size = 5
  1387      boot_index = 0
  1388      destination_type = "volume"
  1389      delete_on_termination = true
  1390    }
  1391  }
  1392  `, OS_IMAGE_ID)
  1393  
  1394  var testAccComputeV2Instance_bootFromVolumeForceNew_2 = fmt.Sprintf(`
  1395  resource "openstack_compute_instance_v2" "instance_1" {
  1396    name = "instance_1"
  1397    security_groups = ["default"]
  1398    block_device {
  1399      uuid = "%s"
  1400      source_type = "image"
  1401      volume_size = 4
  1402      boot_index = 0
  1403      destination_type = "volume"
  1404      delete_on_termination = true
  1405    }
  1406  }
  1407  `, OS_IMAGE_ID)
  1408  
  1409  var testAccComputeV2Instance_blockDeviceNewVolume = fmt.Sprintf(`
  1410  resource "openstack_compute_instance_v2" "instance_1" {
  1411    name = "instance_1"
  1412    security_groups = ["default"]
  1413    block_device {
  1414      uuid = "%s"
  1415      source_type = "image"
  1416      destination_type = "local"
  1417      boot_index = 0
  1418      delete_on_termination = true
  1419    }
  1420    block_device {
  1421      source_type = "blank"
  1422      destination_type = "volume"
  1423      volume_size = 1
  1424      boot_index = 1
  1425      delete_on_termination = true
  1426    }
  1427  }
  1428  `, OS_IMAGE_ID)
  1429  
  1430  var testAccComputeV2Instance_blockDeviceExistingVolume = fmt.Sprintf(`
  1431  resource "openstack_blockstorage_volume_v1" "volume_1" {
  1432    name = "volume_1"
  1433    size = 1
  1434  }
  1435  
  1436  resource "openstack_compute_instance_v2" "instance_1" {
  1437    name = "instance_1"
  1438    security_groups = ["default"]
  1439    block_device {
  1440      uuid = "%s"
  1441      source_type = "image"
  1442      destination_type = "local"
  1443      boot_index = 0
  1444      delete_on_termination = true
  1445    }
  1446    block_device {
  1447      uuid = "${openstack_blockstorage_volume_v1.volume_1.id}"
  1448      source_type = "volume"
  1449      destination_type = "volume"
  1450      boot_index = 1
  1451      delete_on_termination = true
  1452    }
  1453  }
  1454  `, OS_IMAGE_ID)
  1455  
  1456  const testAccComputeV2Instance_personality = `
  1457  resource "openstack_compute_instance_v2" "instance_1" {
  1458    name = "instance_1"
  1459    security_groups = ["default"]
  1460    personality {
  1461      file = "/tmp/foobar.txt"
  1462      content = "happy"
  1463    }
  1464    personality {
  1465      file = "/tmp/barfoo.txt"
  1466      content = "angry"
  1467    }
  1468  }
  1469  `
  1470  
  1471  var testAccComputeV2Instance_multiEphemeral = fmt.Sprintf(`
  1472  resource "openstack_compute_instance_v2" "instance_1" {
  1473    name = "terraform-test"
  1474    security_groups = ["default"]
  1475    block_device {
  1476      boot_index = 0
  1477      delete_on_termination = true
  1478      destination_type = "local"
  1479      source_type = "image"
  1480      uuid = "%s"
  1481    }
  1482    block_device {
  1483      boot_index = -1
  1484      delete_on_termination = true
  1485      destination_type = "local"
  1486      source_type = "blank"
  1487      volume_size = 1
  1488    }
  1489    block_device {
  1490      boot_index = -1
  1491      delete_on_termination = true
  1492      destination_type = "local"
  1493      source_type = "blank"
  1494      volume_size = 1
  1495    }
  1496  }
  1497  `, OS_IMAGE_ID)
  1498  
  1499  var testAccComputeV2Instance_accessIPv4 = fmt.Sprintf(`
  1500  resource "openstack_compute_floatingip_v2" "myip" {
  1501  }
  1502  
  1503  resource "openstack_networking_network_v2" "network_1" {
  1504    name = "network_1"
  1505  }
  1506  
  1507  resource "openstack_networking_subnet_v2" "subnet_1" {
  1508    name = "subnet_1"
  1509    network_id = "${openstack_networking_network_v2.network_1.id}"
  1510    cidr = "192.168.1.0/24"
  1511    ip_version = 4
  1512    enable_dhcp = true
  1513    no_gateway = true
  1514  }
  1515  
  1516  resource "openstack_compute_instance_v2" "instance_1" {
  1517    depends_on = ["openstack_networking_subnet_v2.subnet_1"]
  1518  
  1519    name = "instance_1"
  1520    security_groups = ["default"]
  1521    floating_ip = "${openstack_compute_floatingip_v2.myip.address}"
  1522  
  1523    network {
  1524      uuid = "%s"
  1525    }
  1526  
  1527    network {
  1528      uuid = "${openstack_networking_network_v2.network_1.id}"
  1529      fixed_ip_v4 = "192.168.1.100"
  1530      access_network = true
  1531    }
  1532  }
  1533  `, OS_NETWORK_ID)
  1534  
  1535  var testAccComputeV2Instance_changeFixedIP_1 = fmt.Sprintf(`
  1536  resource "openstack_compute_instance_v2" "instance_1" {
  1537    name = "instance_1"
  1538    security_groups = ["default"]
  1539    network {
  1540      uuid = "%s"
  1541      fixed_ip_v4 = "10.0.0.24"
  1542    }
  1543  }
  1544  `, OS_NETWORK_ID)
  1545  
  1546  var testAccComputeV2Instance_changeFixedIP_2 = fmt.Sprintf(`
  1547  resource "openstack_compute_instance_v2" "instance_1" {
  1548    name = "instance_1"
  1549    security_groups = ["default"]
  1550    network {
  1551      uuid = "%s"
  1552      fixed_ip_v4 = "10.0.0.25"
  1553    }
  1554  }
  1555  `, OS_NETWORK_ID)
  1556  
  1557  const testAccComputeV2Instance_stopBeforeDestroy = `
  1558  resource "openstack_compute_instance_v2" "instance_1" {
  1559    name = "instance_1"
  1560    security_groups = ["default"]
  1561    stop_before_destroy = true
  1562  }
  1563  `
  1564  
  1565  const testAccComputeV2Instance_metadataRemove_1 = `
  1566  resource "openstack_compute_instance_v2" "instance_1" {
  1567    name = "instance_1"
  1568    security_groups = ["default"]
  1569    metadata {
  1570      foo = "bar"
  1571      abc = "def"
  1572    }
  1573  }
  1574  `
  1575  
  1576  const testAccComputeV2Instance_metadataRemove_2 = `
  1577  resource "openstack_compute_instance_v2" "instance_1" {
  1578    name = "instance_1"
  1579    security_groups = ["default"]
  1580    metadata {
  1581      foo = "bar"
  1582      ghi = "jkl"
  1583    }
  1584  }
  1585  `
  1586  
  1587  const testAccComputeV2Instance_forceDelete = `
  1588  resource "openstack_compute_instance_v2" "instance_1" {
  1589    name = "instance_1"
  1590    security_groups = ["default"]
  1591    force_delete = true
  1592  }
  1593  `
  1594  
  1595  const testAccComputeV2Instance_timeout = `
  1596  resource "openstack_compute_instance_v2" "instance_1" {
  1597    name = "instance_1"
  1598    security_groups = ["default"]
  1599  
  1600    timeouts {
  1601      create = "10m"
  1602    }
  1603  }
  1604  `
  1605  
  1606  var testAccComputeV2Instance_networkNameToID = fmt.Sprintf(`
  1607  resource "openstack_networking_network_v2" "network_1" {
  1608    name = "network_1"
  1609  }
  1610  
  1611  resource "openstack_networking_subnet_v2" "subnet_1" {
  1612    name = "subnet_1"
  1613    network_id = "${openstack_networking_network_v2.network_1.id}"
  1614    cidr = "192.168.1.0/24"
  1615    ip_version = 4
  1616    enable_dhcp = true
  1617    no_gateway = true
  1618  }
  1619  
  1620  resource "openstack_compute_instance_v2" "instance_1" {
  1621    depends_on = ["openstack_networking_subnet_v2.subnet_1"]
  1622  
  1623    name = "instance_1"
  1624    security_groups = ["default"]
  1625  
  1626    network {
  1627      uuid = "%s"
  1628    }
  1629  
  1630    network {
  1631      name = "${openstack_networking_network_v2.network_1.name}"
  1632    }
  1633  
  1634  }
  1635  `, OS_NETWORK_ID)