Skip to content

Commit

Permalink
Merge pull request nanovms#664 from fabiodmferreira-work/master
Browse files Browse the repository at this point in the history
Implement GetInstances and GetImages on Azure and Vsphere cloud providers nanovms#663
  • Loading branch information
fabioDMFerreira authored Oct 5, 2020
2 parents f75f5b5 + ce30435 commit fb1d68b
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 66 deletions.
94 changes: 63 additions & 31 deletions lepton/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package lepton
import (
"bytes"
"context"
"errors"
"fmt"
"net/url"
"os"
Expand Down Expand Up @@ -291,11 +290,8 @@ func (a *Azure) CreateImage(ctx *Context) error {

// GetImages return all images for azure
func (a *Azure) GetImages(ctx *Context) ([]CloudImage, error) {
return nil, errors.New("un-implemented")
}
var cimages []CloudImage

// ListImages lists images on azure
func (a *Azure) ListImages(ctx *Context) error {
imagesClient := a.getImagesClient()

images, err := imagesClient.List(context.TODO())
Expand All @@ -305,6 +301,26 @@ func (a *Azure) ListImages(ctx *Context) error {

imgs := images.Values()

for _, image := range imgs {
cImage := CloudImage{
Name: *image.Name,
Status: *(*image.ImageProperties).ProvisioningState,
}

cimages = append(cimages, cImage)
}

return cimages, nil
}

// ListImages lists images on azure
func (a *Azure) ListImages(ctx *Context) error {

cimages, err := a.GetImages(ctx)
if err != nil {
return err
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Status", "Created"})
table.SetHeaderColor(
Expand All @@ -313,10 +329,10 @@ func (a *Azure) ListImages(ctx *Context) error {
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor})
table.SetRowLine(true)

for _, image := range imgs {
for _, image := range cimages {
var row []string
row = append(row, to.String(image.Name))
row = append(row, fmt.Sprintf("%+v", *(*image.ImageProperties).ProvisioningState))
row = append(row, image.Name)
row = append(row, image.Status)
row = append(row, "")
table.Append(row)
}
Expand Down Expand Up @@ -504,11 +520,8 @@ func (a *Azure) CreateInstance(ctx *Context) error {
// GetInstances return all instances on Azure
// TODO
func (a *Azure) GetInstances(ctx *Context) ([]CloudInstance, error) {
return nil, errors.New("un-implemented")
}
var cinstances []CloudInstance

// ListInstances lists instances on Azure
func (a *Azure) ListInstances(ctx *Context) error {
vmClient := a.getVMClient()

vmlist, err := vmClient.List(context.TODO(), a.groupName)
Expand All @@ -518,30 +531,18 @@ func (a *Azure) ListInstances(ctx *Context) error {

instances := vmlist.Values()

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Status", "Created", "Private Ips", "Public Ips"})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor})
table.SetRowLine(true)

for _, instance := range instances {
cinstance := CloudInstance{
Name: *instance.Name,
}
privateIP := ""
publicIP := ""
var rows []string

iname := to.String(instance.Name)
rows = append(rows, iname)
rows = append(rows, "")

nifs := *((*(*instance.VirtualMachineProperties).NetworkProfile).NetworkInterfaces)

for i := 0; i < len(nifs); i++ {
nicClient := a.getNicClient()
nic, err := nicClient.Get(context.TODO(), a.groupName, iname, "")
nic, err := nicClient.Get(context.TODO(), a.groupName, cinstance.Name, "")
if err != nil {
fmt.Println(err)
}
Expand All @@ -552,17 +553,48 @@ func (a *Azure) ListInstances(ctx *Context) error {
privateIP = *format.PrivateIPAddress

ipClient := a.getIPClient()
pubip, err := ipClient.Get(context.TODO(), a.groupName, iname, "")
pubip, err := ipClient.Get(context.TODO(), a.groupName, cinstance.Name, "")
if err != nil {
fmt.Println(err)
}
publicIP = *(*pubip.PublicIPAddressPropertiesFormat).IPAddress
}

}
cinstance.PrivateIps = []string{privateIP}
cinstance.PublicIps = []string{publicIP}

cinstances = append(cinstances, cinstance)
}

return cinstances, nil
}

// ListInstances lists instances on Azure
func (a *Azure) ListInstances(ctx *Context) error {
cinstances, err := a.GetInstances(ctx)
if err != nil {
return err
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Status", "Created", "Private Ips", "Public Ips"})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor})
table.SetRowLine(true)

for _, instance := range cinstances {
var rows []string

rows = append(rows, instance.Name)
rows = append(rows, "")
rows = append(rows, "")
rows = append(rows, privateIP)
rows = append(rows, publicIP)
rows = append(rows, strings.Join(instance.PrivateIps, ","))
rows = append(rows, strings.Join(instance.PrivateIps, ","))
table.Append(rows)
}

Expand Down
95 changes: 60 additions & 35 deletions lepton/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lepton

import (
"context"
"errors"
"fmt"
"io"
"net/url"
Expand Down Expand Up @@ -161,25 +160,18 @@ func (v *Vsphere) CreateImage(ctx *Context) error {

// GetImages return all images for vsphere
func (v *Vsphere) GetImages(ctx *Context) ([]CloudImage, error) {
return nil, errors.New("un-implemented")
}

// ListImages lists images on a datastore.
// This is incredibly naive at the moment and probably worth putting
// under a root folder.
// essentially does the equivalent of 'govc datastore.ls'
func (v *Vsphere) ListImages(ctx *Context) error {
var cimages []CloudImage

f := find.NewFinder(v.client, true)
ds, err := f.DatastoreOrDefault(context.TODO(), v.datastore)
if err != nil {
fmt.Println(err)
return err
return nil, err
}

b, err := ds.Browser(context.TODO())
if err != nil {
return err
return nil, err
}

spec := types.HostDatastoreBrowserSearchSpec{
Expand All @@ -198,8 +190,6 @@ func (v *Vsphere) ListImages(ctx *Context) error {
fmt.Println(err)
}

images := []string{}

switch r := info.Result.(type) {
case types.HostDatastoreBrowserSearchResults:
res := []types.HostDatastoreBrowserSearchResults{r}
Expand All @@ -208,13 +198,28 @@ func (v *Vsphere) ListImages(ctx *Context) error {
if f.GetFileInfo().Path[0] == '.' {
continue
}
images = append(images, f.GetFileInfo().Path)
cimages = append(cimages, CloudImage{
Name: f.GetFileInfo().Path,
})
}
}
case types.ArrayOfHostDatastoreBrowserSearchResults:
fmt.Println("un-implemented")
}

return cimages, nil
}

// ListImages lists images on a datastore.
// This is incredibly naive at the moment and probably worth putting
// under a root folder.
// essentially does the equivalent of 'govc datastore.ls'
func (v *Vsphere) ListImages(ctx *Context) error {
images, err := v.GetImages(ctx)
if err != nil {
return err
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Status", "Created"})
table.SetHeaderColor(
Expand All @@ -225,7 +230,7 @@ func (v *Vsphere) ListImages(ctx *Context) error {

for _, image := range images {
var row []string
row = append(row, image)
row = append(row, image.Name)
row = append(row, "")
row = append(row, "")
table.Append(row)
Expand Down Expand Up @@ -415,25 +420,50 @@ func (v *Vsphere) CreateInstance(ctx *Context) error {
// GetInstances return all instances on vSphere
// TODO
func (v *Vsphere) GetInstances(ctx *Context) ([]CloudInstance, error) {
return nil, errors.New("un-implemented")
}

// ListInstances lists instances on VSphere.
// It essentially does:
// govc ls /ha-datacenter/vm
func (v *Vsphere) ListInstances(ctx *Context) error {
var cinstances []CloudInstance

m := view.NewManager(v.client)

cv, err := m.CreateContainerView(context.TODO(), v.client.ServiceContent.RootFolder, []string{"VirtualMachine"}, true)
if err != nil {
return err
return nil, err
}

defer cv.Destroy(context.TODO())

var vms []mo.VirtualMachine
err = cv.Retrieve(context.TODO(), []string{"VirtualMachine"}, []string{"summary"}, &vms)
if err != nil {
return nil, err
}

for _, vm := range vms {
cInstance := CloudInstance{
Name: vm.Summary.Config.Name,
Status: string(vm.Summary.Runtime.PowerState),
}

if vm.Summary.Runtime.BootTime != nil {
cInstance.Created = vm.Summary.Runtime.BootTime.String()
}

if cInstance.Status == "poweredOn" {
ip := v.ipFor(vm.Summary.Config.Name)
cInstance.PublicIps = []string{ip}
}

cinstances = append(cinstances, cInstance)
}

return cinstances, nil
}

// ListInstances lists instances on VSphere.
// It essentially does:
// govc ls /ha-datacenter/vm
func (v *Vsphere) ListInstances(ctx *Context) error {

cInstances, err := v.GetInstances(ctx)
if err != nil {
return err
}
Expand All @@ -447,20 +477,15 @@ func (v *Vsphere) ListInstances(ctx *Context) error {
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor})
table.SetRowLine(true)

for _, vm := range vms {
for _, instance := range cInstances {
var row []string
row = append(row, vm.Summary.Config.Name)

ps := string(vm.Summary.Runtime.PowerState)
if ps == "poweredOn" {
ip := v.ipFor(vm.Summary.Config.Name)
row = append(row, ip)
} else {
row = append(row, "")
}
row = append(row, instance.Name)

row = append(row, strings.Join(instance.PublicIps, ","))

row = append(row, string(vm.Summary.Runtime.PowerState))
row = append(row, fmt.Sprintf("%s", vm.Summary.Runtime.BootTime))
row = append(row, instance.Status)
row = append(row, instance.Created)
table.Append(row)
}

Expand Down Expand Up @@ -804,7 +829,7 @@ func (v *Vsphere) getCredentials() (*url.URL, error) {
return nil, fmt.Errorf("Incomplete credentials, set either via <GOVC_URL> with https://username:password@host:port or <GOVC_USERNAME and GOVC_PASSWORD>")
}

tempURL = fmt.Sprintf("%s://%s:%s@%s", u.Scheme, un, pw, u.Host)
tempURL = fmt.Sprintf("%s://%s:%s@%s", u.Scheme, un, url.PathEscape(pw), u.Host)
u, err = url.Parse(tempURL + "/sdk")
return u, err
}
Expand Down

0 comments on commit fb1d68b

Please sign in to comment.