I want to get a list of recently completed vcenter tasks using govmomi and I’m using the following code.
package main
import (
"context"
"fmt"
"log"
"net/url"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/soap"
)
func main() {
ctx := context.Background()
// Replace these with your vCenter credentials
vcURL := "https://vcenter urlom/sdk"
username := "Admin"
password := "password"
// Create a new client
u, err := soap.ParseURL(vcURL)
if err != nil {
log.Fatal(err)
}
u.User = url.UserPassword(username, password)
client, err := govmomi.NewClient(ctx, u, true)
if err != nil {
log.Fatal(err)
}
m := view.NewManager(client.Client)
for {
// Create a container view of Task entities
v, err := m.CreateContainerView(ctx, client.ServiceContent.RootFolder, []string{"ManagedEntity"}, true)
if err != nil {
log.Fatal(err)
}
vms, _ := v.Find(ctx, []string{"Task"}, property.Filter{})
if len(vms) > 0 {
fmt.Println(vms)
}
defer v.Destroy(ctx)
// Retrieve all Task entities
var tasks []mo.Task
err = v.Retrieve(ctx, []string{"Task"}, []string{"info"}, &tasks)
if err != nil {
log.Fatal(err)
}
// Print task details
for _, task := range tasks {
fmt.Printf("Task Name: %sn", task.Info.Name)
}
}
But this return nothing. When I tried VirtualMachine instead of Task it returned vm details. But it is not returning task details.
Can anyone help with this?