~ubuntu-branches/ubuntu/saucy/juju-core/saucy-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/state/apiserver/machine/machiner.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-07-11 17:18:27 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130711171827-vjqkg40r0dlf7ys2
Tags: 1.11.2-0ubuntu1
* New upstream release.
* Make juju-core the default juju (LP: #1190634):
  - d/control: Add virtual package juju -> juju-core.
  - d/juju-core.postinst.in: Bump priority of alternatives over that of
    python juju packages.
* Enable for all architectures (LP: #1172505):
  - d/control: Version BD on golang-go to >= 2:1.1.1 to ensure CGO
    support for non-x86 archs, make juju-core Arch: any.
  - d/README.source: Dropped - no longer required.
* d/watch: Updated for new upstream tarball naming.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package machine
 
5
 
 
6
import (
 
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"
 
11
)
 
12
 
 
13
// MachinerAPI implements the API used by the machiner worker.
 
14
type MachinerAPI struct {
 
15
        st        *state.State
 
16
        resources *common.Resources
 
17
        auth      common.Authorizer
 
18
}
 
19
 
 
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
 
24
        }
 
25
        return &MachinerAPI{st, resources, authorizer}, nil
 
26
}
 
27
 
 
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)),
 
32
        }
 
33
        if len(args.Machines) == 0 {
 
34
                return result, nil
 
35
        }
 
36
        for i, arg := range args.Machines {
 
37
                err := common.ErrPerm
 
38
                if m.auth.AuthOwner(arg.Tag) {
 
39
                        var machine *state.Machine
 
40
                        machine, err = m.st.Machine(state.MachineIdFromTag(arg.Tag))
 
41
                        if err == nil {
 
42
                                err = machine.SetStatus(arg.Status, arg.Info)
 
43
                        }
 
44
                }
 
45
                result.Errors[i] = common.ServerError(err)
 
46
        }
 
47
        return result, nil
 
48
}
 
49
 
 
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)),
 
54
        }
 
55
        if len(args.Entities) == 0 {
 
56
                return result, nil
 
57
        }
 
58
        for i, entity := range args.Entities {
 
59
                err := common.ErrPerm
 
60
                if m.auth.AuthOwner(entity.Tag) {
 
61
                        var machine *state.Machine
 
62
                        machine, err = m.st.Machine(state.MachineIdFromTag(entity.Tag))
 
63
                        if err == nil {
 
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)
 
71
                                } else {
 
72
                                        err = watcher.MustErr(watch)
 
73
                                }
 
74
                        }
 
75
                }
 
76
                result.Results[i].Error = common.ServerError(err)
 
77
        }
 
78
        return result, nil
 
79
}
 
80
 
 
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)),
 
85
        }
 
86
        if len(args.Entities) == 0 {
 
87
                return result, nil
 
88
        }
 
89
        for i, entity := range args.Entities {
 
90
                err := common.ErrPerm
 
91
                if m.auth.AuthOwner(entity.Tag) {
 
92
                        var machine *state.Machine
 
93
                        machine, err = m.st.Machine(state.MachineIdFromTag(entity.Tag))
 
94
                        if err == nil {
 
95
                                result.Results[i].Life = params.Life(machine.Life().String())
 
96
                        }
 
97
                }
 
98
                result.Results[i].Error = common.ServerError(err)
 
99
        }
 
100
        return result, nil
 
101
}
 
102
 
 
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)),
 
108
        }
 
109
        if len(args.Entities) == 0 {
 
110
                return result, nil
 
111
        }
 
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))
 
117
                        if err == nil {
 
118
                                err = machine.EnsureDead()
 
119
                        }
 
120
                }
 
121
                result.Errors[i] = common.ServerError(err)
 
122
        }
 
123
        return result, nil
 
124
}