github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/bakerystorage/interface.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package bakerystorage 5 6 import ( 7 "time" 8 9 "github.com/go-macaroon-bakery/macaroon-bakery/v3/bakery" 10 "github.com/juju/errors" 11 12 "github.com/juju/juju/mongo" 13 ) 14 15 // Config contains configuration for creating bakery storage with New. 16 type Config struct { 17 // GetCollection returns a mongo.Collection and a function that 18 // will close any associated resources. 19 GetCollection func() (collection mongo.Collection, closer func()) 20 21 // GetStorage returns a bakery.Storage and a function that will close 22 // any associated resources. 23 GetStorage func(rootKeys *RootKeys, coll mongo.Collection, expireAfter time.Duration) (storage bakery.RootKeyStore) 24 } 25 26 // Validate validates the configuration. 27 func (c Config) Validate() error { 28 if c.GetCollection == nil { 29 return errors.NotValidf("nil GetCollection") 30 } 31 if c.GetStorage == nil { 32 return errors.NotValidf("nil GetStorage") 33 } 34 return nil 35 } 36 37 // ExpirableStorage extends bakery.Storage with the ExpireAfter method, 38 // to expire data added at the specified time. 39 type ExpirableStorage interface { 40 bakery.RootKeyStore 41 42 // ExpireAfter returns a new ExpirableStorage that will expire 43 // added items after the specified duration. 44 ExpireAfter(time.Duration) ExpirableStorage 45 } 46 47 // New returns an implementation of bakery.Storage 48 // that stores all items in MongoDB with an expiry 49 // time. 50 func New(config Config) (ExpirableStorage, error) { 51 if err := config.Validate(); err != nil { 52 return nil, errors.Annotate(err, "validating config") 53 } 54 return &storage{ 55 config: config, 56 rootKeys: NewRootKeys(5), 57 }, nil 58 }