1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
package main
import (
"fmt"
"github.com/spf13/cobra"
"github.com/canonical/lxd/shared/api"
cli "github.com/canonical/lxd/shared/cmd"
"github.com/canonical/lxd/shared/i18n"
)
type cmdNetworkListAllocations struct {
global *cmdGlobal
network *cmdNetwork
flagFormat string
flagProject string
flagAllProjects bool
}
func (c *cmdNetworkListAllocations) pretty(allocs []api.NetworkAllocations) error {
header := []string{
i18n.G("USED BY"),
i18n.G("ADDRESS"),
i18n.G("TYPE"),
i18n.G("NAT"),
i18n.G("HARDWARE ADDRESS"),
}
data := [][]string{}
for _, alloc := range allocs {
row := []string{
alloc.UsedBy,
alloc.Address,
alloc.Type,
fmt.Sprint(alloc.NAT),
alloc.Hwaddr,
}
data = append(data, row)
}
return cli.RenderTable(c.flagFormat, header, data, allocs)
}
func (c *cmdNetworkListAllocations) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("list-allocations")
cmd.Short = i18n.G("List network allocations in use")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("List network allocations in use"))
// Workaround for subcommand usage errors. See: https://github.com/spf13/cobra/issues/706
cmd.Args = cobra.MaximumNArgs(1)
cmd.RunE = c.Run
cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``")
cmd.Flags().StringVarP(&c.flagProject, "project", "p", "default", i18n.G("Run again a specific project"))
cmd.Flags().BoolVar(&c.flagAllProjects, "all-projects", false, i18n.G("Run against all projects"))
return cmd
}
func (c *cmdNetworkListAllocations) Run(cmd *cobra.Command, args []string) error {
remote := ""
if len(args) > 0 {
remote = args[0]
}
resources, err := c.global.ParseServers(remote)
if err != nil {
return err
}
resource := resources[0]
server := resource.server.UseProject(c.flagProject)
addresses, err := server.GetNetworkAllocations(c.flagAllProjects)
if err != nil {
return err
}
return c.pretty(addresses)
}
|