LEFT | RIGHT |
1 package api | 1 package api |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
5 "launchpad.net/juju-core/log" | 5 "launchpad.net/juju-core/log" |
6 "launchpad.net/juju-core/state/statecmd" | 6 "launchpad.net/juju-core/state/statecmd" |
7 "launchpad.net/tomb" | 7 "launchpad.net/tomb" |
8 "strings" | 8 "strings" |
9 "sync" | 9 "sync" |
10 ) | 10 ) |
11 | 11 |
12 // Machine represents the state of a machine. | 12 // Machine represents the state of a machine. |
13 type Machine struct { | 13 type Machine struct { |
14 st *State | 14 st *State |
15 id string | 15 id string |
16 » doc RpcMachine | 16 » doc RPCMachine |
17 } | 17 } |
18 | 18 |
19 // Client represents the client-accessible part of the state. | 19 // Client represents the client-accessible part of the state. |
20 type Client struct { | 20 type Client struct { |
21 st *State | 21 st *State |
22 } | 22 } |
23 | 23 |
24 // Client returns an object that can be used | 24 // Client returns an object that can be used |
25 // to access client-specific functionality. | 25 // to access client-specific functionality. |
26 func (st *State) Client() *Client { | 26 func (st *State) Client() *Client { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 if err := m.Refresh(); err != nil { | 126 if err := m.Refresh(); err != nil { |
127 return nil, err | 127 return nil, err |
128 } | 128 } |
129 return m, nil | 129 return m, nil |
130 } | 130 } |
131 | 131 |
132 // Unit represents the state of a service unit. | 132 // Unit represents the state of a service unit. |
133 type Unit struct { | 133 type Unit struct { |
134 st *State | 134 st *State |
135 name string | 135 name string |
136 » doc RpcUnit | 136 » doc RPCUnit |
137 } | 137 } |
138 | 138 |
139 // Unit returns a unit by name. | 139 // Unit returns a unit by name. |
140 func (st *State) Unit(name string) (*Unit, error) { | 140 func (st *State) Unit(name string) (*Unit, error) { |
141 u := &Unit{ | 141 u := &Unit{ |
142 st: st, | 142 st: st, |
143 name: name, | 143 name: name, |
144 } | 144 } |
145 if err := u.Refresh(); err != nil { | 145 if err := u.Refresh(); err != nil { |
146 return nil, err | 146 return nil, err |
147 } | 147 } |
148 return u, nil | 148 return u, nil |
149 } | 149 } |
150 | 150 |
151 // Login authenticates as the entity with the given name and password. | 151 // Login authenticates as the entity with the given name and password. |
152 // Subsequent requests on the state will act as that entity. | 152 // Subsequent requests on the state will act as that entity. |
153 // This method is usually called automatically by Open. | 153 // This method is usually called automatically by Open. |
154 func (st *State) Login(entityName, password string) error { | 154 func (st *State) Login(entityName, password string) error { |
155 » return st.call("Admin", "", "Login", &RpcCreds{ | 155 » return st.call("Admin", "", "Login", &RPCCreds{ |
156 EntityName: entityName, | 156 EntityName: entityName, |
157 Password: password, | 157 Password: password, |
158 }, nil) | 158 }, nil) |
159 } | 159 } |
160 | 160 |
161 // Id returns the machine id. | 161 // Id returns the machine id. |
162 func (m *Machine) Id() string { | 162 func (m *Machine) Id() string { |
163 return m.id | 163 return m.id |
164 } | 164 } |
165 | 165 |
(...skipping 22 matching lines...) Expand all Loading... |
188 } | 188 } |
189 | 189 |
190 // InstanceId returns the provider specific instance id for this machine | 190 // InstanceId returns the provider specific instance id for this machine |
191 // and whether it has been set. | 191 // and whether it has been set. |
192 func (m *Machine) InstanceId() (string, bool) { | 192 func (m *Machine) InstanceId() (string, bool) { |
193 return m.doc.InstanceId, m.doc.InstanceId != "" | 193 return m.doc.InstanceId, m.doc.InstanceId != "" |
194 } | 194 } |
195 | 195 |
196 // SetPassword sets the password for the machine's agent. | 196 // SetPassword sets the password for the machine's agent. |
197 func (m *Machine) SetPassword(password string) error { | 197 func (m *Machine) SetPassword(password string) error { |
198 » return m.st.call("Machine", m.id, "SetPassword", &RpcPassword{ | 198 » return m.st.call("Machine", m.id, "SetPassword", &RPCPassword{ |
199 Password: password, | 199 Password: password, |
200 }, nil) | 200 }, nil) |
201 } | 201 } |
202 | 202 |
203 func (m *Machine) Watch() *EntityWatcher { | 203 func (m *Machine) Watch() *EntityWatcher { |
204 return newEntityWatcher(m.st, "Machine", m.id) | 204 return newEntityWatcher(m.st, "Machine", m.id) |
205 } | 205 } |
206 | 206 |
207 type EntityWatcher struct { | 207 type EntityWatcher struct { |
208 tomb tomb.Tomb | 208 tomb tomb.Tomb |
(...skipping 14 matching lines...) Expand all Loading... |
223 go func() { | 223 go func() { |
224 defer w.tomb.Done() | 224 defer w.tomb.Done() |
225 defer close(w.out) | 225 defer close(w.out) |
226 defer w.wg.Wait() // Wait for watcher to be stopped. | 226 defer w.wg.Wait() // Wait for watcher to be stopped. |
227 w.tomb.Kill(w.loop()) | 227 w.tomb.Kill(w.loop()) |
228 }() | 228 }() |
229 return w | 229 return w |
230 } | 230 } |
231 | 231 |
232 func (w *EntityWatcher) loop() error { | 232 func (w *EntityWatcher) loop() error { |
233 » var id RpcEntityWatcherId | 233 » var id RPCEntityWatcherId |
234 if err := w.st.call(w.etype, w.eid, "Watch", nil, &id); err != nil { | 234 if err := w.st.call(w.etype, w.eid, "Watch", nil, &id); err != nil { |
235 return err | 235 return err |
236 } | 236 } |
237 callWatch := func(request string) error { | 237 callWatch := func(request string) error { |
238 return w.st.call("EntityWatcher", id.EntityWatcherId, request, n
il, nil) | 238 return w.st.call("EntityWatcher", id.EntityWatcherId, request, n
il, nil) |
239 } | 239 } |
240 w.wg.Add(1) | 240 w.wg.Add(1) |
241 go func() { | 241 go func() { |
242 // When the EntityWatcher has been stopped, we send a | 242 // When the EntityWatcher has been stopped, we send a |
243 // Stop request to the server, which will remove the | 243 // Stop request to the server, which will remove the |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 } | 294 } |
295 | 295 |
296 // Refresh refreshes the contents of the Unit from the underlying | 296 // Refresh refreshes the contents of the Unit from the underlying |
297 // state. TODO(rog) It returns a NotFoundError if the unit has been removed. | 297 // state. TODO(rog) It returns a NotFoundError if the unit has been removed. |
298 func (u *Unit) Refresh() error { | 298 func (u *Unit) Refresh() error { |
299 return u.st.call("Unit", u.name, "Get", nil, &u.doc) | 299 return u.st.call("Unit", u.name, "Get", nil, &u.doc) |
300 } | 300 } |
301 | 301 |
302 // SetPassword sets the password for the unit's agent. | 302 // SetPassword sets the password for the unit's agent. |
303 func (u *Unit) SetPassword(password string) error { | 303 func (u *Unit) SetPassword(password string) error { |
304 » return u.st.call("Unit", u.name, "SetPassword", &RpcPassword{ | 304 » return u.st.call("Unit", u.name, "SetPassword", &RPCPassword{ |
305 Password: password, | 305 Password: password, |
306 }, nil) | 306 }, nil) |
307 } | 307 } |
308 | 308 |
309 // UnitEntityName returns the entity name for the | 309 // UnitEntityName returns the entity name for the |
310 // unit with the given name. | 310 // unit with the given name. |
311 func UnitEntityName(unitName string) string { | 311 func UnitEntityName(unitName string) string { |
312 return "unit-" + strings.Replace(unitName, "/", "-", -1) | 312 return "unit-" + strings.Replace(unitName, "/", "-", -1) |
313 } | 313 } |
314 | 314 |
315 // EntityName returns a name identifying the unit that is safe to use | 315 // EntityName returns a name identifying the unit that is safe to use |
316 // as a file name. The returned name will be different from other | 316 // as a file name. The returned name will be different from other |
317 // EntityName values returned by any other entities from the same state. | 317 // EntityName values returned by any other entities from the same state. |
318 func (u *Unit) EntityName() string { | 318 func (u *Unit) EntityName() string { |
319 return UnitEntityName(u.name) | 319 return UnitEntityName(u.name) |
320 } | 320 } |
321 | 321 |
322 // DeployerName returns the entity name of the agent responsible for deploying | 322 // DeployerName returns the entity name of the agent responsible for deploying |
323 // the unit. If no such entity can be determined, false is returned. | 323 // the unit. If no such entity can be determined, false is returned. |
324 func (u *Unit) DeployerName() (string, bool) { | 324 func (u *Unit) DeployerName() (string, bool) { |
325 return u.doc.DeployerName, u.doc.DeployerName != "" | 325 return u.doc.DeployerName, u.doc.DeployerName != "" |
326 } | 326 } |
327 | 327 |
328 type RpcCreds struct { | 328 // RPCCreds is used in the API protocol. |
| 329 type RPCCreds struct { |
329 EntityName string | 330 EntityName string |
330 Password string | 331 Password string |
331 } | 332 } |
332 | 333 |
333 type RpcMachine struct { | 334 // RPCMachine is used in the API protocol. |
| 335 type RPCMachine struct { |
334 InstanceId string | 336 InstanceId string |
335 } | 337 } |
336 | 338 |
337 type RpcEntityWatcherId struct { | 339 // RPCEntityWatcherId is used in the API protocol. |
| 340 type RPCEntityWatcherId struct { |
338 EntityWatcherId string | 341 EntityWatcherId string |
339 } | 342 } |
340 | 343 |
341 type RpcPassword struct { | 344 // RPCPassword is used in the API protocol. |
| 345 type RPCPassword struct { |
342 Password string | 346 Password string |
343 } | 347 } |
344 | 348 |
345 type RpcUnit struct { | 349 // RPCUnit is used in the API protocol. |
| 350 type RPCUnit struct { |
346 DeployerName string | 351 DeployerName string |
347 // TODO(rog) other unit attributes. | 352 // TODO(rog) other unit attributes. |
348 } | 353 } |
349 | 354 |
350 type RpcUser struct { | 355 // RPCUser is used in the API protocol. |
| 356 type RPCUser struct { |
351 // This is a placeholder for any information | 357 // This is a placeholder for any information |
352 // that may be associated with a user in the | 358 // that may be associated with a user in the |
353 // future. | 359 // future. |
354 } | 360 } |
LEFT | RIGHT |