github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/state/singular.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/errors"
    10  	"gopkg.in/juju/names.v2"
    11  
    12  	"github.com/juju/juju/core/lease"
    13  )
    14  
    15  // singularSecretary implements lease.Secretary to restrict claims to a single
    16  // lease (named for the environ uuid), holdable only by machine-tag strings.
    17  //
    18  // It would be nicer to have a single controller-level component managing all
    19  // singular leases for all environments -- and thus be able to validate that
    20  // proposed holders really are env managers -- but the complexity of threading
    21  // data from *two* states through a single api connection is excessive by
    22  // comparison.
    23  type singularSecretary struct {
    24  	uuid string
    25  }
    26  
    27  // CheckLease is part of the lease.Secretary interface.
    28  func (s singularSecretary) CheckLease(name string) error {
    29  	if name != s.uuid {
    30  		return errors.New("expected environ UUID")
    31  	}
    32  	return nil
    33  }
    34  
    35  // CheckHolder is part of the lease.Secretary interface.
    36  func (s singularSecretary) CheckHolder(name string) error {
    37  	if _, err := names.ParseMachineTag(name); err != nil {
    38  		return errors.New("expected machine tag")
    39  	}
    40  	return nil
    41  }
    42  
    43  // CheckDuration is part of the lease.Secretary interface.
    44  func (s singularSecretary) CheckDuration(duration time.Duration) error {
    45  	if duration <= 0 {
    46  		return errors.NewNotValid(nil, "non-positive")
    47  	}
    48  	return nil
    49  }
    50  
    51  // SingularClaimer returns a lease.Claimer representing the exclusive right to
    52  // manage the environment.
    53  func (st *State) SingularClaimer() lease.Claimer {
    54  	return st.workers.SingularManager()
    55  }