vmware[golang]的一些简单操作
废话不多说,直接代码:
package vmware
import (
"context"
"errors"
"fmt"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
"net/url"
)
var ctx = context.Background()
// 链接vmware
func NewClient(ip,user,pwd string) *vim25.Client{
u := &url.URL{
Scheme: "https",
Host: ip,
Path: "/sdk",
}
//ctx := context.Background()
u.User = url.UserPassword(user, pwd)
client, err := govmomi.NewClient(ctx, u, true)
if err != nil {
panic(err)
}
return client.Client
}
//查找虚拟机by name
func FindVMByName(c *vim25.Client, vmName string) (*object.VirtualMachine,error){
m := view.NewManager(c)
v, err := m.CreateContainerView(ctx, c.ServiceContent.RootFol