forked from dnanexus/dxfuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dx_ops.go
641 lines (534 loc) · 12.1 KB
/
dx_ops.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
package dxfuse
import (
"context"
"crypto/md5"
"encoding/json"
"encoding/hex"
"fmt"
"time"
"github.com/dnanexus/dxda"
"github.com/hashicorp/go-retryablehttp"
)
type DxOps struct {
dxEnv dxda.DXEnvironment
options Options
}
func NewDxOps(dxEnv dxda.DXEnvironment, options Options) *DxOps {
return &DxOps{
dxEnv : dxEnv,
options : options,
}
}
func (ops *DxOps) log(a string, args ...interface{}) {
LogMsg("dx_ops", a, args...)
}
type RequestFolderNew struct {
ProjId string `json:"project"`
Folder string `json:"folder"`
Parents bool `json:"parents"`
}
type ReplyFolderNew struct {
Id string `json:"id"`
}
func (ops *DxOps) DxFolderNew(
ctx context.Context,
httpClient *retryablehttp.Client,
projId string,
folder string) error {
var request RequestFolderNew
request.ProjId = projId
request.Folder = folder
request.Parents = false
payload, err := json.Marshal(request)
if err != nil {
return err
}
repJs, err := dxda.DxAPI(
ctx,
httpClient,
NumRetriesDefault,
&ops.dxEnv,
fmt.Sprintf("%s/newFolder", projId),
string(payload))
if err != nil {
return err
}
var reply ReplyFolderNew
if err := json.Unmarshal(repJs, &reply); err != nil {
return err
}
return nil
}
type RequestFolderRemove struct {
ProjId string `json:"project"`
Folder string `json:"folder"`
}
type ReplyFolderRemove struct {
Id string `json:"id"`
}
func (ops *DxOps) DxFolderRemove(
ctx context.Context,
httpClient *retryablehttp.Client,
projId string,
folder string) error {
var request RequestFolderRemove
request.ProjId = projId
request.Folder = folder
payload, err := json.Marshal(request)
if err != nil {
return err
}
repJs, err := dxda.DxAPI(
ctx,
httpClient,
NumRetriesDefault,
&ops.dxEnv,
fmt.Sprintf("%s/removeFolder", projId),
string(payload))
if err != nil {
return err
}
var reply ReplyFolderRemove
if err := json.Unmarshal(repJs, &reply); err != nil {
return err
}
return nil
}
type RequestRemoveObjects struct {
Objects []string `json:"objects"`
Force bool `json:"force"`
}
type ReplyRemoveObjects struct {
Id string `json:"id"`
}
func (ops *DxOps) DxRemoveObjects(
ctx context.Context,
httpClient *retryablehttp.Client,
projId string,
objectIds []string) error {
var request RequestRemoveObjects
request.Objects = objectIds
request.Force = false
payload, err := json.Marshal(request)
if err != nil {
return err
}
repJs, err := dxda.DxAPI(
ctx,
httpClient,
NumRetriesDefault,
&ops.dxEnv,
fmt.Sprintf("%s/removeObjects", projId),
string(payload))
if err != nil {
return err
}
var reply ReplyFolderRemove
if err := json.Unmarshal(repJs, &reply); err != nil {
return err
}
return nil
}
type RequestNewFile struct {
ProjId string `json:"project"`
Name string `json:"name"`
Folder string `json:"folder"`
Parents bool `json:"parents"`
Nonce string `json:"nonce"`
}
type ReplyNewFile struct {
Id string `json:"id"`
}
func (ops *DxOps) DxFileNew(
ctx context.Context,
httpClient *retryablehttp.Client,
nonceStr string,
projId string,
fname string,
folder string) (string, error) {
var request RequestNewFile
request.ProjId = projId
request.Name = fname
request.Folder = folder
request.Parents = false
request.Nonce = nonceStr
payload, err := json.Marshal(request)
if err != nil {
return "", err
}
repJs, err := dxda.DxAPI(ctx, httpClient, NumRetriesDefault, &ops.dxEnv, "file/new", string(payload))
if err != nil {
return "", err
}
var reply ReplyNewFile
if err := json.Unmarshal(repJs, &reply); err != nil {
return "", err
}
// got a file ID back
return reply.Id, nil
}
func (ops *DxOps) DxFileCloseAndWait(
ctx context.Context,
httpClient *retryablehttp.Client,
fid string,
verbose bool) error {
_, err := dxda.DxAPI(
ctx,
httpClient,
NumRetriesDefault,
&ops.dxEnv,
fmt.Sprintf("%s/close", fid),
"{}")
if err != nil {
return err
}
// wait for file to achieve closed state
start := time.Now()
deadline := start.Add(fileCloseMaxWaitTime)
for true {
fDesc, err := DxDescribe(ctx, httpClient, &ops.dxEnv, fid, false)
if err != nil {
return err
}
switch fDesc.State {
case "closed":
// done. File is closed.
return nil
case "closing":
// not done yet.
if verbose {
elapsed := time.Now().Sub(start)
ops.log("Waited %s for file %s to close", elapsed.String(), fid)
}
time.Sleep(fileCloseWaitTime)
// don't wait too long
if time.Now().After(deadline) {
return fmt.Errorf("Waited %s for file %s to close, stopping the wait",
fileCloseMaxWaitTime.String(), fid)
}
continue
default:
return fmt.Errorf("data object %s has bad state %s", fid, fDesc.State)
}
}
return nil
}
type RequestUploadChunk struct {
Size int `json:"size"`
Index int `json:"index"`
Md5 string `json:"md5"`
}
type ReplyUploadChunk struct {
Url string `json:"url"`
Expires int64 `json:"expires"`
Headers map[string]string `json:"headers"`
}
func (ops *DxOps) DxFileUploadPart(
ctx context.Context,
httpClient *retryablehttp.Client,
fileId string,
index int,
data []byte) error {
md5Sum := md5.Sum(data)
uploadReq := RequestUploadChunk{
Size: len(data),
Index: index,
Md5: hex.EncodeToString(md5Sum[:]),
}
reqJson, err := json.Marshal(uploadReq)
if err != nil {
return err
}
replyJs, err := dxda.DxAPI(
ctx,
httpClient,
NumRetriesDefault,
&ops.dxEnv,
fmt.Sprintf("%s/upload", fileId),
string(reqJson))
if err != nil {
ops.log("DxFileUploadPart: error in dxapi call [%s/upload] %v",
fileId, err.Error())
return err
}
var reply ReplyUploadChunk
if err = json.Unmarshal(replyJs, &reply); err != nil {
return err
}
// the request provides its own implicit length
//delete(reply.Headers, "content-length")
_, err = dxda.DxHttpRequest(ctx, httpClient, NumRetriesDefault, "PUT", reply.Url, reply.Headers, data)
if err != nil {
ops.log("DxFileUploadPart: failure in data upload")
return err
}
return nil
}
type RequestRename struct {
ProjId string `json:"project"`
Name string `json:"name"`
}
type ReplyRename struct {
Id string `json:"id"`
}
// API method: /class-xxxx/rename
//
// rename a data object
func (ops *DxOps) DxRename(
ctx context.Context,
httpClient *retryablehttp.Client,
projId string,
fileId string,
newName string) error {
var request RequestRename
request.ProjId = projId
request.Name = newName
payload, err := json.Marshal(request)
if err != nil {
return err
}
repJs, err := dxda.DxAPI(
ctx, httpClient, NumRetriesDefault, &ops.dxEnv,
fmt.Sprintf("%s/rename", fileId),
string(payload))
if err != nil {
return err
}
var reply ReplyRename
if err := json.Unmarshal(repJs, &reply); err != nil {
return err
}
return nil
}
type RequestMove struct {
Objects []string `json:"objects"`
Folders []string `json:"folders"`
Destination string `json:"destination"`
}
type ReplyMove struct {
Id string `json:"id"`
}
// API method: /class-xxxx/move
//
// Moves the specified data objects and folders to a destination folder in the same container.
func (ops *DxOps) DxMove(
ctx context.Context,
httpClient *retryablehttp.Client,
projId string,
objectIds []string,
folders []string,
destination string) error {
if ops.options.Verbose {
ops.log("%s source folders=%v -> %s", projId, folders, destination)
}
var request RequestMove
request.Objects = objectIds
request.Folders = folders
request.Destination = destination
payload, err := json.Marshal(request)
if err != nil {
return err
}
repJs, err := dxda.DxAPI(
ctx, httpClient, NumRetriesDefault, &ops.dxEnv,
fmt.Sprintf("%s/move", projId),
string(payload))
if err != nil {
return err
}
var reply ReplyMove
if err := json.Unmarshal(repJs, &reply); err != nil {
return err
}
return nil
}
type RequestRenameFolder struct {
Folder string `json:"folder"`
Name string `json:"name"`
}
type ReplyRenameFolder struct {
Id string `json:"id"`
}
func (ops *DxOps) DxRenameFolder(
ctx context.Context,
httpClient *retryablehttp.Client,
projId string,
folder string,
newName string) error {
var request RequestRenameFolder
request.Folder = folder
request.Name = newName
payload, err := json.Marshal(request)
if err != nil {
return err
}
repJs, err := dxda.DxAPI(
ctx, httpClient, NumRetriesDefault, &ops.dxEnv,
fmt.Sprintf("%s/renameFolder", projId),
string(payload))
if err != nil {
return err
}
var reply ReplyRenameFolder
if err := json.Unmarshal(repJs, &reply); err != nil {
return err
}
return nil
}
type RequestClone struct {
Objects []string `json:"objects"`
Folders []string `json:"folders"`
Project string `json:"project"`
Destination string `json:"destination"`
Parents bool `json:"parents"`
}
type ReplyClone struct {
Id string `json:"id"`
Project string `json:"project"`
Exists []string `json:"exists"`
}
func (ops *DxOps) DxClone(
ctx context.Context,
httpClient *retryablehttp.Client,
srcProjId string,
srcId string,
destProjId string,
destProjFolder string) (bool, error) {
var request RequestClone
objs := make([]string, 1)
objs[0] = srcId
request.Objects = objs
request.Folders = make([]string, 0)
request.Project = destProjId
request.Destination = destProjFolder
request.Parents = false
payload, err := json.Marshal(request)
if err != nil {
return false, err
}
repJs, err := dxda.DxAPI(
ctx, httpClient, NumRetriesDefault, &ops.dxEnv,
fmt.Sprintf("%s/clone", srcProjId),
string(payload))
if err != nil {
return false, err
}
var reply ReplyClone
if err := json.Unmarshal(repJs, &reply); err != nil {
return false, err
}
for _,id := range reply.Exists {
if id == srcId {
// was not copied, because there is an existing
// copy in the destination project.
return false, nil
}
}
return true, nil
}
type RequestSetProperties struct {
ProjId string `json:"project"`
Properties map[string](*string) `json:"properties"`
}
type ReplySetProperties struct {
Id string `json:"id"`
}
func (ops *DxOps) DxSetProperty(
ctx context.Context,
httpClient *retryablehttp.Client,
projId string,
objId string,
key string,
value *string) error {
var request RequestSetProperties
request.ProjId = projId
props := make(map[string](*string))
props[key] = value
request.Properties = props
payload, err := json.Marshal(request)
if err != nil {
return err
}
repJs, err := dxda.DxAPI(
ctx, httpClient, NumRetriesDefault, &ops.dxEnv,
fmt.Sprintf("%s/setProperties", objId),
string(payload))
if err != nil {
return err
}
var reply ReplySetProperties
if err := json.Unmarshal(repJs, &reply); err != nil {
return err
}
return nil
}
type RequestAddTags struct {
ProjId string `json:"project"`
Tags []string `json:"tags"`
}
type ReplyAddTags struct {
Id string `json:"id"`
}
func (ops *DxOps) DxAddTag(
ctx context.Context,
httpClient *retryablehttp.Client,
projId string,
objId string,
key string) error {
var request RequestAddTags
request.ProjId = projId
tags := make([]string, 1)
tags[0] = key
request.Tags = tags
payload, err := json.Marshal(request)
if err != nil {
return err
}
repJs, err := dxda.DxAPI(
ctx, httpClient, NumRetriesDefault, &ops.dxEnv,
fmt.Sprintf("%s/addTags", objId),
string(payload))
if err != nil {
return err
}
var reply ReplyAddTags
if err := json.Unmarshal(repJs, &reply); err != nil {
return err
}
return nil
}
type RequestRemoveTags struct {
ProjId string `json:"project"`
Tags []string `json:"tags"`
}
type ReplyRemoveTags struct {
Id string `json:"id"`
}
func (ops *DxOps) DxRemoveTag(
ctx context.Context,
httpClient *retryablehttp.Client,
projId string,
objId string,
key string) error {
var request RequestRemoveTags
request.ProjId = projId
tags := make([]string, 1)
tags[0] = key
request.Tags = tags
payload, err := json.Marshal(request)
if err != nil {
return err
}
repJs, err := dxda.DxAPI(
ctx, httpClient, NumRetriesDefault, &ops.dxEnv,
fmt.Sprintf("%s/removeTags", objId),
string(payload))
if err != nil {
return err
}
var reply ReplyRemoveTags
if err := json.Unmarshal(repJs, &reply); err != nil {
return err
}
return nil
}