github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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/mongo"
    12  )
    13  
    14  // Mongo exposes MongoDB operations for use by the lease package.
    15  type Mongo interface {
    16  
    17  	// RunTransaction should probably delegate to a jujutxn.Runner's Run method.
    18  	RunTransaction(jujutxn.TransactionSource) error
    19  
    20  	// GetCollection should probably call the mongo.CollectionFromName func.
    21  	GetCollection(name string) (collection mongo.Collection, closer func())
    22  }
    23  
    24  // ClientConfig contains the resources and information required to create
    25  // a Client. Multiple clients can collaborate if they share a collection and
    26  // namespace, so long as they do not share ids; but within a collection,
    27  // clients for different namespaces will not interfere with one another,
    28  // regardless of id.
    29  type ClientConfig struct {
    30  
    31  	// Id uniquely identifies the client. Multiple clients with the same id
    32  	// running concurrently will cause undefined behaviour.
    33  	Id string
    34  
    35  	// Namespace identifies a group of clients which operate on the same data.
    36  	Namespace string
    37  
    38  	// Collection names the MongoDB collection in which lease data is stored.
    39  	Collection string
    40  
    41  	// Mongo exposes the mgo[/txn] capabilities required by a Client.
    42  	Mongo Mongo
    43  
    44  	// Clock exposes the wall-clock time to a Client.
    45  	Clock clock.Clock
    46  }
    47  
    48  // Validate returns an error if the supplied config is not valid.
    49  func (config ClientConfig) Validate() error {
    50  	if err := validateString(config.Id); err != nil {
    51  		return errors.Annotatef(err, "invalid id")
    52  	}
    53  	if err := validateString(config.Namespace); err != nil {
    54  		return errors.Annotatef(err, "invalid namespace")
    55  	}
    56  	if err := validateString(config.Collection); err != nil {
    57  		return errors.Annotatef(err, "invalid collection")
    58  	}
    59  	if config.Mongo == nil {
    60  		return errors.New("missing mongo")
    61  	}
    62  	if config.Clock == nil {
    63  		return errors.New("missing clock")
    64  	}
    65  	return nil
    66  }