This repository has been archived by the owner on Mar 21, 2023. It is now read-only.
/
resource.go
367 lines (303 loc) · 9.06 KB
/
resource.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*** Copyright (c) 2016, The BioTeam, Inc. ***
*** For more information please refer to the LICENSE.md file ***/
package gorods
// #include "wrapper.h"
import "C"
import (
"fmt"
"strconv"
"strings"
"time"
"unsafe"
)
// Resource holds information about a resource server registered with iCAT.
type Resource struct {
name string
typ int
comment string
createTime time.Time
modifyTime time.Time
id int
context string
zone *Zone
class int
children string
freeSpace int
info string
status string
parentStr string
net string
freeSpaceTime time.Time
objCount int
storageType string
physPath string
parentSlice *Resources
hasInit bool
con *Connection
}
// Resources is a slice of *Resource.
type Resources []*Resource
// FindByName returns the matching *Resource based on the name.
func (rescs Resources) FindByName(name string) *Resource {
for _, resc := range rescs {
if resc.name == name {
return resc
}
}
return nil
}
// Remove removes the specific element from itself, based on index.
func (rescs *Resources) Remove(index int) {
*rescs = append((*rescs)[:index], (*rescs)[index+1:]...)
}
func initResource(name string, con *Connection) (*Resource, error) {
resc := new(Resource)
zne, er := con.LocalZone()
if er != nil {
return nil, er
}
resc.con = con
resc.name = name
resc.zone = zne
return resc, nil
}
func (resc *Resource) init() error {
if !resc.hasInit {
if err := resc.RefreshInfo(); err != nil {
return err
}
resc.hasInit = true
}
return nil
}
// Remove removes the resource from it's parent slice
func (resc *Resource) Remove() bool {
for n, p := range *resc.parentSlice {
if p.name == resc.name {
resc.parentSlice.Remove(n)
return true
}
}
return false
}
// String returns the resource name and zone name
func (resc *Resource) String() string {
return fmt.Sprintf("%v#%v", resc.Name(), resc.Zone().Name())
}
// Name returns the resource name
func (resc *Resource) Name() string {
return resc.name
}
// Comment loads data from iCAT if needed, and returns the resources comment attribute.
func (resc *Resource) Comment() (string, error) {
if err := resc.init(); err != nil {
return resc.comment, err
}
return resc.comment, nil
}
// CreateTime loads data from iCAT if needed, and returns the resources createTime attribute.
func (resc *Resource) CreateTime() (time.Time, error) {
if err := resc.init(); err != nil {
return resc.createTime, err
}
return resc.createTime, nil
}
// ModifyTime loads data from iCAT if needed, and returns the resources modifyTime attribute.
func (resc *Resource) ModifyTime() (time.Time, error) {
if err := resc.init(); err != nil {
return resc.modifyTime, err
}
return resc.modifyTime, nil
}
// Id loads data from iCAT if needed, and returns the resources id attribute.
func (resc *Resource) Id() (int, error) {
if err := resc.init(); err != nil {
return resc.id, err
}
return resc.id, nil
}
// Id loads data from iCAT if needed, and returns the resources id attribute.
func (resc *Resource) Type() (int, error) {
if err := resc.init(); err != nil {
return resc.typ, err
}
return resc.typ, nil
}
// Context loads data from iCAT if needed, and returns the resources context attribute.
func (resc *Resource) Context() (string, error) {
if err := resc.init(); err != nil {
return resc.context, err
}
return resc.context, nil
}
// Class loads data from iCAT if needed, and returns the resources class attribute.
func (resc *Resource) Class() (int, error) {
if err := resc.init(); err != nil {
return resc.class, err
}
return resc.class, nil
}
// Children loads data from iCAT if needed, and returns the resources children attribute.
func (resc *Resource) Children() (string, error) {
if err := resc.init(); err != nil {
return resc.children, err
}
return resc.children, nil
}
// FreeSpace loads data from iCAT if needed, and returns the resources freeSpace attribute.
func (resc *Resource) FreeSpace() (int, error) {
if err := resc.init(); err != nil {
return resc.freeSpace, err
}
return resc.freeSpace, nil
}
// Info loads data from iCAT if needed, and returns the resources info attribute.
func (resc *Resource) Info() (string, error) {
if err := resc.init(); err != nil {
return resc.info, err
}
return resc.info, nil
}
// Status loads data from iCAT if needed, and returns the resources status attribute.
func (resc *Resource) Status() (string, error) {
if err := resc.init(); err != nil {
return resc.status, err
}
return resc.status, nil
}
// ParentStr loads data from iCAT if needed, and returns the resources parentStr attribute.
func (resc *Resource) ParentStr() (string, error) {
if err := resc.init(); err != nil {
return resc.parentStr, err
}
return resc.parentStr, nil
}
// Net loads data from iCAT if needed, and returns the resources net attribute.
func (resc *Resource) Net() (string, error) {
if err := resc.init(); err != nil {
return resc.net, err
}
return resc.net, nil
}
// FreeSpaceTime loads data from iCAT if needed, and returns the resources freeSpaceTime attribute.
func (resc *Resource) FreeSpaceTime() (time.Time, error) {
if err := resc.init(); err != nil {
return resc.freeSpaceTime, err
}
return resc.freeSpaceTime, nil
}
// ObjCount loads data from iCAT if needed, and returns the resources status attribute.
func (resc *Resource) ObjCount() (int, error) {
if err := resc.init(); err != nil {
return resc.objCount, err
}
return resc.objCount, nil
}
// Status loads data from iCAT if needed, and returns the resources info attribute.
func (resc *Resource) StorageType() (string, error) {
if err := resc.init(); err != nil {
return resc.storageType, err
}
return resc.storageType, nil
}
// PhysPath loads data from iCAT if needed, and returns the resources physPath attribute.
func (resc *Resource) PhysPath() (string, error) {
if err := resc.init(); err != nil {
return resc.physPath, err
}
return resc.physPath, nil
}
// Zone returns the *Zone to which the resource belongs to.
func (resc *Resource) Zone() *Zone {
return resc.zone
}
// RefreshInfo fetches fresh resource data from the iCAT server, and unloads it to the resource struct's fields.
func (resc *Resource) RefreshInfo() error {
//map[
// resc_context:
// resc_id: 10022
// zone_name: tempZone
// resc_class_name: cache
// modify_ts: 01471623935
// resc_children:
// free_space:
// resc_info:
// r_comment:
// resc_status:
// resc_parent:
// resc_net: irods-resource
// free_space_ts:
// resc_objcount: 2
// resc_name: irods-resourceResource
// resc_type_name: unixfilesystem
// resc_def_path: /var/lib/irods/iRODS/Vault
// create_ts: 01471614567
// ]
typeMap := map[string]int{
"cache": Cache,
"archive": Archive,
}
if infoMap, err := resc.FetchInfo(); err == nil {
resc.comment = infoMap["r_comment"]
resc.createTime = timeStringToTime(infoMap["create_ts"])
resc.modifyTime = timeStringToTime(infoMap["modify_ts"])
resc.id, _ = strconv.Atoi(infoMap["resc_id"])
resc.typ = ResourceType
resc.context = infoMap["resc_context"]
resc.class = typeMap[infoMap["resc_class_name"]]
resc.children = infoMap["resc_children"]
resc.freeSpace, _ = strconv.Atoi(infoMap["free_space"])
resc.info = infoMap["resc_info"]
resc.status = infoMap["resc_status"]
resc.parentStr = infoMap["resc_parent"]
resc.net = infoMap["resc_net"]
resc.freeSpaceTime = timeStringToTime(infoMap["free_space_ts"])
resc.objCount, _ = strconv.Atoi(infoMap["resc_objcount"])
resc.storageType = infoMap["resc_type_name"]
resc.physPath = infoMap["resc_def_path"]
if zones, err := resc.con.Zones(); err != nil {
return err
} else {
if zne := zones.FindByName(infoMap["zone_name"], resc.con); zne != nil {
resc.zone = zne
} else {
return newError(Fatal, -1, fmt.Sprintf("iRODS Refresh Resource Info Failed: Unable to locate zone in cache"))
}
}
} else {
return err
}
return nil
}
// FetchInfo fetches fresh resource info from the iCAT server and returns it as a map
func (resc *Resource) FetchInfo() (map[string]string, error) {
var (
result C.goRodsStringResult_t
err *C.char
)
result.size = C.int(0)
cResource := C.CString(resc.name)
defer C.free(unsafe.Pointer(cResource))
ccon := resc.con.GetCcon()
if status := C.gorods_get_resource(cResource, ccon, &result, &err); status != 0 {
resc.con.ReturnCcon(ccon)
return nil, newError(Fatal, status, fmt.Sprintf("iRODS Get Resource Info Failed: %v", C.GoString(err)))
}
resc.con.ReturnCcon(ccon)
defer C.gorods_free_string_result(&result)
unsafeArr := unsafe.Pointer(result.strArr)
arrLen := int(result.size)
// Convert C array to slice, backed by arr *C.char
slice := (*[1 << 30]*C.char)(unsafeArr)[:arrLen:arrLen]
response := make(map[string]string)
for _, resourceInfo := range slice {
resourceAttributes := strings.Split(strings.Trim(C.GoString(resourceInfo), " \n"), "\n")
for _, attr := range resourceAttributes {
split := strings.Split(attr, ": ")
attrName := split[0]
attrVal := split[1]
response[attrName] = attrVal
}
}
return response, nil
}