github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/apiserver/common/setstatus.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common
     5  
     6  import (
     7  	"launchpad.net/juju-core/state"
     8  	"launchpad.net/juju-core/state/api/params"
     9  )
    10  
    11  // StatusSetter implements a common SetStatus method for use by
    12  // various facades.
    13  type StatusSetter struct {
    14  	st           state.EntityFinder
    15  	getCanModify GetAuthFunc
    16  }
    17  
    18  // NewStatusSetter returns a new StatusSetter. The GetAuthFunc will be
    19  // used on each invocation of SetStatus to determine current
    20  // permissions.
    21  func NewStatusSetter(st state.EntityFinder, getCanModify GetAuthFunc) *StatusSetter {
    22  	return &StatusSetter{
    23  		st:           st,
    24  		getCanModify: getCanModify,
    25  	}
    26  }
    27  
    28  func (s *StatusSetter) setEntityStatus(tag string, status params.Status, info string, data params.StatusData) error {
    29  	entity0, err := s.st.FindEntity(tag)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	entity, ok := entity0.(state.StatusSetter)
    34  	if !ok {
    35  		return NotSupportedError(tag, "setting status")
    36  	}
    37  	return entity.SetStatus(status, info, data)
    38  }
    39  
    40  // SetStatus sets the status of each given entity.
    41  func (s *StatusSetter) SetStatus(args params.SetStatus) (params.ErrorResults, error) {
    42  	result := params.ErrorResults{
    43  		Results: make([]params.ErrorResult, len(args.Entities)),
    44  	}
    45  	if len(args.Entities) == 0 {
    46  		return result, nil
    47  	}
    48  	canModify, err := s.getCanModify()
    49  	if err != nil {
    50  		return params.ErrorResults{}, err
    51  	}
    52  	for i, arg := range args.Entities {
    53  		err := ErrPerm
    54  		if canModify(arg.Tag) {
    55  			err = s.setEntityStatus(arg.Tag, arg.Status, arg.Info, arg.Data)
    56  		}
    57  		result.Results[i].Error = ServerError(err)
    58  	}
    59  	return result, nil
    60  }