From 8b0e8a531d56f1997367f16fcedf2355579ef50a Mon Sep 17 00:00:00 2001 From: Rajalakshmi Girish Date: Wed, 8 Feb 2023 17:49:52 +0530 Subject: [PATCH] Added Port Delete command to capibmadm --- cmd/capibmadm/cmd/powervs/port/delete.go | 81 +++++++++++++++++++ cmd/capibmadm/cmd/powervs/port/doc.go | 18 +++++ cmd/capibmadm/cmd/powervs/port/port.go | 32 ++++++++ cmd/capibmadm/cmd/powervs/powervs.go | 2 + docs/book/src/SUMMARY.md | 1 + .../src/topics/capibmadm/powervs/index.md | 2 + .../book/src/topics/capibmadm/powervs/port.md | 24 ++++++ 7 files changed, 160 insertions(+) create mode 100755 cmd/capibmadm/cmd/powervs/port/delete.go create mode 100755 cmd/capibmadm/cmd/powervs/port/doc.go create mode 100755 cmd/capibmadm/cmd/powervs/port/port.go create mode 100755 docs/book/src/topics/capibmadm/powervs/port.md diff --git a/cmd/capibmadm/cmd/powervs/port/delete.go b/cmd/capibmadm/cmd/powervs/port/delete.go new file mode 100755 index 0000000000..c3b599e728 --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/port/delete.go @@ -0,0 +1,81 @@ +/* +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 port + +import ( + "context" + + "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/utils" +) + +type portDeleteOptions struct { + network string + portID string +} + +// DeleteCommand function to delete network's port. +func DeleteCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "delete", + Short: "Delete PowerVS network port", + Example: ` +# Delete PowerVS network port with ID in network "capi-network" +export IBMCLOUD_API_KEY= +capibmadm powervs port delete capi-network --port-id --service-instance-id `, + } + + var portDeleteOption portDeleteOptions + cmd.Flags().StringVar(&portDeleteOption.portID, "port-id", "", "Port ID to be deleted") + cmd.Flags().StringVar(&portDeleteOption.network, "network", "", "Network ID or Name(preference will be given to the ID over Name") + _ = cmd.MarkFlagRequired("port-id") + _ = cmd.MarkFlagRequired("network") + + cmd.RunE = func(cmd *cobra.Command, args []string) error { + if err := deletePort(cmd.Context(), portDeleteOption); err != nil { + return err + } + return nil + } + return cmd +} + +func deletePort(ctx context.Context, portDeleteOption portDeleteOptions) error { + log := logf.Log + log.Info("Deleting Power VS network port", "of network", portDeleteOption.network, "service-instance-id", options.GlobalOptions.ServiceInstanceID, "port-id", portDeleteOption.portID) + auth := iam.GetIAMAuth() + accountID, _ := utils.GetAccountID(ctx, auth) + sess, err := powervs.NewPISession(accountID, options.GlobalOptions.PowerVSZone, options.GlobalOptions.Debug) + if err != nil { + return err + } + c := v.NewIBMPINetworkClient(ctx, sess, options.GlobalOptions.ServiceInstanceID) + errDel := c.DeletePort(portDeleteOption.network, portDeleteOption.portID) + if errDel != nil { + return errDel + } + log.Info("Successfully deleted a port", "of port ID", portDeleteOption.portID) + return nil +} diff --git a/cmd/capibmadm/cmd/powervs/port/doc.go b/cmd/capibmadm/cmd/powervs/port/doc.go new file mode 100755 index 0000000000..832920f55c --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/port/doc.go @@ -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 port contains the commands to operate on Power VS Port resources. +package port diff --git a/cmd/capibmadm/cmd/powervs/port/port.go b/cmd/capibmadm/cmd/powervs/port/port.go new file mode 100755 index 0000000000..6b7ab0743a --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/port/port.go @@ -0,0 +1,32 @@ +/* +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 port + +import ( + "github.com/spf13/cobra" +) + +// Commands function to add PowerVS port commands. +func Commands() *cobra.Command { + cmd := &cobra.Command{ + Use: "port", + Short: "Perform PowerVS port operations", + } + + cmd.AddCommand(DeleteCommand()) + return cmd +} diff --git a/cmd/capibmadm/cmd/powervs/powervs.go b/cmd/capibmadm/cmd/powervs/powervs.go index e6f444e490..b6f867c004 100644 --- a/cmd/capibmadm/cmd/powervs/powervs.go +++ b/cmd/capibmadm/cmd/powervs/powervs.go @@ -20,6 +20,7 @@ import ( "github.com/spf13/cobra" "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs/network" + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs/port" "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options" ) @@ -38,6 +39,7 @@ func Commands() *cobra.Command { _ = cmd.MarkPersistentFlagRequired("zone") cmd.AddCommand(network.Commands()) + cmd.AddCommand(port.Commands()) return cmd } diff --git a/docs/book/src/SUMMARY.md b/docs/book/src/SUMMARY.md index 2830c3b92d..706729cff3 100644 --- a/docs/book/src/SUMMARY.md +++ b/docs/book/src/SUMMARY.md @@ -20,6 +20,7 @@ - [capibmadm CLI](./topics/capibmadm/index.md) - [Power VS Commands](./topics/capibmadm/powervs/index.md) - [Network Commands](./topics/capibmadm/powervs/network.md) + - [Port Commands](./topics/capibmadm/powervs/port.md) - [Developer Guide](./developer/index.md) - [Rapid iterative development with Tilt](./developer/tilt.md) - [Guide for API conversions](./developer/conversion.md) diff --git a/docs/book/src/topics/capibmadm/powervs/index.md b/docs/book/src/topics/capibmadm/powervs/index.md index 918d9aebc4..5040346f4f 100644 --- a/docs/book/src/topics/capibmadm/powervs/index.md +++ b/docs/book/src/topics/capibmadm/powervs/index.md @@ -5,3 +5,5 @@ - [network](./network.md) - [create](/topics/capibmadm/powervs/network.html#1-capibmadm-powervs-network-create) - [list](/topics/capibmadm/powervs/network.html#2-capibmadm-powervs-network-list) +- [port](./port.md) + - [delete](/topics/capibmadm/powervs/port.html#1-capibmadm-powervs-port-delete) diff --git a/docs/book/src/topics/capibmadm/powervs/port.md b/docs/book/src/topics/capibmadm/powervs/port.md new file mode 100755 index 0000000000..4afc8bcfb2 --- /dev/null +++ b/docs/book/src/topics/capibmadm/powervs/port.md @@ -0,0 +1,24 @@ +## Power VS Network Commands + +### 1. capibmadm powervs port delete + +#### Usage: +Delete Power VS network port. + +#### Environmental Variable: +IBMCLOUD_API_KEY: IBM Cloud api key. + +#### Arguments: +--service-instance-id: Power VS service instance id. + +--zone: Power VS zone + +--port-id: ID of network port + +--network: Network ID or Name(preference will be given to the ID over Name + +#### Example: +```shell +export IBMCLOUD_API_KEY= +capibmadm powervs port delete --port-id --network --service-instance-id --zone +`` \ No newline at end of file