-
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.
VPC key create and delete operations added
- Loading branch information
1 parent
7399387
commit 22f635e
Showing
5 changed files
with
264 additions
and
1 deletion.
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,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 | ||
} |
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,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 | ||
} |
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,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 |
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,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 | ||
} |
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