1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
"launchpad.net/juju-core/state"
8
"launchpad.net/juju-core/state/api/params"
9
"launchpad.net/juju-core/state/apiserver/common"
10
"launchpad.net/juju-core/state/watcher"
13
// MachinerAPI implements the API used by the machiner worker.
14
type MachinerAPI struct {
16
resources *common.Resources
17
auth common.Authorizer
20
// NewMachinerAPI creates a new instance of the Machiner API.
21
func NewMachinerAPI(st *state.State, resources *common.Resources, authorizer common.Authorizer) (*MachinerAPI, error) {
22
if !authorizer.AuthMachineAgent() {
23
return nil, common.ErrPerm
25
return &MachinerAPI{st, resources, authorizer}, nil
28
// SetStatus sets the status of each given machine.
29
func (m *MachinerAPI) SetStatus(args params.MachinesSetStatus) (params.ErrorResults, error) {
30
result := params.ErrorResults{
31
Errors: make([]*params.Error, len(args.Machines)),
33
if len(args.Machines) == 0 {
36
for i, arg := range args.Machines {
38
if m.auth.AuthOwner(arg.Tag) {
39
var machine *state.Machine
40
machine, err = m.st.Machine(state.MachineIdFromTag(arg.Tag))
42
err = machine.SetStatus(arg.Status, arg.Info)
45
result.Errors[i] = common.ServerError(err)
50
// Watch starts an NotifyWatcher for each given machine.
51
func (m *MachinerAPI) Watch(args params.Entities) (params.NotifyWatchResults, error) {
52
result := params.NotifyWatchResults{
53
Results: make([]params.NotifyWatchResult, len(args.Entities)),
55
if len(args.Entities) == 0 {
58
for i, entity := range args.Entities {
60
if m.auth.AuthOwner(entity.Tag) {
61
var machine *state.Machine
62
machine, err = m.st.Machine(state.MachineIdFromTag(entity.Tag))
64
watch := machine.Watch()
65
// Consume the initial event. Technically, API
66
// calls to Watch 'transmit' the initial event
67
// in the Watch response. But NotifyWatchers
68
// have no state to transmit.
69
if _, ok := <-watch.Changes(); ok {
70
result.Results[i].NotifyWatcherId = m.resources.Register(watch)
72
err = watcher.MustErr(watch)
76
result.Results[i].Error = common.ServerError(err)
81
// Life returns the lifecycle state of each given machine.
82
func (m *MachinerAPI) Life(args params.Entities) (params.LifeResults, error) {
83
result := params.LifeResults{
84
Results: make([]params.LifeResult, len(args.Entities)),
86
if len(args.Entities) == 0 {
89
for i, entity := range args.Entities {
91
if m.auth.AuthOwner(entity.Tag) {
92
var machine *state.Machine
93
machine, err = m.st.Machine(state.MachineIdFromTag(entity.Tag))
95
result.Results[i].Life = params.Life(machine.Life().String())
98
result.Results[i].Error = common.ServerError(err)
103
// EnsureDead changes the lifecycle of each given machine to Dead if
104
// it's Alive or Dying. It does nothing otherwise.
105
func (m *MachinerAPI) EnsureDead(args params.Entities) (params.ErrorResults, error) {
106
result := params.ErrorResults{
107
Errors: make([]*params.Error, len(args.Entities)),
109
if len(args.Entities) == 0 {
112
for i, entity := range args.Entities {
113
err := common.ErrPerm
114
if m.auth.AuthOwner(entity.Tag) {
115
var machine *state.Machine
116
machine, err = m.st.Machine(state.MachineIdFromTag(entity.Tag))
118
err = machine.EnsureDead()
121
result.Errors[i] = common.ServerError(err)