From 7399387ba30858517ae5e76a251625dcb2b03705 Mon Sep 17 00:00:00 2001 From: Yussuf Shaikh Date: Wed, 8 Feb 2023 13:50:59 +0530 Subject: [PATCH] Add initial code for powervs network list (#1075) * Add initial code for powervs network list Signed-off-by: Yussuf Shaikh * Doc update for powervs network list Signed-off-by: Yussuf Shaikh * updates in common code Signed-off-by: Yussuf Shaikh --------- Signed-off-by: Yussuf Shaikh --- cmd/capibmadm/clients/powervs/client.go | 9 +- cmd/capibmadm/cmd/powervs/network/list.go | 110 ++++++++++++++++++ cmd/capibmadm/cmd/powervs/network/network.go | 1 + cmd/capibmadm/cmd/powervs/network/type.go | 77 ++++++++++++ cmd/capibmadm/cmd/powervs/powervs.go | 4 +- cmd/capibmadm/options/options.go | 3 +- .../src/topics/capibmadm/powervs/index.md | 1 + .../src/topics/capibmadm/powervs/network.md | 25 +++- go.mod | 4 +- go.sum | 7 +- 10 files changed, 226 insertions(+), 15 deletions(-) create mode 100644 cmd/capibmadm/cmd/powervs/network/list.go create mode 100644 cmd/capibmadm/cmd/powervs/network/type.go diff --git a/cmd/capibmadm/clients/powervs/client.go b/cmd/capibmadm/clients/powervs/client.go index 4393e43cd..0d9e20b95 100644 --- a/cmd/capibmadm/clients/powervs/client.go +++ b/cmd/capibmadm/clients/powervs/client.go @@ -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}) } diff --git a/cmd/capibmadm/cmd/powervs/network/list.go b/cmd/capibmadm/cmd/powervs/network/list.go new file mode 100644 index 000000000..6ae968380 --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/network/list.go @@ -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= +capibmadm powervs network list --service-instance-id --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 +} diff --git a/cmd/capibmadm/cmd/powervs/network/network.go b/cmd/capibmadm/cmd/powervs/network/network.go index 715adf03c..5d28c296c 100644 --- a/cmd/capibmadm/cmd/powervs/network/network.go +++ b/cmd/capibmadm/cmd/powervs/network/network.go @@ -28,6 +28,7 @@ func Commands() *cobra.Command { } cmd.AddCommand(CreateCommand()) + cmd.AddCommand(ListCommand()) return cmd } diff --git a/cmd/capibmadm/cmd/powervs/network/type.go b/cmd/capibmadm/cmd/powervs/network/type.go new file mode 100644 index 000000000..9772cbef7 --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/network/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 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 +} diff --git a/cmd/capibmadm/cmd/powervs/powervs.go b/cmd/capibmadm/cmd/powervs/powervs.go index fac509f7c..e6f444e49 100644 --- a/cmd/capibmadm/cmd/powervs/powervs.go +++ b/cmd/capibmadm/cmd/powervs/powervs.go @@ -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") diff --git a/cmd/capibmadm/options/options.go b/cmd/capibmadm/options/options.go index 9b37e9472..cc693f175 100644 --- a/cmd/capibmadm/options/options.go +++ b/cmd/capibmadm/options/options.go @@ -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") } diff --git a/docs/book/src/topics/capibmadm/powervs/index.md b/docs/book/src/topics/capibmadm/powervs/index.md index cd352486d..918d9aebc 100644 --- a/docs/book/src/topics/capibmadm/powervs/index.md +++ b/docs/book/src/topics/capibmadm/powervs/index.md @@ -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) diff --git a/docs/book/src/topics/capibmadm/powervs/network.md b/docs/book/src/topics/capibmadm/powervs/network.md index 49fbbdd53..026862410 100644 --- a/docs/book/src/topics/capibmadm/powervs/network.md +++ b/docs/book/src/topics/capibmadm/powervs/network.md @@ -2,7 +2,7 @@ ### 1. capibmadm powervs network create -#### Usage: +#### Usage: Create Power VS network. #### Environmental Variable: @@ -17,4 +17,25 @@ IBMCLOUD_API_KEY: IBM Cloud api key. ```shell export IBMCLOUD_API_KEY= capibmadm powervs network create --service-instance-id -``` \ No newline at end of file +``` + + + +### 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= +capibmadm powervs network list --service-instance-id --zone +``` diff --git a/go.mod b/go.mod index 924fd5f31..0d7b1d075 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 27a2f51c0..83b286204 100644 --- a/go.sum +++ b/go.sum @@ -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=