~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/state/bakerystorage/interface.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

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