state: share more indexer functions for config_entries

This commit is contained in:
Daniel Nephin 2021-03-29 13:17:26 -04:00
parent f303120f2d
commit d9dacb8388
2 changed files with 54 additions and 48 deletions

View File

@ -23,49 +23,6 @@ func indexFromConfigEntryKindName(arg interface{}) ([]byte, error) {
return b.Bytes(), nil return b.Bytes(), nil
} }
func indexFromConfigEntry(raw interface{}) ([]byte, error) {
c, ok := raw.(structs.ConfigEntry)
if !ok {
return nil, fmt.Errorf("type must be structs.ConfigEntry: %T", raw)
}
if c.GetName() == "" || c.GetKind() == "" {
return nil, errMissingValueForIndex
}
var b indexBuilder
b.String(strings.ToLower(c.GetKind()))
b.String(strings.ToLower(c.GetName()))
return b.Bytes(), nil
}
// indexKindFromConfigEntry indexes kinds, it is a shim for enterprise.
func indexKindFromConfigEntry(raw interface{}) ([]byte, error) {
c, ok := raw.(structs.ConfigEntry)
if !ok {
return nil, fmt.Errorf("type must be structs.ConfigEntry: %T", raw)
}
if c.GetKind() == "" {
return nil, errMissingValueForIndex
}
var b indexBuilder
b.String(strings.ToLower(c.GetKind()))
return b.Bytes(), nil
}
func indexFromConfigEntryKindQuery(raw interface{}) ([]byte, error) {
q, ok := raw.(ConfigEntryKindQuery)
if !ok {
return nil, fmt.Errorf("type must be structs.ConfigEntry: %T", raw)
}
var b indexBuilder
b.String(strings.ToLower(q.Kind))
return b.Bytes(), nil
}
func validateConfigEntryEnterprise(_ ReadTxn, _ structs.ConfigEntry) error { func validateConfigEntryEnterprise(_ ReadTxn, _ structs.ConfigEntry) error {
return nil return nil
} }

View File

@ -1,7 +1,12 @@
package state package state
import ( import (
"fmt"
"strings"
"github.com/hashicorp/go-memdb" "github.com/hashicorp/go-memdb"
"github.com/hashicorp/consul/agent/structs"
) )
const ( const (
@ -23,9 +28,9 @@ func configTableSchema() *memdb.TableSchema {
AllowMissing: false, AllowMissing: false,
Unique: true, Unique: true,
Indexer: indexerSingleWithPrefix{ Indexer: indexerSingleWithPrefix{
readIndex: readIndex(indexFromConfigEntryKindName), readIndex: indexFromConfigEntryKindName,
writeIndex: writeIndex(indexFromConfigEntry), writeIndex: indexFromConfigEntry,
prefixIndex: prefixIndex(indexFromConfigEntryKindName), prefixIndex: indexFromConfigEntryKindName,
}, },
}, },
indexKind: { indexKind: {
@ -33,8 +38,8 @@ func configTableSchema() *memdb.TableSchema {
AllowMissing: false, AllowMissing: false,
Unique: false, Unique: false,
Indexer: indexerSingle{ Indexer: indexerSingle{
readIndex: readIndex(indexFromConfigEntryKindQuery), readIndex: indexFromConfigEntryKindQuery,
writeIndex: writeIndex(indexKindFromConfigEntry), writeIndex: indexKindFromConfigEntry,
}, },
}, },
indexLink: { indexLink: {
@ -58,3 +63,47 @@ func configTableSchema() *memdb.TableSchema {
}, },
} }
} }
func indexFromConfigEntry(raw interface{}) ([]byte, error) {
c, ok := raw.(structs.ConfigEntry)
if !ok {
return nil, fmt.Errorf("type must be structs.ConfigEntry: %T", raw)
}
if c.GetName() == "" || c.GetKind() == "" {
return nil, errMissingValueForIndex
}
var b indexBuilder
b.String(strings.ToLower(c.GetKind()))
b.String(strings.ToLower(c.GetName()))
return b.Bytes(), nil
}
func indexFromConfigEntryKindQuery(raw interface{}) ([]byte, error) {
q, ok := raw.(ConfigEntryKindQuery)
if !ok {
return nil, fmt.Errorf("type must be ConfigEntryKindQuery: %T", raw)
}
var b indexBuilder
b.String(strings.ToLower(q.Kind))
return b.Bytes(), nil
}
// indexKindFromConfigEntry indexes kinds without a namespace for any config
// entries that span all namespaces.
func indexKindFromConfigEntry(raw interface{}) ([]byte, error) {
c, ok := raw.(structs.ConfigEntry)
if !ok {
return nil, fmt.Errorf("type must be structs.ConfigEntry: %T", raw)
}
if c.GetKind() == "" {
return nil, errMissingValueForIndex
}
var b indexBuilder
b.String(strings.ToLower(c.GetKind()))
return b.Bytes(), nil
}