/
engine.go
79 lines (64 loc) · 1.86 KB
/
engine.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
75
76
77
78
79
package storage
import (
"context"
"github.com/cockroachdb/cockroach-go/v2/testserver"
"go.infratographer.com/x/crdbx"
"go.infratographer.com/identity-api/internal/types"
)
// TransactionManager manages the state of sql transactions within a context
type TransactionManager interface {
BeginContext(context.Context) (context.Context, error)
CommitContext(context.Context) error
RollbackContext(context.Context) error
}
// Engine represents a storage engine.
type Engine interface {
types.IssuerService
types.UserInfoService
types.OAuthClientManager
TransactionManager
}
// EngineOption defines an initialization option for a storage engine.
type EngineOption func(*engine) error
// WithSeedData adds seed data to the storage engine.
func WithSeedData(data SeedData) EngineOption {
return func(e *engine) error {
return e.seedDatabase(context.Background(), data)
}
}
// WithMigrations runs migrations on the storage engine.
func WithMigrations() EngineOption {
return func(e *engine) error {
return runMigrations(e.db)
}
}
// NewEngine creates a new storage engine based on the given config.
func NewEngine(config crdbx.Config, opts ...EngineOption) (Engine, error) {
return newCRDBEngine(config, opts...)
}
func buildIssuerFromSeed(seed SeedIssuer) (types.Issuer, error) {
claimMappings, err := types.NewClaimsMapping(seed.ClaimMappings)
if err != nil {
return types.Issuer{}, err
}
out := types.Issuer{
OwnerID: seed.OwnerID,
ID: seed.ID,
Name: seed.Name,
URI: seed.URI,
JWKSURI: seed.JWKSURI,
ClaimMappings: claimMappings,
}
return out, nil
}
// InMemoryCRDB creates an in-memory CRDB test server.
func InMemoryCRDB() (testserver.TestServer, error) {
ts, err := testserver.NewTestServer()
if err != nil {
return nil, err
}
if err := ts.Start(); err != nil {
return nil, err
}
return ts, nil
}