|
| 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 | +} |
0 commit comments