forked from QuentinPerez/go-radosgw
/
helpers.go
878 lines (802 loc) · 21.7 KB
/
helpers.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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
package radosAPI
import (
"encoding/json"
"errors"
"net/url"
"time"
"fmt"
"github.com/QuentinPerez/go-encodeUrl"
)
// UsageConfig usage request
type UsageConfig struct {
UID string `url:"uid,ifStringIsNotEmpty"` // The user for which the information is requested. If not specified will apply to all users
Start *time.Time `url:"start,ifTimeIsNotNilCeph"` // Date and (optional) time that specifies the start time of the requested data
End *time.Time `url:"end,ifTimeIsNotNilCeph"` // Date and (optional) time that specifies the end time of the requested data (non-inclusive)
ShowEntries bool `url:"show-entries,ifBoolIsFalse"` // Specifies whether data entries should be returned.
ShowSummary bool `url:"show-summary,ifBoolIsFalse"` // Specifies whether data summary should be returned
RemoveAll bool `url:"remove-all,ifBoolIsTrue"` // Required when uid is not specified, in order to acknowledge multi user data removal.
}
// GetUsage requests bandwidth usage information.
//
// !! caps: usage=read !!
//
// @UID
// @Start
// @End
// @ShowEntries
// @ShowSummary
//
func (api *API) GetUsage(conf UsageConfig) (*Usage, error) {
var (
ret = &Usage{}
values = url.Values{}
errs []error
)
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return nil, errs[0]
}
values.Add("format", "json")
body, _, err := api.call("GET", "/usage", values, true)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, &ret); err != nil {
return nil, err
}
return ret, nil
}
// DeleteUsage removes usage information. With no dates specified, removes all usage information
//
// !! caps: usage=write !!
//
// @UID
// @Start
// @End
// @RemoveAll
//
func (api *API) DeleteUsage(conf UsageConfig) error {
var (
values = url.Values{}
errs []error
)
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return errs[0]
}
values.Add("format", "json")
_, _, err := api.call("DELETE", "/usage", values, true)
return err
}
// GetUser gets user information. If no user is specified returns the list of all users along with suspension information
//
// !! caps: users=read !!
//
// @uid
//
func (api *API) GetUser(uid string) (*User, error) {
ret := &User{}
values := url.Values{}
values.Add("format", "json")
values.Add("uid", uid)
body, _, err := api.call("GET", "/user", values, true)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, &ret); err != nil {
return nil, err
}
return ret, nil
}
// GetUsers fetches users uid list
//
// !! caps: metadata=read !!
//
func (api *API) GetUsers() ([]string, error) {
ret := make([]string, 0)
values := url.Values{}
values.Add("format", "json")
body, _, err := api.call("GET", "/metadata/user", values, true)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, &ret); err != nil {
return nil, err
}
return ret, nil
}
// UserConfig user request
type UserConfig struct {
UID string `url:"uid,ifStringIsNotEmpty"` // The user ID to be created
DisplayName string `url:"display-name,ifStringIsNotEmpty"` // The display name of the user to be created
Email string `url:"email,ifStringIsNotEmpty"` // The email address associated with the user
KeyType string `url:"key-type,ifStringIsNotEmpty"` // Key type to be generated, options are: swift, s3 (default)
AccessKey string `url:"access-key,ifStringIsNotEmpty"` // Specify access key
SecretKey string `url:"secret-key,ifStringIsNotEmpty"` // Specify secret key
UserCaps string `url:"user-caps,ifStringIsNotEmpty"` // User capabilities
MaxBuckets *int `url:"max-buckets,itoaIfNotNil"` // Specify the maximum number of buckets the user can own
GenerateKey bool `url:"generate-key,ifBoolIsTrue"` // Generate a new key pair and add to the existing keyring
Suspended bool `url:"suspended,ifBoolIsTrue"` // Specify whether the user should be suspended
PurgeData bool `url:"purge-data,ifBoolIsTrue"` // When specified the buckets and objects belonging to the user will also be removed
}
// CreateUser creates a new user. By Default, a S3 key pair will be created automatically and returned in the response.
// If only one of access-key or secret-key is provided, the omitted key will be automatically generated.
// By default, a generated key is added to the keyring without replacing an existing key pair.
// If access-key is specified and refers to an existing key owned by the user then it will be modified
//
// !! caps: users=write !!
//
// @UID
// @DisplayName
// @Email
// @KeyType
// @AccessKey
// @SecretKey
// @UserCaps
// @GenerateKey
// @MaxBuckets
// @Suspended
//
func (api *API) CreateUser(conf UserConfig) (*User, error) {
if conf.UID == "" {
return nil, errors.New("UID field is required")
}
if conf.DisplayName == "" {
return nil, errors.New("DisplayName field is required")
}
var (
ret = &User{}
values = url.Values{}
errs []error
)
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return nil, errs[0]
}
values.Add("format", "json")
body, _, err := api.call("PUT", "/user", values, true)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, &ret); err != nil {
return nil, err
}
return ret, nil
}
// UpdateUser modifies a user
//
// !! caps: users=write !!
//
// @UID
// @DisplayName
// @Email
// @KeyType
// @AccessKey
// @SecretKey
// @UserCaps
// @GenerateKey
// @MaxBuckets
// @Suspended
//
func (api *API) UpdateUser(conf UserConfig) (*User, error) {
if conf.UID == "" {
return nil, errors.New("UID field is required")
}
var (
ret = &User{}
values = url.Values{}
errs []error
)
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return nil, errs[0]
}
values.Add("format", "json")
body, _, err := api.call("POST", "/user", values, true)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, &ret); err != nil {
return nil, err
}
return ret, nil
}
// RemoveUser removes an existing user.
//
// !! caps: users=write !!
//
// @UID
// @PurgeData
//
func (api *API) RemoveUser(conf UserConfig) error {
if conf.UID == "" {
return errors.New("UID field is required")
}
var (
values = url.Values{}
errs []error
)
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return errs[0]
}
values.Add("format", "json")
_, _, err := api.call("DELETE", "/user", values, true)
return err
}
// SubUserConfig subuser request
type SubUserConfig struct {
UID string `url:"uid,ifStringIsNotEmpty"` // The user ID under which a subuser is to be created
SubUser string `url:"subuser,ifStringIsNotEmpty"` // Specify the subuser ID to be created
KeyType string `url:"key-type,ifStringIsNotEmpty"` // Key type to be generated, options are: swift (default), s3
Access string `url:"access,ifStringIsNotEmpty"` // Set access permissions for sub-user, should be one of read, write, readwrite, full
Secret string `url:"secret,ifStringIsNotEmpty"` // Specify secret key
SecretKey string `url:"secret-key,ifStringIsNotEmpty"` // Specify secret key
GenerateSecret bool `url:"generate-secret,ifBoolIsTrue"` // Generate the secret key
PurgeKeys bool `url:"purge-keys,ifBoolIsTrue"` // Remove keys belonging to the subuser
}
// CreateSubUser creates a new subuser (primarily useful for clients using the Swift API).
// Note that either gen-subuser or subuser is required for a valid request.
// Note that in general for a subuser to be useful, it must be granted permissions by specifying access.
// As with user creation if subuser is specified without secret, then a secret key will be automatically generated.
//
// !! caps: users=write !!
//
// @UID
// @SubUser
// @KeyType
// @Access
// @SecretKey
// @GenerateSecret
//
func (api *API) CreateSubUser(conf SubUserConfig) (*SubUsers, error) {
if conf.UID == "" {
return nil, errors.New("UID field is required")
}
var (
ret = &SubUsers{}
values = url.Values{}
errs []error
)
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return nil, errs[0]
}
values.Add("format", "json")
body, _, err := api.call("PUT", "/user", values, true, "subuser")
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, &ret); err != nil {
return nil, err
}
return ret, nil
}
// UpdateSubUser modifies an existing subuser
//
// !! caps: users=write !!
//
// @UID
// @SubUser
// @KeyType
// @Access
// @Secret
// @GenerateSecret
//
func (api *API) UpdateSubUser(conf SubUserConfig) (*SubUsers, error) {
if conf.UID == "" {
return nil, errors.New("UID field is required")
}
if conf.SubUser == "" {
return nil, errors.New("SubUser field is required")
}
var (
ret = &SubUsers{}
values = url.Values{}
errs []error
)
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return nil, errs[0]
}
values.Add("format", "json")
body, _, err := api.call("POST", "/user", values, true, "subuser")
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, &ret); err != nil {
return nil, err
}
return ret, nil
}
// RemoveSubUser remove an existing subuser
//
// !! caps: users=write !!
//
// @UID
// @SubUser
// @PurgeKeys
//
func (api *API) RemoveSubUser(conf SubUserConfig) error {
if conf.UID == "" {
return errors.New("UID field is required")
}
if conf.SubUser == "" {
return errors.New("SubUser field is required")
}
var (
values = url.Values{}
errs []error
)
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return errs[0]
}
values.Add("format", "json")
_, _, err := api.call("DELETE", "/user", values, true, "subuser")
return err
}
// KeyConfig key request
type KeyConfig struct {
UID string `url:"uid,ifStringIsNotEmpty"` // The user ID to receive the new key
SubUser string `url:"subuser,ifStringIsNotEmpty"` // The subuser ID to receive the new key
KeyType string `url:"key-type,ifStringIsNotEmpty"` // Key type to be generated, options are: swift, s3 (default)
AccessKey string `url:"access-key,ifStringIsNotEmpty"` // Specify the access key
SecretKey string `url:"secret-key,ifStringIsNotEmpty"` // Specify secret key
GenerateSecret bool `url:"generate-secret,ifBoolIsTrue"` // Generate a new key pair and add to the existing keyring
}
// CreateKey creates a new key. If a subuser is specified then by default created keys will be swift type.
// If only one of access-key or secret-key is provided the committed key will be automatically generated,
// that is if only secret-key is specified then access-key will be automatically generated.
// By default, a generated key is added to the keyring without replacing an existing key pair.
// If access-key is specified and refers to an existing key owned by the user then it will be modified.
// The response is a container listing all keys of the same type as the key created.
// Note that when creating a swift key, specifying the option access-key will have no effect.
// Additionally, only one swift key may be held by each user or subuser.
//
// !! caps: users=write !!
//
// @UID
// @SubUser
// @KeyType
// @AccessKey
// @SecretKey
// @GenerateSecret
//
func (api *API) CreateKey(conf KeyConfig) (*KeysDefinition, error) {
if conf.UID == "" {
return nil, errors.New("UID field is required")
}
var (
ret = &KeysDefinition{}
values = url.Values{}
errs []error
)
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return nil, errs[0]
}
values.Add("format", "json")
body, _, err := api.call("PUT", "/user", values, true, "key")
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, &ret); err != nil {
return nil, err
}
return ret, nil
}
// RemoveKey removes an existing key
//
// !! caps: users=write !!
//
// @UID
// @SubUser
// @KeyType
// @AccessKey
//
func (api *API) RemoveKey(conf KeyConfig) error {
if conf.AccessKey == "" {
return errors.New("AccessKey field is required")
}
var (
values = url.Values{}
errs []error
)
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return errs[0]
}
values.Add("format", "json")
_, _, err := api.call("DELETE", "/user", values, true, "key")
return err
}
// BucketConfig bucket request
type BucketConfig struct {
Bucket string `url:"bucket,ifStringIsNotEmpty"` // The bucket to return info on
UID string `url:"uid,ifStringIsNotEmpty"` // The user to retrieve bucket information for
Stats bool `url:"stats,ifBoolIsTrue"` // Return bucket statistics
CheckObjects bool `url:"check-objects,ifBoolIsTrue"` // Check multipart object accounting
Fix bool `url:"fix,ifBoolIsTrue"` // Also fix the bucket index when checking
PurgeObjects bool `url:"purge-objects,ifBoolIsTrue"` // Remove a buckets objects before deletion
Object string `url:"object,ifStringIsNotEmpty"` // The object to remove
}
// GetBucket gets information about a subset of the existing buckets.
// If uid is specified without bucket then all buckets beloning to the user will be returned.
// If bucket alone is specified, information for that particular bucket will be retrieved
//
// !! caps: buckets=read !!
//
//@Bucket
//@UID
//@Stats
//
func (api *API) GetBucket(conf BucketConfig) (Buckets, error) {
var (
ret = Buckets{}
values = url.Values{}
errs []error
variant interface{}
)
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return nil, errs[0]
}
values.Add("format", "json")
body, _, err := api.call("GET", "/bucket", values, true)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, &variant); err != nil {
return nil, err
}
if tab, ok := variant.([]interface{}); ok {
add := Bucket{}
for _, v := range tab {
if name, ok := v.(string); ok {
if add.Name != "" {
ret = append(ret, add)
add = Bucket{}
}
add.Name = name
} else {
js, errMarshal := json.Marshal(v)
if errMarshal != nil {
return nil, errMarshal
}
if add.Stats != nil {
ret = append(ret, add)
add = Bucket{}
}
add.Stats = new(Stats)
err = json.Unmarshal(js, add.Stats)
if err != nil {
return nil, err
}
}
}
ret = append(ret, add)
} else {
add := Bucket{}
add.Stats = new(Stats)
err = json.Unmarshal(body, add.Stats)
if err != nil {
return nil, err
}
ret = append(ret, add)
}
return ret, nil
}
// RemoveBucket removes an existing bucket.
//
// !! caps: buckets=write !!
//
//@Bucket
//@PurgeObjects
//
func (api *API) RemoveBucket(conf BucketConfig) error {
var (
values = url.Values{}
errs []error
)
if conf.Bucket == "" {
return errors.New("Bucket field is required")
}
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return errs[0]
}
values.Add("format", "json")
_, _, err := api.call("DELETE", "/bucket", values, true)
return err
}
// UnlinkBucket unlinks a bucket from a specified user.
// Primarily useful for changing bucket ownership.
//
// !! caps: buckets=write !!
//
//@Bucket
//@UID
//
func (api *API) UnlinkBucket(conf BucketConfig) error {
var (
values = url.Values{}
errs []error
)
if conf.Bucket == "" {
return errors.New("Bucket field is required")
}
if conf.UID == "" {
return errors.New("UID field is required")
}
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return errs[0]
}
values.Add("format", "json")
_, _, err := api.call("POST", "/bucket", values, true)
return err
}
// CheckBucket checks the index of an existing bucket.
// NOTE: to check multipart object accounting with check-objects, fix must be set to True.
//
// !! caps: buckets=write !!
//
//@Bucket
//@CheckObjects
//@Fix
//
func (api *API) CheckBucket(conf BucketConfig) (string, error) {
var (
values = url.Values{}
errs []error
)
if conf.Bucket == "" {
return "", errors.New("Bucket field is required")
}
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return "", errs[0]
}
values.Add("format", "json")
body, _, err := api.call("GET", "/bucket", values, true, "index")
return string(body), err
}
// LinkBucket links a bucket to a specified user, unlinking the bucket from any previous user.
//
// !! caps: buckets=write !!
//
//@Bucket
//@UID
//
func (api *API) LinkBucket(conf BucketConfig) error {
var (
values = url.Values{}
errs []error
)
// FIXME doesn't work
return fmt.Errorf("LinkBucket not implemented yet")
if conf.Bucket == "" {
return errors.New("Bucket field is required")
}
if conf.UID == "" {
return errors.New("UID field is required")
}
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return errs[0]
}
values.Add("format", "json")
body, _, err := api.call("PUT", "/bucket", values, true)
// return string(body), err
_ = body
return err
}
// RemoveObject removes an existing object. NOTE: Does not require owner to be non-suspended.
//
// !! caps: buckets=write !!
//
//@Bucket
//@Object
//
func (api *API) RemoveObject(conf BucketConfig) error {
var (
values = url.Values{}
errs []error
)
if conf.Bucket == "" {
return errors.New("Bucket field is required")
}
if conf.Object == "" {
return errors.New("Object field is required")
}
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return errs[0]
}
values.Add("format", "json")
_, _, err := api.call("DELETE", "/bucket", values, true, "object")
return err
}
// GetBucketPolicy reads the bucket policy
//
// !! caps: buckets=read !!
//
//@Bucket
//
func (api *API) GetBucketPolicy(conf BucketConfig) (*Policy, error) {
var (
ret = &Policy{}
values = url.Values{}
errs []error
)
if conf.Bucket == "" {
return nil, errors.New("Bucket field is required")
}
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return nil, errs[0]
}
values.Add("format", "json")
body, _, err := api.call("GET", "/bucket", values, true, "policy")
if err = json.Unmarshal(body, &ret); err != nil {
return nil, err
}
return ret, err
}
// GetObjectPolicy reads the object policy
//
// !! caps: buckets=read !!
//
//@Bucket
//@Object
//
func (api *API) GetObjectPolicy(conf BucketConfig) (*Policy, error) {
var (
ret = &Policy{}
values = url.Values{}
errs []error
)
if conf.Bucket == "" {
return nil, errors.New("Bucket field is required")
}
if conf.Object == "" {
return nil, errors.New("Object field is required")
}
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return nil, errs[0]
}
values.Add("format", "json")
body, _, err := api.call("GET", "/bucket", values, true, "policy")
if err = json.Unmarshal(body, &ret); err != nil {
return nil, err
}
return ret, err
}
// QuotaConfig quota request
type QuotaConfig struct {
UID string `url:"uid,ifStringIsNotEmpty"` // The user to specify a quota
MaxObjects string `url:"max-objects,ifStringIsNotEmpty"` // The max-objects setting allows you to specify the maximum number of objects. A negative value disables this setting.
MaxSizeKB string `url:"max-size-kb,ifStringIsNotEmpty"` // The max-size-kb option allows you to specify a quota for the maximum number of bytes. A negative value disables this setting
Enabled string `url:"enabled,ifStringIsNotEmpty"` // The enabled option enables the quotas
QuotaType string `url:"quota-type,ifStringIsNotEmpty"` // The quota-type option sets the scope for the quota. The options are bucket and user.
}
// GetQuotas returns user's quotas
//
// !! caps: users=read !!
//
//@UID
//@QuotaType
//
func (api *API) GetQuotas(conf QuotaConfig) (*Quotas, error) {
var (
ret = &Quotas{}
values = url.Values{}
errs []error
)
if conf.UID == "" {
return nil, errors.New("UID field is required")
}
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return nil, errs[0]
}
values.Add("format", "json")
body, _, err := api.call("GET", "/user", values, true, "quota")
if err = json.Unmarshal(body, &ret); err != nil {
return nil, err
}
return ret, err
}
// UpdateQuota updates user's quotas
//
// !! caps: users=write !!
//
//@UID
//@Quota [user,bucket]
//
func (api *API) UpdateQuota(conf QuotaConfig) error {
var (
values = url.Values{}
errs []error
)
if conf.UID == "" {
return errors.New("UID field is required")
}
if conf.QuotaType == "" {
return errors.New("QuotaType field is required")
}
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return errs[0]
}
values.Add("format", "json")
_, _, err := api.call("PUT", "/user", values, true, "quota")
return err
}
// CapConfig capability request
type CapConfig struct {
UID string `url:"uid,ifStringIsNotEmpty"` // The user ID
UserCaps string `url:"user-caps,ifStringIsNotEmpty"` // The administrative capabilities
}
// AddCapability returns user's quotas
//
// !! caps: users=write !!
//
//@UID
//@UserCaps
//
func (api *API) AddCapability(conf CapConfig) ([]Capability, error) {
var (
values = url.Values{}
ret = []Capability{}
errs []error
)
if conf.UID == "" {
return nil, errors.New("UID field is required")
}
if conf.UserCaps == "" {
return nil, errors.New("UserCaps field is required")
}
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return nil, errs[0]
}
values.Add("format", "json")
body, _, err := api.call("PUT", "/user", values, true, "caps")
if err = json.Unmarshal(body, &ret); err != nil {
return nil, err
}
return ret, err
}
// DelCapability returns user's quotas
//
// !! caps: users=write !!
//
//@UID
//@UserCaps
//
func (api *API) DelCapability(conf CapConfig) ([]Capability, error) {
var (
values = url.Values{}
ret = []Capability{}
errs []error
)
if conf.UID == "" {
return nil, errors.New("UID field is required")
}
if conf.UserCaps == "" {
return nil, errors.New("UserCaps field is required")
}
values, errs = encurl.Translate(conf)
if len(errs) > 0 {
return nil, errs[0]
}
values.Add("format", "json")
body, _, err := api.call("DELETE", "/user", values, true, "caps")
if err = json.Unmarshal(body, &ret); err != nil {
return nil, err
}
return ret, err
}