Skip to content

Commit

Permalink
Working on VM creation
Browse files Browse the repository at this point in the history
  • Loading branch information
juanfont authored Feb 14, 2021
1 parent 0719b6b commit 98e2182
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 8 deletions.
91 changes: 83 additions & 8 deletions driver/vcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 (
Expand All @@ -39,6 +41,9 @@ const (
defaultMemory = 2048
defaultSSHPort = 22
defaultDockerPort = 2376

defaultDescription = "Created with Docker Machine"
defaultStorageProfile = ""
)

func NewDriver(hostName, storePath string) drivers.Driver {
Expand All @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 98e2182

Please sign in to comment.