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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
package state
import (
memdb "github.com/hashicorp/go-memdb"
"github.com/pkg/errors"
)
type collection struct {
db *memdb.MemDB
}
// KongState is an in-memory database representation
// of Kong's configuration.
type KongState struct {
common collection
Services *ServicesCollection
Routes *RoutesCollection
Upstreams *UpstreamsCollection
Targets *TargetsCollection
Certificates *CertificatesCollection
SNIs *SNIsCollection
CACertificates *CACertificatesCollection
Plugins *PluginsCollection
Consumers *ConsumersCollection
KeyAuths *KeyAuthsCollection
HMACAuths *HMACAuthsCollection
JWTAuths *JWTAuthsCollection
BasicAuths *BasicAuthsCollection
ACLGroups *ACLGroupsCollection
Oauth2Creds *Oauth2CredsCollection
MTLSAuths *MTLSAuthsCollection
}
// NewKongState creates a new in-memory KongState.
func NewKongState() (*KongState, error) {
// TODO FIXME clean up the mess
keyAuthTemp := newKeyAuthsCollection(collection{})
hmacAuthTemp := newHMACAuthsCollection(collection{})
basicAuthTemp := newBasicAuthsCollection(collection{})
jwtAuthTemp := newJWTAuthsCollection(collection{})
oauth2CredsTemp := newOauth2CredsCollection(collection{})
mtlsAuthTemp := newMTLSAuthsCollection(collection{})
var schema = &memdb.DBSchema{
Tables: map[string]*memdb.TableSchema{
serviceTableName: serviceTableSchema,
routeTableName: routeTableSchema,
upstreamTableName: upstreamTableSchema,
targetTableName: targetTableSchema,
certificateTableName: certificateTableSchema,
sniTableName: sniTableSchema,
caCertTableName: caCertTableSchema,
pluginTableName: pluginTableSchema,
consumerTableName: consumerTableSchema,
keyAuthTemp.TableName(): keyAuthTemp.Schema(),
hmacAuthTemp.TableName(): hmacAuthTemp.Schema(),
basicAuthTemp.TableName(): basicAuthTemp.Schema(),
jwtAuthTemp.TableName(): jwtAuthTemp.Schema(),
oauth2CredsTemp.TableName(): oauth2CredsTemp.Schema(),
mtlsAuthTemp.TableName(): mtlsAuthTemp.Schema(),
aclGroupTableName: aclGroupTableSchema,
},
}
memDB, err := memdb.NewMemDB(schema)
if err != nil {
return nil, errors.Wrap(err, "creating new ServiceCollection")
}
var state KongState
state.common = collection{
db: memDB,
}
state.Services = (*ServicesCollection)(&state.common)
state.Routes = (*RoutesCollection)(&state.common)
state.Upstreams = (*UpstreamsCollection)(&state.common)
state.Targets = (*TargetsCollection)(&state.common)
state.Certificates = (*CertificatesCollection)(&state.common)
state.SNIs = (*SNIsCollection)(&state.common)
state.CACertificates = (*CACertificatesCollection)(&state.common)
state.Plugins = (*PluginsCollection)(&state.common)
state.Consumers = (*ConsumersCollection)(&state.common)
state.KeyAuths = newKeyAuthsCollection(state.common)
state.HMACAuths = newHMACAuthsCollection(state.common)
state.BasicAuths = newBasicAuthsCollection(state.common)
state.JWTAuths = newJWTAuthsCollection(state.common)
state.Oauth2Creds = newOauth2CredsCollection(state.common)
state.MTLSAuths = newMTLSAuthsCollection(state.common)
state.ACLGroups = (*ACLGroupsCollection)(&state.common)
return &state, nil
}
|