1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
4
// Package bakerystorage provides an implementation
5
// of the bakery Storage interface that uses MongoDB
8
// This is based on gopkg.in/macaroon-bakery.v1/bakery/mgostorage.
14
"github.com/juju/errors"
15
"gopkg.in/macaroon-bakery.v1/bakery"
18
"github.com/juju/juju/mongo"
21
// Config contains configuration for creating bakery storage with New.
23
// GetCollection returns a mongo.Collection and a function that
24
// will close any associated resources.
25
GetCollection func() (collection mongo.Collection, closer func())
28
// Validate validates the configuration.
29
func (c Config) Validate() error {
30
if c.GetCollection == nil {
31
return errors.NotValidf("nil GetCollection")
36
// ExpirableStorage extends bakery.Storage with the ExpireAt method,
37
// to expire data added at the specified time.
38
type ExpirableStorage interface {
41
// ExpireAt returns a new ExpirableStorage that will expire
42
// added items at the specified time.
43
ExpireAt(time.Time) ExpirableStorage
46
// New returns an implementation of bakery.Storage
47
// that stores all items in MongoDB with an expiry
49
func New(config Config) (ExpirableStorage, error) {
50
if err := config.Validate(); err != nil {
51
return nil, errors.Annotate(err, "validating config")
53
return &storage{config, time.Time{}}, nil
56
// MongoIndexes returns the indexes to apply to the MongoDB collection.
57
func MongoIndexes() []mgo.Index {
58
return []mgo.Index{expiryTimeIndex}