Skip to content

Commit

Permalink
Enhanced code for port create command
Browse files Browse the repository at this point in the history
  • Loading branch information
Shilpa-Gokul committed Feb 9, 2023
1 parent 96b900a commit ab667f5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
47 changes: 20 additions & 27 deletions cmd/capibmadm/cmd/powervs/port/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/printer"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils"
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
"strings"
)

type portCreateOptions struct {
Expand All @@ -46,7 +45,7 @@ func CreateCommand() *cobra.Command {
Example: `
# Create PowerVS port, provided networkID and IPAddress
export IBMCLOUD_API_KEY=<api-key>
capibmadm powervs port create --network <network_id> --ip-address <10.10.0.1> --description <description> --service-instance-id <service-instance-id> --zone <zone>`,
capibmadm powervs port create --network <networdID/Name> --ip-address <IPaddress> --description <description> --service-instance-id <service-instance-id> --zone <zone>`,
}

var portCreateOption portCreateOptions
Expand Down Expand Up @@ -81,25 +80,19 @@ func createPort(ctx context.Context, portCreateOption portCreateOptions) error {
if err != nil {
return err
}
var networkNames, networkIDs []string
for _, net := range networks.Networks {
networkIDs = append(networkIDs, *net.NetworkID)
networkNames = append(networkNames, *net.Name)
}

var netID string

if utils.Contains(networkIDs, portCreateOption.networkID) {
netID = portCreateOption.networkID
} else if utils.Contains(networkNames, portCreateOption.networkID) {
for _, n := range networks.Networks {
if *n.Name == portCreateOption.networkID {
netID = *n.NetworkID
}
for _, net := range networks.Networks {
if *net.NetworkID == portCreateOption.networkID {
netID = portCreateOption.networkID
break
} else if *net.Name == portCreateOption.networkID {
netID = *net.NetworkID
break
} else {
return fmt.Errorf("not able to find network: \"%s\" by ID or name in the list", portCreateOption.networkID)
}
} else {
return fmt.Errorf("not able to find network: \"%s\" by ID or name in the list: ids:[%s], names: [%s]", portCreateOption.networkID, strings.Join(networkIDs, ","), strings.Join(networkNames, ","))
}

params := &models.NetworkPortCreate{
IPAddress: portCreateOption.ipAddress,
Description: portCreateOption.description,
Expand All @@ -111,17 +104,17 @@ func createPort(ctx context.Context, portCreateOption portCreateOptions) error {
}
klog.Infof("Successfully created a port, id: %s", *port.PortID)

listByVersion := PortList{
portInfo := PortList{
Items: []PortSpec{},
}

listByVersion.Items = append(listByVersion.Items, PortSpec{
Description: *port.Description,
portInfo.Items = append(portInfo.Items, PortSpec{
Description: utils.DereferencePointer(port.Description).(string),
ExternalIP: port.ExternalIP,
IPAddress: *port.IPAddress,
MacAddress: *port.MacAddress,
PortID: *port.PortID,
Status: *port.Status,
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)
Expand All @@ -130,10 +123,10 @@ func createPort(ctx context.Context, portCreateOption portCreateOptions) error {
}

if options.GlobalOptions.Output == printer.PrinterTypeTable {
table := listByVersion.ToTable()
table := portInfo.ToTable()
printerObj.Print(table)
} else {
printerObj.Print(listByVersion)
printerObj.Print(portInfo)
}
return nil
}
9 changes: 0 additions & 9 deletions cmd/capibmadm/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,3 @@ func DereferencePointer(value interface{}) interface{} {
}
return nil
}

func Contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
1 change: 1 addition & 0 deletions docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions docs/book/src/topics/capibmadm/powervs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
- [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)
- [create](/topics/capibmadm/powervs/port.html#1-capibmadm-powervs-port-create)
21 changes: 21 additions & 0 deletions docs/book/src/topics/capibmadm/powervs/port.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Power VS Network Port Commands

### 1. capibmadm powervs port create

#### Usage:
Create Power VS network port.

#### Environmental Variable:
IBMCLOUD_API_KEY: IBM Cloud api key.

#### Arguments:
--service-instance-id: Power VS service instance id.

--zone: PowerVS service instance zone

#### Example:
```shell
export IBMCLOUD_API_KEY=<api-key>
capibmadm powervs port create --network <networdID/Name> --ip-address <IPaddress> --description <description> --service-instance-id <service-instance-id> --zone <zone>
```

0 comments on commit ab667f5

Please sign in to comment.