Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1)

Delta Between Two Patch Sets: state/apiserver/machiner/machiner.go

Issue 9937045: state/apiserver: Implement Machiner.Watch (Closed)
Left Patch Set: state/apiserver: Implement Machiner.Watch Created 11 years, 9 months ago
Right Patch Set: state/apiserver: Implement Machiner.Watch Created 11 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 // Copyright 2013 Canonical Ltd. 1 // Copyright 2013 Canonical Ltd.
2 // Licensed under the AGPLv3, see LICENCE file for details. 2 // Licensed under the AGPLv3, see LICENCE file for details.
3 3
4 package machiner 4 package machiner
5 5
6 import ( 6 import (
7 "launchpad.net/juju-core/state" 7 "launchpad.net/juju-core/state"
8 "launchpad.net/juju-core/state/api/params" 8 "launchpad.net/juju-core/state/api/params"
9 "launchpad.net/juju-core/state/apiserver/common" 9 "launchpad.net/juju-core/state/apiserver/common"
10 statewatcher "launchpad.net/juju-core/state/watcher"
11 ) 10 )
12 11
13 // Machiner implements the API used by the machiner worker. 12 // Machiner implements the API used by the machiner worker.
14 type Machiner struct { 13 type Machiner struct {
15 st *state.State 14 st *state.State
16 resourceRegistry common.ResourceRegistry 15 resourceRegistry common.ResourceRegistry
rog 2013/06/07 13:04:37 s/resourceRegistry/resources/ ?
dimitern 2013/06/07 13:56:38 I started liking longer, more descriptive names :)
17 auth common.Authorizer 16 auth common.Authorizer
18 } 17 }
19 18
20 // New creates a new instance of the Machiner facade. 19 // New creates a new instance of the Machiner facade.
21 func New(st *state.State, resourceRegistry common.ResourceRegistry, authorizer c ommon.Authorizer) *Machiner { 20 func New(st *state.State, resourceRegistry common.ResourceRegistry, authorizer c ommon.Authorizer) (*Machiner, error) {
rog 2013/06/07 13:04:37 s/resourceRegistry/resources/ ?
22 » return &Machiner{st, resourceRegistry, authorizer} 21 » if !authorizer.IsLoggedIn() {
22 » » return nil, common.ErrNotLoggedIn
23 » }
24 » if !authorizer.AuthMachineAgent() {
25 » » return nil, common.ErrPerm
26 » }
27 » return &Machiner{st, resourceRegistry, authorizer}, nil
23 } 28 }
24 29
25 // SetStatus sets the status of each given machine. 30 // SetStatus sets the status of each given machine.
26 func (m *Machiner) SetStatus(args params.MachinesSetStatus) (params.ErrorResults , error) { 31 func (m *Machiner) SetStatus(args params.MachinesSetStatus) (params.ErrorResults , error) {
27 result := params.ErrorResults{ 32 result := params.ErrorResults{
28 Errors: make([]*params.Error, len(args.Machines)), 33 Errors: make([]*params.Error, len(args.Machines)),
29 } 34 }
30 if len(args.Machines) == 0 { 35 if len(args.Machines) == 0 {
31 return result, nil 36 return result, nil
32 } 37 }
33 for i, arg := range args.Machines { 38 for i, arg := range args.Machines {
34 machine, err := m.st.Machine(arg.Id) 39 machine, err := m.st.Machine(arg.Id)
35 if err == nil { 40 if err == nil {
36 // Allow only for the owner agent. 41 // Allow only for the owner agent.
37 if !m.auth.AuthOwner(machine) { 42 if !m.auth.AuthOwner(machine) {
38 err = common.ErrPerm 43 err = common.ErrPerm
39 } else { 44 } else {
40 err = machine.SetStatus(arg.Status, arg.Info) 45 err = machine.SetStatus(arg.Status, arg.Info)
41 } 46 }
42 } 47 }
43 » » result.Errors[i] = common.ServerErrorToParams(err) 48 » » result.Errors[i] = common.ServerError(err)
44 } 49 }
45 return result, nil 50 return result, nil
46 } 51 }
47 52
48 // Watch starts an EntityWatcher for each given machine. 53 // Watch starts an EntityWatcher for each given machine.
49 func (m *Machiner) Watch(args params.Machines) (params.MachinesWatchResults, err or) { 54 func (m *Machiner) Watch(args params.Machines) (params.MachinesWatchResults, err or) {
50 result := params.MachinesWatchResults{ 55 result := params.MachinesWatchResults{
51 Results: make([]params.MachineWatchResult, len(args.Ids)), 56 Results: make([]params.MachineWatchResult, len(args.Ids)),
52 } 57 }
53 if len(args.Ids) == 0 { 58 if len(args.Ids) == 0 {
54 return result, nil 59 return result, nil
55 } 60 }
56 for i, id := range args.Ids { 61 for i, id := range args.Ids {
57 machine, err := m.st.Machine(id) 62 machine, err := m.st.Machine(id)
58 if err == nil { 63 if err == nil {
59 // Allow only for the owner agent. 64 // Allow only for the owner agent.
60 if !m.auth.AuthOwner(machine) { 65 if !m.auth.AuthOwner(machine) {
61 err = common.ErrPerm 66 err = common.ErrPerm
62 } else { 67 } else {
63 watcher := machine.Watch() 68 watcher := machine.Watch()
64 » » » » // To save an extra round-trip to call Next afte r Watch, we check 69 » » » » result.Results[i].EntityWatcherId = m.resourceRe gistry.Register(watcher)
65 » » » » // for initial changes.
fwereade 2013/06/06 17:04:38 I *think* I see what's going on here... the client
dimitern 2013/06/07 11:47:54 Will remove this from here and the client-side imp
rog 2013/06/07 13:04:37 There should be no such distinction between watche
66 » » » » if _, ok := <-watcher.Changes(); !ok {
67 » » » » » err = statewatcher.MustErr(watcher)
68 » » » » } else {
69 » » » » » result.Results[i].EntityWatcherId = m.re sourceRegistry.Register(watcher)
70 » » » » }
71 } 70 }
72 } 71 }
73 » » result.Results[i].Error = common.ServerErrorToParams(err) 72 » » result.Results[i].Error = common.ServerError(err)
74 } 73 }
75 return result, nil 74 return result, nil
76 } 75 }
77 76
78 // Life returns the lifecycle state of each given machine. 77 // Life returns the lifecycle state of each given machine.
79 func (m *Machiner) Life(args params.Machines) (params.MachinesLifeResults, error ) { 78 func (m *Machiner) Life(args params.Machines) (params.MachinesLifeResults, error ) {
80 result := params.MachinesLifeResults{ 79 result := params.MachinesLifeResults{
81 Machines: make([]params.MachineLifeResult, len(args.Ids)), 80 Machines: make([]params.MachineLifeResult, len(args.Ids)),
82 } 81 }
83 if len(args.Ids) == 0 { 82 if len(args.Ids) == 0 {
84 return result, nil 83 return result, nil
85 } 84 }
86 for i, id := range args.Ids { 85 for i, id := range args.Ids {
87 machine, err := m.st.Machine(id) 86 machine, err := m.st.Machine(id)
88 if err == nil { 87 if err == nil {
89 // Allow only for the owner agent. 88 // Allow only for the owner agent.
90 if !m.auth.AuthOwner(machine) { 89 if !m.auth.AuthOwner(machine) {
91 err = common.ErrPerm 90 err = common.ErrPerm
92 } else { 91 } else {
93 result.Machines[i].Life = params.Life(machine.Li fe().String()) 92 result.Machines[i].Life = params.Life(machine.Li fe().String())
94 } 93 }
95 } 94 }
96 » » result.Machines[i].Error = common.ServerErrorToParams(err) 95 » » result.Machines[i].Error = common.ServerError(err)
97 } 96 }
98 return result, nil 97 return result, nil
99 } 98 }
100 99
101 // EnsureDead changes the lifecycle of each given machine to Dead if 100 // EnsureDead changes the lifecycle of each given machine to Dead if
102 // it's Alive or Dying. It does nothing otherwise. 101 // it's Alive or Dying. It does nothing otherwise.
103 func (m *Machiner) EnsureDead(args params.Machines) (params.ErrorResults, error) { 102 func (m *Machiner) EnsureDead(args params.Machines) (params.ErrorResults, error) {
104 result := params.ErrorResults{ 103 result := params.ErrorResults{
105 Errors: make([]*params.Error, len(args.Ids)), 104 Errors: make([]*params.Error, len(args.Ids)),
106 } 105 }
107 if len(args.Ids) == 0 { 106 if len(args.Ids) == 0 {
108 return result, nil 107 return result, nil
109 } 108 }
110 for i, id := range args.Ids { 109 for i, id := range args.Ids {
111 machine, err := m.st.Machine(id) 110 machine, err := m.st.Machine(id)
112 if err == nil { 111 if err == nil {
113 // Allow only for the owner agent. 112 // Allow only for the owner agent.
114 if !m.auth.AuthOwner(machine) { 113 if !m.auth.AuthOwner(machine) {
115 err = common.ErrPerm 114 err = common.ErrPerm
116 } else { 115 } else {
117 err = machine.EnsureDead() 116 err = machine.EnsureDead()
118 } 117 }
119 } 118 }
120 » » result.Errors[i] = common.ServerErrorToParams(err) 119 » » result.Errors[i] = common.ServerError(err)
121 } 120 }
122 return result, nil 121 return result, nil
123 } 122 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b