/
helpers.go
executable file
·45 lines (36 loc) · 1.1 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
package chefclient
import (
"context"
"fmt"
"sort"
"time"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
const (
contextTimeout = 10 * time.Second
)
// newContext creates a new context with the default settings, etc.
func newContext() (context.Context, func()) {
return context.WithTimeout(context.Background(), contextTimeout)
}
// errMissingField returns a logical response error that prints a consistent
// error message for when a required field is missing.
func errMissingField(field string) *logical.Response {
return logical.ErrorResponse(fmt.Sprintf("Missing required field '%s'", field))
}
// 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)
}
}
if len(unknownFields) > 0 {
// Sort since this is a human error
sort.Strings(unknownFields)
return fmt.Errorf("unknown fields: %q", unknownFields)
}
return nil
}