diff --git a/cmd/capibmadm/cmd/powervs/port/create.go b/cmd/capibmadm/cmd/powervs/port/create.go new file mode 100644 index 0000000000..9e7f35cfdb --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/port/create.go @@ -0,0 +1,125 @@ +/* +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" + "fmt" + "os" + + "github.com/spf13/cobra" + + client "github.com/IBM-Cloud/power-go-client/clients/instance" + "github.com/IBM-Cloud/power-go-client/power/models" + + "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" +) + +type portCreateOptions struct { + network string + ipAddress string + description string +} + +// CreateCommand create powervs network port. +func CreateCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "create", + Short: "Create PowerVS Port", + Example: ` +# Create PowerVS network port +export IBMCLOUD_API_KEY= +capibmadm powervs port create --network --description --service-instance-id --zone `, + } + + var portCreateOption portCreateOptions + cmd.Flags().StringVar(&portCreateOption.network, "network", "", "Network ID or Name on which port is to be created") + cmd.Flags().StringVar(&portCreateOption.ipAddress, "ip-address", "", "IP Address to be assigned to the port") + cmd.Flags().StringVar(&portCreateOption.description, "description", "", "Description of the port") + cmd.RunE = func(cmd *cobra.Command, args []string) error { + if err := createPort(cmd.Context(), portCreateOption); err != nil { + return err + } + return nil + } + options.AddCommonFlags(cmd) + _ = cmd.MarkFlagRequired("network") + return cmd +} + +func createPort(ctx context.Context, portCreateOption portCreateOptions) error { + logger := log.Log + logger.Info("Creating Port ", "Network ID/Name", portCreateOption.network, "IP Address", portCreateOption.ipAddress, "Description", portCreateOption.description, "service-instance-id", options.GlobalOptions.ServiceInstanceID, "zone", options.GlobalOptions.PowerVSZone) + accountID, err := utils.GetAccountID(ctx, iam.GetIAMAuth()) + if err != nil { + return err + } + session, err := powervs.NewPISession(accountID, options.GlobalOptions.PowerVSZone, options.GlobalOptions.Debug) + if err != nil { + return err + } + + networkClient := client.NewIBMPINetworkClient(ctx, session, options.GlobalOptions.ServiceInstanceID) + + network, err := networkClient.Get(portCreateOption.network) + if err != nil { + return err + } + + params := &models.NetworkPortCreate{ + IPAddress: portCreateOption.ipAddress, + Description: portCreateOption.description, + } + + port, err := networkClient.CreatePort(*network.NetworkID, params) + if err != nil { + return fmt.Errorf("failed to create a port, err: %v", err) + } + logger.Info("Successfully created a port", "portID", *port.PortID) + + portInfo := PList{ + Items: []PSpec{}, + } + + portInfo.Items = append(portInfo.Items, PSpec{ + Description: utils.DereferencePointer(port.Description).(string), + ExternalIP: port.ExternalIP, + IPAddress: utils.DereferencePointer(port.IPAddress).(string), + MacAddress: utils.DereferencePointer(port.MacAddress).(string), + PortID: utils.DereferencePointer(port.PortID).(string), + Status: utils.DereferencePointer(port.Status).(string), + }) + + printerObj, err := printer.New(options.GlobalOptions.Output, os.Stdout) + if err != nil { + return fmt.Errorf("failed creating output printer: %w", err) + } + + if options.GlobalOptions.Output == printer.PrinterTypeTable { + table := portInfo.ToTable() + err = printerObj.Print(table) + } else { + err = printerObj.Print(portInfo) + } + return err +} diff --git a/cmd/capibmadm/cmd/powervs/port/port.go b/cmd/capibmadm/cmd/powervs/port/port.go index a5eef37d0c..2a6939c64a 100644 --- a/cmd/capibmadm/cmd/powervs/port/port.go +++ b/cmd/capibmadm/cmd/powervs/port/port.go @@ -33,6 +33,7 @@ func Commands() *cobra.Command { cmd.AddCommand(DeleteCommand()) cmd.AddCommand(ListCommand()) + cmd.AddCommand(CreateCommand()) return cmd } diff --git a/docs/book/src/topics/capibmadm/powervs/index.md b/docs/book/src/topics/capibmadm/powervs/index.md index 7c96fc8bc2..e97f745e5b 100644 --- a/docs/book/src/topics/capibmadm/powervs/index.md +++ b/docs/book/src/topics/capibmadm/powervs/index.md @@ -9,6 +9,7 @@ - [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) + - [create](/topics/capibmadm/powervs/port.html#1-capibmadm-powervs-port-create) - [delete](/topics/capibmadm/powervs/port.html#1-capibmadm-powervs-port-delete) - [list](/topics/capibmadm/powervs/port.html#1-capibmadm-powervs-port-list) - + diff --git a/docs/book/src/topics/capibmadm/powervs/port.md b/docs/book/src/topics/capibmadm/powervs/port.md index e2b077b643..1b1bc7d174 100644 --- a/docs/book/src/topics/capibmadm/powervs/port.md +++ b/docs/book/src/topics/capibmadm/powervs/port.md @@ -20,6 +20,7 @@ IBMCLOUD_API_KEY: IBM Cloud API key. export IBMCLOUD_API_KEY= capibmadm powervs port list --service-instance-id --zone --network ``` + ### 2. capibmadm powervs port delete #### Usage: @@ -41,4 +42,29 @@ IBMCLOUD_API_KEY: IBM Cloud API key. ```shell export IBMCLOUD_API_KEY= capibmadm powervs port delete --port-id --network --service-instance-id --zone +``` + +### 3. capibmadm powervs port create + +#### Usage: +Create PowerVS network port. + +#### Environmental Variable: +IBMCLOUD_API_KEY: IBM Cloud API key. + +#### Arguments: +--service-instance-id: PowerVS service instance id. + +--zone: PowerVS service instance zone. + +--network: Network ID/ Network Name. + +--description: Description of the port. + +--ip-address: The requested IP address of this port + +#### Example: +```shell +export IBMCLOUD_API_KEY= +capibmadm powervs port create --network --description --service-instance-id --zone ``` \ No newline at end of file diff --git a/go.mod b/go.mod index c725a12ccb..d4f781c22e 100644 --- a/go.mod +++ b/go.mod @@ -88,7 +88,7 @@ require ( github.com/hashicorp/hcl v1.0.0 // indirect github.com/huandu/xstrings v1.3.3 // indirect github.com/imdario/mergo v0.3.13 // indirect - github.com/inconshreveable/mousetrap v1.0.1 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/leodido/go-urn v1.2.1 // indirect diff --git a/go.sum b/go.sum index c239dbe5bc..9b9593a887 100644 --- a/go.sum +++ b/go.sum @@ -504,8 +504,9 @@ github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=