From 367c55fe2058e9b456d9bb530a10e5481f862267 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Tue, 9 Apr 2024 15:03:45 -0700 Subject: [PATCH] api: move vim25.Client.UseServiceVersion to soap.Client This method only changes fields in soap.Client Moving it allows UseServiceVersion to be called after NewServiceVersion. --- vim25/client.go | 40 ---------------------------------------- vim25/soap/client.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 40 deletions(-) diff --git a/vim25/client.go b/vim25/client.go index 610133095..7349183ab 100644 --- a/vim25/client.go +++ b/vim25/client.go @@ -19,15 +19,11 @@ package vim25 import ( "context" "encoding/json" - "fmt" - "net/http" - "path" "strings" "github.com/vmware/govmomi/vim25/methods" "github.com/vmware/govmomi/vim25/soap" "github.com/vmware/govmomi/vim25/types" - "github.com/vmware/govmomi/vim25/xml" ) const ( @@ -88,42 +84,6 @@ func NewClient(ctx context.Context, rt soap.RoundTripper) (*Client, error) { return &c, nil } -// UseServiceVersion sets soap.Client.Version to the current version of the service endpoint via /sdk/vimServiceVersions.xml -func (c *Client) UseServiceVersion(kind ...string) error { - ns := "vim" - if len(kind) != 0 { - ns = kind[0] - } - - u := c.URL() - u.Path = path.Join(Path, ns+"ServiceVersions.xml") - - res, err := c.Get(u.String()) - if err != nil { - return err - } - - if res.StatusCode != http.StatusOK { - return fmt.Errorf("http.Get(%s): %s", u.Path, err) - } - - v := struct { - Namespace *string `xml:"namespace>name"` - Version *string `xml:"namespace>version"` - }{ - &c.Namespace, - &c.Version, - } - - err = xml.NewDecoder(res.Body).Decode(&v) - _ = res.Body.Close() - if err != nil { - return fmt.Errorf("xml.Decode(%s): %s", u.Path, err) - } - - return nil -} - // RoundTrip dispatches to the RoundTripper field. func (c *Client) RoundTrip(ctx context.Context, req, res soap.HasFault) error { return c.RoundTripper.RoundTrip(ctx, req, res) diff --git a/vim25/soap/client.go b/vim25/soap/client.go index a253ab5b8..7a4d9b308 100644 --- a/vim25/soap/client.go +++ b/vim25/soap/client.go @@ -34,6 +34,7 @@ import ( "net/http/cookiejar" "net/url" "os" + "path" "path/filepath" "reflect" "regexp" @@ -477,6 +478,42 @@ func (c *Client) SetCertificate(cert tls.Certificate) { t.TLSClientConfig.Certificates = []tls.Certificate{cert} } +// UseServiceVersion sets Client.Version to the current version of the service endpoint via /sdk/vimServiceVersions.xml +func (c *Client) UseServiceVersion(kind ...string) error { + ns := "vim" + if len(kind) != 0 { + ns = kind[0] + } + + u := c.URL() + u.Path = path.Join("/sdk", ns+"ServiceVersions.xml") + + res, err := c.Get(u.String()) + if err != nil { + return err + } + + if res.StatusCode != http.StatusOK { + return fmt.Errorf("http.Get(%s): %s", u.Path, err) + } + + v := struct { + Namespace *string `xml:"namespace>name"` + Version *string `xml:"namespace>version"` + }{ + &c.Namespace, + &c.Version, + } + + err = xml.NewDecoder(res.Body).Decode(&v) + _ = res.Body.Close() + if err != nil { + return fmt.Errorf("xml.Decode(%s): %s", u.Path, err) + } + + return nil +} + // Tunnel returns a Client configured to proxy requests through vCenter's http port 80, // to the SDK tunnel virtual host. Use of the SDK tunnel is required by LoginExtensionByCertificate() // and optional for other methods.