Skip to content

Commit

Permalink
Merge pull request vmware#3409 from dougm/cns-client
Browse files Browse the repository at this point in the history
fix: make cns.Client independent of vim25.Client
  • Loading branch information
dougm authored Apr 12, 2024
2 parents 7a51544 + 095f926 commit 50fb6a5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
22 changes: 19 additions & 3 deletions cns/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,25 @@ type Client struct {
// NewClient creates a new CNS client
func NewClient(ctx context.Context, c *vim25.Client) (*Client, error) {
sc := c.Client.NewServiceClient(Path, Namespace)
sc.Namespace = c.Namespace
sc.Version = c.Version
return &Client{sc, sc, c}, nil

// Use current vCenter vsan version by default
err := sc.UseServiceVersion(Namespace)
if err != nil {
return nil, err
}

// PropertyCollector related methods (task.Wait) need to send requests to vim25.Path (/sdk).
// This vim25.Client shares the same http.Transport and Namespace/Version, but requests to '/sdk'
rt := sc.NewServiceClient(vim25.Path, Namespace)
rt.Version = sc.Version

vc := &vim25.Client{
ServiceContent: c.ServiceContent,
Client: rt,
RoundTripper: rt,
}

return &Client{sc, sc, vc}, nil
}

// RoundTrip dispatches to the RoundTripper field.
Expand Down
4 changes: 0 additions & 4 deletions cns/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ func TestClient(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// UseServiceVersion sets soap.Client.Version to the current version of the service endpoint via /sdk/vsanServiceVersions.xml
c.UseServiceVersion("vsan")
cnsClient, err := NewClient(ctx, c.Client)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -561,8 +559,6 @@ func TestClient(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// UseServiceVersion sets soap.Client.Version to the current version of the service endpoint via /sdk/vsanServiceVersions.xml
remoteVcClient.UseServiceVersion("vsan")
remoteCnsClient, err := NewClient(ctx, remoteVcClient.Client)
if err != nil {
t.Fatal(err)
Expand Down
1 change: 0 additions & 1 deletion govc/flags/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ func (flag *ClientFlag) CnsClient() (*cns.Client, error) {
if err != nil {
return nil, err
}
_ = vc.UseServiceVersion("vsan")

c, err := cns.NewClient(context.Background(), vc)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions simulator/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,22 @@ func (s *Service) ServiceVersions(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, versions, s.client.ServiceContent.About.ApiVersion)
}

// ServiceVersionsVsan handler for the /sdk/vsanServiceVersions.xml path.
func (s *Service) ServiceVersionsVsan(w http.ResponseWriter, r *http.Request) {
const versions = xml.Header + `<namespaces version="1.0">
<namespace>
<name>urn:vsan</name>
<version>%s</version>
<priorVersions>
<version>6.7</version>
<version>6.6</version>
</priorVersions>
</namespace>
</namespaces>
`
fmt.Fprintf(w, versions, s.client.ServiceContent.About.ApiVersion)
}

// defaultIP returns addr.IP if specified, otherwise attempts to find a non-loopback ipv4 IP
func defaultIP(addr *net.TCPAddr) string {
if !addr.IP.IsUnspecified() {
Expand Down Expand Up @@ -667,6 +683,7 @@ func (s *Service) NewServer() *Server {

mux := s.ServeMux
mux.HandleFunc(Map.Path+"/vimServiceVersions.xml", s.ServiceVersions)
mux.HandleFunc(Map.Path+"/vsanServiceVersions.xml", s.ServiceVersionsVsan)
mux.HandleFunc(folderPrefix, s.ServeDatastore)
mux.HandleFunc(guestPrefix, ServeGuest)
mux.HandleFunc(nfcPrefix, ServeNFC)
Expand Down
2 changes: 1 addition & 1 deletion vim25/soap/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ func (c *Client) UseServiceVersion(kind ...string) error {
}

if res.StatusCode != http.StatusOK {
return fmt.Errorf("http.Get(%s): %s", u.Path, err)
return fmt.Errorf("http.Get(%s): %s", u.Path, res.Status)
}

v := struct {
Expand Down

0 comments on commit 50fb6a5

Please sign in to comment.