Skip to content

Commit

Permalink
implements GetImages on aws and openstack (nanovms#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioDMFerreira committed Oct 2, 2020
1 parent 240b103 commit e641acc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
45 changes: 33 additions & 12 deletions lepton/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,38 @@ func getAWSInstances(region string) *ec2.DescribeInstancesOutput {

// GetImages return all images on AWS
func (p *AWS) GetImages(ctx *Context) ([]CloudImage, error) {
return nil, errors.New("un-implemented")
var cimages []CloudImage

result, err := getAWSImages(ctx.config.CloudConfig.Zone)
if err != nil {
return nil, err
}

images := result.Images
for _, image := range images {
var name string
if image.Tags != nil {
name = aws.StringValue(image.Tags[0].Value)
} else {
name = "n/a"
}

cimage := CloudImage{
Name: name,
ID: *image.Name,
Status: *image.State,
Created: *image.CreationDate,
}

cimages = append(cimages, cimage)
}

return cimages, nil
}

// ListImages lists images on AWS
func (p *AWS) ListImages(ctx *Context) error {
result, err := getAWSImages(ctx.config.CloudConfig.Zone)
cimages, err := p.GetImages(ctx)
if err != nil {
return err
}
Expand All @@ -270,19 +296,14 @@ func (p *AWS) ListImages(ctx *Context) error {
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor})
table.SetRowLine(true)

images := result.Images
for i := 0; i < len(images); i++ {
for _, image := range cimages {
var row []string

if images[i].Tags != nil {
row = append(row, aws.StringValue(images[i].Tags[0].Value))
} else {
row = append(row, "n/a")
}
row = append(row, image.Name)
row = append(row, image.ID)
row = append(row, image.Status)
row = append(row, image.Created)

row = append(row, aws.StringValue(images[i].Name))
row = append(row, aws.StringValue(images[i].State))
row = append(row, aws.StringValue(images[i].CreationDate))
table.Append(row)
}

Expand Down
1 change: 1 addition & 0 deletions lepton/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lepton

// CloudImage abstracts images for various cloud providers
type CloudImage struct {
ID string
Name string
Status string
Created string // TODO: prob. should be datetime w/helpers for human formatting
Expand Down
36 changes: 28 additions & 8 deletions lepton/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,7 @@ func (o *OpenStack) CreateImage(ctx *Context) error {

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

// ListImages lists images on a datastore.
func (o *OpenStack) ListImages(ctx *Context) error {
var cimages []CloudImage

imageClient, err := openstack.NewImageServiceV2(o.provider, gophercloud.EndpointOpts{
Region: os.Getenv("OS_REGION_NAME"),
Expand All @@ -178,6 +174,28 @@ func (o *OpenStack) ListImages(ctx *Context) error {
fmt.Println(err)
}

for _, image := range allImages {

cimage := CloudImage{
Name: image.Name,
Status: string(image.Status),
Created: time2Human(image.CreatedAt),
}

cimages = append(cimages, cimage)
}

return cimages, nil
}

// ListImages lists images on a datastore.
func (o *OpenStack) ListImages(ctx *Context) error {

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

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Status", "Created"})
table.SetHeaderColor(
Expand All @@ -186,11 +204,13 @@ func (o *OpenStack) ListImages(ctx *Context) error {
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor})
table.SetRowLine(true)

for _, image := range allImages {
for _, image := range cimages {
var row []string

row = append(row, image.Name)
row = append(row, fmt.Sprintf("%v", image.Status))
row = append(row, time2Human(image.CreatedAt))
row = append(row, image.Status)
row = append(row, image.Created)

table.Append(row)
}

Expand Down

0 comments on commit e641acc

Please sign in to comment.