5
5
"crypto/tls"
6
6
"encoding/base64"
7
7
"encoding/json"
8
+ "errors"
8
9
"fmt"
9
10
"io"
10
11
"io/ioutil"
@@ -57,35 +58,6 @@ func (a *App) apiRequest(method, url string, data io.Reader) ([]byte, int) {
57
58
return body , resp .StatusCode
58
59
}
59
60
60
- // getUserGroups retrieves the groups of which the current user has rights to administer
61
- func (a * App ) getUserGroups () []GroupUID {
62
- endpoint := a .Config .RPAURL + "/fapi/rest/5_1/users/settings/"
63
- body , _ := a .apiRequest ("GET" , endpoint , nil )
64
- var usr UsersSettingsResponse
65
- json .Unmarshal (body , & usr )
66
-
67
- var allowedGroups []GroupUID
68
- for _ , u := range usr .Users {
69
- if u .Name == a .Config .Username {
70
- allowedGroups = u .Groups
71
- }
72
- }
73
- return allowedGroups
74
- }
75
-
76
- // groupInGroups returns true if a group UID exists in a slice of GroupUID
77
- func (a * App ) groupInGroups (groupID int , usersGroups []GroupUID ) bool {
78
- if usersGroups == nil {
79
- usersGroups = a .getUserGroups ()
80
- }
81
- for _ , g := range usersGroups {
82
- if g .ID == groupID {
83
- return true
84
- }
85
- }
86
- return false
87
- }
88
-
89
61
func (a * App ) getAllGroups () []GroupUID {
90
62
endpoint := a .Config .RPAURL + "/fapi/rest/5_1/groups/"
91
63
body , _ := a .apiRequest ("GET" , endpoint , nil )
@@ -249,7 +221,7 @@ func (a *App) startTransfer(t Task) {
249
221
fmt .Printf ("Starting Transfer for Group %s Copy %s\n " , t .GroupName , t .CopyName )
250
222
}
251
223
252
- func (a * App ) imageAccess (t Task ) {
224
+ func (a * App ) imageAccess (t Task ) error {
253
225
operationName := "Disabling"
254
226
operation := "disable_image_access"
255
227
if t .Enable == true {
@@ -270,13 +242,14 @@ func (a *App) imageAccess(t Task) {
270
242
}
271
243
272
244
if ! a .Config .CheckMode {
273
- _ , statusCode := a .apiRequest ("PUT" , endpoint , bytes .NewBuffer (json ))
245
+ body , statusCode := a .apiRequest ("PUT" , endpoint , bytes .NewBuffer (json ))
274
246
if statusCode != 204 {
275
- log .Errorf ("Expected status code '204' and received: %d\n " , statusCode )
276
- log . Fatalf ( "Error %s Latest Image for Group %s Copy %s \n " , operationName , t . GroupName , t . CopyName )
247
+ log .Debugf ("Expected status code '204' and received: %d\n " , statusCode )
248
+ return errors . New ( string ( body ) )
277
249
}
278
250
}
279
251
fmt .Printf ("%s Latest Image for Group %s Copy %s\n " , operationName , t .GroupName , t .CopyName )
252
+ return nil
280
253
}
281
254
282
255
func (a * App ) pollImageAccessEnabled (groupID int , stateDesired bool ) {
@@ -320,7 +293,7 @@ func (a *App) directAccess(t Task) {
320
293
321
294
// EnableAll wraper for enabling Direct Image Access for all CG
322
295
func (a * App ) EnableAll () {
323
- groups := a .getUserGroups () // only groups user has permission to admin
296
+ groups := a .getAllGroups () // only groups user has permission to admin
324
297
for _ , g := range groups {
325
298
var t Task
326
299
GroupName := a .getGroupName (g .ID )
@@ -338,7 +311,11 @@ func (a *App) EnableAll() {
338
311
t .CopyUID = copySettings .CopyUID .GlobalCopyUID .CopyUID
339
312
t .Enable = true // whether to enable or disable the following tasks
340
313
if ! a .Config .CheckMode {
341
- a .imageAccess (t )
314
+ err := a .imageAccess (t )
315
+ if err != nil {
316
+ log .Warnf ("%s %s\n " , GroupName , err )
317
+ continue
318
+ }
342
319
a .pollImageAccessEnabled (g .ID , true )
343
320
a .directAccess (t )
344
321
}
@@ -349,11 +326,6 @@ func (a *App) EnableAll() {
349
326
// EnableOne wraper for enabling Direct Image Access for a single CG
350
327
func (a * App ) EnableOne () {
351
328
groupID := a .getGroupIDByName (a .Group )
352
- usersGroups := a .getUserGroups ()
353
- if a .groupInGroups (groupID , usersGroups ) == false {
354
- log .Error ("User does not have sufficient access to administer " , a .Group )
355
- return
356
- }
357
329
var t Task
358
330
groupCopiesSettings := a .getGroupCopiesSettings (groupID )
359
331
copySettings := a .getRequestedCopy (groupCopiesSettings )
@@ -369,15 +341,19 @@ func (a *App) EnableOne() {
369
341
t .CopyUID = copySettings .CopyUID .GlobalCopyUID .CopyUID
370
342
t .Enable = true // whether to enable or disable the following tasks
371
343
if ! a .Config .CheckMode {
372
- a .imageAccess (t )
344
+ err := a .imageAccess (t )
345
+ if err != nil {
346
+ log .Warnf ("%s %s\n " , a .Group , err )
347
+ return
348
+ }
373
349
a .pollImageAccessEnabled (groupID , true )
374
350
a .directAccess (t )
375
351
}
376
352
}
377
353
378
354
// FinishAll wraper for finishing Direct Image Access for all CG
379
355
func (a * App ) FinishAll () {
380
- groups := a .getUserGroups () // only groups user has permission to admin
356
+ groups := a .getAllGroups () // only groups user has permission to admin
381
357
for _ , g := range groups {
382
358
var t Task
383
359
GroupName := a .getGroupName (g .ID )
@@ -390,7 +366,11 @@ func (a *App) FinishAll() {
390
366
t .CopyUID = copySettings .CopyUID .GlobalCopyUID .CopyUID
391
367
t .Enable = false // whether to enable or disable the following tasks
392
368
if ! a .Config .CheckMode {
393
- a .imageAccess (t )
369
+ err := a .imageAccess (t )
370
+ if err != nil {
371
+ log .Warnf ("%s %s\n " , GroupName , err )
372
+ continue
373
+ }
394
374
a .pollImageAccessEnabled (g .ID , false )
395
375
a .startTransfer (t )
396
376
}
@@ -401,11 +381,6 @@ func (a *App) FinishAll() {
401
381
// FinishOne wraper for finishing Direct Image Access for a single CG
402
382
func (a * App ) FinishOne () {
403
383
groupID := a .getGroupIDByName (a .Group )
404
- usersGroups := a .getUserGroups ()
405
- if a .groupInGroups (groupID , usersGroups ) == false {
406
- log .Error ("User does not have sufficient access to administer " , a .Group )
407
- return
408
- }
409
384
var t Task
410
385
groupCopiesSettings := a .getGroupCopiesSettings (groupID )
411
386
copySettings := a .getRequestedCopy (groupCopiesSettings )
@@ -416,7 +391,11 @@ func (a *App) FinishOne() {
416
391
t .CopyUID = copySettings .CopyUID .GlobalCopyUID .CopyUID
417
392
t .Enable = false // whether to enable or disable the following tasks
418
393
if ! a .Config .CheckMode {
419
- a .imageAccess (t )
394
+ err := a .imageAccess (t )
395
+ if err != nil {
396
+ log .Warnf ("%s %s\n " , a .Group , err )
397
+ return
398
+ }
420
399
a .pollImageAccessEnabled (groupID , false )
421
400
a .startTransfer (t )
422
401
}
0 commit comments