Skip to content

Commit

Permalink
Merge pull request #12331 from markylaing/network-allocations-cli
Browse files Browse the repository at this point in the history
Allows usage of `remote:` with `lxc network list-allocations` and fixes project usage.
  • Loading branch information
tomponline authored Sep 27, 2023
2 parents eb5f174 + 8b9ae52 commit faf9192
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lxc/network_allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/spf13/cobra"

lxd "github.com/canonical/lxd/client"
"github.com/canonical/lxd/shared/api"
cli "github.com/canonical/lxd/shared/cmd"
"github.com/canonical/lxd/shared/i18n"
Expand Down Expand Up @@ -52,7 +51,7 @@ func (c *cmdNetworkListAllocations) Command() *cobra.Command {
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.NoArgs
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)")+"``")
Expand All @@ -62,18 +61,19 @@ func (c *cmdNetworkListAllocations) Command() *cobra.Command {
}

func (c *cmdNetworkListAllocations) Run(cmd *cobra.Command, args []string) error {
d, err := lxd.ConnectLXDUnix("", nil)
if err != nil {
return nil
remote := ""
if len(args) > 0 {
remote = args[0]
}

// Check if server is initialized.
_, _, err = d.GetServer()
resources, err := c.global.ParseServers(remote)
if err != nil {
return err
}

addresses, err := d.UseProject(c.flagProject).GetNetworkAllocations(c.flagAllProjects)
resource := resources[0]
server := resource.server.UseProject(c.flagProject)
addresses, err := server.GetNetworkAllocations(c.flagAllProjects)
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions test/suites/network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ test_network() {
lxc network list-allocations | grep -e "${net_ipv4}" -e "${net_ipv6}"
lxc network list-allocations | grep -e "/1.0/networks/lxdt$$" -e "/1.0/instances/nettest"
lxc network list-allocations | grep -e "${v4_addr}" -e "${v6_addr}"
lxc network list-allocations localhost: | grep -e "${net_ipv4}" -e "${net_ipv6}"
lxc network list-allocations localhost: | grep -e "/1.0/networks/lxdt$$" -e "/1.0/instances/nettest"
lxc network list-allocations localhost: | grep -e "${v4_addr}" -e "${v6_addr}"

lxc delete nettest -f
lxc network delete lxdt$$
Expand Down

0 comments on commit faf9192

Please sign in to comment.