github.com/anfernee/terraform@v0.6.16-0.20160430000239-06e5085a92f2/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go (about)

     1  package vsphere
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/vmware/govmomi"
    11  	"github.com/vmware/govmomi/find"
    12  	"github.com/vmware/govmomi/object"
    13  	"github.com/vmware/govmomi/property"
    14  	"github.com/vmware/govmomi/vim25/mo"
    15  	"github.com/vmware/govmomi/vim25/types"
    16  	"golang.org/x/net/context"
    17  )
    18  
    19  func TestAccVSphereVirtualMachine_basic(t *testing.T) {
    20  	var vm virtualMachine
    21  	var locationOpt string
    22  	var datastoreOpt string
    23  
    24  	if v := os.Getenv("VSPHERE_DATACENTER"); v != "" {
    25  		locationOpt += fmt.Sprintf("    datacenter = \"%s\"\n", v)
    26  	}
    27  	if v := os.Getenv("VSPHERE_CLUSTER"); v != "" {
    28  		locationOpt += fmt.Sprintf("    cluster = \"%s\"\n", v)
    29  	}
    30  	if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" {
    31  		locationOpt += fmt.Sprintf("    resource_pool = \"%s\"\n", v)
    32  	}
    33  	if v := os.Getenv("VSPHERE_DATASTORE"); v != "" {
    34  		datastoreOpt = fmt.Sprintf("        datastore = \"%s\"\n", v)
    35  	}
    36  	template := os.Getenv("VSPHERE_TEMPLATE")
    37  	gateway := os.Getenv("VSPHERE_NETWORK_GATEWAY")
    38  	label := os.Getenv("VSPHERE_NETWORK_LABEL")
    39  	ip_address := os.Getenv("VSPHERE_NETWORK_IP_ADDRESS")
    40  
    41  	resource.Test(t, resource.TestCase{
    42  		PreCheck:     func() { testAccPreCheck(t) },
    43  		Providers:    testAccProviders,
    44  		CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
    45  		Steps: []resource.TestStep{
    46  			resource.TestStep{
    47  				Config: fmt.Sprintf(
    48  					testAccCheckVSphereVirtualMachineConfig_basic,
    49  					locationOpt,
    50  					gateway,
    51  					label,
    52  					ip_address,
    53  					datastoreOpt,
    54  					template,
    55  				),
    56  				Check: resource.ComposeTestCheckFunc(
    57  					testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.foo", &vm),
    58  					resource.TestCheckResourceAttr(
    59  						"vsphere_virtual_machine.foo", "name", "terraform-test"),
    60  					resource.TestCheckResourceAttr(
    61  						"vsphere_virtual_machine.foo", "vcpu", "2"),
    62  					resource.TestCheckResourceAttr(
    63  						"vsphere_virtual_machine.foo", "memory", "4096"),
    64  					resource.TestCheckResourceAttr(
    65  						"vsphere_virtual_machine.foo", "memory_reservation", "4096"),
    66  					resource.TestCheckResourceAttr(
    67  						"vsphere_virtual_machine.foo", "disk.#", "2"),
    68  					resource.TestCheckResourceAttr(
    69  						"vsphere_virtual_machine.foo", "disk.0.template", template),
    70  					resource.TestCheckResourceAttr(
    71  						"vsphere_virtual_machine.foo", "network_interface.#", "1"),
    72  					resource.TestCheckResourceAttr(
    73  						"vsphere_virtual_machine.foo", "network_interface.0.label", label),
    74  				),
    75  			},
    76  		},
    77  	})
    78  }
    79  
    80  func TestAccVSphereVirtualMachine_diskInitType(t *testing.T) {
    81  	var vm virtualMachine
    82  	var locationOpt string
    83  	var datastoreOpt string
    84  
    85  	if v := os.Getenv("VSPHERE_DATACENTER"); v != "" {
    86  		locationOpt += fmt.Sprintf("    datacenter = \"%s\"\n", v)
    87  	}
    88  	if v := os.Getenv("VSPHERE_CLUSTER"); v != "" {
    89  		locationOpt += fmt.Sprintf("    cluster = \"%s\"\n", v)
    90  	}
    91  	if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" {
    92  		locationOpt += fmt.Sprintf("    resource_pool = \"%s\"\n", v)
    93  	}
    94  	if v := os.Getenv("VSPHERE_DATASTORE"); v != "" {
    95  		datastoreOpt = fmt.Sprintf("        datastore = \"%s\"\n", v)
    96  	}
    97  	template := os.Getenv("VSPHERE_TEMPLATE")
    98  	gateway := os.Getenv("VSPHERE_NETWORK_GATEWAY")
    99  	label := os.Getenv("VSPHERE_NETWORK_LABEL")
   100  	ip_address := os.Getenv("VSPHERE_NETWORK_IP_ADDRESS")
   101  
   102  	resource.Test(t, resource.TestCase{
   103  		PreCheck:     func() { testAccPreCheck(t) },
   104  		Providers:    testAccProviders,
   105  		CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
   106  		Steps: []resource.TestStep{
   107  			resource.TestStep{
   108  				Config: fmt.Sprintf(
   109  					testAccCheckVSphereVirtualMachineConfig_initType,
   110  					locationOpt,
   111  					gateway,
   112  					label,
   113  					ip_address,
   114  					datastoreOpt,
   115  					template,
   116  				),
   117  				Check: resource.ComposeTestCheckFunc(
   118  					testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.thin", &vm),
   119  					resource.TestCheckResourceAttr(
   120  						"vsphere_virtual_machine.thin", "name", "terraform-test"),
   121  					resource.TestCheckResourceAttr(
   122  						"vsphere_virtual_machine.thin", "vcpu", "2"),
   123  					resource.TestCheckResourceAttr(
   124  						"vsphere_virtual_machine.thin", "memory", "4096"),
   125  					resource.TestCheckResourceAttr(
   126  						"vsphere_virtual_machine.thin", "disk.#", "2"),
   127  					resource.TestCheckResourceAttr(
   128  						"vsphere_virtual_machine.thin", "disk.0.template", template),
   129  					resource.TestCheckResourceAttr(
   130  						"vsphere_virtual_machine.thin", "disk.0.type", "thin"),
   131  					resource.TestCheckResourceAttr(
   132  						"vsphere_virtual_machine.thin", "disk.1.type", "eager_zeroed"),
   133  					resource.TestCheckResourceAttr(
   134  						"vsphere_virtual_machine.thin", "network_interface.#", "1"),
   135  					resource.TestCheckResourceAttr(
   136  						"vsphere_virtual_machine.thin", "network_interface.0.label", label),
   137  				),
   138  			},
   139  		},
   140  	})
   141  }
   142  
   143  func TestAccVSphereVirtualMachine_dhcp(t *testing.T) {
   144  	var vm virtualMachine
   145  	var locationOpt string
   146  	var datastoreOpt string
   147  
   148  	if v := os.Getenv("VSPHERE_DATACENTER"); v != "" {
   149  		locationOpt += fmt.Sprintf("    datacenter = \"%s\"\n", v)
   150  	}
   151  	if v := os.Getenv("VSPHERE_CLUSTER"); v != "" {
   152  		locationOpt += fmt.Sprintf("    cluster = \"%s\"\n", v)
   153  	}
   154  	if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" {
   155  		locationOpt += fmt.Sprintf("    resource_pool = \"%s\"\n", v)
   156  	}
   157  	if v := os.Getenv("VSPHERE_DATASTORE"); v != "" {
   158  		datastoreOpt = fmt.Sprintf("        datastore = \"%s\"\n", v)
   159  	}
   160  	template := os.Getenv("VSPHERE_TEMPLATE")
   161  	label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP")
   162  
   163  	resource.Test(t, resource.TestCase{
   164  		PreCheck:     func() { testAccPreCheck(t) },
   165  		Providers:    testAccProviders,
   166  		CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
   167  		Steps: []resource.TestStep{
   168  			resource.TestStep{
   169  				Config: fmt.Sprintf(
   170  					testAccCheckVSphereVirtualMachineConfig_dhcp,
   171  					locationOpt,
   172  					label,
   173  					datastoreOpt,
   174  					template,
   175  				),
   176  				Check: resource.ComposeTestCheckFunc(
   177  					testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm),
   178  					resource.TestCheckResourceAttr(
   179  						"vsphere_virtual_machine.bar", "name", "terraform-test"),
   180  					resource.TestCheckResourceAttr(
   181  						"vsphere_virtual_machine.bar", "vcpu", "2"),
   182  					resource.TestCheckResourceAttr(
   183  						"vsphere_virtual_machine.bar", "memory", "4096"),
   184  					resource.TestCheckResourceAttr(
   185  						"vsphere_virtual_machine.bar", "disk.#", "1"),
   186  					resource.TestCheckResourceAttr(
   187  						"vsphere_virtual_machine.bar", "disk.0.template", template),
   188  					resource.TestCheckResourceAttr(
   189  						"vsphere_virtual_machine.bar", "network_interface.#", "1"),
   190  					resource.TestCheckResourceAttr(
   191  						"vsphere_virtual_machine.bar", "network_interface.0.label", label),
   192  				),
   193  			},
   194  		},
   195  	})
   196  }
   197  
   198  func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) {
   199  	var vm virtualMachine
   200  	var locationOpt string
   201  	var datastoreOpt string
   202  
   203  	if v := os.Getenv("VSPHERE_DATACENTER"); v != "" {
   204  		locationOpt += fmt.Sprintf("    datacenter = \"%s\"\n", v)
   205  	}
   206  	if v := os.Getenv("VSPHERE_CLUSTER"); v != "" {
   207  		locationOpt += fmt.Sprintf("    cluster = \"%s\"\n", v)
   208  	}
   209  	if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" {
   210  		locationOpt += fmt.Sprintf("    resource_pool = \"%s\"\n", v)
   211  	}
   212  	if v := os.Getenv("VSPHERE_DATASTORE"); v != "" {
   213  		datastoreOpt = fmt.Sprintf("        datastore = \"%s\"\n", v)
   214  	}
   215  	template := os.Getenv("VSPHERE_TEMPLATE")
   216  	label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP")
   217  
   218  	resource.Test(t, resource.TestCase{
   219  		PreCheck:     func() { testAccPreCheck(t) },
   220  		Providers:    testAccProviders,
   221  		CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
   222  		Steps: []resource.TestStep{
   223  			resource.TestStep{
   224  				Config: fmt.Sprintf(
   225  					testAccCheckVSphereVirtualMachineConfig_custom_configs,
   226  					locationOpt,
   227  					label,
   228  					datastoreOpt,
   229  					template,
   230  				),
   231  				Check: resource.ComposeTestCheckFunc(
   232  					testAccCheckVSphereVirtualMachineExistsHasCustomConfig("vsphere_virtual_machine.car", &vm),
   233  					resource.TestCheckResourceAttr(
   234  						"vsphere_virtual_machine.car", "name", "terraform-test-custom"),
   235  					resource.TestCheckResourceAttr(
   236  						"vsphere_virtual_machine.car", "vcpu", "2"),
   237  					resource.TestCheckResourceAttr(
   238  						"vsphere_virtual_machine.car", "memory", "4096"),
   239  					resource.TestCheckResourceAttr(
   240  						"vsphere_virtual_machine.car", "disk.#", "1"),
   241  					resource.TestCheckResourceAttr(
   242  						"vsphere_virtual_machine.car", "disk.0.template", template),
   243  					resource.TestCheckResourceAttr(
   244  						"vsphere_virtual_machine.car", "network_interface.#", "1"),
   245  					resource.TestCheckResourceAttr(
   246  						"vsphere_virtual_machine.car", "custom_configuration_parameters.foo", "bar"),
   247  					resource.TestCheckResourceAttr(
   248  						"vsphere_virtual_machine.car", "custom_configuration_parameters.car", "ferrari"),
   249  					resource.TestCheckResourceAttr(
   250  						"vsphere_virtual_machine.car", "custom_configuration_parameters.num", "42"),
   251  					resource.TestCheckResourceAttr(
   252  						"vsphere_virtual_machine.car", "network_interface.0.label", label),
   253  				),
   254  			},
   255  		},
   256  	})
   257  }
   258  
   259  func TestAccVSphereVirtualMachine_createInExistingFolder(t *testing.T) {
   260  	var vm virtualMachine
   261  	var locationOpt string
   262  	var datastoreOpt string
   263  	var datacenter string
   264  
   265  	folder := "tf_test_createInExistingFolder"
   266  
   267  	if v := os.Getenv("VSPHERE_DATACENTER"); v != "" {
   268  		locationOpt += fmt.Sprintf("    datacenter = \"%s\"\n", v)
   269  		datacenter = v
   270  	}
   271  	if v := os.Getenv("VSPHERE_CLUSTER"); v != "" {
   272  		locationOpt += fmt.Sprintf("    cluster = \"%s\"\n", v)
   273  	}
   274  	if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" {
   275  		locationOpt += fmt.Sprintf("    resource_pool = \"%s\"\n", v)
   276  	}
   277  	if v := os.Getenv("VSPHERE_DATASTORE"); v != "" {
   278  		datastoreOpt = fmt.Sprintf("        datastore = \"%s\"\n", v)
   279  	}
   280  	template := os.Getenv("VSPHERE_TEMPLATE")
   281  	label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP")
   282  
   283  	resource.Test(t, resource.TestCase{
   284  		PreCheck:  func() { testAccPreCheck(t) },
   285  		Providers: testAccProviders,
   286  		CheckDestroy: resource.ComposeTestCheckFunc(
   287  			testAccCheckVSphereVirtualMachineDestroy,
   288  			removeVSphereFolder(datacenter, folder, ""),
   289  		),
   290  		Steps: []resource.TestStep{
   291  			resource.TestStep{
   292  				PreConfig: func() { createVSphereFolder(datacenter, folder) },
   293  				Config: fmt.Sprintf(
   294  					testAccCheckVSphereVirtualMachineConfig_createInFolder,
   295  					folder,
   296  					locationOpt,
   297  					label,
   298  					datastoreOpt,
   299  					template,
   300  				),
   301  				Check: resource.ComposeTestCheckFunc(
   302  					testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.folder", &vm),
   303  					resource.TestCheckResourceAttr(
   304  						"vsphere_virtual_machine.folder", "name", "terraform-test-folder"),
   305  					resource.TestCheckResourceAttr(
   306  						"vsphere_virtual_machine.folder", "folder", folder),
   307  					resource.TestCheckResourceAttr(
   308  						"vsphere_virtual_machine.folder", "vcpu", "2"),
   309  					resource.TestCheckResourceAttr(
   310  						"vsphere_virtual_machine.folder", "memory", "4096"),
   311  					resource.TestCheckResourceAttr(
   312  						"vsphere_virtual_machine.folder", "disk.#", "1"),
   313  					resource.TestCheckResourceAttr(
   314  						"vsphere_virtual_machine.folder", "disk.0.template", template),
   315  					resource.TestCheckResourceAttr(
   316  						"vsphere_virtual_machine.folder", "network_interface.#", "1"),
   317  					resource.TestCheckResourceAttr(
   318  						"vsphere_virtual_machine.folder", "network_interface.0.label", label),
   319  				),
   320  			},
   321  		},
   322  	})
   323  }
   324  
   325  func TestAccVSphereVirtualMachine_createWithFolder(t *testing.T) {
   326  	var vm virtualMachine
   327  	var f folder
   328  	var locationOpt string
   329  	var folderLocationOpt string
   330  	var datastoreOpt string
   331  
   332  	folder := "tf_test_createWithFolder"
   333  
   334  	if v := os.Getenv("VSPHERE_DATACENTER"); v != "" {
   335  		folderLocationOpt = fmt.Sprintf("    datacenter = \"%s\"\n", v)
   336  		locationOpt += folderLocationOpt
   337  	}
   338  	if v := os.Getenv("VSPHERE_CLUSTER"); v != "" {
   339  		locationOpt += fmt.Sprintf("    cluster = \"%s\"\n", v)
   340  	}
   341  	if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" {
   342  		locationOpt += fmt.Sprintf("    resource_pool = \"%s\"\n", v)
   343  	}
   344  	if v := os.Getenv("VSPHERE_DATASTORE"); v != "" {
   345  		datastoreOpt = fmt.Sprintf("        datastore = \"%s\"\n", v)
   346  	}
   347  	template := os.Getenv("VSPHERE_TEMPLATE")
   348  	label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP")
   349  
   350  	resource.Test(t, resource.TestCase{
   351  		PreCheck:  func() { testAccPreCheck(t) },
   352  		Providers: testAccProviders,
   353  		CheckDestroy: resource.ComposeTestCheckFunc(
   354  			testAccCheckVSphereVirtualMachineDestroy,
   355  			testAccCheckVSphereFolderDestroy,
   356  		),
   357  		Steps: []resource.TestStep{
   358  			resource.TestStep{
   359  				Config: fmt.Sprintf(
   360  					testAccCheckVSphereVirtualMachineConfig_createWithFolder,
   361  					folder,
   362  					folderLocationOpt,
   363  					locationOpt,
   364  					label,
   365  					datastoreOpt,
   366  					template,
   367  				),
   368  				Check: resource.ComposeTestCheckFunc(
   369  					testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.with_folder", &vm),
   370  					testAccCheckVSphereFolderExists("vsphere_folder.with_folder", &f),
   371  					resource.TestCheckResourceAttr(
   372  						"vsphere_virtual_machine.with_folder", "name", "terraform-test-with-folder"),
   373  					// resource.TestCheckResourceAttr(
   374  					// 	"vsphere_virtual_machine.with_folder", "folder", folder),
   375  					resource.TestCheckResourceAttr(
   376  						"vsphere_virtual_machine.with_folder", "vcpu", "2"),
   377  					resource.TestCheckResourceAttr(
   378  						"vsphere_virtual_machine.with_folder", "memory", "4096"),
   379  					resource.TestCheckResourceAttr(
   380  						"vsphere_virtual_machine.with_folder", "disk.#", "1"),
   381  					resource.TestCheckResourceAttr(
   382  						"vsphere_virtual_machine.with_folder", "disk.0.template", template),
   383  					resource.TestCheckResourceAttr(
   384  						"vsphere_virtual_machine.with_folder", "network_interface.#", "1"),
   385  					resource.TestCheckResourceAttr(
   386  						"vsphere_virtual_machine.with_folder", "network_interface.0.label", label),
   387  				),
   388  			},
   389  		},
   390  	})
   391  }
   392  
   393  func TestAccVSphereVirtualMachine_createWithCdrom(t *testing.T) {
   394  	var vm virtualMachine
   395  	var locationOpt string
   396  	var datastoreOpt string
   397  
   398  	if v := os.Getenv("VSPHERE_DATACENTER"); v != "" {
   399  		locationOpt += fmt.Sprintf("    datacenter = \"%s\"\n", v)
   400  	}
   401  	if v := os.Getenv("VSPHERE_CLUSTER"); v != "" {
   402  		locationOpt += fmt.Sprintf("    cluster = \"%s\"\n", v)
   403  	}
   404  	if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" {
   405  		locationOpt += fmt.Sprintf("    resource_pool = \"%s\"\n", v)
   406  	}
   407  	if v := os.Getenv("VSPHERE_DATASTORE"); v != "" {
   408  		datastoreOpt = fmt.Sprintf("        datastore = \"%s\"\n", v)
   409  	}
   410  	template := os.Getenv("VSPHERE_TEMPLATE")
   411  	label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP")
   412  	cdromDatastore := os.Getenv("VSPHERE_CDROM_DATASTORE")
   413  	cdromPath := os.Getenv("VSPHERE_CDROM_PATH")
   414  
   415  	resource.Test(t, resource.TestCase{
   416  		PreCheck:     func() { testAccPreCheck(t) },
   417  		Providers:    testAccProviders,
   418  		CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
   419  		Steps: []resource.TestStep{
   420  			resource.TestStep{
   421  				Config: fmt.Sprintf(
   422  					testAccCheckVsphereVirtualMachineConfig_cdrom,
   423  					locationOpt,
   424  					label,
   425  					datastoreOpt,
   426  					template,
   427  					cdromDatastore,
   428  					cdromPath,
   429  				),
   430  				Check: resource.ComposeTestCheckFunc(
   431  					testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.with_cdrom", &vm),
   432  					resource.TestCheckResourceAttr(
   433  						"vsphere_virtual_machine.with_cdrom", "name", "terraform-test-with-cdrom"),
   434  					resource.TestCheckResourceAttr(
   435  						"vsphere_virtual_machine.with_cdrom", "vcpu", "2"),
   436  					resource.TestCheckResourceAttr(
   437  						"vsphere_virtual_machine.with_cdrom", "memory", "4096"),
   438  					resource.TestCheckResourceAttr(
   439  						"vsphere_virtual_machine.with_cdrom", "disk.#", "1"),
   440  					resource.TestCheckResourceAttr(
   441  						"vsphere_virtual_machine.with_cdrom", "disk.0.template", template),
   442  					resource.TestCheckResourceAttr(
   443  						"vsphere_virtual_machine.with_cdrom", "cdrom.#", "1"),
   444  					resource.TestCheckResourceAttr(
   445  						"vsphere_virtual_machine.with_cdrom", "cdrom.0.datastore", cdromDatastore),
   446  					resource.TestCheckResourceAttr(
   447  						"vsphere_virtual_machine.with_cdrom", "cdrom.0.path", cdromPath),
   448  					resource.TestCheckResourceAttr(
   449  						"vsphere_virtual_machine.with_cdrom", "network_interface.#", "1"),
   450  					resource.TestCheckResourceAttr(
   451  						"vsphere_virtual_machine.with_cdrom", "network_interface.0.label", label),
   452  				),
   453  			},
   454  		},
   455  	})
   456  }
   457  
   458  func TestAccVSphereVirtualMachine_createWithExistingVmdk(t *testing.T) {
   459  	vmdk_path := os.Getenv("VSPHERE_VMDK_PATH")
   460  	gateway := os.Getenv("VSPHERE_NETWORK_GATEWAY")
   461  	label := os.Getenv("VSPHERE_NETWORK_LABEL")
   462  	ip_address := os.Getenv("VSPHERE_NETWORK_IP_ADDRESS")
   463  
   464  	var vm virtualMachine
   465  	var locationOpt string
   466  	var datastoreOpt string
   467  
   468  	if v := os.Getenv("VSPHERE_DATACENTER"); v != "" {
   469  		locationOpt += fmt.Sprintf("    datacenter = \"%s\"\n", v)
   470  	}
   471  	if v := os.Getenv("VSPHERE_CLUSTER"); v != "" {
   472  		locationOpt += fmt.Sprintf("    cluster = \"%s\"\n", v)
   473  	}
   474  	if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" {
   475  		locationOpt += fmt.Sprintf("    resource_pool = \"%s\"\n", v)
   476  	}
   477  	if v := os.Getenv("VSPHERE_DATASTORE"); v != "" {
   478  		datastoreOpt = fmt.Sprintf("        datastore = \"%s\"\n", v)
   479  	}
   480  
   481  	resource.Test(t, resource.TestCase{
   482  		PreCheck:     func() { testAccPreCheck(t) },
   483  		Providers:    testAccProviders,
   484  		CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
   485  		Steps: []resource.TestStep{
   486  			resource.TestStep{
   487  				Config: fmt.Sprintf(
   488  					testAccCheckVSphereVirtualMachineConfig_withExistingVmdk,
   489  					locationOpt,
   490  					gateway,
   491  					label,
   492  					ip_address,
   493  					datastoreOpt,
   494  					vmdk_path,
   495  				),
   496  				Check: resource.ComposeTestCheckFunc(
   497  					testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.with_existing_vmdk", &vm),
   498  					resource.TestCheckResourceAttr(
   499  						"vsphere_virtual_machine.with_existing_vmdk", "name", "terraform-test-with-existing-vmdk"),
   500  					resource.TestCheckResourceAttr(
   501  						"vsphere_virtual_machine.with_existing_vmdk", "vcpu", "2"),
   502  					resource.TestCheckResourceAttr(
   503  						"vsphere_virtual_machine.with_existing_vmdk", "memory", "4096"),
   504  					resource.TestCheckResourceAttr(
   505  						"vsphere_virtual_machine.with_existing_vmdk", "disk.#", "1"),
   506  					resource.TestCheckResourceAttr(
   507  						"vsphere_virtual_machine.with_existing_vmdk", "disk.0.vmdk", vmdk_path),
   508  					resource.TestCheckResourceAttr(
   509  						"vsphere_virtual_machine.with_existing_vmdk", "disk.0.bootable", "true"),
   510  					resource.TestCheckResourceAttr(
   511  						"vsphere_virtual_machine.with_existing_vmdk", "network_interface.#", "1"),
   512  					resource.TestCheckResourceAttr(
   513  						"vsphere_virtual_machine.with_existing_vmdk", "network_interface.0.label", label),
   514  				),
   515  			},
   516  		},
   517  	})
   518  }
   519  
   520  func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error {
   521  	client := testAccProvider.Meta().(*govmomi.Client)
   522  	finder := find.NewFinder(client.Client, true)
   523  
   524  	for _, rs := range s.RootModule().Resources {
   525  		if rs.Type != "vsphere_virtual_machine" {
   526  			continue
   527  		}
   528  
   529  		dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"])
   530  		if err != nil {
   531  			return fmt.Errorf("error %s", err)
   532  		}
   533  
   534  		dcFolders, err := dc.Folders(context.TODO())
   535  		if err != nil {
   536  			return fmt.Errorf("error %s", err)
   537  		}
   538  
   539  		folder := dcFolders.VmFolder
   540  		if len(rs.Primary.Attributes["folder"]) > 0 {
   541  			si := object.NewSearchIndex(client.Client)
   542  			folderRef, err := si.FindByInventoryPath(
   543  				context.TODO(), fmt.Sprintf("%v/vm/%v", rs.Primary.Attributes["datacenter"], rs.Primary.Attributes["folder"]))
   544  			if err != nil {
   545  				return err
   546  			} else if folderRef != nil {
   547  				folder = folderRef.(*object.Folder)
   548  			}
   549  		}
   550  
   551  		v, err := object.NewSearchIndex(client.Client).FindChild(context.TODO(), folder, rs.Primary.Attributes["name"])
   552  
   553  		if v != nil {
   554  			return fmt.Errorf("Record still exists")
   555  		}
   556  	}
   557  
   558  	return nil
   559  }
   560  
   561  func testAccCheckVSphereVirtualMachineExistsHasCustomConfig(n string, vm *virtualMachine) resource.TestCheckFunc {
   562  	return func(s *terraform.State) error {
   563  
   564  		rs, ok := s.RootModule().Resources[n]
   565  		if !ok {
   566  			return fmt.Errorf("Not found: %s", n)
   567  		}
   568  
   569  		if rs.Primary.ID == "" {
   570  			return fmt.Errorf("No ID is set")
   571  		}
   572  
   573  		client := testAccProvider.Meta().(*govmomi.Client)
   574  		finder := find.NewFinder(client.Client, true)
   575  
   576  		dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"])
   577  		if err != nil {
   578  			return fmt.Errorf("error %s", err)
   579  		}
   580  
   581  		dcFolders, err := dc.Folders(context.TODO())
   582  		if err != nil {
   583  			return fmt.Errorf("error %s", err)
   584  		}
   585  
   586  		_, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"])
   587  		if err != nil {
   588  			return fmt.Errorf("error %s", err)
   589  		}
   590  
   591  		finder = finder.SetDatacenter(dc)
   592  		instance, err := finder.VirtualMachine(context.TODO(), rs.Primary.Attributes["name"])
   593  		if err != nil {
   594  			return fmt.Errorf("error %s", err)
   595  		}
   596  
   597  		var mvm mo.VirtualMachine
   598  
   599  		collector := property.DefaultCollector(client.Client)
   600  
   601  		if err := collector.RetrieveOne(context.TODO(), instance.Reference(), []string{"config.extraConfig"}, &mvm); err != nil {
   602  			return fmt.Errorf("error %s", err)
   603  		}
   604  
   605  		var configMap = make(map[string]types.AnyType)
   606  		if mvm.Config != nil && mvm.Config.ExtraConfig != nil && len(mvm.Config.ExtraConfig) > 0 {
   607  			for _, v := range mvm.Config.ExtraConfig {
   608  				value := v.GetOptionValue()
   609  				configMap[value.Key] = value.Value
   610  			}
   611  		} else {
   612  			return fmt.Errorf("error no ExtraConfig")
   613  		}
   614  
   615  		if configMap["foo"] == nil {
   616  			return fmt.Errorf("error no ExtraConfig for 'foo'")
   617  		}
   618  
   619  		if configMap["foo"] != "bar" {
   620  			return fmt.Errorf("error ExtraConfig 'foo' != bar")
   621  		}
   622  
   623  		if configMap["car"] == nil {
   624  			return fmt.Errorf("error no ExtraConfig for 'car'")
   625  		}
   626  
   627  		if configMap["car"] != "ferrari" {
   628  			return fmt.Errorf("error ExtraConfig 'car' != ferrari")
   629  		}
   630  
   631  		if configMap["num"] == nil {
   632  			return fmt.Errorf("error no ExtraConfig for 'num'")
   633  		}
   634  
   635  		// todo this should be an int, getting back a string
   636  		if configMap["num"] != "42" {
   637  			return fmt.Errorf("error ExtraConfig 'num' != 42")
   638  		}
   639  		*vm = virtualMachine{
   640  			name: rs.Primary.ID,
   641  		}
   642  
   643  		return nil
   644  	}
   645  }
   646  
   647  func testAccCheckVSphereVirtualMachineExists(n string, vm *virtualMachine) resource.TestCheckFunc {
   648  	return func(s *terraform.State) error {
   649  		rs, ok := s.RootModule().Resources[n]
   650  		if !ok {
   651  			return fmt.Errorf("Not found: %s", n)
   652  		}
   653  
   654  		if rs.Primary.ID == "" {
   655  			return fmt.Errorf("No ID is set")
   656  		}
   657  
   658  		client := testAccProvider.Meta().(*govmomi.Client)
   659  		finder := find.NewFinder(client.Client, true)
   660  
   661  		dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"])
   662  		if err != nil {
   663  			return fmt.Errorf("error %s", err)
   664  		}
   665  
   666  		dcFolders, err := dc.Folders(context.TODO())
   667  		if err != nil {
   668  			return fmt.Errorf("error %s", err)
   669  		}
   670  
   671  		folder := dcFolders.VmFolder
   672  		if len(rs.Primary.Attributes["folder"]) > 0 {
   673  			si := object.NewSearchIndex(client.Client)
   674  			folderRef, err := si.FindByInventoryPath(
   675  				context.TODO(), fmt.Sprintf("%v/vm/%v", rs.Primary.Attributes["datacenter"], rs.Primary.Attributes["folder"]))
   676  			if err != nil {
   677  				return err
   678  			} else if folderRef != nil {
   679  				folder = folderRef.(*object.Folder)
   680  			}
   681  		}
   682  
   683  		_, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), folder, rs.Primary.Attributes["name"])
   684  
   685  		*vm = virtualMachine{
   686  			name: rs.Primary.ID,
   687  		}
   688  
   689  		return nil
   690  	}
   691  }
   692  
   693  const testAccCheckVSphereVirtualMachineConfig_basic = `
   694  resource "vsphere_virtual_machine" "foo" {
   695      name = "terraform-test"
   696  %s
   697      vcpu = 2
   698      memory = 4096
   699      memory_reservation = 4096
   700      gateway = "%s"
   701      network_interface {
   702          label = "%s"
   703          ipv4_address = "%s"
   704          ipv4_prefix_length = 24
   705      }
   706      disk {
   707  %s
   708          template = "%s"
   709          iops = 500
   710      }
   711      disk {
   712          size = 1
   713          iops = 500
   714      }
   715  }
   716  `
   717  const testAccCheckVSphereVirtualMachineConfig_initType = `
   718  resource "vsphere_virtual_machine" "thin" {
   719      name = "terraform-test"
   720  %s
   721      vcpu = 2
   722      memory = 4096
   723      gateway = "%s"
   724      network_interface {
   725          label = "%s"
   726          ipv4_address = "%s"
   727          ipv4_prefix_length = 24
   728      }
   729      disk {
   730  %s
   731          template = "%s"
   732          iops = 500
   733          type = "thin"
   734      }
   735      disk {
   736          size = 1
   737          iops = 500
   738      }
   739  }
   740  `
   741  const testAccCheckVSphereVirtualMachineConfig_dhcp = `
   742  resource "vsphere_virtual_machine" "bar" {
   743      name = "terraform-test"
   744  %s
   745      vcpu = 2
   746      memory = 4096
   747      network_interface {
   748          label = "%s"
   749      }
   750      disk {
   751  %s
   752          template = "%s"
   753      }
   754  }
   755  `
   756  
   757  const testAccCheckVSphereVirtualMachineConfig_custom_configs = `
   758  resource "vsphere_virtual_machine" "car" {
   759      name = "terraform-test-custom"
   760  %s
   761      vcpu = 2
   762      memory = 4096
   763      network_interface {
   764          label = "%s"
   765      }
   766      custom_configuration_parameters {
   767  	"foo" = "bar"
   768  	"car" = "ferrari"
   769  	"num" = 42
   770      }
   771      disk {
   772  %s
   773          template = "%s"
   774      }
   775  }
   776  `
   777  
   778  const testAccCheckVSphereVirtualMachineConfig_createInFolder = `
   779  resource "vsphere_virtual_machine" "folder" {
   780      name = "terraform-test-folder"
   781      folder = "%s"
   782  %s
   783      vcpu = 2
   784      memory = 4096
   785      network_interface {
   786          label = "%s"
   787      }
   788      disk {
   789  %s
   790          template = "%s"
   791      }
   792  }
   793  `
   794  
   795  const testAccCheckVSphereVirtualMachineConfig_createWithFolder = `
   796  resource "vsphere_folder" "with_folder" {
   797  	path = "%s"
   798  %s
   799  }
   800  resource "vsphere_virtual_machine" "with_folder" {
   801      name = "terraform-test-with-folder"
   802      folder = "${vsphere_folder.with_folder.path}"
   803  %s
   804      vcpu = 2
   805      memory = 4096
   806      network_interface {
   807          label = "%s"
   808      }
   809      disk {
   810  %s
   811          template = "%s"
   812      }
   813  }
   814  `
   815  
   816  const testAccCheckVsphereVirtualMachineConfig_cdrom = `
   817  resource "vsphere_virtual_machine" "with_cdrom" {
   818      name = "terraform-test-with-cdrom"
   819  %s
   820      vcpu = 2
   821      memory = 4096
   822      network_interface {
   823          label = "%s"
   824      }
   825      disk {
   826  %s
   827          template = "%s"
   828      }
   829  
   830      cdrom {
   831          datastore = "%s"
   832          path = "%s"
   833      }
   834  }
   835  `
   836  
   837  const testAccCheckVSphereVirtualMachineConfig_withExistingVmdk = `
   838  resource "vsphere_virtual_machine" "with_existing_vmdk" {
   839      name = "terraform-test-with-existing-vmdk"
   840  %s
   841      vcpu = 2
   842      memory = 4096
   843      gateway = "%s"
   844      network_interface {
   845          label = "%s"
   846          ipv4_address = "%s"
   847          ipv4_prefix_length = 24
   848      }
   849      disk {
   850  %s
   851          vmdk = "%s"
   852  		bootable = true
   853      }
   854  }
   855  `