From e33491b384827815b64476bb1b964315c9e67e6d Mon Sep 17 00:00:00 2001 From: Punith Kenchappa Date: Wed, 8 Feb 2023 12:04:27 +0530 Subject: [PATCH] vpc key list command Signed-off-by: Punith Kenchappa --- cmd/capibmadm/cmd/vpc/key/key.go | 34 ++++++ cmd/capibmadm/cmd/vpc/key/list.go | 128 ++++++++++++++++++++ cmd/capibmadm/cmd/vpc/key/type.go | 86 +++++++++++++ cmd/capibmadm/cmd/vpc/vpc.go | 2 + cmd/capibmadm/printer/printer.go | 2 +- docs/book/src/SUMMARY.md | 1 + docs/book/src/topics/capibmadm/vpc/image.md | 2 +- docs/book/src/topics/capibmadm/vpc/index.md | 2 + docs/book/src/topics/capibmadm/vpc/key.md | 20 +++ 9 files changed, 275 insertions(+), 2 deletions(-) create mode 100644 cmd/capibmadm/cmd/vpc/key/key.go create mode 100644 cmd/capibmadm/cmd/vpc/key/list.go create mode 100644 cmd/capibmadm/cmd/vpc/key/type.go create mode 100644 docs/book/src/topics/capibmadm/vpc/key.md diff --git a/cmd/capibmadm/cmd/vpc/key/key.go b/cmd/capibmadm/cmd/vpc/key/key.go new file mode 100644 index 000000000..0c66b1da7 --- /dev/null +++ b/cmd/capibmadm/cmd/vpc/key/key.go @@ -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 +} diff --git a/cmd/capibmadm/cmd/vpc/key/list.go b/cmd/capibmadm/cmd/vpc/key/list.go new file mode 100644 index 000000000..e3e9bccfb --- /dev/null +++ b/cmd/capibmadm/cmd/vpc/key/list.go @@ -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= +capibmadm vpc key list --region --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 +} diff --git a/cmd/capibmadm/cmd/vpc/key/type.go b/cmd/capibmadm/cmd/vpc/key/type.go new file mode 100644 index 000000000..effe5795f --- /dev/null +++ b/cmd/capibmadm/cmd/vpc/key/type.go @@ -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 +} diff --git a/cmd/capibmadm/cmd/vpc/vpc.go b/cmd/capibmadm/cmd/vpc/vpc.go index ef3236df4..dc4945346 100644 --- a/cmd/capibmadm/cmd/vpc/vpc.go +++ b/cmd/capibmadm/cmd/vpc/vpc.go @@ -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" ) @@ -36,6 +37,7 @@ func Commands() *cobra.Command { _ = cmd.MarkPersistentFlagRequired("region") + cmd.AddCommand(key.Commands()) cmd.AddCommand(image.Commands()) return cmd diff --git a/cmd/capibmadm/printer/printer.go b/cmd/capibmadm/printer/printer.go index 90309fde2..3ac0f6aea 100644 --- a/cmd/capibmadm/printer/printer.go +++ b/cmd/capibmadm/printer/printer.go @@ -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 } diff --git a/docs/book/src/SUMMARY.md b/docs/book/src/SUMMARY.md index f6b040281..f696cf3cf 100644 --- a/docs/book/src/SUMMARY.md +++ b/docs/book/src/SUMMARY.md @@ -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) diff --git a/docs/book/src/topics/capibmadm/vpc/image.md b/docs/book/src/topics/capibmadm/vpc/image.md index 9d7a9f93c..b99e61605 100644 --- a/docs/book/src/topics/capibmadm/vpc/image.md +++ b/docs/book/src/topics/capibmadm/vpc/image.md @@ -1,4 +1,4 @@ -## PowerVS VPC Commands +## VPC image Commands ### 1. capibmadm vpc image list diff --git a/docs/book/src/topics/capibmadm/vpc/index.md b/docs/book/src/topics/capibmadm/vpc/index.md index 233aa0302..d6a85385a 100644 --- a/docs/book/src/topics/capibmadm/vpc/index.md +++ b/docs/book/src/topics/capibmadm/vpc/index.md @@ -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) diff --git a/docs/book/src/topics/capibmadm/vpc/key.md b/docs/book/src/topics/capibmadm/vpc/key.md new file mode 100644 index 000000000..4459b8eec --- /dev/null +++ b/docs/book/src/topics/capibmadm/vpc/key.md @@ -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= +capibmadm vpc key list --region --resource-group-name +``` \ No newline at end of file