Skip to content

Commit 7399387

Browse files
authored
Add initial code for powervs network list (#1075)
* 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]>
1 parent a38eb0b commit 7399387

File tree

10 files changed

+226
-15
lines changed

10 files changed

+226
-15
lines changed

cmd/capibmadm/clients/powervs/client.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ import (
2626
// NewPISession creates new powervs client.
2727
// To-Do: Need to handle custom endpoint URL if user wants to use staging env.
2828
func NewPISession(accountID string, zone string, debug bool) (*ibmpisession.IBMPISession, error) {
29-
return ibmpisession.NewIBMPISession(&ibmpisession.IBMPIOptions{Authenticator: iam.GetIAMAuth(),
30-
Debug: debug,
31-
UserAccount: accountID,
32-
Zone: zone})
29+
return ibmpisession.NewIBMPISession(&ibmpisession.IBMPIOptions{
30+
Authenticator: iam.GetIAMAuth(),
31+
Debug: debug,
32+
UserAccount: accountID,
33+
Zone: zone})
3334
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package network
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"os"
23+
24+
"github.com/spf13/cobra"
25+
26+
v "github.com/IBM-Cloud/power-go-client/clients/instance"
27+
28+
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
29+
30+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/iam"
31+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/powervs"
32+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
33+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/printer"
34+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils"
35+
)
36+
37+
// ListCommand function to create PowerVS network.
38+
func ListCommand() *cobra.Command {
39+
cmd := &cobra.Command{
40+
Use: "list",
41+
Short: "List PowerVS network",
42+
Example: `
43+
# List PowerVS networks
44+
export IBMCLOUD_API_KEY=<api-key>
45+
capibmadm powervs network list --service-instance-id <service-instance-id> --zone <zone>`,
46+
RunE: func(cmd *cobra.Command, args []string) error {
47+
if err := listNetwork(cmd.Context()); err != nil {
48+
return err
49+
}
50+
return nil
51+
},
52+
}
53+
54+
options.AddCommonFlags(cmd)
55+
return cmd
56+
}
57+
58+
func listNetwork(ctx context.Context) error {
59+
log := logf.Log
60+
log.Info("Listing PowerVS networks", "service-instance-id", options.GlobalOptions.ServiceInstanceID, "zone", options.GlobalOptions.PowerVSZone)
61+
62+
accountID, err := utils.GetAccountID(ctx, iam.GetIAMAuth())
63+
if err != nil {
64+
return err
65+
}
66+
sess, err := powervs.NewPISession(accountID, options.GlobalOptions.PowerVSZone, options.GlobalOptions.Debug)
67+
if err != nil {
68+
return err
69+
}
70+
71+
c := v.NewIBMPINetworkClient(ctx, sess, options.GlobalOptions.ServiceInstanceID)
72+
nets, err := c.GetAll()
73+
if err != nil {
74+
return err
75+
}
76+
77+
if len(nets.Networks) == 0 {
78+
fmt.Println("No Networks found")
79+
return nil
80+
}
81+
82+
listByVersion := IList{
83+
Items: []NetSpec{},
84+
}
85+
86+
for _, network := range nets.Networks {
87+
listByVersion.Items = append(listByVersion.Items, NetSpec{
88+
NetworkID: *network.NetworkID,
89+
Name: *network.Name,
90+
Type: *network.Type,
91+
VlanID: *network.VlanID,
92+
Jumbo: *network.Jumbo,
93+
DhcpManaged: network.DhcpManaged,
94+
})
95+
}
96+
97+
pr, err := printer.New(options.GlobalOptions.Output, os.Stdout)
98+
if err != nil {
99+
return err
100+
}
101+
102+
if options.GlobalOptions.Output == printer.PrinterTypeJSON {
103+
err = pr.Print(listByVersion)
104+
} else {
105+
table := listByVersion.ToTable()
106+
err = pr.Print(table)
107+
}
108+
109+
return err
110+
}

cmd/capibmadm/cmd/powervs/network/network.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func Commands() *cobra.Command {
2828
}
2929

3030
cmd.AddCommand(CreateCommand())
31+
cmd.AddCommand(ListCommand())
3132

3233
return cmd
3334
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package network
15+
16+
import (
17+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18+
)
19+
20+
// NetSpec defines a Network.
21+
type NetSpec struct {
22+
NetworkID string `json:"id"`
23+
Name string `json:"name"`
24+
Type string `json:"type"`
25+
VlanID float64 `json:"vlanID"`
26+
Jumbo bool `json:"jumbo"`
27+
DhcpManaged bool `json:"dhcpManaged"`
28+
}
29+
30+
// IList defines a list of Networks.
31+
type IList struct {
32+
Items []NetSpec `json:"items"`
33+
}
34+
35+
// ToTable converts List to *metav1.Table.
36+
func (netList *IList) ToTable() *metav1.Table {
37+
table := &metav1.Table{
38+
TypeMeta: metav1.TypeMeta{
39+
APIVersion: metav1.SchemeGroupVersion.String(),
40+
Kind: "Table",
41+
},
42+
ColumnDefinitions: []metav1.TableColumnDefinition{
43+
{
44+
Name: "NETWORK ID",
45+
Type: "string",
46+
},
47+
{
48+
Name: "Name",
49+
Type: "string",
50+
},
51+
{
52+
Name: "Type",
53+
Type: "string",
54+
},
55+
{
56+
Name: "VLAN ID",
57+
Type: "string",
58+
},
59+
{
60+
Name: "Jumbo",
61+
Type: "bool",
62+
},
63+
{
64+
Name: "DHCP Managed",
65+
Type: "bool",
66+
},
67+
},
68+
}
69+
70+
for _, network := range netList.Items {
71+
row := metav1.TableRow{
72+
Cells: []interface{}{network.NetworkID, network.Name, network.Type, network.VlanID, network.Jumbo, network.DhcpManaged},
73+
}
74+
table.Rows = append(table.Rows, row)
75+
}
76+
return table
77+
}

cmd/capibmadm/cmd/powervs/powervs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ func Commands() *cobra.Command {
3030
Short: "Commands for operations on PowerVS resources",
3131
}
3232

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

3737
_ = cmd.MarkPersistentFlagRequired("service-instance-id")

cmd/capibmadm/options/options.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,5 @@ type options struct {
4141

4242
// AddCommonFlags will add common flags to the cli.
4343
func AddCommonFlags(cmd *cobra.Command) {
44-
GlobalOptions.Output = printer.PrinterTypeTable
45-
cmd.Flags().Var(&GlobalOptions.Output, "output", "Supported printer types: table, json")
44+
cmd.Flags().StringVarP((*string)(&GlobalOptions.Output), "output", "o", "table", "The output format of the results. Supported printer types: table, json")
4645
}

docs/book/src/topics/capibmadm/powervs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
## 1. Power VS commands
55
- [network](./network.md)
66
- [create](/topics/capibmadm/powervs/network.html#1-capibmadm-powervs-network-create)
7+
- [list](/topics/capibmadm/powervs/network.html#2-capibmadm-powervs-network-list)

docs/book/src/topics/capibmadm/powervs/network.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### 1. capibmadm powervs network create
44

5-
#### Usage:
5+
#### Usage:
66
Create Power VS network.
77

88
#### Environmental Variable:
@@ -17,4 +17,25 @@ IBMCLOUD_API_KEY: IBM Cloud api key.
1717
```shell
1818
export IBMCLOUD_API_KEY=<api-key>
1919
capibmadm powervs network create <network-name> --service-instance-id <service-instance-id>
20-
```
20+
```
21+
22+
23+
24+
### 2. capibmadm powervs network list
25+
26+
#### Usage:
27+
List PowerVS networks.
28+
29+
#### Environmental Variable:
30+
IBMCLOUD_API_KEY: IBM Cloud api key.
31+
32+
#### Arguments:
33+
--service-instance-id: PowerVS service instance id.
34+
35+
--zone: PowerVS service instance zone.
36+
37+
#### Example:
38+
```shell
39+
export IBMCLOUD_API_KEY=<api-key>
40+
capibmadm powervs network list --service-instance-id <service-instance-id> --zone <zone>
41+
```

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ require (
109109
github.com/pelletier/go-toml v1.9.5 // indirect
110110
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
111111
github.com/pmezard/go-difflib v1.0.0 // indirect
112-
github.com/prometheus/client_golang v1.13.0 // indirect
113-
github.com/prometheus/client_model v0.2.0 // indirect
112+
github.com/prometheus/client_golang v1.14.0 // indirect
113+
github.com/prometheus/client_model v0.3.0 // indirect
114114
github.com/prometheus/common v0.37.0 // indirect
115115
github.com/prometheus/procfs v0.8.0 // indirect
116116
github.com/shopspring/decimal v1.3.1 // indirect

go.sum

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -661,13 +661,14 @@ github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5Fsn
661661
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
662662
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
663663
github.com/prometheus/client_golang v1.12.1/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY=
664-
github.com/prometheus/client_golang v1.13.0 h1:b71QUfeo5M8gq2+evJdTPfZhYMAU0uKPkyPJ7TPsloU=
665-
github.com/prometheus/client_golang v1.13.0/go.mod h1:vTeo+zgvILHsnnj/39Ou/1fPN5nJFOEMgftOUOmlvYQ=
664+
github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw=
665+
github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y=
666666
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
667667
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
668668
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
669-
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
670669
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
670+
github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4=
671+
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
671672
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
672673
github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
673674
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=

0 commit comments

Comments
 (0)