github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/state/lease/config.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package lease 5 6 import ( 7 "time" 8 9 "github.com/juju/errors" 10 jujutxn "github.com/juju/txn" 11 12 "github.com/juju/juju/mongo" 13 ) 14 15 // Mongo exposes MongoDB operations for use by the lease package. 16 type Mongo interface { 17 18 // RunTransaction should probably delegate to a jujutxn.Runner's Run method. 19 RunTransaction(jujutxn.TransactionSource) error 20 21 // GetCollection should probably call the mongo.CollectionFromName func. 22 GetCollection(name string) (collection mongo.Collection, closer func()) 23 } 24 25 // Clock exposes wall-clock time for use by and with the lease package. 26 type Clock interface { 27 28 // Now returns the current wall-clock time. 29 Now() time.Time 30 31 // Alarm returns a channel that will have the time sent on it at some point 32 // after the supplied time occurs. 33 Alarm(time.Time) <-chan time.Time 34 } 35 36 // ClientConfig contains the resources and information required to create 37 // a Client. Multiple clients can collaborate if they share a collection and 38 // namespace, so long as they do not share ids; but within a collection, 39 // clients for different namespaces will not interfere with one another, 40 // regardless of id. 41 type ClientConfig struct { 42 43 // Id uniquely identifies the client. Multiple clients with the same id 44 // running concurrently will cause undefined behaviour. 45 Id string 46 47 // Namespace identifies a group of clients which operate on the same data. 48 Namespace string 49 50 // Collection names the MongoDB collection in which lease data is stored. 51 Collection string 52 53 // Mongo exposes the mgo[/txn] capabilities required by a Client. 54 Mongo Mongo 55 56 // Clock exposes the wall-clock time to a Client. 57 Clock Clock 58 } 59 60 // Validate returns an error if the supplied config is not valid. 61 func (config ClientConfig) Validate() error { 62 if err := validateString(config.Id); err != nil { 63 return errors.Annotatef(err, "invalid id") 64 } 65 if err := validateString(config.Namespace); err != nil { 66 return errors.Annotatef(err, "invalid namespace") 67 } 68 if err := validateString(config.Collection); err != nil { 69 return errors.Annotatef(err, "invalid collection") 70 } 71 if config.Mongo == nil { 72 return errors.New("missing mongo") 73 } 74 if config.Clock == nil { 75 return errors.New("missing clock") 76 } 77 return nil 78 }