diff --git a/cmd/capibmadm/cmd/powervs/port/doc.go b/cmd/capibmadm/cmd/powervs/port/doc.go new file mode 100644 index 0000000000..24970ae945 --- /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 Network Port resources. +package port diff --git a/cmd/capibmadm/cmd/powervs/port/list.go b/cmd/capibmadm/cmd/powervs/port/list.go new file mode 100644 index 0000000000..01d5b86d71 --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/port/list.go @@ -0,0 +1,136 @@ +/* + 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" + + powerClient "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/printer" + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils" +) + +var network string + +// ListCommand powervs port list command. +func ListCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List PowerVS ports", + Example: ` + # List ports in PowerVS Network + export IBMCLOUD_API_KEY= + capibmadm powervs port list --service-instance-id --zone --network `, + } + + cmd.Flags().StringVar(&network, "network", "", "Network ID or Name(preference will be given to the ID over Name)") + _ = cmd.MarkFlagRequired("network") + + cmd.RunE = func(cmd *cobra.Command, args []string) error { + if err := listPorts(cmd.Context(), network); err != nil { + return err + } + return nil + } + + options.AddCommonFlags(cmd) + return cmd +} + +func listPorts(ctx context.Context, network string) error { + + log := logf.Log + log.Info("Listing PowerVS ports", "service-instance-id", options.GlobalOptions.ServiceInstanceID, "network", network) + + auth := iam.GetIAMAuth() + accountID, err := utils.GetAccountID(ctx, auth) + if err != nil { + return err + } + sess, err := powervs.NewPISession(accountID, options.GlobalOptions.PowerVSZone, options.GlobalOptions.Debug) + if err != nil { + return err + } + + networkClient := powerClient.NewIBMPINetworkClient(ctx, sess, options.GlobalOptions.ServiceInstanceID) + networks, err := networkClient.GetAll() + if err != nil { + return fmt.Errorf("failed to get the networks, err: %v", err) + } + + var netID string + + for _, net := range networks.Networks { + if *net.NetworkID == network { + netID = network + break + } + if *net.Name == network { + netID = *net.NetworkID + break + } + } + + if netID == "" { + return fmt.Errorf("Not able to find network: \"%s\" by ID or name in the network list", network) + } + + portListResp, err := networkClient.GetAllPorts(netID) + if err != nil { + return err + } + + portList := PortList{ + Items: []PortSpec{}, + } + + for _, port := range portListResp.Ports { + portList.Items = append(portList.Items, PortSpec{ + 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 := portList.ToTable() + err = printerObj.Print(table) + } else { + err = printerObj.Print(portList) + } + + return err +} diff --git a/cmd/capibmadm/cmd/powervs/port/port.go b/cmd/capibmadm/cmd/powervs/port/port.go new file mode 100644 index 0000000000..2f20e85883 --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/port/port.go @@ -0,0 +1,36 @@ +/* +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" + + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options" +) + +// Commands function to add PowerVS port commands. +func Commands() *cobra.Command { + cmd := &cobra.Command{ + Use: "port", + Short: "Perform PowerVS port operations", + } + options.AddCommonFlags(cmd) + + cmd.AddCommand(ListCommand()) + + return cmd +} diff --git a/cmd/capibmadm/cmd/powervs/port/type.go b/cmd/capibmadm/cmd/powervs/port/type.go new file mode 100644 index 0000000000..a90b912ba2 --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/port/type.go @@ -0,0 +1,77 @@ +/* + 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 ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// PortSpec defines a port +type PortSpec struct { + PortID string `json:"id"` + Description string `json:"description"` + ExternalIP string `json:"externalIP,omitempty"` + IPAddress string `json:"ipAddress"` + MacAddress string `json:"macAddress"` + Status string `json:"status"` +} + +// PortList defines a list of Ports. +type PortList struct { + Items []PortSpec `json:"items"` +} + +// ToTable converts List to *metav1.Table. +func (portList *PortList) ToTable() *metav1.Table { + table := &metav1.Table{ + TypeMeta: metav1.TypeMeta{ + APIVersion: metav1.SchemeGroupVersion.String(), + Kind: "Table", + }, + ColumnDefinitions: []metav1.TableColumnDefinition{ + { + Name: "DESCRIPTION", + Type: "string", + }, + { + Name: "EXTERNALIP", + Type: "string", + }, + { + Name: "IPADDRESS", + Type: "string", + }, + { + Name: "MACADDRESS", + Type: "string", + }, + { + Name: "PORTID", + Type: "string", + }, + { + Name: "STATUS", + Type: "string", + }, + }, + } + + for _, port := range portList.Items { + row := metav1.TableRow{ + Cells: []interface{}{port.Description, port.ExternalIP, port.IPAddress, port.MacAddress, port.PortID, port.Status}, + } + table.Rows = append(table.Rows, row) + } + return table +} 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..652b61b6a5 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) + - [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 new file mode 100644 index 0000000000..2ab097d2e7 --- /dev/null +++ b/docs/book/src/topics/capibmadm/powervs/port.md @@ -0,0 +1,22 @@ +## Power VS Network Commands + +### 1. capibmadm powervs port list + +#### Usage: +List Power VS ports. + +#### Environmental Variable: +IBMCLOUD_API_KEY: IBM Cloud api key. + +#### Arguments: +--service-instance-id: Power VS service instance id. + +--zone: Power VS zone + +--network: Network ID or Name(preference will be given to the ID over Name) + +#### Example: +```shell +export IBMCLOUD_API_KEY= +capibmadm powervs port list --service-instance-id --zone --network +``` \ No newline at end of file