Skip to content

Commit a31f8c9

Browse files
authored
refactor: enhance resource pool handling (#665)
1 parent e5b9780 commit a31f8c9

2 files changed

Lines changed: 79 additions & 14 deletions

File tree

builder/vsphere/driver/resource_pool.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99
"fmt"
1010
"log"
11+
"strings"
1112

1213
"github.com/vmware/govmomi/find"
1314
"github.com/vmware/govmomi/object"
@@ -34,29 +35,47 @@ func (d *VCenterDriver) NewResourcePool(ref *types.ManagedObjectReference) *Reso
3435
// pool or a vApp if the specified pool is not found. Returns a ResourcePool
3536
// object or an error if neither the specified nor default pool is accessible.
3637
func (d *VCenterDriver) FindResourcePool(cluster string, host string, name string) (*ResourcePool, error) {
38+
name = strings.TrimSpace(name)
39+
3740
var res string
3841
if cluster != "" {
3942
res = cluster
4043
} else {
4144
res = host
4245
}
4346

44-
resourcePath := fmt.Sprintf("%v/Resources/%v", res, name)
47+
var resourcePath string
48+
switch {
49+
case name == "":
50+
resourcePath = fmt.Sprintf("%v/Resources", res)
51+
case strings.HasPrefix(name, "/"):
52+
resourcePath = name
53+
default:
54+
resourcePath = fmt.Sprintf("%v/Resources/%v", res, name)
55+
}
56+
4557
p, err := d.Finder.ResourcePool(d.Ctx, resourcePath)
4658
if err != nil {
47-
log.Printf("[WARN] %s not found. Looking for default resource pool.", resourcePath)
59+
log.Printf("[WARN] %s not found. Attempting to use default resource pool.", resourcePath)
60+
4861
dp, dperr := d.Finder.DefaultResourcePool(d.Ctx)
4962
var notFoundError *find.NotFoundError
5063
if errors.As(dperr, &notFoundError) {
5164
vapp, verr := d.Finder.VirtualApp(d.Ctx, name)
5265
if verr != nil {
53-
return nil, err
66+
return nil, fmt.Errorf("could not resolve '%s': %w", name, verr)
5467
}
5568
dp = vapp.ResourcePool
69+
} else if dperr != nil {
70+
return nil, fmt.Errorf("error finding default resource pool: %w", dperr)
5671
}
5772
p = dp
5873
}
5974

75+
if p == nil {
76+
return nil, fmt.Errorf("resource pool '%s' resolved to nil", resourcePath)
77+
}
78+
6079
return &ResourcePool{
6180
pool: p,
6281
driver: d,

builder/vsphere/driver/resource_pool_test.go

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,63 @@ func TestVCenterDriver_FindResourcePool(t *testing.T) {
1717
}
1818
defer sim.Close()
1919

20-
res, err := sim.driver.FindResourcePool("", "DC0_H0", "")
21-
if err != nil {
22-
t.Fatalf("unexpected error: '%s'", err)
23-
}
24-
if res == nil {
25-
t.Fatalf("unexpected result: expected '%v', but returned 'nil'", res)
26-
}
27-
expectedResourcePool := "Resources"
28-
if res.pool.Name() != expectedResourcePool {
29-
t.Fatalf("unexpected result: expected '%s', but returned '%s'", expectedResourcePool, res.pool.Name())
30-
}
20+
t.Run("empty name with host", func(t *testing.T) {
21+
res, err := sim.driver.FindResourcePool("", "DC0_H0", "")
22+
if err != nil {
23+
t.Fatalf("unexpected error: '%s'", err)
24+
}
25+
if res == nil {
26+
t.Fatalf("unexpected result: expected resource pool, but returned 'nil'")
27+
}
28+
expectedResourcePool := "Resources"
29+
if res.pool.Name() != expectedResourcePool {
30+
t.Fatalf("unexpected result: expected '%s', but returned '%s'", expectedResourcePool, res.pool.Name())
31+
}
32+
})
33+
34+
t.Run("empty name with cluster", func(t *testing.T) {
35+
res, err := sim.driver.FindResourcePool("DC0_C0", "", "")
36+
if err != nil {
37+
t.Fatalf("unexpected error: '%s'", err)
38+
}
39+
if res == nil {
40+
t.Fatalf("unexpected result: expected resource pool, but returned 'nil'")
41+
}
42+
if res.pool.Name() != "Resources" {
43+
t.Fatalf("unexpected result: expected 'Resources', but returned '%s'", res.pool.Name())
44+
}
45+
})
46+
47+
t.Run("relative path", func(t *testing.T) {
48+
res, err := sim.driver.FindResourcePool("DC0_C0", "", "foo")
49+
50+
if err == nil {
51+
t.Fatalf("expected error when using unknown relative resource pool path 'foo', but got none")
52+
}
53+
if res != nil {
54+
t.Fatalf("unexpected result: expected no resource pool for unknown path 'foo', but got one")
55+
}
56+
})
57+
58+
t.Run("absolute path", func(t *testing.T) {
59+
res, err := sim.driver.FindResourcePool("", "", "/DC0/host/DC0_H0/Resources")
60+
if err != nil {
61+
t.Fatalf("unexpected error: '%s'", err)
62+
}
63+
if res == nil || res.pool == nil {
64+
t.Fatalf("unexpected result: expected resource pool, but returned 'nil'")
65+
}
66+
})
67+
68+
t.Run("whitespace trimming", func(t *testing.T) {
69+
res, err := sim.driver.FindResourcePool("", "DC0_H0", " ")
70+
if err != nil {
71+
t.Fatalf("unexpected error: '%s'", err)
72+
}
73+
if res == nil {
74+
t.Fatalf("unexpected result: expected resource pool, but returned 'nil'")
75+
}
76+
})
3177
}
3278

3379
func TestVCenterDriver_FindResourcePoolStandaloneESX(t *testing.T) {

0 commit comments

Comments
 (0)