-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added feature of image and keypair listing
- Loading branch information
Showing
3 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters