github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/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  	"launchpad.net/juju-core/errors"
    10  	"launchpad.net/juju-core/state"
    11  	"launchpad.net/juju-core/state/api/params"
    12  	"launchpad.net/juju-core/state/apiserver/common"
    13  )
    14  
    15  // MachinerAPI implements the API used by the machiner worker.
    16  type MachinerAPI struct {
    17  	*common.LifeGetter
    18  	*common.StatusSetter
    19  	*common.DeadEnsurer
    20  	*common.AgentEntityWatcher
    21  
    22  	st           *state.State
    23  	auth         common.Authorizer
    24  	getCanModify common.GetAuthFunc
    25  	getCanRead   common.GetAuthFunc
    26  }
    27  
    28  // NewMachinerAPI creates a new instance of the Machiner API.
    29  func NewMachinerAPI(st *state.State, resources *common.Resources, authorizer common.Authorizer) (*MachinerAPI, error) {
    30  	if !authorizer.AuthMachineAgent() {
    31  		return nil, common.ErrPerm
    32  	}
    33  	getCanModify := func() (common.AuthFunc, error) {
    34  		return authorizer.AuthOwner, nil
    35  	}
    36  	getCanRead := func() (common.AuthFunc, error) {
    37  		return authorizer.AuthOwner, nil
    38  	}
    39  	return &MachinerAPI{
    40  		LifeGetter:         common.NewLifeGetter(st, getCanRead),
    41  		StatusSetter:       common.NewStatusSetter(st, getCanModify),
    42  		DeadEnsurer:        common.NewDeadEnsurer(st, getCanModify),
    43  		AgentEntityWatcher: common.NewAgentEntityWatcher(st, resources, getCanRead),
    44  		st:                 st,
    45  		auth:               authorizer,
    46  		getCanModify:       getCanModify,
    47  	}, nil
    48  }
    49  
    50  func (api *MachinerAPI) getMachine(tag string) (*state.Machine, error) {
    51  	entity, err := api.st.FindEntity(tag)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	return entity.(*state.Machine), nil
    56  }
    57  
    58  func (api *MachinerAPI) SetMachineAddresses(args params.SetMachinesAddresses) (params.ErrorResults, error) {
    59  	results := params.ErrorResults{
    60  		Results: make([]params.ErrorResult, len(args.MachineAddresses)),
    61  	}
    62  	canModify, err := api.getCanModify()
    63  	if err != nil {
    64  		return params.ErrorResults{}, err
    65  	}
    66  	for i, arg := range args.MachineAddresses {
    67  		err := common.ErrPerm
    68  		if canModify(arg.Tag) {
    69  			var m *state.Machine
    70  			m, err = api.getMachine(arg.Tag)
    71  			if err == nil {
    72  				err = m.SetMachineAddresses(arg.Addresses)
    73  			} else if errors.IsNotFoundError(err) {
    74  				err = common.ErrPerm
    75  			}
    76  		}
    77  		results.Results[i].Error = common.ServerError(err)
    78  	}
    79  	return results, nil
    80  }