Skip to content

Commit

Permalink
Merge pull request #3131 from dougm/issue-3073
Browse files Browse the repository at this point in the history
fix: add esxcli.Fault and revert Error() string
  • Loading branch information
akutz authored May 19, 2023
2 parents 896f1ed + c4ffe46 commit 88aa85c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
8 changes: 6 additions & 2 deletions govc/host/esxcli/esxcli.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved.
Copyright (c) 2014-2023 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -18,6 +18,7 @@ package esxcli

import (
"context"
"errors"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -92,6 +93,9 @@ func (cmd *esxcli) Run(ctx context.Context, f *flag.FlagSet) error {

res, err := e.Run(f.Args())
if err != nil {
if f, ok := err.(*Fault); ok {
return errors.New(f.messageDetail())
}
return err
}

Expand Down
29 changes: 22 additions & 7 deletions govc/host/esxcli/executor.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved.
Copyright (c) 2014-2023 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -18,7 +18,6 @@ package esxcli

import (
"context"
"errors"
"fmt"

"github.com/vmware/govmomi/internal"
Expand All @@ -27,6 +26,23 @@ import (
"github.com/vmware/govmomi/vim25/xml"
)

type Fault struct {
Message string
Detail string
}

func (f Fault) Error() string {
return f.Message
}

func (f Fault) messageDetail() string {
if f.Detail != "" {
return fmt.Sprintf("%s %s", f.Message, f.Detail)
}

return f.Message
}

type Executor struct {
c *vim25.Client
host *object.HostSystem
Expand Down Expand Up @@ -138,11 +154,10 @@ func (e *Executor) Execute(req *internal.ExecuteSoapRequest, res interface{}) er

if x.Returnval != nil {
if x.Returnval.Fault != nil {
msg := x.Returnval.Fault.FaultMsg
if x.Returnval.Fault.FaultDetail != "" {
msg = fmt.Sprintf("%s %s", msg, x.Returnval.Fault.FaultDetail)
return &Fault{
x.Returnval.Fault.FaultMsg,
x.Returnval.Fault.FaultDetail,
}
return errors.New(msg)
}

if err := xml.Unmarshal([]byte(x.Returnval.Response), res); err != nil {
Expand Down

0 comments on commit 88aa85c

Please sign in to comment.