Skip to content

Commit 16e8328

Browse files
authored
Add cmd for ssh key list (#1160)
Signed-off-by: Yussuf Shaikh <[email protected]>
1 parent 3e0af2a commit 16e8328

File tree

5 files changed

+199
-2
lines changed

5 files changed

+199
-2
lines changed

cmd/capibmadm/cmd/powervs/key/key.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ func Commands() *cobra.Command {
2929
}
3030
cmd.AddCommand(CreateSSHKeyCommand())
3131
cmd.AddCommand(DeleteSSHKeyCommand())
32+
cmd.AddCommand(ListSSHKeyCommand())
3233
return cmd
3334
}

cmd/capibmadm/cmd/powervs/key/list.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package key
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"os"
23+
24+
"github.com/spf13/cobra"
25+
26+
v "github.com/IBM-Cloud/power-go-client/clients/instance"
27+
28+
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
29+
30+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/iam"
31+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/powervs"
32+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
33+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/printer"
34+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils"
35+
)
36+
37+
// ListSSHKeyCommand function to list PowerVS SSH Keys.
38+
func ListSSHKeyCommand() *cobra.Command {
39+
cmd := &cobra.Command{
40+
Use: "list",
41+
Short: "List SSH Keys",
42+
Example: `
43+
# List PowerVS SSH Keys
44+
export IBMCLOUD_API_KEY=<api-key>
45+
capibmadm powervs key list --service-instance-id <service-instance-id> --zone <zone>`,
46+
RunE: func(cmd *cobra.Command, args []string) error {
47+
if err := listSSHKeys(cmd.Context()); err != nil {
48+
return err
49+
}
50+
return nil
51+
},
52+
}
53+
54+
options.AddCommonFlags(cmd)
55+
return cmd
56+
}
57+
58+
func listSSHKeys(ctx context.Context) error {
59+
log := logf.Log
60+
log.Info("Listing PowerVS SSH Keys", "service-instance-id", options.GlobalOptions.ServiceInstanceID, "zone", options.GlobalOptions.PowerVSZone)
61+
62+
accountID, err := utils.GetAccountID(ctx, iam.GetIAMAuth())
63+
if err != nil {
64+
return err
65+
}
66+
sess, err := powervs.NewPISession(accountID, options.GlobalOptions.PowerVSZone, options.GlobalOptions.Debug)
67+
if err != nil {
68+
return err
69+
}
70+
71+
c := v.NewIBMPIKeyClient(ctx, sess, options.GlobalOptions.ServiceInstanceID)
72+
keys, err := c.GetAll()
73+
if err != nil {
74+
return err
75+
}
76+
77+
if len(keys.SSHKeys) == 0 {
78+
fmt.Println("No SSH Key found")
79+
return nil
80+
}
81+
82+
listByVersion := IList{
83+
Items: []SSHKeySpec{},
84+
}
85+
86+
for _, key := range keys.SSHKeys {
87+
listByVersion.Items = append(listByVersion.Items, SSHKeySpec{
88+
Name: *key.Name,
89+
Key: *key.SSHKey,
90+
CreationDate: *key.CreationDate,
91+
})
92+
}
93+
94+
pr, err := printer.New(options.GlobalOptions.Output, os.Stdout)
95+
if err != nil {
96+
return err
97+
}
98+
99+
if options.GlobalOptions.Output == printer.PrinterTypeJSON {
100+
err = pr.Print(listByVersion)
101+
} else {
102+
table := listByVersion.ToTable()
103+
err = pr.Print(table)
104+
}
105+
106+
return err
107+
}

cmd/capibmadm/cmd/powervs/key/type.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package key
18+
19+
import (
20+
"time"
21+
22+
"github.com/go-openapi/strfmt"
23+
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
)
26+
27+
// SSHKeySpec defines an SSH Key.
28+
type SSHKeySpec struct {
29+
Name string `json:"name"`
30+
Key string `json:"key"`
31+
CreationDate strfmt.DateTime `json:"creationDate"`
32+
}
33+
34+
// IList defines a list of SSH Keys.
35+
type IList struct {
36+
Items []SSHKeySpec `json:"items"`
37+
}
38+
39+
// ToTable converts List to *metav1.Table.
40+
func (keyList *IList) ToTable() *metav1.Table {
41+
table := &metav1.Table{
42+
TypeMeta: metav1.TypeMeta{
43+
APIVersion: metav1.SchemeGroupVersion.String(),
44+
Kind: "Table",
45+
},
46+
ColumnDefinitions: []metav1.TableColumnDefinition{
47+
{
48+
Name: "Name",
49+
Type: "string",
50+
},
51+
{
52+
Name: "Creation Date",
53+
Type: "string",
54+
},
55+
{
56+
Name: "Key",
57+
Type: "string",
58+
},
59+
},
60+
}
61+
62+
for _, key := range keyList.Items {
63+
row := metav1.TableRow{
64+
Cells: []interface{}{key.Name, time.Time(key.CreationDate).Format(time.RFC822), key.Key},
65+
}
66+
table.Rows = append(table.Rows, row)
67+
}
68+
return table
69+
}

docs/book/src/topics/capibmadm/powervs/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [key](./key.md)
66
- [create](/topics/capibmadm/powervs/key.html#1-capibmadm-powervs-key-create)
77
- [delete](/topics/capibmadm/powervs/key.html#2-capibmadm-powervs-key-delete)
8+
- [list](/topics/capibmadm/powervs/key.html#3-capibmadm-powervs-key-list)
89
- [network](./network.md)
910
- [create](/topics/capibmadm/powervs/network.html#1-capibmadm-powervs-network-create)
1011
- [list](/topics/capibmadm/powervs/network.html#2-capibmadm-powervs-network-list)
@@ -14,4 +15,4 @@
1415
- [list](/topics/capibmadm/powervs/port.html#3-capibmadm-powervs-port-list)
1516
- [image](./image.md)
1617
- [list] (/topics/capibmadm/powervs/image.html#1-capibmadm-powervs-image-list))
17-
18+

docs/book/src/topics/capibmadm/powervs/key.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### 1. capibmadm powervs key create
44

5-
#### Usage:
5+
#### Usage:
66
Create an SSH key in the PowerVS environment.
77

88
#### Environmental Variable:
@@ -52,3 +52,22 @@ IBMCLOUD_API_KEY: IBM Cloud API key.
5252
export IBMCLOUD_API_KEY=<api-key>
5353
capibmadm powervs key delete --name <key-name> --service-instance-id <service-instance-id> --zone <zone>
5454
```
55+
56+
### 3. capibmadm powervs key list
57+
58+
#### Usage:
59+
List all SSH Keys in the PowerVS environment.
60+
61+
#### Environmental Variable:
62+
IBMCLOUD_API_KEY: IBM Cloud API key.
63+
64+
#### Arguments:
65+
--service-instance-id: PowerVS service instance id.
66+
67+
--zone: PowerVS zone.
68+
69+
#### Example:
70+
```shell
71+
export IBMCLOUD_API_KEY=<api-key>
72+
capibmadm powervs key list --service-instance-id <service-instance-id> --zone <zone>
73+
```

0 commit comments

Comments
 (0)