Skip to content

Commit

Permalink
Added feature of image and keypair listing
Browse files Browse the repository at this point in the history
  • Loading branch information
belgaied2 committed Jan 21, 2022
1 parent bbf0152 commit 44433a6
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
79 changes: 79 additions & 0 deletions cmd/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cmd

import (
"context"

rcmd "github.com/rancher/cli/cmd"
"github.com/urfave/cli"
k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type ImageData struct {
Name string
Id string
SourceType string
Url string
}

// TemplateCommand defines the CLI command that lists VM templates in Harvester
func ImageCommand() cli.Command {
return cli.Command{
Name: "image",
Aliases: []string{"img"},
Usage: "Manipulate VM images",
Action: imageList,
Flags: []cli.Flag{
nsFlag,
},
Subcommands: cli.Commands{
cli.Command{
Name: "list",
Aliases: []string{"ls"},
Usage: "List VM images",
Description: "\nLists all the VM images available in Harvester",
ArgsUsage: "None",
Action: imageList,
Flags: []cli.Flag{
nsFlag,
},
},
},
}
}

func imageList(ctx *cli.Context) (err error) {
c, err := GetHarvesterClient(ctx)

if err != nil {
return
}

imgList, err := c.HarvesterhciV1beta1().VirtualMachineImages(ctx.String("namespace")).List(context.TODO(), k8smetav1.ListOptions{})

if err != nil {
return
}

writer := rcmd.NewTableWriter([][]string{
{"NAME", "Name"},
{"ID", "Id"},
{"SOURCE TYPE", "SourceType"},
{"URL", "Url"},
},
ctx)

defer writer.Close()

for _, imgItem := range imgList.Items {

writer.Write(&ImageData{
Name: imgItem.Spec.DisplayName,
Id: imgItem.Name,
SourceType: imgItem.Spec.SourceType,
Url: imgItem.Spec.URL,
})

}

return writer.Err()
}
77 changes: 77 additions & 0 deletions cmd/keypair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cmd

import (
"context"
"time"

rcmd "github.com/rancher/cli/cmd"
"github.com/urfave/cli"
k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type KeypairData struct {
Name string
Fingerprint string
CreationTimestamp string
}

// TemplateCommand defines the CLI command that lists VM templates in Harvester
func KeypairCommand() cli.Command {
return cli.Command{
Name: "keypair",
Aliases: []string{"key", "ssh-key"},
Usage: "Manipulate SSH Keypairs",
Action: keypairList,
Flags: []cli.Flag{
nsFlag,
},
Subcommands: cli.Commands{
cli.Command{
Name: "list",
Aliases: []string{"ls"},
Usage: "List SSH Keypairs",
Description: "\nLists all the SSH Keypairs available in Harvester",
ArgsUsage: "None",
Action: keypairList,
Flags: []cli.Flag{
nsFlag,
},
},
},
}
}

func keypairList(ctx *cli.Context) (err error) {
c, err := GetHarvesterClient(ctx)

if err != nil {
return
}

keyList, err := c.HarvesterhciV1beta1().KeyPairs(ctx.String("namespace")).List(context.TODO(), k8smetav1.ListOptions{})

if err != nil {
return
}

writer := rcmd.NewTableWriter([][]string{
{"NAME", "Name"},
{"FINGERPRINT", "Fingerprint"},
{"CREATION TIMESTAMP", "CreationTimestamp"},
},
ctx)

defer writer.Close()

for _, keyItem := range keyList.Items {

writer.Write(&KeypairData{
Name: keyItem.Name,
Fingerprint: keyItem.Status.FingerPrint,
CreationTimestamp: keyItem.CreationTimestamp.Format(time.RFC822),
})

}

return writer.Err()
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func mainErr() error {
cmd.VMCommand(),
cmd.ShellCommand(),
cmd.TemplateCommand(),
cmd.ImageCommand(),
cmd.KeypairCommand(),
}

for _, com := range app.Commands {
Expand Down

0 comments on commit 44433a6

Please sign in to comment.