-
Notifications
You must be signed in to change notification settings - Fork 494
/
interface.go
58 lines (48 loc) · 1.62 KB
/
interface.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package bakerystorage
import (
"time"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/bakery"
"github.com/juju/errors"
"github.com/juju/juju/mongo"
)
// Config contains configuration for creating bakery storage with New.
type Config struct {
// GetCollection returns a mongo.Collection and a function that
// will close any associated resources.
GetCollection func() (collection mongo.Collection, closer func())
// GetStorage returns a bakery.Storage and a function that will close
// any associated resources.
GetStorage func(rootKeys *RootKeys, coll mongo.Collection, expireAfter time.Duration) (storage bakery.RootKeyStore)
}
// Validate validates the configuration.
func (c Config) Validate() error {
if c.GetCollection == nil {
return errors.NotValidf("nil GetCollection")
}
if c.GetStorage == nil {
return errors.NotValidf("nil GetStorage")
}
return nil
}
// ExpirableStorage extends bakery.Storage with the ExpireAfter method,
// to expire data added at the specified time.
type ExpirableStorage interface {
bakery.RootKeyStore
// ExpireAfter returns a new ExpirableStorage that will expire
// added items after the specified duration.
ExpireAfter(time.Duration) ExpirableStorage
}
// New returns an implementation of bakery.Storage
// that stores all items in MongoDB with an expiry
// time.
func New(config Config) (ExpirableStorage, error) {
if err := config.Validate(); err != nil {
return nil, errors.Annotate(err, "validating config")
}
return &storage{
config: config,
rootKeys: NewRootKeys(5),
}, nil
}