-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added changes for deletion of sshKey/sshKeys
- Loading branch information
Showing
5 changed files
with
195 additions
and
0 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,81 @@ | ||
// Copyright 2021 IBM Corp | ||
// | ||
// 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 keys | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/klog/v2" | ||
|
||
"github.com/ppc64le-cloud/pvsadm/pkg" | ||
"github.com/ppc64le-cloud/pvsadm/pkg/client" | ||
"github.com/ppc64le-cloud/pvsadm/pkg/utils" | ||
) | ||
|
||
const deletePromptMessage = "Deleting all the above ssh key/key's, Do you really want to continue?" | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "keys", | ||
Short: "Delete PowerVS ssh key/keys", | ||
Long: `Delete PowerVS ssh key/keys matching regex | ||
pvsadm purge --help for information | ||
`, | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
if pkg.Options.Expr == "" { | ||
return fmt.Errorf("--regexp is required and shouldn't be empty string") | ||
} | ||
|
||
return nil | ||
}, | ||
|
||
RunE: func(cmd *cobra.Command, args []string) error { | ||
opt := pkg.Options | ||
|
||
c, err := client.NewClientWithEnv(opt.APIKey, opt.Environment, opt.Debug) | ||
if err != nil { | ||
klog.Errorf("failed to create a session with IBM cloud: %v", err) | ||
return err | ||
} | ||
|
||
pvmclient, err := client.NewPVMClientWithEnv(c, opt.InstanceID, opt.InstanceName, opt.Environment) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
keys, err := pvmclient.KeyClient.GetAllPurgeable(pkg.Options.Before, pkg.Options.Since, pkg.Options.Expr) | ||
if err != nil { | ||
return fmt.Errorf("failed to get the ssh keys, err: %v", err) | ||
} | ||
|
||
klog.Infof("keys matched are %s", keys) | ||
if len(keys) != 0 { | ||
if opt.NoPrompt || utils.AskConfirmation(deletePromptMessage) { | ||
for _, key := range keys { | ||
err = pvmclient.KeyClient.Delete(key) | ||
if err != nil { | ||
if opt.IgnoreErrors { | ||
klog.Infof("error occurred while deleting the key: %v", err) | ||
} else { | ||
return fmt.Errorf("failed to delete a key, err: %v", err) | ||
} | ||
} | ||
klog.Infof("Successfully deleted a key, id: %s", key) | ||
} | ||
} | ||
} | ||
return nil | ||
}, | ||
} |
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,92 @@ | ||
// Copyright 2021 IBM Corp | ||
// | ||
// 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 ( | ||
"fmt" | ||
"regexp" | ||
"time" | ||
|
||
"github.com/ppc64le-cloud/pvsadm/pkg" | ||
|
||
"github.com/IBM-Cloud/power-go-client/clients/instance" | ||
"github.com/IBM-Cloud/power-go-client/errors" | ||
"github.com/IBM-Cloud/power-go-client/helpers" | ||
"github.com/IBM-Cloud/power-go-client/ibmpisession" | ||
"github.com/IBM-Cloud/power-go-client/power/client/p_cloud_tenants_ssh_keys" | ||
"github.com/IBM-Cloud/power-go-client/power/models" | ||
) | ||
|
||
type Client struct { | ||
session *ibmpisession.IBMPISession | ||
client *instance.IBMPIKeyClient | ||
instanceID string | ||
} | ||
|
||
func NewClient(sess *ibmpisession.IBMPISession, powerinstanceid string) *Client { | ||
c := &Client{ | ||
session: sess, | ||
instanceID: powerinstanceid, | ||
} | ||
c.client = instance.NewIBMPIKeyClient(sess, powerinstanceid) | ||
return c | ||
} | ||
|
||
func (c *Client) Get(id string) (*models.SSHKey, error) { | ||
return c.client.Get(id, c.instanceID) | ||
} | ||
|
||
func (c *Client) Create(name, sshKey string) (*models.SSHKey, *models.SSHKey, error) { | ||
return c.client.Create(name, sshKey, c.instanceID) | ||
} | ||
|
||
func (c *Client) Delete(id string) error { | ||
return c.client.Delete(id, c.instanceID) | ||
} | ||
|
||
// Get Keys... | ||
//TODO:Replace Getall function with upstream code | ||
func (c *Client) GetAll() (*models.SSHKeys, error) { | ||
|
||
var tenantid = c.session.UserAccount | ||
params := p_cloud_tenants_ssh_keys.NewPcloudTenantsSshkeysGetallParamsWithTimeout(helpers.PIGetTimeOut).WithTenantID(tenantid) | ||
resp, err := c.session.Power.PCloudTenantsSSHKeys.PcloudTenantsSshkeysGetall(params, ibmpisession.NewAuth(c.session, c.instanceID)) | ||
|
||
if err != nil || resp == nil || resp.Payload == nil { | ||
return nil, fmt.Errorf(errors.GetPIKeyOperationFailed, c.instanceID, err) | ||
} | ||
return resp.Payload, nil | ||
} | ||
|
||
func (c *Client) GetAllPurgeable(before, since time.Duration, expr string) ([]string, error) { | ||
keys, err := c.GetAll() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get the list of instances: %v", err) | ||
} | ||
|
||
var keysMatched []string | ||
r, _ := regexp.Compile(expr) | ||
|
||
for _, key := range keys.SSHKeys { | ||
if !r.MatchString(*key.Name) { | ||
continue | ||
} | ||
if !pkg.IsPurgeable(time.Time(*key.CreationDate), before, since) { | ||
continue | ||
} | ||
keysMatched = append(keysMatched, *key.Name) | ||
} | ||
return keysMatched, nil | ||
} |
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