Fix for Support selection of datastore for dynamic provisioning in vSphere
This commit is contained in:
committed by
Ritesh H Shukla
parent
b201ac2f8f
commit
12f75f0b86
127
vendor/github.com/vmware/govmomi/object/datastore.go
generated
vendored
127
vendor/github.com/vmware/govmomi/object/datastore.go
generated
vendored
@@ -24,6 +24,7 @@ import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
@@ -33,7 +34,6 @@ import (
|
||||
"github.com/vmware/govmomi/vim25/mo"
|
||||
"github.com/vmware/govmomi/vim25/soap"
|
||||
"github.com/vmware/govmomi/vim25/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// DatastoreNoSuchDirectoryError is returned when a directory could not be found.
|
||||
@@ -58,6 +58,8 @@ func (e DatastoreNoSuchFileError) Error() string {
|
||||
|
||||
type Datastore struct {
|
||||
Common
|
||||
|
||||
DatacenterPath string
|
||||
}
|
||||
|
||||
func NewDatastore(c *vim25.Client, ref types.ManagedObjectReference) *Datastore {
|
||||
@@ -67,26 +69,14 @@ func NewDatastore(c *vim25.Client, ref types.ManagedObjectReference) *Datastore
|
||||
}
|
||||
|
||||
func (d Datastore) Path(path string) string {
|
||||
name := d.Name()
|
||||
if name == "" {
|
||||
panic("expected non-empty name")
|
||||
}
|
||||
|
||||
return fmt.Sprintf("[%s] %s", name, path)
|
||||
return (&DatastorePath{
|
||||
Datastore: d.Name(),
|
||||
Path: path,
|
||||
}).String()
|
||||
}
|
||||
|
||||
// URL for datastore access over HTTP
|
||||
func (d Datastore) URL(ctx context.Context, dc *Datacenter, path string) (*url.URL, error) {
|
||||
var mdc mo.Datacenter
|
||||
if err := dc.Properties(ctx, dc.Reference(), []string{"name"}, &mdc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var mds mo.Datastore
|
||||
if err := d.Properties(ctx, d.Reference(), []string{"name"}, &mds); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 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()
|
||||
|
||||
return &url.URL{
|
||||
@@ -94,10 +84,15 @@ func (d Datastore) URL(ctx context.Context, dc *Datacenter, path string) (*url.U
|
||||
Host: u.Host,
|
||||
Path: fmt.Sprintf("/folder/%s", path),
|
||||
RawQuery: url.Values{
|
||||
"dcPath": []string{mdc.Name},
|
||||
"dsName": []string{mds.Name},
|
||||
"dcPath": []string{d.DatacenterPath},
|
||||
"dsName": []string{d.Name()},
|
||||
}.Encode(),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// URL is deprecated, use NewURL instead.
|
||||
func (d Datastore) URL(ctx context.Context, dc *Datacenter, path string) (*url.URL, error) {
|
||||
return d.NewURL(path), nil
|
||||
}
|
||||
|
||||
func (d Datastore) Browser(ctx context.Context) (*HostDatastoreBrowser, error) {
|
||||
@@ -111,6 +106,27 @@ func (d Datastore) Browser(ctx context.Context) (*HostDatastoreBrowser, error) {
|
||||
return NewHostDatastoreBrowser(d.c, do.Browser), nil
|
||||
}
|
||||
|
||||
func (d Datastore) useServiceTicket() bool {
|
||||
// If connected to workstation, service ticketing not supported
|
||||
// If connected to ESX, service ticketing not needed
|
||||
if !d.c.IsVC() {
|
||||
return false
|
||||
}
|
||||
|
||||
key := "GOVMOMI_USE_SERVICE_TICKET"
|
||||
|
||||
val := d.c.URL().Query().Get(key)
|
||||
if val == "" {
|
||||
val = os.Getenv(key)
|
||||
}
|
||||
|
||||
if val == "1" || val == "true" {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (d Datastore) useServiceTicketHostName(name string) bool {
|
||||
// No need if talking directly to ESX.
|
||||
if !d.c.IsVC() {
|
||||
@@ -147,39 +163,62 @@ func (d Datastore) useServiceTicketHostName(name string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ServiceTicket obtains a ticket via AcquireGenericServiceTicket and returns it an http.Cookie with the url.URL
|
||||
// that can be used along with the ticket cookie to access the given path.
|
||||
func (d Datastore) ServiceTicket(ctx context.Context, path string, method string) (*url.URL, *http.Cookie, error) {
|
||||
// We are uploading to an ESX host
|
||||
u := &url.URL{
|
||||
Scheme: d.c.URL().Scheme,
|
||||
Host: d.c.URL().Host,
|
||||
Path: fmt.Sprintf("/folder/%s", path),
|
||||
RawQuery: url.Values{
|
||||
"dsName": []string{d.Name()},
|
||||
}.Encode(),
|
||||
}
|
||||
type datastoreServiceTicketHostKey struct{}
|
||||
|
||||
// HostContext returns a Context where the given host will be used for datastore HTTP access
|
||||
// via the ServiceTicket method.
|
||||
func (d Datastore) HostContext(ctx context.Context, host *HostSystem) context.Context {
|
||||
return context.WithValue(ctx, datastoreServiceTicketHostKey{}, host)
|
||||
}
|
||||
|
||||
// ServiceTicket obtains a ticket via AcquireGenericServiceTicket and returns it an http.Cookie with the url.URL
|
||||
// that can be used along with the ticket cookie to access the given path. An host is chosen at random unless the
|
||||
// the given Context was created with a specific host via the HostContext method.
|
||||
func (d Datastore) ServiceTicket(ctx context.Context, path string, method string) (*url.URL, *http.Cookie, error) {
|
||||
u := d.NewURL(path)
|
||||
|
||||
host, ok := ctx.Value(datastoreServiceTicketHostKey{}).(*HostSystem)
|
||||
|
||||
if !ok {
|
||||
if !d.useServiceTicket() {
|
||||
return u, nil, nil
|
||||
}
|
||||
|
||||
// If connected to VC, the ticket request must be for an ESX host.
|
||||
if d.c.IsVC() {
|
||||
hosts, err := d.AttachedHosts(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if len(hosts) == 0 {
|
||||
return nil, nil, fmt.Errorf("no hosts attached to datastore %#v", d.Reference())
|
||||
// Fallback to letting vCenter choose a host
|
||||
return u, nil, nil
|
||||
}
|
||||
|
||||
// Pick a random attached host
|
||||
host := hosts[rand.Intn(len(hosts))]
|
||||
name, err := host.ObjectName(ctx)
|
||||
host = hosts[rand.Intn(len(hosts))]
|
||||
}
|
||||
|
||||
ips, err := host.ManagementIPs(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if len(ips) > 0 {
|
||||
// prefer a ManagementIP
|
||||
u.Host = ips[0].String()
|
||||
} else {
|
||||
// fallback to inventory name
|
||||
u.Host, err = host.ObjectName(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
u.Host = name
|
||||
}
|
||||
|
||||
// VC datacenter path will not be valid against ESX
|
||||
q := u.Query()
|
||||
delete(q, "dcPath")
|
||||
u.RawQuery = q.Encode()
|
||||
|
||||
spec := types.SessionManagerHttpServiceRequestSpec{
|
||||
Url: u.String(),
|
||||
// See SessionManagerHttpServiceRequestSpecMethod enum
|
||||
@@ -202,6 +241,8 @@ func (d Datastore) ServiceTicket(ctx context.Context, path string, method string
|
||||
u.Host = ticket.HostName
|
||||
}
|
||||
|
||||
d.Client().SetThumbprint(u.Host, ticket.SslThumbprint)
|
||||
|
||||
return u, cookie, nil
|
||||
}
|
||||
|
||||
@@ -313,7 +354,7 @@ func (d Datastore) AttachedHosts(ctx context.Context) ([]*HostSystem, error) {
|
||||
return hosts, nil
|
||||
}
|
||||
|
||||
// AttachedHosts returns hosts that have this Datastore attached, accessible and writable and are members of the given cluster.
|
||||
// AttachedClusterHosts returns hosts that have this Datastore attached, accessible and writable and are members of the given cluster.
|
||||
func (d Datastore) AttachedClusterHosts(ctx context.Context, cluster *ComputeResource) ([]*HostSystem, error) {
|
||||
var hosts []*HostSystem
|
||||
|
||||
@@ -358,12 +399,12 @@ func (d Datastore) Stat(ctx context.Context, file string) (types.BaseFileInfo, e
|
||||
}
|
||||
|
||||
dsPath := d.Path(path.Dir(file))
|
||||
task, err := b.SearchDatastore(context.TODO(), dsPath, &spec)
|
||||
task, err := b.SearchDatastore(ctx, dsPath, &spec)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info, err := task.WaitForResult(context.TODO(), nil)
|
||||
info, err := task.WaitForResult(ctx, nil)
|
||||
if err != nil {
|
||||
if info == nil || info.Error != nil {
|
||||
_, ok := info.Error.Fault.(*types.FileNotFound)
|
||||
|
||||
Reference in New Issue
Block a user