Skip to content

Commit

Permalink
Merge pull request #1285 from k8s-infra-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…1283-to-release-1.31

[release-1.31] Add support for MOID on datacenter field
  • Loading branch information
k8s-ci-robot authored Oct 31, 2024
2 parents 80926e1 + 2af5b6a commit f635d30
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 125 deletions.
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)
}
}

0 comments on commit f635d30

Please sign in to comment.