Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-1.31] Add support for MOID on datacenter field #1285

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions pkg/common/vclib/datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,33 @@ type Datacenter struct {
*object.Datacenter
}

// GetDatacenter returns the DataCenter Object for the given datacenterPath
// GetDatacenter returns the DataCenter Object for the given datacenterPath or datacenter MOID.
// If datacenter is located in a folder, include full path to datacenter else just provide the datacenter name
func GetDatacenter(ctx context.Context, connection *VSphereConnection, datacenterPath string) (*Datacenter, error) {
finder := find.NewFinder(connection.Client, false)
datacenter, err := finder.Datacenter(ctx, datacenterPath)
if err != nil {
klog.Errorf("Failed to find the datacenter: %s. err: %+v", datacenterPath, err)
return nil, err
var datacenter *object.Datacenter
var err error

// Try to get an object reference based on the requested datacenter name.
// eg.: if datacenterPath == Datacenter:datacenter-3, this is a valid MOID
// so dcRef will not be null.
dcRef := object.ReferenceFromString(datacenterPath)
if dcRef != nil {
datacenter = object.NewDatacenter(connection.Client, *dcRef)
datacenter.InventoryPath, err = find.InventoryPath(ctx, connection.Client, dcRef.Reference())
if err != nil {
klog.Errorf("Failed to find datacenter by MOID: %s. err: %+v", datacenterPath, err)
return nil, err
}
klog.Infof("Datacenter found by Moid: %s", datacenter.InventoryPath)
} else {
finder := find.NewFinder(connection.Client, false)
datacenter, err = finder.Datacenter(ctx, datacenterPath)
if err != nil {
klog.Errorf("Failed to find the datacenter: %s. err: %+v", datacenterPath, err)
return nil, err
}
}

dc := Datacenter{datacenter}
return &dc, nil
}
Expand Down
255 changes: 136 additions & 119 deletions pkg/common/vclib/datacenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package vclib

import (
"context"
"strings"
"testing"

"github.com/vmware/govmomi"
Expand Down Expand Up @@ -49,131 +50,147 @@ func TestDatacenter(t *testing.T) {

vc := &VSphereConnection{Client: c.Client}

_, err = GetDatacenter(ctx, vc, testNameNotFound)
if err == nil {
t.Error("expected error")
}

dc, err := GetDatacenter(ctx, vc, TestDefaultDatacenter)
if err != nil {
t.Error(err)
}

_, err = dc.GetVMByUUID(ctx, testNameNotFound)
if err == nil {
t.Error("expected error")
}

_, err = dc.GetVMByUUID(ctx, avm.Summary.Config.Uuid)
if err != nil {
t.Error(err)
}

_, err = dc.GetVMByPath(ctx, testNameNotFound)
if err == nil {
t.Error("expected error")
}

vm, err := dc.GetVMByPath(ctx, TestDefaultDatacenter+"/vm/"+avm.Name)
if err != nil {
t.Error(err)
runDatacenterTest := func(t *testing.T, dc *Datacenter) {
_, err = dc.GetVMByUUID(ctx, testNameNotFound)
if err == nil || err != ErrNoVMFound {
t.Error("expected error")
}

_, err = dc.GetVMByUUID(ctx, avm.Summary.Config.Uuid)
if err != nil {
t.Error(err)
}

_, err = dc.GetVMByPath(ctx, testNameNotFound)
if err == nil || !strings.Contains(err.Error(), "not found") {
t.Error("expected error")
}

vm, err := dc.GetVMByPath(ctx, TestDefaultDatacenter+"/vm/"+avm.Name)
if err != nil {
t.Error(err)
}

_, err = dc.GetDatastoreByPath(ctx, testNameNotFound) // invalid format
if err == nil || !strings.Contains(err.Error(), "Failed to parse vmDiskPath") {
t.Error("expected error")
}

invalidPath := object.DatastorePath{
Datastore: testNameNotFound,
Path: testNameNotFound,
}

_, err = dc.GetDatastoreByPath(ctx, invalidPath.String())
if err == nil || !strings.Contains(err.Error(), "not found") {
t.Error("expected error")
}

_, err = dc.GetDatastoreByPath(ctx, avm.Summary.Config.VmPathName)
if err != nil {
t.Error(err)
}

_, err = dc.GetDatastoreByName(ctx, testNameNotFound)
if err == nil || !strings.Contains(err.Error(), "not found") {
t.Error("expected error")
}

ds, err := dc.GetDatastoreByName(ctx, TestDefaultDatastore)
if err != nil {
t.Error(err)
}

_, err = dc.GetFolderByPath(ctx, testNameNotFound)
if err == nil || !strings.Contains(err.Error(), "not found") {
t.Error("expected error")
}

_, err = dc.GetFolderByPath(ctx, TestDefaultDatacenter+"/vm")
if err != nil {
t.Error(err)
}

_, err = dc.GetVMMoList(ctx, nil, nil)
if err == nil || !strings.Contains(err.Error(), "VirtualMachine Object list is empty") {
t.Error("expected error")
}

_, err = dc.GetVMMoList(ctx, []*VirtualMachine{vm}, []string{testNameNotFound}) // invalid property
if err == nil || !strings.Contains(err.Error(), "InvalidProperty") {
t.Error("expected error")
}

_, err = dc.GetVMMoList(ctx, []*VirtualMachine{vm}, []string{"summary"})
if err != nil {
t.Error(err)
}

diskPath := ds.Datastore.Path(avm.Name + "/disk1.vmdk")

_, err = dc.GetVirtualDiskPage83Data(ctx, diskPath+testNameNotFound)
if err == nil || !strings.Contains(err.Error(), "not found") {
t.Error("expected error")
}

_, err = dc.GetVirtualDiskPage83Data(ctx, diskPath)
if err != nil {
t.Errorf("GetVirtualDiskPage83Data: %v", err)
}

_, err = dc.GetDatastoreMoList(ctx, nil, nil)
if err == nil || !strings.Contains(err.Error(), "Datastore Object list is empty") {
t.Error("expected error")
}

_, err = dc.GetDatastoreMoList(ctx, []*Datastore{ds.Datastore}, []string{testNameNotFound}) // invalid property
if err == nil || !strings.Contains(err.Error(), "InvalidProperty") {
t.Error("expected error")
}

_, err = dc.GetDatastoreMoList(ctx, []*Datastore{ds.Datastore}, []string{DatastoreInfoProperty})
if err != nil {
t.Error(err)
}

nodeVolumes := map[string][]string{
avm.Name: {testNameNotFound, diskPath},
}

attached, err := dc.CheckDisksAttached(ctx, nodeVolumes)
if err != nil {
t.Error(err)
}

if attached[avm.Name][testNameNotFound] {
t.Error("should not be attached")
}

if !attached[avm.Name][diskPath] {
t.Errorf("%s should be attached", diskPath)
}
}

_, err = dc.GetDatastoreByPath(ctx, testNameNotFound) // invalid format
if err == nil {
t.Error("expected error")
}

invalidPath := object.DatastorePath{
Datastore: testNameNotFound,
Path: testNameNotFound,
}
_, err = dc.GetDatastoreByPath(ctx, invalidPath.String())
if err == nil {
t.Error("expected error")
}

_, err = dc.GetDatastoreByPath(ctx, avm.Summary.Config.VmPathName)
if err != nil {
t.Error(err)
}

_, err = dc.GetDatastoreByName(ctx, testNameNotFound)
if err == nil {
t.Error("expected error")
}

ds, err := dc.GetDatastoreByName(ctx, TestDefaultDatastore)
if err != nil {
t.Error(err)
}

_, err = dc.GetFolderByPath(ctx, testNameNotFound)
if err == nil {
t.Error("expected error")
}

_, err = dc.GetFolderByPath(ctx, TestDefaultDatacenter+"/vm")
if err != nil {
t.Error(err)
}

_, err = dc.GetVMMoList(ctx, nil, nil)
if err == nil {
t.Error("expected error")
}

_, err = dc.GetVMMoList(ctx, []*VirtualMachine{vm}, []string{testNameNotFound}) // invalid property
if err == nil {
t.Error("expected error")
}

_, err = dc.GetVMMoList(ctx, []*VirtualMachine{vm}, []string{"summary"})
if err != nil {
t.Error(err)
}

diskPath := ds.Datastore.Path(avm.Name + "/disk1.vmdk")

_, err = dc.GetVirtualDiskPage83Data(ctx, diskPath+testNameNotFound)
if err == nil {
t.Error("expected error")
}

_, err = dc.GetVirtualDiskPage83Data(ctx, diskPath)
if err != nil {
t.Errorf("GetVirtualDiskPage83Data: %v", err)
}

_, err = dc.GetDatastoreMoList(ctx, nil, nil)
_, err = GetDatacenter(ctx, vc, testNameNotFound)
if err == nil {
t.Error("expected error")
}

_, err = dc.GetDatastoreMoList(ctx, []*Datastore{ds.Datastore}, []string{testNameNotFound}) // invalid property
if err == nil {
t.Error("expected error")
}
t.Run("should get objects using Datacenter path", func(t *testing.T) {
dc, err := GetDatacenter(ctx, vc, TestDefaultDatacenter)
if err != nil {
t.Error(err)
}
runDatacenterTest(t, dc)
})

_, err = dc.GetDatastoreMoList(ctx, []*Datastore{ds.Datastore}, []string{DatastoreInfoProperty})
if err != nil {
t.Error(err)
}

nodeVolumes := map[string][]string{
avm.Name: {testNameNotFound, diskPath},
}
t.Run("should get objects using Datacenter MOID", func(t *testing.T) {
dcRef := simulator.Map.Any("Datacenter")
dc, err := GetDatacenter(ctx, vc, dcRef.Reference().String())
if err != nil {
t.Error(err)
}
runDatacenterTest(t, dc)
})

attached, err := dc.CheckDisksAttached(ctx, nodeVolumes)
if err != nil {
t.Error(err)
}

if attached[avm.Name][testNameNotFound] {
t.Error("should not be attached")
}

if !attached[avm.Name][diskPath] {
t.Errorf("%s should be attached", diskPath)
}
}