Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2012, 2013 Canonical Ltd. | |
2 // Licensed under the AGPLv3, see LICENCE file for details. | |
3 | |
4 package api | |
5 | |
6 import ( | |
7 "fmt" | |
8 "launchpad.net/juju-core/state/api/params" | |
9 ) | |
10 | |
11 // Machiner provides access to the Machiner API facade. | |
12 type Machiner struct { | |
13 st *State | |
14 } | |
15 | |
16 // MachinerMachine provides access to state.Machine methods through | |
17 // the Machiner facade. | |
18 type MachinerMachine struct { | |
19 id string | |
20 life params.Life | |
21 machiner *Machiner | |
22 } | |
23 | |
24 // machineLife requests the lifecycle of the given machine from the server. | |
25 func (m *Machiner) machineLife(id string) (params.Life, error) { | |
26 var result params.MachinesLifeResults | |
27 args := params.Machines{ | |
28 Ids: []string{id}, | |
29 } | |
30 err := m.st.call("Machiner", "", "Life", args, &result) | |
31 if err != nil { | |
32 return "", err | |
33 } | |
34 if len(result.Machines) != 1 { | |
35 return "", fmt.Errorf("expected server response, got nothing") | |
fwereade
2013/06/04 11:59:09
expected one result, got <N>?
dimitern
2013/06/04 13:10:14
As agreed, will remove the client-side machiner fr
| |
36 } | |
37 if err := result.Machines[0].Error; err != nil { | |
38 return "", err | |
39 } | |
40 return result.Machines[0].Life, nil | |
41 } | |
42 | |
43 // Machine provides access to methods of a state.Machine through the facade. | |
44 func (m *Machiner) Machine(id string) (*MachinerMachine, error) { | |
45 life, err := m.machineLife(id) | |
46 if err != nil { | |
47 return nil, err | |
48 } | |
49 return &MachinerMachine{ | |
50 id: id, | |
51 life: life, | |
52 machiner: m, | |
53 }, nil | |
54 } | |
55 | |
56 // SetStatus changes the status of the machine. | |
57 func (mm *MachinerMachine) SetStatus(status params.Status, info string) error { | |
58 var result params.ErrorResults | |
59 args := params.MachinesSetStatus{ | |
60 Machines: []params.MachineSetStatus{ | |
61 {Id: mm.id, Status: status, Info: info}, | |
62 }, | |
63 } | |
64 err := mm.machiner.st.call("Machiner", "", "SetStatus", args, &result) | |
65 if err != nil { | |
66 return err | |
67 } | |
68 return result.Errors[0] | |
69 } | |
70 | |
71 // Refresh updates the cached local copy of the machine's data. | |
72 func (mm *MachinerMachine) Refresh() error { | |
73 life, err := mm.machiner.machineLife(mm.id) | |
74 if err != nil { | |
75 return err | |
76 } | |
77 mm.life = life | |
78 return nil | |
79 } | |
80 | |
81 // Id returns the machine id. | |
82 func (mm *MachinerMachine) Id() string { | |
83 return mm.id | |
84 } | |
85 | |
86 // Life returns the machine's lifecycle value. | |
87 func (mm *MachinerMachine) Life() params.Life { | |
88 return mm.life | |
89 } | |
OLD | NEW |