-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpath_list.go
74 lines (68 loc) · 2.55 KB
/
path_list.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
package datastax_astra
import (
"context"
"errors"
"github.com/hashicorp/vault/sdk/logical"
"strings"
)
type PathList int
type ToResponseDataFunc func() map[string]interface{}
const (
ConfigPathList = iota
RolePathList
CredentialsPathList
)
func (pl PathList) String() string {
switch pl {
case ConfigPathList:
return "config"
case RolePathList:
return "role"
case CredentialsPathList:
return "token"
default:
return "unknown"
}
}
// GetObjectInformation Retrieves the object entry's ID and data. Specifically, we return the object ID, a callback
// function to retrieve the response data, and an error to determine if we should call the callback function.
func (pl PathList) GetObjectInformation(ctx context.Context, req *logical.Request, key string) (string, ToResponseDataFunc, error) {
switch pl {
case ConfigPathList:
obj, err := readConfig(ctx, req.Storage, key)
return key, obj.ToResponseData, err
case RolePathList:
obj, err := readRoleUsingKey(ctx, req.Storage, key)
// The role storage key is contains the Org ID and Role Name delimited by ":"
// For consistency and to avoid confusion, we return just the Role Name
return strings.Split(key, roleStorageKeyDelimiter)[1], obj.ToResponseData, err
case CredentialsPathList:
obj, err := readToken(ctx, req.Storage, key)
// Regardless of the storage key used for the token, always return the Client ID
return obj.ClientID, obj.ToResponseData, err
default:
return "", nil, errors.New("response requested for unknown object type")
}
}
func (b *datastaxAstraBackend) pathObjectList(ctx context.Context, req *logical.Request, pl PathList) (*logical.Response, error) {
pathList := pl.String()
objList, err := req.Storage.List(ctx, pathList+"/")
if err != nil {
return nil, errors.New("error loading " + pathList + " list: " + err.Error())
}
if len(objList) == 0 {
return nil, errors.New("no " + pathList + "s found")
}
keyInfo := map[string]interface{}{}
var keys []string
for _, key := range objList {
// Get the key/value (ID/Data) of the object
objId, objResponseData, err := pl.GetObjectInformation(ctx, req, key)
if err != nil {
return nil, errors.New("failed to retrieve" + pathList + "information: " + err.Error())
}
keys = append(keys, objId)
keyInfo[objId] = objResponseData()
}
return logical.ListResponseWithInfo(keys, keyInfo), nil
}