-
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: Yussuf Shaikh <[email protected]>
- Loading branch information
Showing
5 changed files
with
199 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
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,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 | ||
} |
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,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 | ||
} |
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