This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
xmlresource.go
112 lines (87 loc) · 2.38 KB
/
xmlresource.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package goca
import (
"bytes"
"errors"
"strconv"
"gopkg.in/xmlpath.v2"
)
const (
// PoolWhoPrimaryGroup resources belonging to the user’s primary group.
PoolWhoPrimaryGroup = -4
// PoolWhoMine to list resources that belong to the user that performs the
// query.
PoolWhoMine = -3
// PoolWhoAll to list all the resources seen by the user that performs the
// query.
PoolWhoAll = -2
// PoolWhoGroup to list all the resources that belong to the group that performs
// the query.
PoolWhoGroup = -1
)
// XMLResource contains an XML body field. All the resources in OpenNebula are
// of this kind.
type XMLResource struct {
body string
}
// XMLIter is used to iterate over XML xpaths in an object.
type XMLIter struct {
iter *xmlpath.Iter
}
// XMLNode represent an XML node.
type XMLNode struct {
node *xmlpath.Node
}
// Body accesses the body of an XMLResource
func (r *XMLResource) Body() string {
return r.body
}
// XPath returns the string pointed at by xpath, for an XMLResource
func (r *XMLResource) XPath(xpath string) (string, bool) {
path := xmlpath.MustCompile(xpath)
b := bytes.NewBufferString(r.Body())
root, _ := xmlpath.Parse(b)
return path.String(root)
}
// XPathIter returns an XMLIter object pointed at by the xpath
func (r *XMLResource) XPathIter(xpath string) *XMLIter {
path := xmlpath.MustCompile(xpath)
b := bytes.NewBufferString(string(r.Body()))
root, _ := xmlpath.Parse(b)
return &XMLIter{iter: path.Iter(root)}
}
// GetIDFromName finds the a resource by ID by looking at an xpath contained
// in that resource
func (r *XMLResource) GetIDFromName(name string, xpath string) (uint, error) {
var id int
var match = false
iter := r.XPathIter(xpath)
for iter.Next() {
node := iter.Node()
n, _ := node.XPath("NAME")
if n == name {
if match {
return 0, errors.New("multiple resources with that name")
}
idString, _ := node.XPath("ID")
id, _ = strconv.Atoi(idString)
match = true
}
}
if !match {
return 0, errors.New("resource not found")
}
return uint(id), nil
}
// Next moves on to the next resource
func (i *XMLIter) Next() bool {
return i.iter.Next()
}
// Node returns the XMLNode
func (i *XMLIter) Node() *XMLNode {
return &XMLNode{node: i.iter.Node()}
}
// XPath returns an XMLNode pointed at by xpath
func (n *XMLNode) XPath(xpath string) (string, bool) {
path := xmlpath.MustCompile(xpath)
return path.String(n.node)
}