github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/apiserver/uniter/status.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/names"
     9  
    10  	"github.com/juju/juju/apiserver/common"
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/juju/state"
    13  )
    14  
    15  // StatusAPI is the uniter part that deals with setting/getting
    16  // status from different entities, this particular separation from
    17  // base is because we have a shim to support unit/agent split.
    18  type StatusAPI struct {
    19  	agentSetter   *common.StatusSetter
    20  	unitSetter    *common.StatusSetter
    21  	unitGetter    *common.StatusGetter
    22  	serviceSetter *common.ServiceStatusSetter
    23  	serviceGetter *common.ServiceStatusGetter
    24  	getCanModify  common.GetAuthFunc
    25  }
    26  
    27  type unitAgentFinder struct {
    28  	state.EntityFinder
    29  }
    30  
    31  func (ua *unitAgentFinder) FindEntity(tag names.Tag) (state.Entity, error) {
    32  	_, ok := tag.(names.UnitTag)
    33  	if !ok {
    34  		return nil, errors.Errorf("unsupported tag %T", tag)
    35  	}
    36  	entity, err := ua.EntityFinder.FindEntity(tag)
    37  	if err != nil {
    38  		return nil, errors.Trace(err)
    39  	}
    40  	return entity.(*state.Unit).Agent(), nil
    41  }
    42  
    43  // NewStatusAPI creates a new server-side Status setter API facade.
    44  func NewStatusAPI(st *state.State, getCanModify common.GetAuthFunc) *StatusAPI {
    45  	// TODO(fwereade): so *all* of these have exactly the same auth
    46  	// characteristics? I think not.
    47  	unitSetter := common.NewStatusSetter(st, getCanModify)
    48  	unitGetter := common.NewStatusGetter(st, getCanModify)
    49  	serviceSetter := common.NewServiceStatusSetter(st, getCanModify)
    50  	serviceGetter := common.NewServiceStatusGetter(st, getCanModify)
    51  	agentSetter := common.NewStatusSetter(&unitAgentFinder{st}, getCanModify)
    52  	return &StatusAPI{
    53  		agentSetter:   agentSetter,
    54  		unitSetter:    unitSetter,
    55  		unitGetter:    unitGetter,
    56  		serviceSetter: serviceSetter,
    57  		serviceGetter: serviceGetter,
    58  		getCanModify:  getCanModify,
    59  	}
    60  }
    61  
    62  // SetStatus will set status for a entities passed in args. If the entity is
    63  // a Unit it will instead set status to its agent, to emulate backwards
    64  // compatibility.
    65  func (s *StatusAPI) SetStatus(args params.SetStatus) (params.ErrorResults, error) {
    66  	return s.SetAgentStatus(args)
    67  }
    68  
    69  // SetAgentStatus will set status for agents of Units passed in args, if one
    70  // of the args is not an Unit it will fail.
    71  func (s *StatusAPI) SetAgentStatus(args params.SetStatus) (params.ErrorResults, error) {
    72  	return s.agentSetter.SetStatus(args)
    73  }
    74  
    75  // SetUnitStatus sets status for all elements passed in args, the difference
    76  // with SetStatus is that if an entity is a Unit it will set its status instead
    77  // of its agent.
    78  func (s *StatusAPI) SetUnitStatus(args params.SetStatus) (params.ErrorResults, error) {
    79  	return s.unitSetter.SetStatus(args)
    80  }
    81  
    82  // SetServiceStatus sets the status for all the Services in args if the given Unit is
    83  // the leader.
    84  func (s *StatusAPI) SetServiceStatus(args params.SetStatus) (params.ErrorResults, error) {
    85  	return s.serviceSetter.SetStatus(args)
    86  }
    87  
    88  // UnitStatus returns the workload status information for the unit.
    89  func (s *StatusAPI) UnitStatus(args params.Entities) (params.StatusResults, error) {
    90  	return s.unitGetter.Status(args)
    91  }
    92  
    93  // ServiceStatus returns the status of the Services and its workloads
    94  // if the given unit is the leader.
    95  func (s *StatusAPI) ServiceStatus(args params.Entities) (params.ServiceStatusResults, error) {
    96  	return s.serviceGetter.Status(args)
    97  }