Skip to content

Commit

Permalink
api: move vim25.Client.UseServiceVersion to soap.Client
Browse files Browse the repository at this point in the history
This method only changes fields in soap.Client
Moving it allows UseServiceVersion to be called after NewServiceVersion.
  • Loading branch information
dougm committed Apr 9, 2024
1 parent 007b3cd commit 367c55f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 40 deletions.
40 changes: 0 additions & 40 deletions vim25/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions vim25/soap/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"net/http/cookiejar"
"net/url"
"os"
"path"
"path/filepath"
"reflect"
"regexp"
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 367c55f

Please sign in to comment.