diff --git a/vsphere/helper_test.go b/vsphere/helper_test.go new file mode 100644 index 000000000..7c59bc3a3 --- /dev/null +++ b/vsphere/helper_test.go @@ -0,0 +1,43 @@ +package vsphere + +import ( + "fmt" + "os" + "time" + + "github.com/hashicorp/terraform/terraform" + "github.com/vmware/govmomi" +) + +// testCheckVariables bundles common variables needed by various test checkers. +type testCheckVariables struct { + // A client for various operations. + client *govmomi.Client + + // The subject resource's ID. + resourceID string + + // The ESXi host that a various API call is directed at. + esxiHost string + + // The datacenter that a various API call is directed at. + datacenter string + + // A timeout to pass to various context creation calls. + timeout time.Duration +} + +func testClientVariablesForResource(s *terraform.State, addr string) (testCheckVariables, error) { + rs, ok := s.RootModule().Resources[addr] + if !ok { + return testCheckVariables{}, fmt.Errorf("%s not found in state", addr) + } + + return testCheckVariables{ + client: testAccProvider.Meta().(*govmomi.Client), + resourceID: rs.Primary.ID, + esxiHost: os.Getenv("VSPHERE_ESXI_HOST"), + datacenter: os.Getenv("VSPHERE_DATACENTER"), + timeout: time.Minute * 5, + }, nil +} diff --git a/vsphere/resource_vsphere_host_virtual_switch.go b/vsphere/resource_vsphere_host_virtual_switch.go index 67daa5d8f..11465e8ce 100644 --- a/vsphere/resource_vsphere_host_virtual_switch.go +++ b/vsphere/resource_vsphere_host_virtual_switch.go @@ -131,6 +131,5 @@ func resourceVSphereHostVirtualSwitchDelete(d *schema.ResourceData, meta interfa return fmt.Errorf("error deleting host vSwitch: %s", err) } - d.SetId("") return nil } diff --git a/vsphere/resource_vsphere_host_virtual_switch_test.go b/vsphere/resource_vsphere_host_virtual_switch_test.go index f319ccd98..1dcff6ae4 100644 --- a/vsphere/resource_vsphere_host_virtual_switch_test.go +++ b/vsphere/resource_vsphere_host_virtual_switch_test.go @@ -5,11 +5,9 @@ import ( "fmt" "os" "testing" - "time" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" - "github.com/vmware/govmomi" ) func TestAccResourceVSphereHostVirtualSwitch(t *testing.T) { @@ -130,26 +128,21 @@ func testAccResourceVSphereHostVirtualSwitchPreCheck(t *testing.T) { func testAccResourceVSphereHostVirtualSwitchExists(expected bool) resource.TestCheckFunc { return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources["vsphere_host_virtual_switch.switch"] - if !ok { + vars, err := testClientVariablesForResource(s, "vsphere_host_virtual_switch.switch") + if err != nil { return errors.New("vsphere_host_virtual_switch.switch not found in state") } - client := testAccProvider.Meta().(*govmomi.Client) - id := rs.Primary.ID - host := os.Getenv("VSPHERE_ESXI_HOST") - datacenter := os.Getenv("VSPHERE_DATACENTER") - timeout := time.Minute * 5 - _, err := hostVSwitchFromName(client, id, host, datacenter, timeout) + _, err = hostVSwitchFromName(vars.client, vars.resourceID, vars.esxiHost, vars.datacenter, vars.timeout) if err != nil { - if err.Error() == fmt.Sprintf("vSwitch %s not found on host %s", id, host) && !expected { + if err.Error() == fmt.Sprintf("vSwitch %s not found on host %s", vars.resourceID, vars.esxiHost) && !expected { // Expected missing return nil } return err } if !expected { - return fmt.Errorf("expected vSwitch %s to still be missing", id) + return fmt.Errorf("expected vSwitch %s to still be missing", vars.resourceID) } return nil }