commit c8483de7a9a4bdafeb39dc29ee8bc55106c87a33 Author: Danny Bessems Date: Thu Dec 22 13:07:21 2022 +0100 Initial commit/push diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fa0d2d1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vscode + +bin** +config.yml diff --git a/cmd/vappprop-manager/main.go b/cmd/vappprop-manager/main.go new file mode 100644 index 0000000..67c1b1e --- /dev/null +++ b/cmd/vappprop-manager/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "context" + "flag" + "log" + + "spamasaurus.com/m/pkg/config" + "spamasaurus.com/m/pkg/hypervisor" +) + +func main() { + var virtualmachine string + + flag.StringVar(&virtualmachine, "vm", "", "name of VM") + flag.Parse() + + if virtualmachine == "" { + log.Fatalf("Name of vm is required") + } + + cfg, err := config.NewConfig() + if err != nil { + log.Fatal(err) + } + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + clt, err := hypervisor.NewClient(ctx, cfg.Hypervisor.Url, cfg.Hypervisor.Username, cfg.Hypervisor.Password, true) + if err != nil { + log.Fatalf("Login failed: %s", err) + } + + fnd, err := hypervisor.DatacenterFinder(ctx, clt, cfg.Hypervisor.Datacenter) + if err != nil { + log.Fatalf("Foo indeed: %s", err) + } + + if err := hypervisor.SetVirtualMachineProperties(ctx, fnd, virtualmachine); err != nil { + log.Fatalf("Could not apply vApp properties: %s", err) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6b65094 --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module spamasaurus.com/m + +go 1.19 + +require github.com/vmware/govmomi v0.30.0 + +require gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..1f9b21e --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/vmware/govmomi v0.30.0 h1:Fm8ugPnnlMSTSceDKY9goGvjmqc6eQLPUSUeNXdpeXA= +github.com/vmware/govmomi v0.30.0/go.mod h1:F7adsVewLNHsW/IIm7ziFURaXDaHEwcc+ym4r3INMdY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..ae5c675 --- /dev/null +++ b/pkg/config/config.go @@ -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 +} diff --git a/pkg/hypervisor/client.go b/pkg/hypervisor/client.go new file mode 100644 index 0000000..e7400ba --- /dev/null +++ b/pkg/hypervisor/client.go @@ -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 +} diff --git a/pkg/hypervisor/vm.go b/pkg/hypervisor/vm.go new file mode 100644 index 0000000..eb0e857 --- /dev/null +++ b/pkg/hypervisor/vm.go @@ -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 +}