From c4cab69048d671fe5a93391e8e627bcc7f0e8092 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Mon, 25 Sep 2017 20:24:35 -0700 Subject: [PATCH] Add support for SOAP request operation ID header Fixes #749 --- govc/cli/command.go | 7 +++++++ govc/test/logs.bats | 11 +++++++++++ vim25/soap/client.go | 18 ++++++++++++------ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/govc/cli/command.go b/govc/cli/command.go index bac228962..20dae57a4 100644 --- a/govc/cli/command.go +++ b/govc/cli/command.go @@ -25,6 +25,8 @@ import ( "os" "sort" "text/tabwriter" + + "github.com/vmware/govmomi/vim25/types" ) type HasFlags interface { @@ -136,6 +138,11 @@ func Run(args []string) int { fs.SetOutput(ioutil.Discard) ctx := context.Background() + + if id := os.Getenv("GOVC_OPERATION_ID"); id != "" { + ctx = context.WithValue(ctx, types.ID{}, id) + } + cmd.Register(ctx, fs) if err = fs.Parse(args[1:]); err != nil { diff --git a/govc/test/logs.bats b/govc/test/logs.bats index e9ceb48a7..a90f4c4ef 100755 --- a/govc/test/logs.bats +++ b/govc/test/logs.bats @@ -44,3 +44,14 @@ load test_helper run govc logs.ls -host enoent assert_success } + +@test "logs opid" { + esx_env + + id=$(new_id) + + run env GOVC_OPERATION_ID="$id" govc events + assert_success + + govc logs | grep "$id" +} diff --git a/vim25/soap/client.go b/vim25/soap/client.go index 8747e07b5..9f9830ad5 100644 --- a/vim25/soap/client.go +++ b/vim25/soap/client.go @@ -60,6 +60,7 @@ const ( type header struct { Cookie string `xml:"vcSessionCookie,omitempty"` + ID string `xml:"operationID,omitempty"` } type Client struct { @@ -78,7 +79,7 @@ type Client struct { Version string // Vim version UserAgent string - header *header + cookie string } var schemeMatch = regexp.MustCompile(`^\w+://`) @@ -168,10 +169,7 @@ func (c *Client) NewServiceClient(path string, namespace string) *Client { // Set SOAP Header cookie for _, cookie := range client.Jar.Cookies(u) { if cookie.Name == "vmware_soap_session" { - client.header = &header{ - Cookie: cookie.Value, - } - + client.cookie = cookie.Value break } } @@ -433,7 +431,15 @@ func (c *Client) RoundTrip(ctx context.Context, reqBody, resBody HasFault) error reqEnv := Envelope{Body: reqBody} resEnv := Envelope{Body: resBody} - reqEnv.Header = c.header + h := &header{ + Cookie: c.cookie, + } + + if id, ok := ctx.Value(types.ID{}).(string); ok { + h.ID = id + } + + reqEnv.Header = h // Create debugging context for this round trip d := c.d.newRoundTrip()