forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bakerystorage.go
39 lines (33 loc) · 1.38 KB
/
bakerystorage.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"time"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/bakery"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/bakery/mgorootkeystore"
"github.com/juju/juju/mongo"
"github.com/juju/juju/state/bakerystorage"
)
// NewBakeryStorage returns a new bakery.Storage. By default, items
// added to the store are retained until deleted explicitly. The
// store's ExpireAfter method can be called to derive a new store that
// will expire items at the specified time.
func (st *State) NewBakeryStorage() (bakerystorage.ExpirableStorage, error) {
return bakerystorage.New(bakerystorage.Config{
GetCollection: func() (mongo.Collection, func()) {
return st.db().GetCollection(bakeryStorageItemsC)
},
GetStorage: func(rootKeys *mgorootkeystore.RootKeys, coll mongo.Collection, expireAfter time.Duration) bakery.RootKeyStore {
return rootKeys.NewStore(coll.Writeable().Underlying(), mgorootkeystore.Policy{
ExpiryDuration: expireAfter,
})
},
})
}
// NewBakeryConfig returns a new bakerystorage.BakeryConfig instance.
func (st *State) NewBakeryConfig() bakerystorage.BakeryConfig {
collectionGetter := func(collection string) (mongo.Collection, func()) {
return st.db().GetCollection(collection)
}
return bakerystorage.NewBakeryConfig(controllersC, collectionGetter)
}