From 04ccf69ec633ed6bf918741d7f8c173d5da4eb1c Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Tue, 21 Nov 2023 15:57:09 -0800 Subject: [PATCH] vcsim: handle HostNotConnected when saving inventory --- govc/object/save.go | 14 ++++++++++++++ govc/test/host.bats | 14 ++++++++++++++ simulator/host_network_system.go | 6 ++++++ 3 files changed, 34 insertions(+) diff --git a/govc/object/save.go b/govc/object/save.go index e6c19bd4b..796b54900 100644 --- a/govc/object/save.go +++ b/govc/object/save.go @@ -31,6 +31,7 @@ import ( "github.com/vmware/govmomi/vim25" "github.com/vmware/govmomi/vim25/methods" "github.com/vmware/govmomi/vim25/mo" + "github.com/vmware/govmomi/vim25/soap" "github.com/vmware/govmomi/vim25/types" "github.com/vmware/govmomi/vim25/xml" ) @@ -153,6 +154,16 @@ var saveObjects = map[string]func(context.Context, *vim25.Client, types.ManagedO "HostNetworkSystem": saveHostNetworkSystem, } +func isNotConnected(err error) bool { + if soap.IsSoapFault(err) { + switch soap.ToSoapFault(err).VimFault().(type) { + case types.HostNotConnected: + return true + } + } + return false +} + func (cmd *save) save(content []types.ObjectContent) error { for _, x := range content { x.MissingSet = nil // drop any NoPermission faults @@ -174,6 +185,9 @@ func (cmd *save) save(content []types.ObjectContent) error { if method, ok := saveObjects[x.Obj.Type]; ok { objs, err := method(context.Background(), c, x.Obj) if err != nil { + if isNotConnected(err) { + continue + } return err } dir := filepath.Join(cmd.dir, ref) diff --git a/govc/test/host.bats b/govc/test/host.bats index 83fbb5e40..1aac47129 100755 --- a/govc/test/host.bats +++ b/govc/test/host.bats @@ -114,6 +114,20 @@ load test_helper govc host.vnic.info -json | jq . } +@test "host.vnic.hint" { + vcsim_env + + run govc host.vnic.hint -xml -host DC0_C0_H0 + assert_success + + run govc host.disconnect DC0_C0_H0 + assert_success + + run govc host.vnic.hint -xml -host DC0_C0_H0 + assert_failure + assert_matches HostNotConnected "$output" +} + @test "host.vswitch.info" { vcsim_env -esx diff --git a/simulator/host_network_system.go b/simulator/host_network_system.go index 4afdda0a1..351604f82 100644 --- a/simulator/host_network_system.go +++ b/simulator/host_network_system.go @@ -198,6 +198,12 @@ func (s *HostNetworkSystem) UpdateNetworkConfig(req *types.UpdateNetworkConfig) } func (s *HostNetworkSystem) QueryNetworkHint(req *types.QueryNetworkHint) soap.HasFault { + if s.Host.Runtime.ConnectionState != types.HostSystemConnectionStateConnected { + return &methods.QueryNetworkHintBody{ + Fault_: Fault("", &types.HostNotConnected{}), + } + } + return &methods.QueryNetworkHintBody{ Res: &types.QueryNetworkHintResponse{ Returnval: s.QueryNetworkHintResponse.Returnval,