@@ -18,29 +18,22 @@ import (
18
18
19
19
type vmwareAvailZone struct {
20
20
r mo.ComputeResource
21
- rPath string
22
21
pool * object.ResourcePool
23
22
hostFolder string
24
23
}
25
24
26
- // Name implements common.AvailabilityZone
25
+ // Name returns the "name" of the Vsphere availability zone, which is the path
26
+ // relative to the datacenter's host folder.
27
27
func (z * vmwareAvailZone ) Name () string {
28
- // Strip "/DataCenter1/host/" prefix from compute resource or resource pool path
29
- return strings .TrimPrefix (z .path () , z .hostFolder + "/" )
28
+ // Strip "/DataCenter1/host/" prefix from resource pool path
29
+ return strings .TrimPrefix (z .pool . InventoryPath , z .hostFolder + "/" )
30
30
}
31
31
32
32
// Available implements common.AvailabilityZone
33
33
func (z * vmwareAvailZone ) Available () bool {
34
34
return true
35
35
}
36
36
37
- func (z * vmwareAvailZone ) path () string {
38
- if z .pool == nil {
39
- return z .rPath
40
- }
41
- return z .pool .InventoryPath
42
- }
43
-
44
37
// AvailabilityZones is part of the common.ZonedEnviron interface.
45
38
func (env * environ ) AvailabilityZones (ctx context.ProviderCallContext ) (zones []common.AvailabilityZone , err error ) {
46
39
err = env .withSession (ctx , func (env * sessionEnviron ) error {
@@ -65,49 +58,45 @@ func (env *sessionEnviron) AvailabilityZones(ctx context.ProviderCallContext) ([
65
58
folders .HostFolder .InventoryPath , folders .HostFolder .Name ())
66
59
hostFolder := folders .HostFolder .InventoryPath
67
60
68
- computeResources , crPaths , err := env .client .ComputeResources (env .ctx )
61
+ computeResources , err := env .client .ComputeResources (env .ctx )
69
62
if err != nil {
70
63
HandleCredentialError (err , env , ctx )
71
64
return nil , errors .Trace (err )
72
65
}
73
66
var zones []common.AvailabilityZone
74
- for i , cr := range computeResources {
75
- if cr .Summary .GetComputeResourceSummary ().EffectiveCpu == 0 {
76
- logger .Debugf ("skipping empty compute resource %q" , cr .Name )
67
+ for _ , cr := range computeResources {
68
+ if cr .Resource . Summary .GetComputeResourceSummary ().EffectiveCpu == 0 {
69
+ logger .Debugf ("skipping empty compute resource %q" , cr .Resource . Name )
77
70
continue
78
71
}
79
72
80
- // Add an availability zone for this compute resource directly, eg:
81
- // "/DataCenter1/host/Host1"
82
- zone := & vmwareAvailZone {
83
- r : * cr ,
84
- rPath : crPaths [i ],
85
- hostFolder : hostFolder ,
86
- }
87
- logger .Tracef ("zone from compute resource: %s (cr.Name=%q rPath=%q hostFolder=%q)" ,
88
- zone .Name (), zone .r .Name , zone .rPath , zone .hostFolder )
89
- zones = append (zones , zone )
90
-
91
- // Then add an availability zone for each resource pool under this
92
- // compute resource, eg: "/DataCenter1/host/Host1/Resources"
93
- pools , err := env .client .ResourcePools (env .ctx , crPaths [i ]+ "/..." )
73
+ // Add an availability zone for each resource pool under this compute
74
+ // resource, eg: "/DataCenter1/host/Host1/Resources"
75
+ pools , err := env .client .ResourcePools (env .ctx , cr .Path + "/..." )
94
76
if err != nil {
95
77
HandleCredentialError (err , env , ctx )
96
78
return nil , errors .Trace (err )
97
79
}
98
80
for _ , pool := range pools {
99
- zone = & vmwareAvailZone {
100
- r : * cr ,
101
- rPath : crPaths [i ],
81
+ zone := & vmwareAvailZone {
82
+ r : * cr .Resource ,
102
83
pool : pool ,
103
84
hostFolder : hostFolder ,
104
85
}
105
- logger .Tracef ("zone from resource pool : %s (cr.Name=%q rPath =%q pool.InventoryPath=%q hostFolder=%q)" ,
106
- zone .Name (), zone .r .Name , zone .rPath , zone . pool .InventoryPath , zone .hostFolder )
86
+ logger .Tracef ("zone: %s (cr.Name=%q pool.InventoryPath=%q hostFolder=%q)" ,
87
+ zone .Name (), zone .r .Name , zone .pool .InventoryPath , zone .hostFolder )
107
88
zones = append (zones , zone )
108
89
}
109
90
}
110
91
92
+ if logger .IsDebugEnabled () {
93
+ zoneNames := make ([]string , len (zones ))
94
+ for i , zone := range zones {
95
+ zoneNames [i ] = zone .Name ()
96
+ }
97
+ logger .Debugf ("fetched availability zones: %q" , zoneNames )
98
+ }
99
+
111
100
env .zones = zones
112
101
return env .zones , nil
113
102
}
@@ -145,10 +134,6 @@ func (env *sessionEnviron) InstanceAvailabilityZoneNames(ctx context.ProviderCal
145
134
vm := inst .(* environInstance ).base
146
135
for _ , zone := range zones {
147
136
pool := zone .(* vmwareAvailZone ).pool
148
- if pool == nil {
149
- // Skip availability zones that aren't resource pools
150
- continue
151
- }
152
137
if pool .Reference ().Value == vm .ResourcePool .Value {
153
138
results [i ] = zone .Name ()
154
139
break
@@ -197,47 +182,25 @@ func (env *sessionEnviron) availZone(ctx context.ProviderCallContext, name strin
197
182
198
183
// ZoneMatches implements zoneMatcher interface to allow custom matching (see
199
184
// provider/common.ZoneMatches). For a Vsphere "availability zone" (host,
200
- // cluster, or resource pool), allow match on absolute path, path relative to
201
- // host folder, or legacy resource pool match.
185
+ // cluster, or resource pool), allow a match on the path relative to the host
186
+ // folder, or a legacy resource pool match.
202
187
func (env * environ ) ZoneMatches (zone , constraint string ) bool {
203
- return zoneMatches (zone , constraint )
204
- }
205
-
206
- func zoneMatches (zone , constraint string ) bool {
207
- // If they've specified an absolute path, strip the datacenter/host
208
- // segments; for example "/DataCenter1/host/Cluster1/Host1"
209
- // becomes "Cluster1/Host1". This allows the user to specify an
210
- // absolute path, like those from "govc find".
211
- //
212
- // TODO benhoyt: maybe we don't want absolute path matching at all,
213
- // because if there's a datacenter folder we don't know how deep it is,
214
- // so this will fail.
215
- if strings .HasPrefix (constraint , "/" ) {
216
- parts := strings .Split (constraint , "/" )
217
- // Must be at least ["" "DataCenter1" "host" "Cluster1"]
218
- if len (parts ) < 4 {
219
- return false
220
- }
221
- constraint = strings .Join (parts [3 :], "/" )
222
- }
223
-
224
188
// Allow match on full zone name (path without host folder prefix), for
225
189
// example "Cluster1/Host1".
226
190
if zone == constraint {
227
191
return true
228
192
}
229
193
230
- // Otherwise, for resource pools, allow them to omit the "Resources"
231
- // part of the path (for backwards compatibility). For example, for pool
194
+ // Otherwise allow them to omit the "Resources" part of the path (for
195
+ // backwards compatibility). For example, for pool
232
196
// "Host1/Resources/Parent/Child", allow a match on "Host1/Parent/Child".
233
- //
234
- // TODO: should we consider stripping "Resources" at any level? That may help with folders
235
197
parts := strings .Split (zone , "/" )
236
- if len ( parts ) > 2 && parts [ 1 ] == "Resources" {
237
- legacyZone := parts [ 0 ] + "/" + strings . Join ( parts [ 2 :], "/" )
238
- if legacyZone == constraint {
239
- return true
198
+ var partsWithoutResources [] string
199
+ for _ , part := range parts {
200
+ if part != "Resources" {
201
+ partsWithoutResources = append ( partsWithoutResources , part )
240
202
}
241
203
}
242
- return false
204
+ legacyZone := strings .Join (partsWithoutResources , "/" )
205
+ return legacyZone == constraint
243
206
}
0 commit comments