github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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  	unitSetter := common.NewStatusSetter(st, getCanModify)
    46  	unitGetter := common.NewStatusGetter(st, getCanModify)
    47  	serviceSetter := common.NewServiceStatusSetter(st, getCanModify)
    48  	serviceGetter := common.NewServiceStatusGetter(st, getCanModify)
    49  	agentSetter := common.NewStatusSetter(&unitAgentFinder{st}, getCanModify)
    50  	return &StatusAPI{
    51  		agentSetter:   agentSetter,
    52  		unitSetter:    unitSetter,
    53  		unitGetter:    unitGetter,
    54  		serviceSetter: serviceSetter,
    55  		serviceGetter: serviceGetter,
    56  		getCanModify:  getCanModify,
    57  	}
    58  }
    59  
    60  // SetStatus will set status for a entities passed in args. If the entity is
    61  // a Unit it will instead set status to its agent, to emulate backwards
    62  // compatibility.
    63  func (s *StatusAPI) SetStatus(args params.SetStatus) (params.ErrorResults, error) {
    64  	return s.SetAgentStatus(args)
    65  }
    66  
    67  // SetAgentStatus will set status for agents of Units passed in args, if one
    68  // of the args is not an Unit it will fail.
    69  func (s *StatusAPI) SetAgentStatus(args params.SetStatus) (params.ErrorResults, error) {
    70  	return s.agentSetter.SetStatus(args)
    71  }
    72  
    73  // SetUnitStatus sets status for all elements passed in args, the difference
    74  // with SetStatus is that if an entity is a Unit it will set its status instead
    75  // of its agent.
    76  func (s *StatusAPI) SetUnitStatus(args params.SetStatus) (params.ErrorResults, error) {
    77  	return s.unitSetter.SetStatus(args)
    78  }
    79  
    80  // SetServiceStatus sets the status for all the Services in args if the given Unit is
    81  // the leader.
    82  func (s *StatusAPI) SetServiceStatus(args params.SetStatus) (params.ErrorResults, error) {
    83  	return s.serviceSetter.SetStatus(args)
    84  }
    85  
    86  // UnitStatus returns the workload status information for the unit.
    87  func (s *StatusAPI) UnitStatus(args params.Entities) (params.StatusResults, error) {
    88  	return s.unitGetter.Status(args)
    89  }
    90  
    91  // ServiceStatus returns the status of the Services and its workloads
    92  // if the given unit is the leader.
    93  func (s *StatusAPI) ServiceStatus(args params.Entities) (params.ServiceStatusResults, error) {
    94  	return s.serviceGetter.Status(args)
    95  }