github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  	"github.com/juju/errors"
     8  	jujutxn "github.com/juju/txn"
     9  	"github.com/juju/utils/clock"
    10  
    11  	"github.com/juju/juju/core/lease"
    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  // ClientConfig contains the resources and information required to create
    26  // a Client. Multiple clients can collaborate if they share a collection and
    27  // namespace, so long as they do not share ids; but within a collection,
    28  // clients for different namespaces will not interfere with one another,
    29  // regardless of id.
    30  type ClientConfig struct {
    31  
    32  	// Id uniquely identifies the client. Multiple clients with the same id
    33  	// running concurrently will cause undefined behaviour.
    34  	Id string
    35  
    36  	// Namespace identifies a group of clients which operate on the same data.
    37  	Namespace string
    38  
    39  	// Collection names the MongoDB collection in which lease data is stored.
    40  	Collection string
    41  
    42  	// Mongo exposes the mgo[/txn] capabilities required by a Client.
    43  	Mongo Mongo
    44  
    45  	// Clock exposes the wall-clock time to a Client.
    46  	Clock clock.Clock
    47  }
    48  
    49  // validate returns an error if the supplied config is not valid.
    50  func (config ClientConfig) validate() error {
    51  	if err := lease.ValidateString(config.Id); err != nil {
    52  		return errors.Annotatef(err, "invalid id")
    53  	}
    54  	if err := lease.ValidateString(config.Namespace); err != nil {
    55  		return errors.Annotatef(err, "invalid namespace")
    56  	}
    57  	if err := lease.ValidateString(config.Collection); err != nil {
    58  		return errors.Annotatef(err, "invalid collection")
    59  	}
    60  	if config.Mongo == nil {
    61  		return errors.New("missing mongo")
    62  	}
    63  	if config.Clock == nil {
    64  		return errors.New("missing clock")
    65  	}
    66  	return nil
    67  }