diff --git a/lepton/aws.go b/lepton/aws.go index 7495bc26..1b4d1adb 100644 --- a/lepton/aws.go +++ b/lepton/aws.go @@ -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 } @@ -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) } diff --git a/lepton/cloud.go b/lepton/cloud.go index a572b5dd..f455e1b2 100644 --- a/lepton/cloud.go +++ b/lepton/cloud.go @@ -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 diff --git a/lepton/openstack.go b/lepton/openstack.go index 532d557e..29165577 100644 --- a/lepton/openstack.go +++ b/lepton/openstack.go @@ -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"), @@ -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( @@ -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) }