Skip to content

Commit

Permalink
vpc key list command
Browse files Browse the repository at this point in the history
Signed-off-by: Punith Kenchappa <[email protected]>
  • Loading branch information
pkenchap committed Feb 27, 2023
1 parent caec424 commit e33491b
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 2 deletions.
34 changes: 34 additions & 0 deletions cmd/capibmadm/cmd/vpc/key/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
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 contains the commands to operate on vpc key resources.
package key

import (
"github.com/spf13/cobra"
)

// Commands function to add VPC key commands.
func Commands() *cobra.Command {
cmd := &cobra.Command{
Use: "key",
Short: "Perform VPC key operations",
}

cmd.AddCommand(ListCommand())

return cmd
}
128 changes: 128 additions & 0 deletions cmd/capibmadm/cmd/vpc/key/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
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"
"os"

"github.com/go-openapi/strfmt"
"github.com/spf13/cobra"

"github.com/IBM/vpc-go-sdk/vpcv1"

"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/vpc"
"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"
pagingUtil "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

// ListCommand vpc key list command.
func ListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List VPC key",
Example: `
# List key in VPC
export IBMCLOUD_API_KEY=<api-key>
capibmadm vpc key list --region <region> --resource-group-name <resource-group-name>`,
}

options.AddCommonFlags(cmd)

cmd.RunE = func(cmd *cobra.Command, args []string) error {
if err := listKeys(cmd.Context()); err != nil {
return err
}
return nil
}

return cmd
}

func listKeys(ctx context.Context) error {
v1, err := vpc.NewV1Client(options.GlobalOptions.VPCRegion)
if err != nil {
return err
}

var keyNesList []*vpcv1.KeyCollection
f := func(start string) (bool, string, error) {
var listKeyOpt vpcv1.ListKeysOptions

if start != "" {
listKeyOpt.Start = &start
}

keyL, _, err := v1.ListKeysWithContext(ctx, &listKeyOpt)
if err != nil {
return false, "", err
}
keyNesList = append(keyNesList, keyL)

if keyL.Next != nil && *keyL.Next.Href != "" {
return false, *keyL.Next.Href, nil
}

return true, "", nil
}

if err = pagingUtil.PagingHelper(f); err != nil {
return err
}

return display(keyNesList)
}

func display(keyNesList []*vpcv1.KeyCollection) error {
var keyListToDisplay List
for _, keyL := range keyNesList {
for _, key := range keyL.Keys {
keyToAppend := Key{
CreatedAt: utils.DereferencePointer(key.CreatedAt).(strfmt.DateTime),
ID: utils.DereferencePointer(key.ID).(string),
Name: utils.DereferencePointer(key.Name).(string),
Type: utils.DereferencePointer(key.Type).(string),
Length: utils.DereferencePointer(key.Length).(int64),
FingerPrint: utils.DereferencePointer(key.Fingerprint).(string),
}

if key.ResourceGroup != nil {
keyToAppend.ResourceGroup = utils.DereferencePointer(key.ResourceGroup.Name).(string)
}

keyListToDisplay = append(keyListToDisplay, keyToAppend)
}
}

printkeys, err := printer.New(options.GlobalOptions.Output, os.Stdout)

if err != nil {
return err
}

switch options.GlobalOptions.Output {
case printer.PrinterTypeJSON:
err = printkeys.Print(keyListToDisplay)
default:
table := keyListToDisplay.ToTable()
err = printkeys.Print(table)
}

return err
}
86 changes: 86 additions & 0 deletions cmd/capibmadm/cmd/vpc/key/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
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 contains the commands to operate on vpc key resources.
package key

import (
"github.com/go-openapi/strfmt"

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

// Key vpc key info.
type Key struct {
ID string `json:"id"`
CreatedAt strfmt.DateTime `json:"created_at"`
Name string `json:"name"`
Type string `json:"type"`
ResourceGroup string `json:"resourceGroup"`
FingerPrint string `json:"fingerPrint"`
Length int64 `json:"length"`
}

// List is list of Key.
type List []Key

// ToTable converts List to *metav1.Table.
func (keyList *List) ToTable() *metav1.Table {
table := &metav1.Table{
TypeMeta: metav1.TypeMeta{
APIVersion: metav1.SchemeGroupVersion.String(),
Kind: "Table",
},
ColumnDefinitions: []metav1.TableColumnDefinition{
{
Name: "ID",
Type: "string",
},
{
Name: "NAME",
Type: "string",
},
{
Name: "TYPE",
Type: "string",
},
{
Name: "CREATED AT",
Type: "string",
},
{
Name: "LENGTH",
Type: "integer",
},
{
Name: "FINGERPRINT",
Type: "string",
},
{
Name: "RESOURCE GROUP",
Type: "string",
},
},
}

for _, key := range *keyList {
row := metav1.TableRow{
Cells: []interface{}{key.ID, key.Name, key.Type, key.CreatedAt, key.Length, key.FingerPrint, key.ResourceGroup},
}
table.Rows = append(table.Rows, row)
}
return table
}
2 changes: 2 additions & 0 deletions cmd/capibmadm/cmd/vpc/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/spf13/cobra"

"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/vpc/image"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/vpc/key"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
)

Expand All @@ -36,6 +37,7 @@ func Commands() *cobra.Command {

_ = cmd.MarkPersistentFlagRequired("region")

cmd.AddCommand(key.Commands())
cmd.AddCommand(image.Commands())

return cmd
Expand Down
2 changes: 1 addition & 1 deletion cmd/capibmadm/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var (

// Printer is an interface for a printer.
type Printer interface {
// Print is a method to print an object
// Print is a method to print an object.
Print(in interface{}) error
}

Expand Down
1 change: 1 addition & 0 deletions docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- [SSH key Commands](./topics/capibmadm/powervs/key.md)
- [VPC Commands](./topics/capibmadm/vpc/index.md)
- [Image Commands](./topics/capibmadm/vpc/image.md)
- [Key Commands](./topics/capibmadm/vpc/key.md)
- [Developer Guide](./developer/index.md)
- [Rapid iterative development with Tilt](./developer/tilt.md)
- [Guide for API conversions](./developer/conversion.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/topics/capibmadm/vpc/image.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## PowerVS VPC Commands
## VPC image Commands

### 1. capibmadm vpc image list

Expand Down
2 changes: 2 additions & 0 deletions docs/book/src/topics/capibmadm/vpc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
## 1. VPC commands
- [image](./image.md)
- [list](/topics/capibmadm/vpc/image.html#1-capibmadm-vpc-image-list)
- [key](./key.md)
- [list](/topics/capibmadm/vpc/key.html#1-capibmadm-vpc-key-list)
20 changes: 20 additions & 0 deletions docs/book/src/topics/capibmadm/vpc/key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## VPC SSH key Commands

### 1. capibmadm vpc key list

#### Usage:
List SSH keys in given VPC region.

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

#### Arguments:
--region: VPC region.

--resource-group-name: IBM Cloud resource group name.

#### Example:
```shell
export IBMCLOUD_API_KEY=<api-key>
capibmadm vpc key list --region <region> --resource-group-name <resource-group>
```

0 comments on commit e33491b

Please sign in to comment.