github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/state/apiserver/machine/machiner.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // The machiner package implements the API interface
     5  // used by the machiner worker.
     6  package machine
     7  
     8  import (
     9  	"github.com/juju/errors"
    10  
    11  	"github.com/juju/juju/state"
    12  	"github.com/juju/juju/state/api/params"
    13  	"github.com/juju/juju/state/apiserver/common"
    14  )
    15  
    16  // MachinerAPI implements the API used by the machiner worker.
    17  type MachinerAPI struct {
    18  	*common.LifeGetter
    19  	*common.StatusSetter
    20  	*common.DeadEnsurer
    21  	*common.AgentEntityWatcher
    22  	*common.APIAddresser
    23  
    24  	st           *state.State
    25  	auth         common.Authorizer
    26  	getCanModify common.GetAuthFunc
    27  	getCanRead   common.GetAuthFunc
    28  }
    29  
    30  // NewMachinerAPI creates a new instance of the Machiner API.
    31  func NewMachinerAPI(st *state.State, resources *common.Resources, authorizer common.Authorizer) (*MachinerAPI, error) {
    32  	if !authorizer.AuthMachineAgent() {
    33  		return nil, common.ErrPerm
    34  	}
    35  	getCanModify := func() (common.AuthFunc, error) {
    36  		return authorizer.AuthOwner, nil
    37  	}
    38  	getCanRead := func() (common.AuthFunc, error) {
    39  		return authorizer.AuthOwner, nil
    40  	}
    41  	return &MachinerAPI{
    42  		LifeGetter:         common.NewLifeGetter(st, getCanRead),
    43  		StatusSetter:       common.NewStatusSetter(st, getCanModify),
    44  		DeadEnsurer:        common.NewDeadEnsurer(st, getCanModify),
    45  		AgentEntityWatcher: common.NewAgentEntityWatcher(st, resources, getCanRead),
    46  		APIAddresser:       common.NewAPIAddresser(st, resources),
    47  		st:                 st,
    48  		auth:               authorizer,
    49  		getCanModify:       getCanModify,
    50  	}, nil
    51  }
    52  
    53  func (api *MachinerAPI) getMachine(tag string) (*state.Machine, error) {
    54  	entity, err := api.st.FindEntity(tag)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	return entity.(*state.Machine), nil
    59  }
    60  
    61  func (api *MachinerAPI) SetMachineAddresses(args params.SetMachinesAddresses) (params.ErrorResults, error) {
    62  	results := params.ErrorResults{
    63  		Results: make([]params.ErrorResult, len(args.MachineAddresses)),
    64  	}
    65  	canModify, err := api.getCanModify()
    66  	if err != nil {
    67  		return params.ErrorResults{}, err
    68  	}
    69  	for i, arg := range args.MachineAddresses {
    70  		err := common.ErrPerm
    71  		if canModify(arg.Tag) {
    72  			var m *state.Machine
    73  			m, err = api.getMachine(arg.Tag)
    74  			if err == nil {
    75  				err = m.SetMachineAddresses(arg.Addresses...)
    76  			} else if errors.IsNotFound(err) {
    77  				err = common.ErrPerm
    78  			}
    79  		}
    80  		results.Results[i].Error = common.ServerError(err)
    81  	}
    82  	return results, nil
    83  }