ports

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cmd = &cobra.Command{
	Use:   "ports",
	Short: "Get PowerVS network ports",
	Long:  `Get PowerVS network ports`,
	PreRunE: func(cmd *cobra.Command, args []string) error {
		if pkg.Options.InstanceID == "" && pkg.Options.InstanceName == "" {
			return fmt.Errorf("--instance-id or --instance-name required")
		}
		return nil
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		opt := pkg.Options

		c, err := client.NewClientWithEnv(opt.APIKey, opt.Environment, opt.Debug)
		if err != nil {
			klog.Errorf("failed to create a session with IBM cloud: %v", err)
			return err
		}

		pvmclient, err := client.NewPVMClientWithEnv(c, opt.InstanceID, opt.InstanceName, opt.Environment)
		if err != nil {
			return err
		}

		networks, err := pvmclient.NetworkClient.GetAll()
		if err != nil {
			return fmt.Errorf("failed to get the networks, err: %v", err)
		}

		var networkNames, networkIDs []string
		for _, net := range networks.Networks {
			networkIDs = append(networkIDs, *net.NetworkID)
			networkNames = append(networkNames, *net.Name)
		}

		var netID string

		if utils.Contains(networkIDs, network) {
			netID = network
		} else if utils.Contains(networkNames, network) {
			for _, n := range networks.Networks {
				if *n.Name == network {
					netID = *n.NetworkID
				}
			}
		} else {
			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, ","))
		}

		ports, err := pvmclient.NetworkClient.GetAllPorts(netID)
		if err != nil {
			return fmt.Errorf("failed to get the ports, err: %v", err)
		}

		table := utils.NewTable()
		table.Render(ports.Ports, []string{"href", "pvminstance"})
		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL