Initial commit/push
This commit is contained in:
51
pkg/config/config.go
Normal file
51
pkg/config/config.go
Normal file
@ -0,0 +1,51 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Hypervisor struct {
|
||||
Url string `yaml:"url"`
|
||||
Username string `yaml:"username"`
|
||||
Password string `yaml:"password"`
|
||||
|
||||
Datacenter string `yaml:"datacenter"`
|
||||
} `yaml:"hypervisor"`
|
||||
}
|
||||
|
||||
func NewConfig() (*Config, error) {
|
||||
var configPath string
|
||||
|
||||
flag.StringVar(&configPath, "config", "./config.yml", "path to config file")
|
||||
flag.Parse()
|
||||
|
||||
s, err := os.Stat(configPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s.IsDir() {
|
||||
return nil, fmt.Errorf("'%s' is a directory, not a regular file", configPath)
|
||||
}
|
||||
|
||||
|
||||
config := &Config{}
|
||||
|
||||
file, err := os.Open(configPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
d := yaml.NewDecoder(file)
|
||||
|
||||
if err := d.Decode(&config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
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