2017-08-23 20:06:50 -04:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2018-10-31 17:42:24 -04:00
|
|
|
"fmt"
|
2020-02-14 11:42:29 -05:00
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
"github.com/vmware/govmomi/object"
|
2017-08-24 16:18:14 -04:00
|
|
|
"github.com/vmware/govmomi/vim25/mo"
|
2018-02-01 06:47:09 -05:00
|
|
|
"github.com/vmware/govmomi/vim25/soap"
|
2018-10-31 17:42:24 -04:00
|
|
|
"github.com/vmware/govmomi/vim25/types"
|
2017-08-23 20:06:50 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Datastore struct {
|
2018-02-01 06:47:09 -05:00
|
|
|
ds *object.Datastore
|
2017-08-23 20:06:50 -04:00
|
|
|
driver *Driver
|
|
|
|
}
|
|
|
|
|
2017-08-24 16:18:14 -04:00
|
|
|
func (d *Driver) NewDatastore(ref *types.ManagedObjectReference) *Datastore {
|
|
|
|
return &Datastore{
|
2018-02-01 06:47:09 -05:00
|
|
|
ds: object.NewDatastore(d.client.Client, *ref),
|
2017-08-24 16:18:14 -04:00
|
|
|
driver: d,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 19:03:47 -04:00
|
|
|
// If name is an empty string, then resolve host's one
|
|
|
|
func (d *Driver) FindDatastore(name string, host string) (*Datastore, error) {
|
|
|
|
if name == "" {
|
|
|
|
h, err := d.FindHost(host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
i, err := h.Info("datastore")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(i.Datastore) > 1 {
|
|
|
|
return nil, fmt.Errorf("Host has multiple datastores. Specify it explicitly")
|
|
|
|
}
|
|
|
|
|
|
|
|
ds := d.NewDatastore(&i.Datastore[0])
|
|
|
|
inf, err := ds.Info("name")
|
2019-07-08 10:48:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-20 19:03:47 -04:00
|
|
|
name = inf.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
ds, err := d.finder.Datastore(d.ctx, name)
|
2018-01-24 08:35:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-20 19:03:47 -04:00
|
|
|
|
2018-01-24 08:35:04 -05:00
|
|
|
return &Datastore{
|
|
|
|
ds: ds,
|
|
|
|
driver: d,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-08-24 16:18:14 -04:00
|
|
|
func (ds *Datastore) Info(params ...string) (*mo.Datastore, error) {
|
|
|
|
var p []string
|
|
|
|
if len(params) == 0 {
|
|
|
|
p = []string{"*"}
|
|
|
|
} else {
|
|
|
|
p = params
|
|
|
|
}
|
|
|
|
var info mo.Datastore
|
|
|
|
err := ds.ds.Properties(ds.driver.ctx, ds.ds.Reference(), p, &info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &info, nil
|
|
|
|
}
|
2018-01-24 08:35:04 -05:00
|
|
|
|
|
|
|
func (ds *Datastore) FileExists(path string) bool {
|
|
|
|
_, err := ds.ds.Stat(ds.driver.ctx, path)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *Datastore) Name() string {
|
|
|
|
return ds.ds.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *Datastore) ResolvePath(path string) string {
|
|
|
|
return ds.ds.Path(path)
|
|
|
|
}
|
2018-02-01 06:47:09 -05:00
|
|
|
|
2020-05-05 14:34:15 -04:00
|
|
|
func (ds *Datastore) UploadFile(src, dst, host string, set_host_for_datastore_uploads bool) error {
|
2018-02-01 06:47:09 -05:00
|
|
|
p := soap.DefaultUpload
|
2019-01-04 16:55:04 -05:00
|
|
|
ctx := ds.driver.ctx
|
2020-05-05 14:34:15 -04:00
|
|
|
|
|
|
|
if set_host_for_datastore_uploads && host != "" {
|
|
|
|
h, err := ds.driver.FindHost(host)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx = ds.ds.HostContext(ctx, h.host)
|
|
|
|
}
|
|
|
|
|
2018-11-13 12:22:19 -05:00
|
|
|
return ds.ds.UploadFile(ctx, src, dst, &p)
|
2018-02-01 06:47:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *Datastore) Delete(path string) error {
|
|
|
|
dc, err := ds.driver.finder.Datacenter(ds.driver.ctx, ds.ds.DatacenterPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fm := ds.ds.NewFileManager(dc, false)
|
|
|
|
return fm.Delete(ds.driver.ctx, path)
|
|
|
|
}
|
|
|
|
|
2019-01-04 09:07:09 -05:00
|
|
|
func (ds *Datastore) MakeDirectory(path string) error {
|
|
|
|
dc, err := ds.driver.finder.Datacenter(ds.driver.ctx, ds.ds.DatacenterPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fm := ds.ds.NewFileManager(dc, false)
|
|
|
|
return fm.FileManager.MakeDirectory(ds.driver.ctx, path, dc, true)
|
|
|
|
}
|
|
|
|
|
2018-02-01 06:47:09 -05:00
|
|
|
// Cuts out the datastore prefix
|
|
|
|
// Example: "[datastore1] file.ext" --> "file.ext"
|
|
|
|
func RemoveDatastorePrefix(path string) string {
|
|
|
|
res := object.DatastorePath{}
|
|
|
|
if hadPrefix := res.FromString(path); hadPrefix {
|
|
|
|
return res.Path
|
|
|
|
} else {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|