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 8, 2023
1 parent a38eb0b commit 626c20e
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 19 deletions.
15 changes: 15 additions & 0 deletions cmd/capibmadm/cmd/vpc/doc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
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 vpc contains the commands to operate on vpc resources.
package vpc
30 changes: 30 additions & 0 deletions cmd/capibmadm/cmd/vpc/key/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
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 (
"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
}
148 changes: 148 additions & 0 deletions cmd/capibmadm/cmd/vpc/key/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
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-aws/v2/cmd/clusterawsadm/printers"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils"
pagingUtil "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
)

var resourceGroupName string

// ListCommand vpc image 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)
options.AddVPCCommonFlags(cmd)

cmd.Flags().StringVar(&resourceGroupName, "resource-group-name", resourceGroupName, "IBM cloud resource group name")

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

return cmd
}

func listKeys(ctx context.Context, resourceGroupName string) error {
v1, err := utils.CreateVPCService(options.GlobalOptions.VPCRegion)
if err != nil {
return err
}

accountID, err := utils.GetAccountID(ctx, utils.GetIAMAuth())
if err != nil {
return err
}

if resourceGroupName != "" {
_, err = utils.GetResourceGroupID(ctx, resourceGroupName, accountID)
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),
ResourceGroup: utils.DereferencePointer(key.ResourceGroup.Name).(string),
}

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

keyListToDisplay = append(keyListToDisplay, keyToAppend)
}
}

var printer printers.Printer
var err error

if options.GlobalOptions.Output == "json" {
printer, err = printers.New("json", os.Stdout)
} else {
printer, err = printers.New("table", os.Stdout)
}

if err != nil {
return err
}

switch options.GlobalOptions.Output {
case "json":
err = printer.Print(keyListToDisplay)
default:
table := keyListToDisplay.ToTable()
err = printer.Print(table)
}
return err
}
81 changes: 81 additions & 0 deletions cmd/capibmadm/cmd/vpc/key/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
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 (
"github.com/go-openapi/strfmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Image vpc image 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 Image.
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: "string",
},
{
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
}
4 changes: 4 additions & 0 deletions cmd/capibmadm/cmd/vpc/vpc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
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
Expand All @@ -15,11 +16,13 @@ limitations under the License.
*/

// Package vpc contains the commands to operate on vpc resources.

package vpc

import (
"github.com/spf13/cobra"

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

Expand All @@ -34,6 +37,7 @@ func Commands() *cobra.Command {
cmd.PersistentFlags().StringVar(&options.GlobalOptions.ResourceGroupName, "resource-group-name", options.GlobalOptions.ResourceGroupName, "IBM cloud resource group name")

_ = cmd.MarkPersistentFlagRequired("region")
cmd.AddCommand(key.Commands())

return cmd
}
11 changes: 7 additions & 4 deletions cmd/capibmadm/options/options.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/*
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.
Expand All @@ -19,7 +16,6 @@ package options

import (
"github.com/spf13/cobra"

"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/printer"
)

Expand All @@ -44,3 +40,10 @@ func AddCommonFlags(cmd *cobra.Command) {
GlobalOptions.Output = printer.PrinterTypeTable
cmd.Flags().Var(&GlobalOptions.Output, "output", "Supported printer types: table, json")
}

// AddVPCCommonFlags will add common VPC flags to the cli.
func AddVPCCommonFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(&GlobalOptions.VPCRegion, "region", GlobalOptions.VPCRegion, "IBM cloud vpc region. (Required)")

_ = cmd.MarkPersistentFlagRequired("region")
}
Loading

0 comments on commit 626c20e

Please sign in to comment.