-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get a list of virter VMs based on available metadata. If virter metadata is found, also show the ID and access network based on the defined VM XML. When getting a list of VMs for shell completion, only include VMs that were created by virter.
- Loading branch information
Showing
4 changed files
with
140 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package cmd | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/rodaine/table" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/LINBIT/virter/internal/virter" | ||
) | ||
|
||
func vmListCommand() *cobra.Command { | ||
existsCmd := &cobra.Command{ | ||
Use: "list", | ||
Aliases: []string{"ls"}, | ||
Short: "List all VMs", | ||
Long: `List all VMs along with their ID and access network if they were created by Virter`, | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
v, err := InitVirter() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer v.ForceDisconnect() | ||
|
||
vms, err := v.VMList() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
vmInfos := make([]*virter.VMInfo, 0, len(vms)) | ||
|
||
for _, vm := range vms { | ||
vmInfo, err := v.VMInfo(vm) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
vmInfos = append(vmInfos, vmInfo) | ||
} | ||
|
||
t := table.New("Name", "ID", "Access Network") | ||
for _, val := range vmInfos { | ||
id := "" | ||
if val.ID != 0 { | ||
id = strconv.Itoa(int(val.ID)) | ||
} | ||
t.AddRow(val.Name, id, val.AccessNetwork) | ||
} | ||
t.Print() | ||
}, | ||
ValidArgsFunction: suggestVmNames, | ||
} | ||
return existsCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters