/
document.go
248 lines (209 loc) 路 8.69 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2023, OpenNebula Project, OpenNebula Systems */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/*--------------------------------------------------------------------------- */
package goca
import (
"context"
"encoding/xml"
"errors"
"github.com/OpenNebula/one/src/oca/go/src/goca/parameters"
"github.com/OpenNebula/one/src/oca/go/src/goca/schemas/document"
"github.com/OpenNebula/one/src/oca/go/src/goca/schemas/shared"
)
// DocumentsController is a controller for documents entities
type DocumentsController struct {
entitiesController
dType int
}
// DocumentController is a controller for document entity
type DocumentController entityController
// Documents returns a Documents controller
func (c *Controller) Documents(dType int) *DocumentsController {
return &DocumentsController{entitiesController{c}, dType}
}
// Document returns a Document controller
func (c *Controller) Document(id int) *DocumentController {
return &DocumentController{c, id}
}
// ByName returns a Document ID from name
func (dc *DocumentsController) ByName(name string, args ...int) (int, error) {
return dc.ByNameContext(context.Background(), name, args...)
}
// ByNameContext returns a Document ID from name
func (dc *DocumentsController) ByNameContext(ctx context.Context, name string, args ...int) (int, error) {
var id int
documentPool, err := dc.InfoContext(ctx, args...)
if err != nil {
return -1, err
}
match := false
for i := 0; i < len(documentPool.Documents); i++ {
if documentPool.Documents[i].Name != name {
continue
}
if match {
return -1, errors.New("multiple resources with that name")
}
id = documentPool.Documents[i].ID
match = true
}
if !match {
return -1, errors.New("resource not found")
}
return id, nil
}
// Info returns a document pool. A connection to OpenNebula is
// performed.
func (dc *DocumentsController) Info(args ...int) (*document.Pool, error) {
return dc.InfoContext(context.Background(), args...)
}
// InfoContext returns a document pool. A connection to OpenNebula is
// performed.
func (dc *DocumentsController) InfoContext(ctx context.Context, args ...int) (*document.Pool, error) {
fArgs, err := handleArgs(args)
if err != nil {
return nil, err
}
fArgs = append(fArgs, dc.dType)
response, err := dc.c.Client.CallContext(ctx, "one.documentpool.info", fArgs...)
if err != nil {
return nil, err
}
documentPool := &document.Pool{}
err = xml.Unmarshal([]byte(response.Body()), documentPool)
if err != nil {
return nil, err
}
return documentPool, nil
}
// Info retrieves information for the document.
func (dc *DocumentController) Info(decrypt bool) (*document.Document, error) {
return dc.InfoContext(context.Background(), decrypt)
}
// InfoContext retrieves information for the document.
func (dc *DocumentController) InfoContext(ctx context.Context, decrypt bool) (*document.Document, error) {
response, err := dc.c.Client.CallContext(ctx, "one.document.info", dc.ID, decrypt)
if err != nil {
return nil, err
}
document := &document.Document{}
err = xml.Unmarshal([]byte(response.Body()), document)
if err != nil {
return nil, err
}
return document, nil
}
// Create allocates a new document. It returns the new document ID.
func (dc *DocumentsController) Create(tpl string) (int, error) {
return dc.CreateContext(context.Background(), tpl)
}
// CreateContext allocates a new document. It returns the new document ID.
func (dc *DocumentsController) CreateContext(ctx context.Context, tpl string) (int, error) {
response, err := dc.c.Client.CallContext(ctx, "one.document.allocate", tpl)
if err != nil {
return -1, err
}
return response.BodyInt(), nil
}
// Clone clones an existing document.
// * newName: Name for the new document.
func (dc *DocumentController) Clone(newName string) error {
return dc.CloneContext(context.Background(), newName)
}
// CloneContext clones an existing document.
// * ctx: context for cancelation
// * newName: Name for the new document.
func (dc *DocumentController) CloneContext(ctx context.Context, newName string) error {
_, err := dc.c.Client.CallContext(ctx, "one.document.clone", dc.ID, newName)
return err
}
// Delete deletes the given document from the pool.
func (dc *DocumentController) Delete() error {
return dc.DeleteContext(context.Background())
}
// DeleteContext deletes the given document from the pool.
func (dc *DocumentController) DeleteContext(ctx context.Context) error {
_, err := dc.c.Client.CallContext(ctx, "one.document.delete", dc.ID)
return err
}
// Update adds document content.
// * tpl: The new document contents. Syntax can be the usual attribute=value or XML.
// * uType: Update type: Replace: Replace the whole template.
// Merge: Merge new template with the existing one.
func (dc *DocumentController) Update(tpl string, uType parameters.UpdateType) error {
return dc.UpdateContext(context.Background(), tpl, uType)
}
// UpdateContext adds document content.
// - ctx: context for cancelation
// - tpl: The new document contents. Syntax can be the usual attribute=value or XML.
// - uType: Update type: Replace: Replace the whole template.
// Merge: Merge new template with the existing one.
func (dc *DocumentController) UpdateContext(ctx context.Context, tpl string, uType parameters.UpdateType) error {
_, err := dc.c.Client.CallContext(ctx, "one.document.update", dc.ID, tpl, uType)
return err
}
// Chmod changes the permission bits of a document.
func (dc *DocumentController) Chmod(perm shared.Permissions) error {
return dc.ChmodContext(context.Background(), perm)
}
// ChmodContext changes the permission bits of a document.
func (dc *DocumentController) ChmodContext(ctx context.Context, perm shared.Permissions) error {
args := append([]interface{}{dc.ID}, perm.ToArgs()...)
_, err := dc.c.Client.CallContext(ctx, "one.document.chmod", args...)
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 (dc *DocumentController) Chown(userID, groupID int) error {
return dc.ChownContext(context.Background(), userID, groupID)
}
// ChownContext changes the ownership of a document.
// * ctx: context for cancelation
// * 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 (dc *DocumentController) ChownContext(ctx context.Context, userID, groupID int) error {
_, err := dc.c.Client.CallContext(ctx, "one.document.chown", dc.ID, userID, groupID)
return err
}
// Rename renames a document.
// * newName: The new name.
func (dc *DocumentController) Rename(newName string) error {
return dc.RenameContext(context.Background(), newName)
}
// RenameContext renames a document.
// * ctx: context for cancelation
// * newName: The new name.
func (dc *DocumentController) RenameContext(ctx context.Context, newName string) error {
_, err := dc.c.Client.CallContext(ctx, "one.document.rename", dc.ID, newName)
return err
}
// Lock locks the document following lock level. See levels in locks.go.
func (dc *DocumentController) Lock(level shared.LockLevel) error {
return dc.LockContext(context.Background(), level)
}
// LockContext locks the document following lock level. See levels in locks.go.
func (dc *DocumentController) LockContext(ctx context.Context, level shared.LockLevel) error {
_, err := dc.c.Client.CallContext(ctx, "one.document.lock", dc.ID, level)
return err
}
// Unlock unlocks the document.
func (dc *DocumentController) Unlock() error {
return dc.UnlockContext(context.Background())
}
func (dc *DocumentController) UnlockContext(ctx context.Context) error {
_, err := dc.c.Client.CallContext(ctx, "one.document.unlock", dc.ID)
return err
}