Skip to content

Commit

Permalink
helper/dvportgroup: Handle nil results from FromKey
Browse files Browse the repository at this point in the history
I'm not too sure how this can happen exactly, but we have gotten a crash
report on it (#456). Every invocation of DVSManagerLookupDvPortGroup
that I can see should return some sort of fault, however the report here
indicates that under some scenarios, a nil result can be returned
instead with no fault.

This patch handles this, wrapping the case in a specific error. Based on
the results of this (we will probably have to rely on user feedback to
see what exactly kinds of scenarios this can happen under), we might
handle the wrapped error higher up the stack and possibly ignore it,
depending on whether or not this is coming from cloned templates
immediately on post-refresh.
  • Loading branch information
vancluever committed Apr 12, 2018
1 parent 205031c commit 039ade1
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ import (
"github.com/vmware/govmomi/vim25/types"
)

// MissingPortGroupReferenceError is an error that gets returned when a port
// group lookup succeeds but does not return a MO to a
// DistributedVirtualPortgroup.
type MissingPortGroupReferenceError struct {
message string
}

// NewMissingPortGroupReferenceError returns a MissingPortGroupReferenceError
// with the supplied message.
func NewMissingPortGroupReferenceError(message string) error {
return &MissingPortGroupReferenceError{message}
}

func (e *MissingPortGroupReferenceError) Error() string {
return e.message
}

// FromKey gets a portgroup object from its key.
func FromKey(client *govmomi.Client, dvsUUID, pgKey string) (*object.DistributedVirtualPortgroup, error) {
dvsm := types.ManagedObjectReference{Type: "DistributedVirtualSwitchManager", Value: "DVSManager"}
Expand All @@ -28,6 +45,16 @@ func FromKey(client *govmomi.Client, dvsUUID, pgKey string) (*object.Distributed
return nil, err
}

if resp.Returnval == nil {
return nil, NewMissingPortGroupReferenceError(
fmt.Sprintf(
"portgroup lookup by key returned nil result for DVS UUID %q and portgroup key %q",
dvsUUID,
pgKey,
),
)
}

return FromMOID(client, resp.Returnval.Reference().Value)
}

Expand Down

0 comments on commit 039ade1

Please sign in to comment.