LEFT | RIGHT |
1 // launchpad.net/juju/state | |
2 // | |
3 // Copyright (c) 2011-2012 Canonical Ltd. | |
4 | |
5 package state | |
6 | 1 |
7 import ( | 2 import ( |
8 "fmt" | 3 "fmt" |
| 4 "launchpad.net/gozk/zookeeper" |
9 "strconv" | 5 "strconv" |
10 "strings" | 6 "strings" |
11 ) | 7 ) |
12 | 8 |
13 // Machine represents the state of a machine. | 9 // Machine represents the state of a machine. |
14 type Machine struct { | 10 type Machine struct { |
15 » st *State | 11 » zk *zookeeper.Conn |
16 key string | 12 key string |
17 } | 13 } |
18 | 14 |
19 // Id returns the machine id. | |
20 func (m *Machine) Id() int { | |
21 return machineId(m.key) | |
22 } | |
23 | |
24 // zkKey returns the ZooKeeper key of the machine. | |
25 func (m *Machine) zkKey() string { | |
26 return m.key | |
27 } | |
28 | |
29 // zkPath returns the ZooKeeper base path for the machine. | |
30 func (m *Machine) zkPath() string { | |
31 return fmt.Sprintf("/machines/%s", m.key) | |
32 } | |
33 | |
34 // zkAgentPath returns the ZooKeeper path for the machine agent. | |
35 func (m *Machine) zkAgentPath() string { | |
36 return fmt.Sprintf("/machines/%s/agent", m.key) | |
37 } | |
38 | |
39 // machineId returns the machine id corresponding to machineKey. | |
40 func machineId(machineKey string) (id int) { | |
41 if machineKey == "" { | |
42 panic("machineId: empty machine key") | |
43 } | |
44 i := strings.Index(machineKey, "-") | |
45 var id64 int64 | |
46 var err error | |
47 if i >= 0 { | |
48 id64, err = strconv.ParseInt(machineKey[i+1:], 10, 32) | |
49 } | |
50 if i < 0 || err != nil { | |
51 panic("machineId: invalid machine key: " + machineKey) | |
52 } | |
53 return int(id64) | |
54 } | |
LEFT | RIGHT |