|
| 1 | +// Copyright 2021 IBM Corp |
| 2 | +// |
| 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 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package port |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/IBM-Cloud/power-go-client/power/client/p_cloud_networks" |
| 22 | + "github.com/IBM-Cloud/power-go-client/power/models" |
| 23 | + "github.com/spf13/cobra" |
| 24 | + "k8s.io/klog/v2" |
| 25 | + |
| 26 | + "github.com/ppc64le-cloud/pvsadm/pkg" |
| 27 | + "github.com/ppc64le-cloud/pvsadm/pkg/client" |
| 28 | + "github.com/ppc64le-cloud/pvsadm/pkg/utils" |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + network, ipaddress, description string |
| 33 | +) |
| 34 | + |
| 35 | +var Cmd = &cobra.Command{ |
| 36 | + Use: "port", |
| 37 | + Short: "Create PowerVS network port", |
| 38 | + Long: `Create PowerVS network port`, |
| 39 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 40 | + opt := pkg.Options |
| 41 | + |
| 42 | + c, err := client.NewClientWithEnv(opt.APIKey, opt.Environment, opt.Debug) |
| 43 | + if err != nil { |
| 44 | + klog.Errorf("failed to create a session with IBM cloud: %v", err) |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + pvmclient, err := client.NewPVMClientWithEnv(c, opt.InstanceID, opt.InstanceName, opt.Environment) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + networks, err := pvmclient.NetworkClient.GetAll() |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("failed to get the networks, err: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + var networkNames, networkIDs []string |
| 59 | + for _, net := range networks.Networks { |
| 60 | + networkIDs = append(networkIDs, *net.NetworkID) |
| 61 | + networkNames = append(networkNames, *net.Name) |
| 62 | + } |
| 63 | + |
| 64 | + var netID string |
| 65 | + |
| 66 | + if utils.Contains(networkIDs, network) { |
| 67 | + netID = network |
| 68 | + } else if utils.Contains(networkNames, network) { |
| 69 | + for _, n := range networks.Networks { |
| 70 | + if *n.Name == network { |
| 71 | + netID = *n.NetworkID |
| 72 | + } |
| 73 | + } |
| 74 | + } else { |
| 75 | + return fmt.Errorf("not able to find network: \"%s\" by ID or name in the list: ids:[%s], names: [%s]", network, strings.Join(networkIDs, ","), strings.Join(networkNames, ",")) |
| 76 | + } |
| 77 | + |
| 78 | + params := &p_cloud_networks.PcloudNetworksPortsPostParams{ |
| 79 | + Body: &models.NetworkPortCreate{ |
| 80 | + Description: description, |
| 81 | + IPAddress: ipaddress, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + port, err := pvmclient.NetworkClient.CreatePort(netID, params) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("failed to create a port, err: %v", err) |
| 88 | + } |
| 89 | + klog.Infof("Successfully created a port, id: %s", *port.PortID) |
| 90 | + |
| 91 | + table := utils.NewTable() |
| 92 | + table.Render([]*models.NetworkPort{port}, []string{}) |
| 93 | + return nil |
| 94 | + }, |
| 95 | +} |
| 96 | + |
| 97 | +func init() { |
| 98 | + Cmd.Flags().StringVar(&network, "network", "", "Network ID or Name(preference will be given to the ID over Name)") |
| 99 | + Cmd.Flags().StringVar(&ipaddress, "ip-address", "", "The requested ip address of this port") |
| 100 | + Cmd.Flags().StringVar(&description, "description", "", "Description of the port") |
| 101 | + _ = Cmd.MarkFlagRequired("network") |
| 102 | +} |
0 commit comments