Skip to content

Instantly share code, notes, and snippets.

@akutz
Created April 8, 2016 21:12
Show Gist options
  • Save akutz/74c79c90987016b3479826630b04a291 to your computer and use it in GitHub Desktop.
Save akutz/74c79c90987016b3479826630b04a291 to your computer and use it in GitHub Desktop.
vbox.InstanceID
1. Login
REQUEST
POST /
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body xmlns="http://schemas.xmlsoap.org/soap/envelope/"><IWebsessionManager_logon xmlns="http://www.virtualbox.org/"><username>$USERNAME</username><password>$PASSWORD</password></IWebsessionManager_logon></Body></Envelope>
RESPONSE
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:vbox="http://www.virtualbox.org/"><SOAP-ENV:Body><vbox:IWebsessionManager_logonResponse><returnval>d1d85019c9abbff5-0000000000000001</returnval></vbox:IWebsessionManager_logonResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
The value in the `returnval` tag in the above response is the auth token needed for subsequent
requests. Let it be $AUTH_REF
2. FindMachine
REQUEST
POST /
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body xmlns="http://schemas.xmlsoap.org/soap/envelope/"><IVirtualBox_findMachine xmlns="http://www.virtualbox.org/"><_this>$AUTH_REF</_this><nameOrId>$MACHINE_NAME_OR_ID</nameOrId></IVirtualBox_findMachine></Body></Envelope>
RESPONSE
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:vbox="http://www.virtualbox.org/"><SOAP-ENV:Body><vbox:IVirtualBox_findMachineResponse><returnval>d1d85019c9abbff5-0000000000000002</returnval></vbox:IVirtualBox_findMachineResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
The value in the `returnval` tag in the above response is the machine reference needed for
subsequent requests. Let it be $MACHINE_REF
3. GetMachineID
REQUEST
POST /
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body xmlns="http://schemas.xmlsoap.org/soap/envelope/"><IMachine_getId xmlns="http://www.virtualbox.org/"><_this>$MACHINE_REF</_this></IMachine_getId></Body></Envelope>
RESPONSE
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:vbox="http://www.virtualbox.org/"><SOAP-ENV:Body><vbox:IMachine_getIdResponse><returnval>4f772b31-274d-4780-9225-838cad37a8aa</returnval></vbox:IMachine_getIdResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
The value in the `returnval` tag in the above response is the machine ID.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
)
const (
url = "http://127.0.0.1:18083"
user = ""
pass = ""
machineName = "hello"
bodyType = "text/xml"
)
var (
returnValRX = regexp.MustCompile(`<returnval>([^<]*?)</returnval>`)
)
func main() {
// login to the vbox web server
res, err := http.Post(
url,
bodyType,
bytes.NewBufferString(fmt.Sprintf(loginXML, user, pass)))
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
// get the authentication ref from the response
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
match := returnValRX.FindSubmatch(resBody)
if match == nil {
fmt.Fprintln(os.Stderr, "error: unable to parse auth token")
os.Exit(1)
}
authRef := string(match[1])
// get the machine ref
res, err = http.Post(
url,
bodyType,
bytes.NewBufferString(fmt.Sprintf(machineXML, authRef, machineName)))
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
// get the machine ref from the response
resBody, err = ioutil.ReadAll(res.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
match = returnValRX.FindSubmatch(resBody)
if match == nil {
fmt.Fprintln(os.Stderr, "error: unable to parse machine ref")
os.Exit(1)
}
machineRef := string(match[1])
// get the machine id
res, err = http.Post(
url,
bodyType,
bytes.NewBufferString(fmt.Sprintf(machineIDXML, machineRef)))
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
// get the machine ID from the response
resBody, err = ioutil.ReadAll(res.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
match = returnValRX.FindSubmatch(resBody)
if match == nil {
fmt.Fprintln(os.Stderr, "error: unable to parse machine ID")
os.Exit(1)
}
machineID := string(match[1])
fmt.Println(machineID)
}
const (
loginXML = `<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<IWebsessionManager_logon xmlns="http://www.virtualbox.org/">
<username>%s</username>
<password>%s</password>
</IWebsessionManager_logon>
</Body>
</Envelope>`
machineXML = `<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<IVirtualBox_findMachine xmlns="http://www.virtualbox.org/">
<_this>%s</_this>
<nameOrId>%s</nameOrId>
</IVirtualBox_findMachine>
</Body>
</Envelope>`
machineIDXML = `<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<IMachine_getId xmlns="http://www.virtualbox.org/">
<_this>%s</_this>
</IMachine_getId>
</Body>
</Envelope>
`
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment