Skip to content

Commit

Permalink
Merge pull request #11800 from wallyworld/vsphere-absolute-vmfolder
Browse files Browse the repository at this point in the history
#11800

## Description of change

The vSphere vmfolder was not able to be set as an absolute path - it was always being interpreted as a subfolder of the DC folder. We now allow absolute paths to be handled.

## QA steps

A version of the fix was tested on site.

## Bug reference

https://bugs.launchpad.net/juju/+bug/1884490
  • Loading branch information
jujubot committed Jul 3, 2020
2 parents 16439b3 + 20bbefe commit 639a379
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
6 changes: 5 additions & 1 deletion provider/vsphere/internal/vsphereclient/client.go
Expand Up @@ -116,8 +116,12 @@ func (c *Client) FindFolder(ctx context.Context, folderPath string) (vmFolder *o
return dcfolders.VmFolder, nil
}

// We either have a folder that is a relative path or an absolute path.
// We'll accept an absolute path as is. Relative paths will use the DC vm folder as a parent.
folderPath = strings.TrimPrefix(folderPath, "./")
if !strings.HasPrefix(folderPath, dcfolders.VmFolder.InventoryPath) {
if strings.HasPrefix(folderPath, "/") {
c.logger.Debugf("using absolute folder path %q", folderPath)
} else if !strings.HasPrefix(folderPath, dcfolders.VmFolder.InventoryPath) {
c.logger.Debugf("relative folderPath %q found, join with DC vm folder %q now", folderPath, dcfolders.VmFolder.InventoryPath)
folderPath = path.Join(dcfolders.VmFolder.InventoryPath, folderPath)
}
Expand Down
31 changes: 31 additions & 0 deletions provider/vsphere/internal/vsphereclient/client_test.go
Expand Up @@ -660,6 +660,37 @@ func (s *clientSuite) TestEnsureVMFolder(c *gc.C) {
})
}

func (s *clientSuite) TestFindFolder(c *gc.C) {
client := s.newFakeClient(&s.roundTripper, "dc0")
folder, err := client.FindFolder(context.Background(), "foo/bar")
c.Assert(err, jc.ErrorIsNil)
c.Assert(folder, gc.NotNil)
c.Assert(folder.InventoryPath, gc.Equals, "/dc0/vm/foo/bar")
}

func (s *clientSuite) TestFindFolderRelativePath(c *gc.C) {
client := s.newFakeClient(&s.roundTripper, "dc0")
folder, err := client.FindFolder(context.Background(), "./foo/bar")
c.Assert(err, jc.ErrorIsNil)
c.Assert(folder, gc.NotNil)
c.Assert(folder.InventoryPath, gc.Equals, "/dc0/vm/foo/bar")
}

func (s *clientSuite) TestFindFolderAbsolutePath(c *gc.C) {
client := s.newFakeClient(&s.roundTripper, "dc0")
_, err := client.FindFolder(context.Background(), "/foo/bar")
// mock not set up to have a /foo/bar folder
c.Assert(err, gc.ErrorMatches, `folder path "/foo/bar" not found`)
}

func (s *clientSuite) TestFindFolderSubPath(c *gc.C) {
client := s.newFakeClient(&s.roundTripper, "dc0")
folder, err := client.FindFolder(context.Background(), "/dc0/vm/foo/bar")
c.Assert(err, jc.ErrorIsNil)
c.Assert(folder, gc.NotNil)
c.Assert(folder.InventoryPath, gc.Equals, "/dc0/vm/foo/bar")
}

func (s *clientSuite) TestMoveVMFolderInto(c *gc.C) {
client := s.newFakeClient(&s.roundTripper, "dc0")
err := client.MoveVMFolderInto(context.Background(), "foo", "foo/bar")
Expand Down
20 changes: 0 additions & 20 deletions provider/vsphere/internal/vsphereclient/createvm_test.go
Expand Up @@ -611,23 +611,3 @@ func baseCisp() types.OvfCreateImportSpecParams {
func newBool(v bool) *bool {
return &v
}

func findStubCall(c *gc.C, calls []testing.StubCall, name string) testing.StubCall {
c.Logf("finding stub \"%s\"", name)
for i, call := range calls {
c.Logf("stub %d: %s(%v)", i, call.FuncName, call.Args)
if call.FuncName == name {
return call
}
}
c.Fatalf("failed to find call %q", name)
panic("unreachable")
}

func assertNoCall(c *gc.C, calls []testing.StubCall, name string) {
for _, call := range calls {
if call.FuncName == name {
c.Fatalf("found call %q", name)
}
}
}

0 comments on commit 639a379

Please sign in to comment.