From 49436012ba87b400b8e4aa56f5af5cdec33eec0e Mon Sep 17 00:00:00 2001 From: dkalleg Date: Wed, 15 Jun 2016 14:29:33 -0700 Subject: [PATCH] vSphere provider - Adding config for working dir This allows the user the set "working-dir" in their vsphere.cfg file. The value should be a path in the vSphere datastore in which the provider will look for vms. --- pkg/cloudprovider/providers/vsphere/vsphere.go | 17 ++++++++++++++--- .../providers/vsphere/vsphere_test.go | 1 + 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/cloudprovider/providers/vsphere/vsphere.go b/pkg/cloudprovider/providers/vsphere/vsphere.go index 654dd48093c..8561ed3693f 100644 --- a/pkg/cloudprovider/providers/vsphere/vsphere.go +++ b/pkg/cloudprovider/providers/vsphere/vsphere.go @@ -22,6 +22,7 @@ import ( "io" "net" "net/url" + "path" "strings" "github.com/vmware/govmomi" @@ -71,6 +72,7 @@ type VSphereConfig struct { InsecureFlag bool `gcfg:"insecure-flag"` Datacenter string `gcfg:"datacenter"` Datastore string `gcfg:"datastore"` + WorkingDir string `gcfg:"working-dir"` } Network struct { @@ -171,6 +173,9 @@ func newVSphere(cfg VSphereConfig) (*VSphere, error) { glog.Errorf("%v is not a supported SCSI Controller type. Please configure 'lsilogic-sas' OR 'pvscsi'", cfg.Disk.SCSIControllerType) return nil, errors.New("Controller type not supported. Please configure 'lsilogic-sas' OR 'pvscsi'") } + if cfg.Global.WorkingDir != "" { + cfg.Global.WorkingDir = path.Clean(cfg.Global.WorkingDir) + "/" + } vs := VSphere{ cfg: &cfg, localInstanceID: id, @@ -217,9 +222,11 @@ func getVirtualMachineByName(cfg *VSphereConfig, ctx context.Context, c *govmomi } f.SetDatacenter(dc) + vmRegex := cfg.Global.WorkingDir + name + // Retrieve vm by name //TODO: also look for vm inside subfolders - vm, err := f.VirtualMachine(ctx, name) + vm, err := f.VirtualMachine(ctx, vmRegex) if err != nil { return nil, err } @@ -247,8 +254,10 @@ func getInstances(cfg *VSphereConfig, ctx context.Context, c *govmomi.Client, fi f.SetDatacenter(dc) + vmRegex := cfg.Global.WorkingDir + filter + //TODO: get all vms inside subfolders - vms, err := f.VirtualMachineList(ctx, filter) + vms, err := f.VirtualMachineList(ctx, vmRegex) if err != nil { return nil, err } @@ -492,7 +501,9 @@ func getVirtualMachineDevices(cfg *VSphereConfig, ctx context.Context, c *govmom return nil, nil, nil, err } - vm, err := f.VirtualMachine(ctx, name) + vmRegex := cfg.Global.WorkingDir + name + + vm, err := f.VirtualMachine(ctx, vmRegex) if err != nil { return nil, nil, nil, err } diff --git a/pkg/cloudprovider/providers/vsphere/vsphere_test.go b/pkg/cloudprovider/providers/vsphere/vsphere_test.go index 29fccd0a355..26edbd3ce56 100644 --- a/pkg/cloudprovider/providers/vsphere/vsphere_test.go +++ b/pkg/cloudprovider/providers/vsphere/vsphere_test.go @@ -38,6 +38,7 @@ func configFromEnv() (cfg VSphereConfig, ok bool) { cfg.Network.PublicNetwork = os.Getenv("VSPHERE_PUBLIC_NETWORK") cfg.Global.Datastore = os.Getenv("VSPHERE_DATASTORE") cfg.Disk.SCSIControllerType = os.Getenv("VSPHERE_SCSICONTROLLER_TYPE") + cfg.Global.WorkingDir = os.Getenv("VSPHERE_WORKING_DIR") if os.Getenv("VSPHERE_INSECURE") != "" { InsecureFlag, err = strconv.ParseBool(os.Getenv("VSPHERE_INSECURE")) } else {