-
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.
Ports list command implementation - PowerVS (#1084)
- Loading branch information
1 parent
e542018
commit dc92a2a
Showing
7 changed files
with
260 additions
and
0 deletions.
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,116 @@ | ||
/* | ||
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" | ||
) | ||
|
||
// 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=<api-key> | ||
capibmadm powervs port list --service-instance-id <service-instance-id> --zone <zone> --network <network-name/network-id>`, | ||
} | ||
|
||
var network string | ||
|
||
cmd.Flags().StringVar(&network, "network", "", "Network ID or Name") | ||
_ = cmd.MarkFlagRequired("network") | ||
|
||
cmd.RunE = func(cmd *cobra.Command, args []string) error { | ||
return listPorts(cmd.Context(), network) | ||
} | ||
|
||
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) | ||
|
||
// validating if network exists before fetching all ports. | ||
_, err = networkClient.Get(network) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
portListResp, err := networkClient.GetAllPorts(network) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
portList := PList{ | ||
Items: []PSpec{}, | ||
} | ||
|
||
for _, port := range portListResp.Ports { | ||
portList.Items = append(portList.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 := portList.ToTable() | ||
err = printerObj.Print(table) | ||
} else { | ||
err = printerObj.Print(portList) | ||
} | ||
|
||
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,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 | ||
} |
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,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 contains the commands to operate on PowerVS Network Port resources. | ||
package port | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// PSpec defines a port. | ||
type PSpec 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"` | ||
} | ||
|
||
// PList defines a list of Ports. | ||
type PList struct { | ||
Items []PSpec `json:"items"` | ||
} | ||
|
||
// ToTable converts List to *metav1.Table. | ||
func (portList *PList) ToTable() *metav1.Table { | ||
table := &metav1.Table{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: metav1.SchemeGroupVersion.String(), | ||
Kind: "Table", | ||
}, | ||
ColumnDefinitions: []metav1.TableColumnDefinition{ | ||
{ | ||
Name: "DESCRIPTION", | ||
Type: "string", | ||
}, | ||
{ | ||
Name: "EXTERNAL IP", | ||
Type: "string", | ||
}, | ||
{ | ||
Name: "IP ADDRESS", | ||
Type: "string", | ||
}, | ||
{ | ||
Name: "MAC ADDRESS", | ||
Type: "string", | ||
}, | ||
{ | ||
Name: "PORT ID", | ||
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 | ||
} |
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
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
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
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,22 @@ | ||
## PowerVS Network Commands | ||
|
||
### 1. capibmadm powervs port list | ||
|
||
#### Usage: | ||
List PowerVS ports. | ||
|
||
#### Environmental Variable: | ||
IBMCLOUD_API_KEY: IBM Cloud API key. | ||
|
||
#### Arguments: | ||
--service-instance-id: PowerVS service instance id. | ||
|
||
--zone: PowerVS zone. | ||
|
||
--network: Network ID or Name. | ||
|
||
#### Example: | ||
```shell | ||
export IBMCLOUD_API_KEY=<api-key> | ||
capibmadm powervs port list --service-instance-id <service-instance-id> --zone <zone> --network <network-name/network-id> | ||
``` |