Skip to content

Commit

Permalink
Merge pull request #3563 from dougm/datastore-url
Browse files Browse the repository at this point in the history
api: extract NewDatastoreURL from Datastore.NewURL method
  • Loading branch information
dougm authored Sep 25, 2024
2 parents 44d4fb3 + cb0453c commit 3ec404a
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions object/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,33 @@ func (d Datastore) Path(path string) string {
}).String()
}

// NewURL constructs a url.URL with the given file path for datastore access over HTTP.
func (d Datastore) NewURL(path string) *url.URL {
u := d.c.URL()

scheme := u.Scheme
// NewDatastoreURL constructs a url.URL with the given file path for datastore access over HTTP.
func NewDatastoreURL(base url.URL, dcPath, dsName, path string) *url.URL {
scheme := base.Scheme
// In rare cases where vCenter and ESX are accessed using different schemes.
if overrideScheme := os.Getenv("GOVMOMI_DATASTORE_ACCESS_SCHEME"); overrideScheme != "" {
scheme = overrideScheme
}

return &url.URL{
Scheme: scheme,
Host: u.Host,
Path: fmt.Sprintf("/folder/%s", path),
RawQuery: url.Values{
"dcPath": []string{d.DatacenterPath},
"dsName": []string{d.Name()},
}.Encode(),
}
base.Scheme = scheme
base.Path = fmt.Sprintf("/folder/%s", path)
base.RawQuery = url.Values{
"dcPath": []string{dcPath},
"dsName": []string{dsName},
}.Encode()

return &base
}

// NewURL constructs a url.URL with the given file path for datastore access over HTTP.
// The Datastore object is used to derive url, dcPath and dsName params to NewDatastoreURL.
// For dcPath, Datastore.DatacenterPath must be set and for dsName, Datastore.InventoryPath.
// This is the case when the object.Datastore instance is created by Finder.
// Otherwise, Datastore.FindInventoryPath should be called first, to set DatacenterPath
// and InventoryPath.
func (d Datastore) NewURL(path string) *url.URL {
u := d.c.URL()
return NewDatastoreURL(*u, d.DatacenterPath, d.Name(), path)
}

func (d Datastore) Browser(ctx context.Context) (*HostDatastoreBrowser, error) {
Expand Down

0 comments on commit 3ec404a

Please sign in to comment.