Skip to content

Commit

Permalink
Improve tests, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgenunez committed Sep 19, 2017
1 parent 2d2cf3b commit 9324332
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 27 deletions.
34 changes: 17 additions & 17 deletions vsphere/distributed_virtual_switch_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,25 +186,25 @@ func expandDistributedVirtualSwitchHostMemberConfigSpec(client *govmomi.Client,
}
}
}
}

if hosts != nil {
// Add whatever is left
for _, host := range hosts {
hi := host.(map[string]interface{})
if val, ok := refs[hi["host_system_id"].(string)]; ok {
backing := new(types.DistributedVirtualSwitchHostMemberPnicBacking)
for _, nic := range hi["backing"].([]interface{}) {
backing.PnicSpec = append(backing.PnicSpec, types.DistributedVirtualSwitchHostMemberPnicSpec{
PnicDevice: strings.TrimSpace(nic.(string)),
})
}
h := types.DistributedVirtualSwitchHostMemberConfigSpec{
Host: val,
Backing: backing,
Operation: "add", // Options: "add", "edit", "remove"
}
hmc = append(hmc, h)
if hosts != nil {
// Add whatever is left
for _, host := range hosts {
hi := host.(map[string]interface{})
if val, ok := refs[hi["host_system_id"].(string)]; ok {
backing := new(types.DistributedVirtualSwitchHostMemberPnicBacking)
for _, nic := range hi["backing"].([]interface{}) {
backing.PnicSpec = append(backing.PnicSpec, types.DistributedVirtualSwitchHostMemberPnicSpec{
PnicDevice: strings.TrimSpace(nic.(string)),
})
}
h := types.DistributedVirtualSwitchHostMemberConfigSpec{
Host: val,
Backing: backing,
Operation: "add", // Options: "add", "edit", "remove"
}
hmc = append(hmc, h)
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions vsphere/resource_vsphere_distributed_virtual_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,45 @@ func resourceVSphereDistributedVirtualSwitchUpdate(d *schema.ResourceData, meta
return fmt.Errorf("%s", err)
}

// Wait for the distributed virtual switch resource to be destroyed
stateConf := &resource.StateChangeConf{
Pending: []string{"Updating"},
Target: []string{"Updated"},
Refresh: resourceVSphereDVSStateUpdateRefreshFunc(d, meta),
Timeout: 10 * time.Minute,
MinTimeout: 3 * time.Second,
Delay: 5 * time.Second,
}

_, err = stateConf.WaitForState()
if err != nil {
name := d.Get("name").(string)
return fmt.Errorf("error waiting for distributed virtual switch (%s) to be updated: %s", name, err)
}

return resourceVSphereDistributedVirtualSwitchRead(d, meta)
}

func resourceVSphereDVSStateUpdateRefreshFunc(d *schema.ResourceData, meta interface{}) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
log.Print("[TRACE] Refreshing distributed virtual switch state, looking for config changes")
client := meta.(*govmomi.Client)
dvs, err := dvsFromUuid(client, d.Id())
if err != nil {
return nil, "Failed", err
}
config := dvs.Config.GetDVSConfigInfo()
cv := d.Get("config_version").(string)
log.Printf("[TRACE] Current version %s. Old version %s", config.ConfigVersion, cv)
if config.ConfigVersion != cv {
log.Print("[TRACE] Distributed virtual switch config updated")
return dvs, "Updated", nil
} else {
return dvs, "Updating", nil
}
}
}

func resourceVSphereDVSStateRefreshFunc(d *schema.ResourceData, meta interface{}) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
log.Print("[TRACE] Refreshing distributed virtual switch state")
Expand Down
48 changes: 38 additions & 10 deletions vsphere/resource_vsphere_distributed_virtual_switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/vmware/govmomi"
// "github.com/vmware/govmomi/find"
// "golang.org/x/net/context"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/mo"
"golang.org/x/net/context"
)

func testAccCheckVSphereDVSConfigNoUplinks() string {
Expand Down Expand Up @@ -91,7 +93,7 @@ func testAccResourceVSphereDistributedVirtualSwitchPreCheck(t *testing.T) {

// Create a distributed virtual switch with no uplinks
func TestAccVSphereDVS_createWithoutUplinks(t *testing.T) {
resourceName := "vsphere_distributed_virtual_switch.testDVS"
resourceName := "testDVS"

resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand All @@ -111,7 +113,7 @@ func TestAccVSphereDVS_createWithoutUplinks(t *testing.T) {

// Create a distributed virtual switch with one uplink
func TestAccVSphereDVS_createWithUplinks(t *testing.T) {
resourceName := "vsphere_distributed_virtual_switch.testDVS"
resourceName := "testDVS"

resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand All @@ -131,7 +133,7 @@ func TestAccVSphereDVS_createWithUplinks(t *testing.T) {

// Create a distributed virtual switch with an uplink, delete it and add it again
func TestAccVSphereDVS_createAndUpdateWithUplinks(t *testing.T) {
resourceName := "vsphere_distributed_virtual_switch.testDVS"
resourceName := "testDVS"

resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand Down Expand Up @@ -189,6 +191,7 @@ func testAccCheckVSphereDVSExists(name string, n int) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*govmomi.Client)

var id string
for _, rs := range s.RootModule().Resources {
if rs.Type != "vsphere_distributed_virtual_switch" {
continue
Expand All @@ -197,19 +200,44 @@ func testAccCheckVSphereDVSExists(name string, n int) resource.TestCheckFunc {
continue
}

id := rs.Primary.ID
id = rs.Primary.ID
dvs, err := dvsFromUuid(client, id)
if err != nil {
return fmt.Errorf("distributed virtual switch '%s' doesn't exists", id)
} else {
config := dvs.Config.GetDVSConfigInfo()
if len(config.Host) != n {
return fmt.Errorf("expected '%d' uplinks, found '%d'", n, len(config.Host))

for _, h := range config.Host {
finder := find.NewFinder(client.Client, false)

ds, err := finder.ObjectReference(context.TODO(), *h.Config.Host)
if err != nil {
continue
}
dso := ds.(*object.HostSystem)
var mh mo.HostSystem
err = dso.Properties(context.TODO(), ds.Reference(), []string{"config"}, &mh)
if err != nil {
continue
}

var j int
for _, ps := range mh.Config.Network.ProxySwitch {
if ps.DvsName == name {
j = len(ps.Pnic)
}
}
if j != n {
return fmt.Errorf("expected '%d' uplinks, found '%d'", n, j)
}
fmt.Println("DVS exists and has the correct number of uplinks")
return nil
}
fmt.Println("DVS exists and has the correct number of uplinks")
return nil
}
}
if id == "" {
return fmt.Errorf("DVS not found")
}
return nil
}
}

0 comments on commit 9324332

Please sign in to comment.