Skip to content

Commit 89942b7

Browse files
committed
vcsim: Use the simulator's URL.Host in NFC lease URLs
Host should only be '*' when connecting directly to an ESX host. Signed-off-by: Doug MacEachern <[email protected]>
1 parent 8d78f54 commit 89942b7

File tree

5 files changed

+58
-20
lines changed

5 files changed

+58
-20
lines changed

govc/test/import.bats

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,31 @@ load test_helper
287287

288288
run govc import.ova -ds $pod "$GOVC_IMAGES/$TTYLINUX_NAME.ova"
289289
assert_success
290+
291+
run govc import.ova -ds $pod -name test-url -lease -json "$GOVC_IMAGES/$TTYLINUX_NAME.ova"
292+
assert_success
293+
294+
run jq -r .info.deviceUrl[].url <<<"$output"
295+
assert_success
296+
297+
# Device URL.Host should be the same as vcsim's https://127.0.0.1:$port/nfc/.../disk-0.vmdk
298+
run govc env -x -u "$output" GOVC_URL_HOST
299+
assert_success "$(govc env -x GOVC_URL_HOST)"
300+
}
301+
302+
@test "import esx" {
303+
vcsim_env -esx
304+
305+
run govc import.ova "$GOVC_IMAGES/$TTYLINUX_NAME.ova"
306+
assert_success
307+
308+
run govc import.ova -name test-url -lease -json "$GOVC_IMAGES/$TTYLINUX_NAME.ova"
309+
assert_success
310+
311+
run jq -r .info.deviceUrl[].url <<<"$output"
312+
assert_success
313+
314+
# Device URL.Host should be '*' https://*:$port/nfc/.../disk-0.vmdk
315+
run govc env -x -u "$output" GOVC_URL_HOST
316+
assert_success "*"
290317
}

simulator/http_nfc_lease.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"io"
1313
"log"
1414
"net/http"
15+
"net/url"
1516
"os"
1617
"strings"
1718
"sync"
@@ -134,6 +135,22 @@ func newHttpNfcLease(ctx *Context) *HttpNfcLease {
134135
return lease
135136
}
136137

138+
func leaseURL(ctx *Context) *url.URL {
139+
opt := ctx.Map.OptionManager().find("vcsim.server.url")
140+
141+
u, _ := url.Parse(opt.Value.(string))
142+
143+
// See NfcLease.DeviceUrl doc:
144+
// If a "*" is returned the client must substitute "*" with the
145+
// hostname or IP address used when connecting to the server.
146+
// This is the case when connecting directly to an ESX host.
147+
if ctx.Map.IsESX() {
148+
u.Host = "*"
149+
}
150+
151+
return u
152+
}
153+
137154
func (l *HttpNfcLease) HttpNfcLeaseComplete(ctx *Context, req *types.HttpNfcLeaseComplete) soap.HasFault {
138155
ctx.Session.Remove(ctx, req.This)
139156
nfcLease.Delete(req.This)

simulator/resource_pool.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package simulator
66

77
import (
88
"fmt"
9-
"net/url"
109
"path"
1110
"strings"
1211

@@ -269,6 +268,7 @@ func (p *ResourcePool) ImportVApp(ctx *Context, req *types.ImportVApp) soap.HasF
269268
device := object.VirtualDeviceList(vm.Config.Hardware.Device)
270269
ndevice := make(map[string]int)
271270
var urls []types.HttpNfcLeaseDeviceUrl
271+
u := leaseURL(ctx)
272272

273273
for _, d := range device {
274274
info, ok := d.GetVirtualDevice().Backing.(types.BaseVirtualDeviceFileBackingInfo)
@@ -286,14 +286,11 @@ func (p *ResourcePool) ImportVApp(ctx *Context, req *types.ImportVApp) soap.HasF
286286
n := ndevice[kind]
287287
ndevice[kind]++
288288

289+
u.Path = nfcPrefix + path.Join(ref.Value, name)
289290
urls = append(urls, types.HttpNfcLeaseDeviceUrl{
290-
Key: fmt.Sprintf("/%s/%s:%d", vm.Self.Value, kind, n),
291-
ImportKey: fmt.Sprintf("/%s/%s:%d", vm.Name, kind, n),
292-
Url: (&url.URL{
293-
Scheme: "https",
294-
Host: "*",
295-
Path: nfcPrefix + path.Join(ref.Value, name),
296-
}).String(),
291+
Key: fmt.Sprintf("/%s/%s:%d", vm.Self.Value, kind, n),
292+
ImportKey: fmt.Sprintf("/%s/%s:%d", vm.Name, kind, n),
293+
Url: u.String(),
297294
SslThumbprint: "",
298295
Disk: types.NewBool(disk),
299296
TargetId: name,

simulator/simulator.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -744,12 +744,12 @@ func (s *Service) NewServer() *Server {
744744
}
745745
}
746746
}
747-
m.Setting = append(m.Setting,
748-
&types.OptionValue{
747+
m.UpdateOptions(&types.UpdateOptions{
748+
ChangedValue: []types.BaseOptionValue{&types.OptionValue{
749749
Key: "vcsim.server.url",
750750
Value: u.String(),
751-
},
752-
)
751+
}},
752+
})
753753

754754
u.User = s.Listen.User
755755
if u.User == nil {

simulator/snapshot.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package simulator
66

77
import (
88
"fmt"
9-
"net/url"
109
"os"
1110
"path"
1211

@@ -180,6 +179,7 @@ func (v *VirtualMachineSnapshot) ExportSnapshot(ctx *Context, req *types.ExportS
180179
device := object.VirtualDeviceList(v.Config.Hardware.Device)
181180
ndevice := make(map[string]int)
182181
var urls []types.HttpNfcLeaseDeviceUrl
182+
u := leaseURL(ctx)
183183

184184
for _, d := range device {
185185
info, ok := d.GetVirtualDevice().Backing.(types.BaseVirtualDeviceFileBackingInfo)
@@ -197,14 +197,11 @@ func (v *VirtualMachineSnapshot) ExportSnapshot(ctx *Context, req *types.ExportS
197197
n := ndevice[kind]
198198
ndevice[kind]++
199199

200+
u.Path = nfcPrefix + path.Join(lease.Reference().Value, name)
200201
urls = append(urls, types.HttpNfcLeaseDeviceUrl{
201-
Key: fmt.Sprintf("/%s/%s:%d", vm.Self.Value, kind, n),
202-
ImportKey: fmt.Sprintf("/%s/%s:%d", vm.Name, kind, n),
203-
Url: (&url.URL{
204-
Scheme: "https",
205-
Host: "*",
206-
Path: nfcPrefix + path.Join(lease.Reference().Value, name),
207-
}).String(),
202+
Key: fmt.Sprintf("/%s/%s:%d", vm.Self.Value, kind, n),
203+
ImportKey: fmt.Sprintf("/%s/%s:%d", vm.Name, kind, n),
204+
Url: u.String(),
208205
SslThumbprint: "",
209206
Disk: types.NewBool(disk),
210207
TargetId: name,

0 commit comments

Comments
 (0)