-
Notifications
You must be signed in to change notification settings - Fork 8
/
keypair.go
77 lines (63 loc) · 1.55 KB
/
keypair.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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()
}