Skip to content
Snippets Groups Projects
Select Git revision
  • dfc598c4be51fa49bbe46fecedc2bacf2978104a
  • live_exam_os_ubuntu default protected
2 results

vmListSingle.go

Blame
  • Florent Gluck's avatar
    Florent Gluck authored
    Previously, the VM_LIST VM capability allowed one to list the VM as well as attach to it. This capability was too coarse. Instead, VM_LIST should only allow one to list the VM and nothing more. The VM_ATTACH capability was added specifically to allow a user to attach to the VM.
    dfc598c4
    History
    vmListSingle.go 1.97 KiB
    package cmdVM
    
    import (
        u "nexus-client/utils"
        g "nexus-client/globals"
    )
    
    type ListSingle struct {
        Name string
    }
    
    func (cmd *ListSingle)GetName() string {
        return cmd.Name
    }
    
    func (cmd *ListSingle)GetDesc() []string {
        return []string{
            "Lists a single VM.",
            "If not the VM's owner: requires VM_LIST VM access capability or VM_LIST_ANY user capability."}
    }
    
    func (cmd *ListSingle)PrintUsage() {
        for _, desc := range cmd.GetDesc() {
            u.PrintlnErr(desc)
        }
        u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――")
        u.PrintlnErr("USAGE: ",cmd.GetName(), " ID")
        u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――")
    }
    
    func (cmd *ListSingle)Run(args []string) int {
        client := g.GetInstance().Client
        host := g.GetInstance().Host
    
        argc := len(args)
        if argc < 1 {
            cmd.PrintUsage()
            return 1
        }
    
        vmID := args[0]
        resp, err := client.R().Get(host+"/vms/"+vmID)
        if err != nil {
            u.PrintlnErr("Failed retrieving VM \""+vmID+"\": "+err.Error())
            return 1
        } else {
            if resp.IsSuccess() {
                vm, err := deserializeVM(resp)
                if err != nil {
                    u.PrintlnErr("Failed retrieving server's response: "+err.Error())
                    return 1
                }
                str, err := vm.String()
                if err != nil {
                    u.PrintlnErr("Failed decoding VM "+vm.ID.String()+" to string. Skipped.")
                } else {
                    u.Println(str)
                }
            } else {
                u.PrintlnErr("Failed retrieving VM \""+vmID+"\": "+resp.Status()+": "+resp.String())
                return 1
            }
        }
    
        return 0
    }