From 98e21823c37dafcf950f07fb55e40275f8a38343 Mon Sep 17 00:00:00 2001 From: Juan Font Date: Sun, 14 Feb 2021 01:03:17 +0000 Subject: [PATCH] Working on VM creation --- driver/vcd.go | 91 ++++++++++++++++++++++++++++++++++++++++++++++----- go.sum | 2 ++ 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/driver/vcd.go b/driver/vcd.go index 31d680b..730650c 100644 --- a/driver/vcd.go +++ b/driver/vcd.go @@ -7,9 +7,11 @@ import ( "strconv" "github.com/docker/machine/libmachine/drivers" + "github.com/docker/machine/libmachine/log" "github.com/docker/machine/libmachine/mcnflag" "github.com/docker/machine/libmachine/state" "github.com/vmware/go-vcloud-director/v2/govcd" + "github.com/vmware/go-vcloud-director/v2/types/v56" ) type Driver struct { @@ -22,14 +24,14 @@ type Driver struct { VcdPassword string VcdOrgVDCNetwork string - EdgeGateway string - PublicIP string - Catalog string - Template string - DockerPort int - CPUCount int - MemorySize int - VAppID string + Catalog string + Template string + DockerPort int + CPUCount int + MemorySize int + VAppID string + Description string + StorageProfile string } const ( @@ -39,6 +41,9 @@ const ( defaultMemory = 2048 defaultSSHPort = 22 defaultDockerPort = 2376 + + defaultDescription = "Created with Docker Machine" + defaultStorageProfile = "" ) func NewDriver(hostName, storePath string) drivers.Driver { @@ -53,11 +58,81 @@ func NewDriver(hostName, storePath string) drivers.Driver { MachineName: hostName, StorePath: storePath, }, + Description: defaultDescription, + StorageProfile: defaultStorageProfile, } } // Create configures and creates a new vCD vm func (d *Driver) Create() (err error) { + client := govcd.NewVCDClient(*d.VcdURL, d.VcdInsecure) + err := client.Authenticate(d.VcdUser, d.VcdPassword, d.VcdOrg) + if err != nil { + return err + } + org, err := client.GetOrgByName(d.VcdOrg) + if err != nil { + return err + } + vdc, err := org.GetVDCByName(d.VcdVdc, false) + if err != nil { + return err + } + + log.Infof("Finding network...") + net, err := vdc.GetOrgVdcNetworkByName(d.VcdOrgVDCNetwork, true) + if err != nil { + return err + } + + log.Infof("Finding catalog...") + catalog, err := org.GetCatalogByName(d.Catalog, true) + if err != nil { + return err + } + + log.Infof("Finding template...") + template, err := catalog.GetCatalogItemByName(d.Template, true) + if err != nil { + return err + } + vapptemplate, err := template.GetVAppTemplate() + if err != nil { + return err + } + + var storageProfile types.Reference + if d.StorageProfile != "" { + storageProfile, err = vdc.FindStorageProfileReference(d.StorageProfile) + if err != nil { + return err + } + } else { + + storageProfile, err = vdc.GetDefaultStorageProfileReference() + if err != nil { + return err + } + } + + log.Infof("Creating a new vApp: %s...", d.MachineName) + networks := []*types.OrgVDCNetwork{} + networks = append(networks, net.OrgVDCNetwork) + task, err := vdc.ComposeVApp( + networks, + vapptemplate, + storageProfile, + d.MachineName, + d.Description, + true) + + if err != nil { + return err + } + if err = task.WaitTaskCompletion(); err != nil { + return err + } + return nil } diff --git a/go.sum b/go.sum index 73cb2c8..82536ea 100644 --- a/go.sum +++ b/go.sum @@ -34,6 +34,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/vmware/go-vcloud-director v2.0.0+incompatible h1:3B121XZVdEOxRhv5ARswKVxXt4KznAbun8GoXNbbZWs= +github.com/vmware/go-vcloud-director v2.0.0+incompatible/go.mod h1:imYhjsPjkPsgrNsbA/wE2hoy0RK27MXzpOSPjHW89S8= github.com/vmware/go-vcloud-director/v2 v2.10.0 h1:u/MWs+RgM9bjPPTGp0u3JtWVuj1IgApsyUxbf6I2rjw= github.com/vmware/go-vcloud-director/v2 v2.10.0/go.mod h1:czvTQZlB4/WsOsL7rMVCb+SYAPJhx/dYoS/Sk7rc/O0= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=