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
/
document.go
144 lines (122 loc) · 4.48 KB
/
document.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package goca
import "errors"
// Document represents an OpenNebula Document
type Document struct {
XMLResource
ID uint
Name string
}
// DocumentPool represents an OpenNebula DocumentPool
type DocumentPool struct {
XMLResource
}
// NewDocumentPool returns a document pool. A connection to OpenNebula is
// performed.
func NewDocumentPool(documentType int, args ...int) (*DocumentPool, error) {
var who, start, end int
switch len(args) {
case 0:
who = PoolWhoMine
start = -1
end = -1
case 1:
who = args[0]
start = -1
end = -1
case 3:
who = args[0]
start = args[1]
end = args[2]
default:
return nil, errors.New("Wrong number of arguments")
}
response, err := client.Call("one.documentpool.info", who, start, end, documentType)
if err != nil {
return nil, err
}
documentpool := &DocumentPool{XMLResource{body: response.Body()}}
return documentpool, err
}
// NewDocument finds a document object by ID. No connection to OpenNebula.
func NewDocument(id uint) *Document {
return &Document{ID: id}
}
// NewDocumentFromName finds a document object by name. It connects to
// OpenNebula to retrieve the pool, but doesn't perform the Info() call to
// retrieve the attributes of the document.
func NewDocumentFromName(name string, documentType int) (*Document, error) {
documentPool, err := NewDocumentPool(documentType)
if err != nil {
return nil, err
}
id, err := documentPool.GetIDFromName(name, "/DOCUMENT_POOL/DOCUMENT")
if err != nil {
return nil, err
}
return NewDocument(id), nil
}
// CreateDocument allocates a new document. It returns the new document ID.
func CreateDocument(tpl string, documentType int) (uint, error) {
response, err := client.Call("one.document.allocate", tpl, documentType)
if err != nil {
return 0, err
}
return uint(response.BodyInt()), nil
}
// Clone clones an existing document.
// * newName: Name for the new document.
func (document *Document) Clone(newName string) error {
_, err := client.Call("one.document.clone", document.ID, newName)
return err
}
// Delete deletes the given document from the pool.
func (document *Document) Delete() error {
_, err := client.Call("one.document.delete", document.ID)
return err
}
// Update replaces the document template contents.
// * tpl: The new document template contents. Syntax can be the usual attribute=value or XML.
// * appendTemplate: Update type: 0: Replace the whole template. 1: Merge new template with the existing one.
func (document *Document) Update(tpl string, appendTemplate int) error {
_, err := client.Call("one.document.update", document.ID, tpl, appendTemplate)
return err
}
// Chmod changes the permission bits of a document.
// * uu: USER USE bit. If set to -1, it will not change.
// * um: USER MANAGE bit. If set to -1, it will not change.
// * ua: USER ADMIN bit. If set to -1, it will not change.
// * gu: GROUP USE bit. If set to -1, it will not change.
// * gm: GROUP MANAGE bit. If set to -1, it will not change.
// * ga: GROUP ADMIN bit. If set to -1, it will not change.
// * ou: OTHER USE bit. If set to -1, it will not change.
// * om: OTHER MANAGE bit. If set to -1, it will not change.
// * oa: OTHER ADMIN bit. If set to -1, it will not change.
func (document *Document) Chmod(uu, um, ua, gu, gm, ga, ou, om, oa int) error {
_, err := client.Call("one.document.chmod", document.ID, uu, um, ua, gu, gm, ga, ou, om, oa)
return err
}
// Chown changes the ownership of a document.
// * userID: The User ID of the new owner. If set to -1, it will not change.
// * groupID: The Group ID of the new group. If set to -1, it will not change.
func (document *Document) Chown(userID, groupID uint) error {
_, err := client.Call("one.document.chown", document.ID, int(userID), int(groupID))
return err
}
// Rename renames a document.
// * newName: The new name.
func (document *Document) Rename(newName string) error {
_, err := client.Call("one.document.rename", document.ID, newName)
return err
}
// Lock locks the document at the api level. The lock automatically expires after 2 minutes.
// * applicationName: String to identify the application requesting the lock.
func (document *Document) Lock(applicationName string) error {
_, err := client.Call("one.document.lock", document.ID, applicationName)
return err
}
// Unlock unlocks the document at the api level.
// * applicationName: String to identify the application requesting the lock.
func (document *Document) Unlock(applicationName string) error {
_, err := client.Call("one.document.unlock", document.ID, applicationName)
return err
}