Skip to content

Commit

Permalink
Add initial code for powervs network list (#1075)
Browse files Browse the repository at this point in the history
* Add initial code for powervs network list

Signed-off-by: Yussuf Shaikh <[email protected]>

* Doc update for powervs network list

Signed-off-by: Yussuf Shaikh <[email protected]>

* updates in common code

Signed-off-by: Yussuf Shaikh <[email protected]>

---------

Signed-off-by: Yussuf Shaikh <[email protected]>
  • Loading branch information
yussufsh authored Feb 8, 2023
1 parent a38eb0b commit 7399387
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 15 deletions.
9 changes: 5 additions & 4 deletions cmd/capibmadm/clients/powervs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import (
// NewPISession creates new powervs client.
// To-Do: Need to handle custom endpoint URL if user wants to use staging env.
func NewPISession(accountID string, zone string, debug bool) (*ibmpisession.IBMPISession, error) {
return ibmpisession.NewIBMPISession(&ibmpisession.IBMPIOptions{Authenticator: iam.GetIAMAuth(),
Debug: debug,
UserAccount: accountID,
Zone: zone})
return ibmpisession.NewIBMPISession(&ibmpisession.IBMPIOptions{
Authenticator: iam.GetIAMAuth(),
Debug: debug,
UserAccount: accountID,
Zone: zone})
}
110 changes: 110 additions & 0 deletions cmd/capibmadm/cmd/powervs/network/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
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 network

import (
"context"
"fmt"
"os"

"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/printer"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils"
)

// ListCommand function to create PowerVS network.
func ListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List PowerVS network",
Example: `
# List PowerVS networks
export IBMCLOUD_API_KEY=<api-key>
capibmadm powervs network list --service-instance-id <service-instance-id> --zone <zone>`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := listNetwork(cmd.Context()); err != nil {
return err
}
return nil
},
}

options.AddCommonFlags(cmd)
return cmd
}

func listNetwork(ctx context.Context) error {
log := logf.Log
log.Info("Listing PowerVS networks", "service-instance-id", options.GlobalOptions.ServiceInstanceID, "zone", options.GlobalOptions.PowerVSZone)

accountID, err := utils.GetAccountID(ctx, iam.GetIAMAuth())
if err != nil {
return err
}
sess, err := powervs.NewPISession(accountID, options.GlobalOptions.PowerVSZone, options.GlobalOptions.Debug)
if err != nil {
return err
}

c := v.NewIBMPINetworkClient(ctx, sess, options.GlobalOptions.ServiceInstanceID)
nets, err := c.GetAll()
if err != nil {
return err
}

if len(nets.Networks) == 0 {
fmt.Println("No Networks found")
return nil
}

listByVersion := IList{
Items: []NetSpec{},
}

for _, network := range nets.Networks {
listByVersion.Items = append(listByVersion.Items, NetSpec{
NetworkID: *network.NetworkID,
Name: *network.Name,
Type: *network.Type,
VlanID: *network.VlanID,
Jumbo: *network.Jumbo,
DhcpManaged: network.DhcpManaged,
})
}

pr, err := printer.New(options.GlobalOptions.Output, os.Stdout)
if err != nil {
return err
}

if options.GlobalOptions.Output == printer.PrinterTypeJSON {
err = pr.Print(listByVersion)
} else {
table := listByVersion.ToTable()
err = pr.Print(table)
}

return err
}
1 change: 1 addition & 0 deletions cmd/capibmadm/cmd/powervs/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func Commands() *cobra.Command {
}

cmd.AddCommand(CreateCommand())
cmd.AddCommand(ListCommand())

return cmd
}
77 changes: 77 additions & 0 deletions cmd/capibmadm/cmd/powervs/network/type.go
Original file line number Diff line number Diff line change
@@ -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 network

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// NetSpec defines a Network.
type NetSpec struct {
NetworkID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
VlanID float64 `json:"vlanID"`
Jumbo bool `json:"jumbo"`
DhcpManaged bool `json:"dhcpManaged"`
}

// IList defines a list of Networks.
type IList struct {
Items []NetSpec `json:"items"`
}

// ToTable converts List to *metav1.Table.
func (netList *IList) ToTable() *metav1.Table {
table := &metav1.Table{
TypeMeta: metav1.TypeMeta{
APIVersion: metav1.SchemeGroupVersion.String(),
Kind: "Table",
},
ColumnDefinitions: []metav1.TableColumnDefinition{
{
Name: "NETWORK ID",
Type: "string",
},
{
Name: "Name",
Type: "string",
},
{
Name: "Type",
Type: "string",
},
{
Name: "VLAN ID",
Type: "string",
},
{
Name: "Jumbo",
Type: "bool",
},
{
Name: "DHCP Managed",
Type: "bool",
},
},
}

for _, network := range netList.Items {
row := metav1.TableRow{
Cells: []interface{}{network.NetworkID, network.Name, network.Type, network.VlanID, network.Jumbo, network.DhcpManaged},
}
table.Rows = append(table.Rows, row)
}
return table
}
4 changes: 2 additions & 2 deletions cmd/capibmadm/cmd/powervs/powervs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func Commands() *cobra.Command {
Short: "Commands for operations on PowerVS resources",
}

cmd.PersistentFlags().StringVar(&options.GlobalOptions.ServiceInstanceID, "service-instance-id", "", "PowerVS service instance id")
cmd.PersistentFlags().StringVar(&options.GlobalOptions.PowerVSZone, "zone", options.GlobalOptions.PowerVSZone, "IBM cloud PowerVS zone (Required)")
cmd.PersistentFlags().StringVar(&options.GlobalOptions.ServiceInstanceID, "service-instance-id", "", "PowerVS service instance id (Required)")
cmd.PersistentFlags().StringVar(&options.GlobalOptions.PowerVSZone, "zone", options.GlobalOptions.PowerVSZone, "PowerVS service instance location (Required)")
cmd.PersistentFlags().BoolVar(&options.GlobalOptions.Debug, "debug", false, "Enable/Disable http transport debugging log")

_ = cmd.MarkPersistentFlagRequired("service-instance-id")
Expand Down
3 changes: 1 addition & 2 deletions cmd/capibmadm/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,5 @@ type options struct {

// AddCommonFlags will add common flags to the cli.
func AddCommonFlags(cmd *cobra.Command) {
GlobalOptions.Output = printer.PrinterTypeTable
cmd.Flags().Var(&GlobalOptions.Output, "output", "Supported printer types: table, json")
cmd.Flags().StringVarP((*string)(&GlobalOptions.Output), "output", "o", "table", "The output format of the results. Supported printer types: table, json")
}
1 change: 1 addition & 0 deletions docs/book/src/topics/capibmadm/powervs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
## 1. Power VS commands
- [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)
25 changes: 23 additions & 2 deletions docs/book/src/topics/capibmadm/powervs/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### 1. capibmadm powervs network create

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

#### Environmental Variable:
Expand All @@ -17,4 +17,25 @@ IBMCLOUD_API_KEY: IBM Cloud api key.
```shell
export IBMCLOUD_API_KEY=<api-key>
capibmadm powervs network create <network-name> --service-instance-id <service-instance-id>
```
```



### 2. capibmadm powervs network list

#### Usage:
List PowerVS networks.

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

#### Arguments:
--service-instance-id: PowerVS service instance id.

--zone: PowerVS service instance zone.

#### Example:
```shell
export IBMCLOUD_API_KEY=<api-key>
capibmadm powervs network list --service-instance-id <service-instance-id> --zone <zone>
```
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ require (
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.13.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -661,13 +661,14 @@ github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5Fsn
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
github.com/prometheus/client_golang v1.12.1/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY=
github.com/prometheus/client_golang v1.13.0 h1:b71QUfeo5M8gq2+evJdTPfZhYMAU0uKPkyPJ7TPsloU=
github.com/prometheus/client_golang v1.13.0/go.mod h1:vTeo+zgvILHsnnj/39Ou/1fPN5nJFOEMgftOUOmlvYQ=
github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw=
github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4=
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
Expand Down

0 comments on commit 7399387

Please sign in to comment.