Skip to content

Commit

Permalink
VPC key create and delete operations added
Browse files Browse the repository at this point in the history
  • Loading branch information
rcmadhankumar committed Feb 8, 2023
1 parent 7399387 commit 22f635e
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 1 deletion.
105 changes: 105 additions & 0 deletions cmd/capibmadm/cmd/vpc/key/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
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"

"github.com/spf13/cobra"

"github.com/IBM/vpc-go-sdk/vpcv1"
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/vpc"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils"
)

type keyCreateOptions struct {
name string
publicKey string
resourceGroupName string
}

// CreateCommand vpc key create command
func CreateCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Create VPC key",
Example: `
# Create key in VPC
export IBMCLOUD_API_KEY=<api-key>
capibmadm vpc key create key-name --region <region> --resource-group-name <resource-group-name>
--public-key <public-key-string> `,
}

options.AddCommonFlags(cmd)
var keyCreateOption keyCreateOptions
cmd.Flags().StringVar(&keyCreateOption.resourceGroupName, "resource-group-name", keyCreateOption.resourceGroupName, "IBM cloud resource group name")
cmd.Flags().StringVar(&keyCreateOption.publicKey, "public-key", keyCreateOption.publicKey, "Public Key")
_ = cmd.MarkFlagRequired("public-key")
cmd.RunE = func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("key-name name is not provided")
}

keyCreateOption.name = args[0]
if err := createKey(cmd.Context(), keyCreateOption); err != nil {
return err
}
return nil
}

return cmd
}

func createKey(ctx context.Context, keyCreateOption keyCreateOptions) error {
log := logf.Log
v1, err := vpc.NewV1Client(options.GlobalOptions.VPCRegion)
if err != nil {
return err
}

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

options := &vpcv1.CreateKeyOptions{}

options.SetName(keyCreateOption.name)
options.SetPublicKey(keyCreateOption.publicKey)

if keyCreateOption.resourceGroupName != "" {
resourceGroupID, err := utils.GetResourceGroupID(ctx, keyCreateOption.resourceGroupName, accountID)
if err != nil {
return err
}
resourceGroup := &vpcv1.ResourceGroupIdentity{
ID: &resourceGroupID,
}
options.SetResourceGroup(resourceGroup)
}

key, _, err := v1.CreateKey(options)
if err == nil {
log.Info("VPC Key created successfully,", "key id", *key.ID)
}
return err
}
106 changes: 106 additions & 0 deletions cmd/capibmadm/cmd/vpc/key/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
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"

"github.com/spf13/cobra"
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"

"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"
)

type keyDeleteOptions struct {
name string
}

// DeleteCommand vpc key delete command
func DeleteCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Delete VPC key",
Example: `
# Create key in VPC
export IBMCLOUD_API_KEY=<api-key>
capibmadm vpc key delete key-name --region <region>`,
}

options.AddCommonFlags(cmd)
var keyDeleteOption keyDeleteOptions
cmd.RunE = func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("key-name name is not provided")
}

keyDeleteOption.name = args[0]
if err := deleteKey(cmd.Context(), keyDeleteOption); err != nil {
return err
}
return nil
}

return cmd
}

func deleteKey(ctx context.Context, keyDeleteOption keyDeleteOptions) error {
log := logf.Log
v1, err := vpc.NewV1Client(options.GlobalOptions.VPCRegion)
if err != nil {
return err
}

listKeysOptions := &vpcv1.ListKeysOptions{}
pager, err := v1.NewKeysPager(listKeysOptions)
if err != nil {
panic(err)
}

var allResults []vpcv1.Key
for pager.HasNext() {
nextPage, err := pager.GetNext()
if err != nil {
panic(err)
}
allResults = append(allResults, nextPage...)
}

var keyId string
for _, key := range allResults {
if *key.Name == keyDeleteOption.name {
keyId = *key.ID
break
}
}

if keyId == "" {
return fmt.Errorf("key with the given name is not found")
}

options := &vpcv1.DeleteKeyOptions{}
options.SetID(keyId)

_, err = v1.DeleteKey(options)
if err == nil {
log.Info("VPC Key deleted succssfully, ", " key name", keyDeleteOption.name)
}
return err
}
18 changes: 18 additions & 0 deletions cmd/capibmadm/cmd/vpc/key/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
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
33 changes: 33 additions & 0 deletions cmd/capibmadm/cmd/vpc/key/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
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(CreateCommand())
cmd.AddCommand(DeleteCommand())
return cmd
}
3 changes: 2 additions & 1 deletion cmd/capibmadm/cmd/vpc/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ 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 +35,6 @@ 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
}

0 comments on commit 22f635e

Please sign in to comment.