Skip to content

Commit

Permalink
Add cmd for ssh key list (#1160)
Browse files Browse the repository at this point in the history
Signed-off-by: Yussuf Shaikh <[email protected]>
  • Loading branch information
yussufsh authored Mar 15, 2023
1 parent 3e0af2a commit 16e8328
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/capibmadm/cmd/powervs/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ func Commands() *cobra.Command {
}
cmd.AddCommand(CreateSSHKeyCommand())
cmd.AddCommand(DeleteSSHKeyCommand())
cmd.AddCommand(ListSSHKeyCommand())
return cmd
}
107 changes: 107 additions & 0 deletions cmd/capibmadm/cmd/powervs/key/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package key

import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"

v "github.com/IBM-Cloud/power-go-client/clients/instance"

logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"

"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/iam"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/powervs"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/printer"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils"
)

// ListSSHKeyCommand function to list PowerVS SSH Keys.
func ListSSHKeyCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List SSH Keys",
Example: `
# List PowerVS SSH Keys
export IBMCLOUD_API_KEY=<api-key>
capibmadm powervs key list --service-instance-id <service-instance-id> --zone <zone>`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := listSSHKeys(cmd.Context()); err != nil {
return err
}
return nil
},
}

options.AddCommonFlags(cmd)
return cmd
}

func listSSHKeys(ctx context.Context) error {
log := logf.Log
log.Info("Listing PowerVS SSH Keys", "service-instance-id", options.GlobalOptions.ServiceInstanceID, "zone", options.GlobalOptions.PowerVSZone)

accountID, err := utils.GetAccountID(ctx, iam.GetIAMAuth())
if err != nil {
return err
}
sess, err := powervs.NewPISession(accountID, options.GlobalOptions.PowerVSZone, options.GlobalOptions.Debug)
if err != nil {
return err
}

c := v.NewIBMPIKeyClient(ctx, sess, options.GlobalOptions.ServiceInstanceID)
keys, err := c.GetAll()
if err != nil {
return err
}

if len(keys.SSHKeys) == 0 {
fmt.Println("No SSH Key found")
return nil
}

listByVersion := IList{
Items: []SSHKeySpec{},
}

for _, key := range keys.SSHKeys {
listByVersion.Items = append(listByVersion.Items, SSHKeySpec{
Name: *key.Name,
Key: *key.SSHKey,
CreationDate: *key.CreationDate,
})
}

pr, err := printer.New(options.GlobalOptions.Output, os.Stdout)
if err != nil {
return err
}

if options.GlobalOptions.Output == printer.PrinterTypeJSON {
err = pr.Print(listByVersion)
} else {
table := listByVersion.ToTable()
err = pr.Print(table)
}

return err
}
69 changes: 69 additions & 0 deletions cmd/capibmadm/cmd/powervs/key/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package key

import (
"time"

"github.com/go-openapi/strfmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// SSHKeySpec defines an SSH Key.
type SSHKeySpec struct {
Name string `json:"name"`
Key string `json:"key"`
CreationDate strfmt.DateTime `json:"creationDate"`
}

// IList defines a list of SSH Keys.
type IList struct {
Items []SSHKeySpec `json:"items"`
}

// ToTable converts List to *metav1.Table.
func (keyList *IList) ToTable() *metav1.Table {
table := &metav1.Table{
TypeMeta: metav1.TypeMeta{
APIVersion: metav1.SchemeGroupVersion.String(),
Kind: "Table",
},
ColumnDefinitions: []metav1.TableColumnDefinition{
{
Name: "Name",
Type: "string",
},
{
Name: "Creation Date",
Type: "string",
},
{
Name: "Key",
Type: "string",
},
},
}

for _, key := range keyList.Items {
row := metav1.TableRow{
Cells: []interface{}{key.Name, time.Time(key.CreationDate).Format(time.RFC822), key.Key},
}
table.Rows = append(table.Rows, row)
}
return table
}
3 changes: 2 additions & 1 deletion docs/book/src/topics/capibmadm/powervs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [key](./key.md)
- [create](/topics/capibmadm/powervs/key.html#1-capibmadm-powervs-key-create)
- [delete](/topics/capibmadm/powervs/key.html#2-capibmadm-powervs-key-delete)
- [list](/topics/capibmadm/powervs/key.html#3-capibmadm-powervs-key-list)
- [network](./network.md)
- [create](/topics/capibmadm/powervs/network.html#1-capibmadm-powervs-network-create)
- [list](/topics/capibmadm/powervs/network.html#2-capibmadm-powervs-network-list)
Expand All @@ -14,4 +15,4 @@
- [list](/topics/capibmadm/powervs/port.html#3-capibmadm-powervs-port-list)
- [image](./image.md)
- [list] (/topics/capibmadm/powervs/image.html#1-capibmadm-powervs-image-list))

21 changes: 20 additions & 1 deletion docs/book/src/topics/capibmadm/powervs/key.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### 1. capibmadm powervs key create

#### Usage:
#### Usage:
Create an SSH key in the PowerVS environment.

#### Environmental Variable:
Expand Down Expand Up @@ -52,3 +52,22 @@ IBMCLOUD_API_KEY: IBM Cloud API key.
export IBMCLOUD_API_KEY=<api-key>
capibmadm powervs key delete --name <key-name> --service-instance-id <service-instance-id> --zone <zone>
```

### 3. capibmadm powervs key list

#### Usage:
List all SSH Keys in the PowerVS environment.

#### Environmental Variable:
IBMCLOUD_API_KEY: IBM Cloud API key.

#### Arguments:
--service-instance-id: PowerVS service instance id.

--zone: PowerVS zone.

#### Example:
```shell
export IBMCLOUD_API_KEY=<api-key>
capibmadm powervs key list --service-instance-id <service-instance-id> --zone <zone>
```

0 comments on commit 16e8328

Please sign in to comment.