Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VmFolder info #1116

Closed
rodolphewachter opened this issue May 3, 2018 · 6 comments
Closed

VmFolder info #1116

rodolphewachter opened this issue May 3, 2018 · 6 comments

Comments

@rodolphewachter
Copy link

Hi,
How to search Parent folder of a Virtual machine ?

Thanks,

@dougm
Copy link
Member

dougm commented May 3, 2018

Can you be more specific? And using govc or the API directly?

@rodolphewachter
Copy link
Author

rodolphewachter commented May 4, 2018

Hi dougm,

Actually, i'm show virtual machines information with the sample file , except the name of the parent folder and i don't have any idea for query this task. (i use the API directly)

Thanks,

@dougm
Copy link
Member

dougm commented May 4, 2018

Sure, here's an example:

modified   examples/virtualmachines/main.go
@@ -55,14 +55,16 @@ func main() {
 	// Retrieve summary property for all machines
 	// Reference: http://pubs.vmware.com/vsphere-60/topic/com.vmware.wssdk.apiref.doc/vim.VirtualMachine.html
 	var vms []mo.VirtualMachine
-	err = v.Retrieve(ctx, []string{"VirtualMachine"}, []string{"summary"}, &vms)
+	err = v.Retrieve(ctx, []string{"VirtualMachine"}, []string{"summary", "parent"}, &vms)
 	if err != nil {
 		log.Fatal(err)
 	}
 
 	// Print summary per vm (see also: govc/vm/info.go)
-
+	pc := c.PropertyCollector()
 	for _, vm := range vms {
-		fmt.Printf("%s: %s\n", vm.Summary.Config.Name, vm.Summary.Config.GuestFullName)
+		var parent mo.Folder
+		pc.RetrieveOne(ctx, *vm.Parent, []string{"name"}, &parent)
+		fmt.Printf("%s/%s: %s\n", parent.Name, vm.Summary.Config.Name, vm.Summary.Config.GuestFullName)
 	}
 }

dougm added a commit to dougm/govmomi that referenced this issue May 4, 2018
The VM annotation would only be set if -c or -m were also specified.

Add -annotation flag to vm.change command.

Fixes vmware#1116
@rodolphewachter
Copy link
Author

Thanks for your good response.
I have another question with folder if it doesn't bother you.
It's possible to show the entire path : exemple : folder1/folder2/folder3/Vm1

Thanks,

@dougm
Copy link
Member

dougm commented May 4, 2018

In that case, you may want to use Finder instead of ContainerView. In this case, InventoryPath is set for all VMs:

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/vmware/govmomi/examples"
	"github.com/vmware/govmomi/find"
)

func main() {
	ctx := context.Background()

	// Connect and login to ESX or vCenter
	c, err := examples.NewClient(ctx)
	if err != nil {
		log.Fatal(err)
	}

	defer c.Logout(ctx)

	finder := find.NewFinder(c.Client, false)
	dc, err := finder.DatacenterOrDefault(ctx, "")
	if err != nil {
		log.Fatal(err)
	}
	finder.SetDatacenter(dc)

	vms, err := finder.VirtualMachineList(ctx, "*")
	if err != nil {
		log.Fatal(err)
	}

	for _, vm := range vms {
		fmt.Println(vm.InventoryPath)
	}
}

@rodolphewachter
Copy link
Author

Many thanks Doug, i close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants