-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Punith Kenchappa <[email protected]>
- Loading branch information
Showing
9 changed files
with
275 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
``` |