Initial commit/push
This commit is contained in:
44
pkg/hypervisor/client.go
Normal file
44
pkg/hypervisor/client.go
Normal file
@ -0,0 +1,44 @@
|
||||
package hypervisor
|
||||
|
||||
import (
|
||||
"context"
|
||||
neturl "net/url"
|
||||
|
||||
"github.com/vmware/govmomi/find"
|
||||
"github.com/vmware/govmomi/session/cache"
|
||||
"github.com/vmware/govmomi/vim25"
|
||||
"github.com/vmware/govmomi/vim25/soap"
|
||||
)
|
||||
|
||||
func NewClient(ctx context.Context, host, username, password string, insecure bool) (*vim25.Client, error) {
|
||||
url, err := soap.ParseURL(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url.User = neturl.UserPassword(username, password)
|
||||
|
||||
session := &cache.Session{
|
||||
URL: url,
|
||||
Insecure: insecure,
|
||||
}
|
||||
|
||||
clt := new(vim25.Client)
|
||||
err = session.Login(ctx, clt, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return clt, err
|
||||
}
|
||||
|
||||
func DatacenterFinder(ctx context.Context, clt *vim25.Client, datacenter string) (*find.Finder, error) {
|
||||
fnd := find.NewFinder(clt)
|
||||
|
||||
founddc, err := fnd.DatacenterOrDefault(ctx, datacenter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return fnd.SetDatacenter(founddc), nil
|
||||
}
|
62
pkg/hypervisor/vm.go
Normal file
62
pkg/hypervisor/vm.go
Normal file
@ -0,0 +1,62 @@
|
||||
package hypervisor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/vmware/govmomi/find"
|
||||
"github.com/vmware/govmomi/vim25/types"
|
||||
)
|
||||
|
||||
type vAPPProperty struct {
|
||||
Key string
|
||||
Value string
|
||||
}
|
||||
|
||||
func SetVirtualMachineProperties(ctx context.Context, fnd *find.Finder, name string) error {
|
||||
vm, err := fnd.VirtualMachine(ctx, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
vappproperties := []vAPPProperty{
|
||||
{
|
||||
Key: "foo",
|
||||
Value: "bar",
|
||||
},
|
||||
{
|
||||
Key: "woot",
|
||||
Value: "dude",
|
||||
},
|
||||
}
|
||||
|
||||
vappconfig := &types.VmConfigSpec{
|
||||
// OvfEnvironmentTransport: []string{"com.vmware.guestinfo"},
|
||||
}
|
||||
|
||||
for i, vappproperty := range vappproperties {
|
||||
vappconfig.Property = append(vappconfig.Property, types.VAppPropertySpec{
|
||||
ArrayUpdateSpec: types.ArrayUpdateSpec{
|
||||
Operation: types.ArrayUpdateOperationAdd,
|
||||
},
|
||||
Info: &types.VAppPropertyInfo{
|
||||
Key: int32(i),
|
||||
Id: vappproperty.Key,
|
||||
DefaultValue: fmt.Sprintf("${%v}", vappproperty.Value),
|
||||
Type: "expression",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
task, err := vm.Reconfigure(ctx, types.VirtualMachineConfigSpec{
|
||||
VAppConfig: vappconfig,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := task.Wait(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user