-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.go
68 lines (60 loc) · 1.78 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
package vaultpkcs11
import (
"context"
"fmt"
"sort"
"strings"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/jeffchao/backoff"
)
// withFieldValidator wraps an OperationFunc and validates the user-supplied
// fields match the schema.
func withFieldValidator(f framework.OperationFunc) framework.OperationFunc {
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
if err := validateFields(req, d); err != nil {
return nil, logical.CodedError(400, err.Error())
}
return f(ctx, req, d)
}
}
// validateFields verifies that no bad arguments were given to the request.
func validateFields(req *logical.Request, data *framework.FieldData) error {
var unknownFields []string
for k := range req.Data {
if _, ok := data.Schema[k]; !ok {
unknownFields = append(unknownFields, k)
}
}
switch len(unknownFields) {
case 0:
return nil
case 1:
return fmt.Errorf("unknown field: %s", unknownFields[0])
default:
sort.Strings(unknownFields)
return fmt.Errorf("unknown fields: %s", strings.Join(unknownFields, ","))
}
}
// errMissingFields is a helper to return an error when required fields are
// missing.
func errMissingFields(f ...string) error {
return logical.CodedError(400, fmt.Sprintf(
"missing required field(s): %q", f))
}
// retryFib accepts a function and retries using a fibonacci algorithm.
func retryFib(op func() error) error {
f := backoff.Fibonacci()
f.Interval = 100 * time.Millisecond
f.MaxRetries = 10
return f.Retry(op)
}
// retryExp accepts a function and retries using an exponential backoff
// algorithm.
func retryExp(op func() error) error {
f := backoff.Exponential()
f.Interval = 100 * time.Millisecond
f.MaxRetries = 10
return f.Retry(op)
}