state: convert acl-roles.name index to the functional indexer pattern
This commit is contained in:
parent
f836ed256b
commit
474e95b9f5
|
@ -1371,7 +1371,8 @@ func aclRoleSetTxn(tx *txn, idx uint64, role *structs.ACLRole, allowMissing bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure the name is unique (cannot conflict with another role with a different ID)
|
// ensure the name is unique (cannot conflict with another role with a different ID)
|
||||||
_, nameMatch, err := aclRoleGetByName(tx, role.Name, &role.EnterpriseMeta)
|
q := Query{EnterpriseMeta: role.EnterpriseMeta, Value: role.Name}
|
||||||
|
nameMatch, err := tx.First(tableACLRoles, indexName, q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed acl role lookup: %v", err)
|
return fmt.Errorf("failed acl role lookup: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -1424,6 +1425,15 @@ func (s *Store) ACLRoleGetByName(ws memdb.WatchSet, name string, entMeta *struct
|
||||||
return s.aclRoleGet(ws, name, aclRoleGetByName, entMeta)
|
return s.aclRoleGet(ws, name, aclRoleGetByName, entMeta)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func aclRoleGetByName(tx ReadTxn, name string, entMeta *structs.EnterpriseMeta) (<-chan struct{}, interface{}, error) {
|
||||||
|
// TODO: accept non-pointer value
|
||||||
|
if entMeta == nil {
|
||||||
|
entMeta = structs.DefaultEnterpriseMeta()
|
||||||
|
}
|
||||||
|
q := Query{EnterpriseMeta: *entMeta, Value: name}
|
||||||
|
return tx.FirstWatch(tableACLRoles, indexName, q)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Store) ACLRoleBatchGet(ws memdb.WatchSet, ids []string) (uint64, structs.ACLRoles, error) {
|
func (s *Store) ACLRoleBatchGet(ws memdb.WatchSet, ids []string) (uint64, structs.ACLRoles, error) {
|
||||||
tx := s.db.Txn(false)
|
tx := s.db.Txn(false)
|
||||||
defer tx.Abort()
|
defer tx.Abort()
|
||||||
|
@ -1488,7 +1498,7 @@ func (s *Store) ACLRoleList(ws memdb.WatchSet, policy string, entMeta *structs.E
|
||||||
if policy != "" {
|
if policy != "" {
|
||||||
iter, err = aclRoleListByPolicy(tx, policy, entMeta)
|
iter, err = aclRoleListByPolicy(tx, policy, entMeta)
|
||||||
} else {
|
} else {
|
||||||
iter, err = aclRoleList(tx, entMeta)
|
iter, err = tx.Get(tableACLRoles, indexName+"_prefix", entMeta)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -38,6 +38,21 @@ func indexNameFromACLPolicy(raw interface{}) ([]byte, error) {
|
||||||
return b.Bytes(), nil
|
return b.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func indexNameFromACLRole(raw interface{}) ([]byte, error) {
|
||||||
|
p, ok := raw.(*structs.ACLRole)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected type %T for structs.ACLRole index", raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.Name == "" {
|
||||||
|
return nil, errMissingValueForIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
var b indexBuilder
|
||||||
|
b.String(strings.ToLower(p.Name))
|
||||||
|
return b.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
func aclPolicyGetByID(tx ReadTxn, id string, _ *structs.EnterpriseMeta) (<-chan struct{}, interface{}, error) {
|
func aclPolicyGetByID(tx ReadTxn, id string, _ *structs.EnterpriseMeta) (<-chan struct{}, interface{}, error) {
|
||||||
return tx.FirstWatch(tableACLPolicies, indexID, id)
|
return tx.FirstWatch(tableACLPolicies, indexID, id)
|
||||||
}
|
}
|
||||||
|
@ -159,14 +174,6 @@ func aclRoleGetByID(tx ReadTxn, id string, _ *structs.EnterpriseMeta) (<-chan st
|
||||||
return tx.FirstWatch(tableACLRoles, indexID, id)
|
return tx.FirstWatch(tableACLRoles, indexID, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func aclRoleGetByName(tx ReadTxn, name string, _ *structs.EnterpriseMeta) (<-chan struct{}, interface{}, error) {
|
|
||||||
return tx.FirstWatch(tableACLRoles, indexName, name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func aclRoleList(tx ReadTxn, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
|
|
||||||
return tx.Get(tableACLRoles, indexID)
|
|
||||||
}
|
|
||||||
|
|
||||||
func aclRoleListByPolicy(tx ReadTxn, policy string, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
|
func aclRoleListByPolicy(tx ReadTxn, policy string, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
|
||||||
return tx.Get(tableACLRoles, indexPolicies, policy)
|
return tx.Get(tableACLRoles, indexPolicies, policy)
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,7 @@ func testIndexerTableACLRoles() map[string]indexerTestCase {
|
||||||
},
|
},
|
||||||
indexName: {
|
indexName: {
|
||||||
read: indexValue{
|
read: indexValue{
|
||||||
source: "RoLe",
|
source: Query{Value: "RoLe"},
|
||||||
expected: []byte("role\x00"),
|
expected: []byte("role\x00"),
|
||||||
},
|
},
|
||||||
write: indexValue{
|
write: indexValue{
|
||||||
|
|
|
@ -151,9 +151,10 @@ func rolesTableSchema() *memdb.TableSchema {
|
||||||
Name: indexName,
|
Name: indexName,
|
||||||
AllowMissing: false,
|
AllowMissing: false,
|
||||||
Unique: true,
|
Unique: true,
|
||||||
Indexer: &memdb.StringFieldIndex{
|
Indexer: indexerSingleWithPrefix{
|
||||||
Field: "Name",
|
readIndex: readIndex(indexFromQuery),
|
||||||
Lowercase: true,
|
writeIndex: writeIndex(indexNameFromACLRole),
|
||||||
|
prefixIndex: prefixIndex(prefixIndexFromQuery),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
indexPolicies: {
|
indexPolicies: {
|
||||||
|
|
|
@ -18,7 +18,7 @@ table=acl-roles
|
||||||
index=id unique
|
index=id unique
|
||||||
indexer=github.com/hashicorp/go-memdb.UUIDFieldIndex Field=ID
|
indexer=github.com/hashicorp/go-memdb.UUIDFieldIndex Field=ID
|
||||||
index=name unique
|
index=name unique
|
||||||
indexer=github.com/hashicorp/go-memdb.StringFieldIndex Field=Name Lowercase=true
|
indexer=github.com/hashicorp/consul/agent/consul/state.indexerSingleWithPrefix readIndex=github.com/hashicorp/consul/agent/consul/state.indexFromQuery writeIndex=github.com/hashicorp/consul/agent/consul/state.indexNameFromACLRole prefixIndex=github.com/hashicorp/consul/agent/consul/state.prefixIndexFromQuery
|
||||||
index=policies allow-missing
|
index=policies allow-missing
|
||||||
indexer=github.com/hashicorp/consul/agent/consul/state.RolePoliciesIndex
|
indexer=github.com/hashicorp/consul/agent/consul/state.RolePoliciesIndex
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue