-
Notifications
You must be signed in to change notification settings - Fork 6
/
sandboxlistparser.go
47 lines (42 loc) · 1.15 KB
/
sandboxlistparser.go
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
package vcodeapi
import (
"bytes"
"encoding/xml"
"errors"
)
// Sandbox is a an individual sandbox with an application profile
type Sandbox struct {
SandboxID string `xml:"sandbox_id,attr"`
SandboxName string `xml:"sandbox_name,attr"`
Owner string `xml:"owner,attr"`
}
// ParseSandboxList parses the getsandboxlist.do API and returns an array of Sandboxes
func ParseSandboxList(credsFile, appID string) ([]Sandbox, error) {
var sandboxes []Sandbox
sandboxListAPI, err := sandboxList(credsFile, appID)
if err != nil {
return nil, err
}
decoder := xml.NewDecoder(bytes.NewReader(sandboxListAPI))
for {
// Read tokens from the XML document in a stream.
t, _ := decoder.Token()
if t == nil {
break
}
// Inspect the type of the token just read
switch se := t.(type) {
case xml.StartElement:
// Read StartElement and check for flaw
if se.Name.Local == "sandbox" {
var sandbox Sandbox
decoder.DecodeElement(&sandbox, &se)
sandboxes = append(sandboxes, sandbox)
}
if se.Name.Local == "error" {
return nil, errors.New("api for GetSandboxList returned with an error element")
}
}
}
return sandboxes, nil
}