LEFT | RIGHT |
1 // Dummy is a bare minimum provider that doesn't actually do anything. | 1 // Dummy is a bare minimum provider that doesn't actually do anything. |
2 // The configuration requires a single value, "basename", which | 2 // The configuration requires a single value, "basename", which |
3 // is used as the base name of any machines that are "created". | 3 // is used as the base name of any machines that are "created". |
4 // It has no persistent state. | 4 // It has no persistent state. |
5 // | 5 // |
6 // Note that this file contains no tests as such - it is | 6 // Note that this file contains no tests as such - it is |
7 // just used by the testing code. | 7 // just used by the testing code. |
8 package juju_test | 8 package juju_test |
9 | 9 |
10 import ( | 10 import ( |
11 "fmt" | 11 "fmt" |
12 "launchpad.net/juju/go/juju" | 12 "launchpad.net/juju/go/juju" |
13 "launchpad.net/juju/go/schema" | 13 "launchpad.net/juju/go/schema" |
14 "sync" | 14 "sync" |
15 ) | 15 ) |
16 | 16 |
17 func init() { | 17 func init() { |
18 juju.RegisterProvider("dummy", dummyProvider{}) | 18 juju.RegisterProvider("dummy", dummyProvider{}) |
19 } | 19 } |
20 | 20 |
21 type dummyInstance struct { | 21 type dummyInstance struct { |
22 name string | 22 name string |
23 id string | |
24 } | 23 } |
25 | 24 |
26 func (m *dummyInstance) Id() string { | 25 func (m *dummyInstance) Id() string { |
27 » return fmt.Sprintf("dummy-%d", m.id) | 26 » return fmt.Sprintf("dummy-%s", m.name) |
28 } | 27 } |
29 | 28 |
30 func (m *dummyInstance) DNSName() string { | 29 func (m *dummyInstance) DNSName() string { |
31 return m.name | 30 return m.name |
32 } | 31 } |
33 | 32 |
34 type dummyProvider struct{} | 33 type dummyProvider struct{} |
35 | 34 |
36 func (dummyProvider) ConfigChecker() schema.Checker { | 35 func (dummyProvider) ConfigChecker() schema.Checker { |
37 return schema.FieldMap( | 36 return schema.FieldMap( |
38 schema.Fields{ | 37 schema.Fields{ |
39 "type": schema.Const("dummy"), | 38 "type": schema.Const("dummy"), |
40 "basename": schema.String(), | 39 "basename": schema.String(), |
41 }, | 40 }, |
42 nil, | 41 nil, |
43 ) | 42 ) |
44 } | 43 } |
45 | 44 |
46 type dummyEnviron struct { | 45 type dummyEnviron struct { |
47 mu sync.Mutex | 46 mu sync.Mutex |
48 baseName string | 47 baseName string |
49 » n int // machine count | 48 » n int // instance count |
50 » machines map[string]*dummyInstance | 49 |
| 50 » instances map[string]*dummyInstance |
51 } | 51 } |
52 | 52 |
53 func (dummyProvider) Open(name string, attributes interface{}) (e juju.Environ,
err error) { | 53 func (dummyProvider) Open(name string, attributes interface{}) (e juju.Environ,
err error) { |
54 cfg := attributes.(schema.MapType) | 54 cfg := attributes.(schema.MapType) |
55 return &dummyEnviron{ | 55 return &dummyEnviron{ |
56 » » baseName: cfg["basename"].(string), | 56 » » baseName: cfg["basename"].(string), |
57 » » machines: make(map[string]*dummyInstance), | 57 » » instances: make(map[string]*dummyInstance), |
58 }, nil | 58 }, nil |
59 } | 59 } |
60 | 60 |
61 func (*dummyEnviron) Bootstrap() error { | 61 func (*dummyEnviron) Bootstrap() error { |
62 return nil | 62 return nil |
63 } | 63 } |
64 | 64 |
65 func (*dummyEnviron) Destroy() error { | 65 func (*dummyEnviron) Destroy() error { |
66 return nil | 66 return nil |
67 } | 67 } |
68 | 68 |
69 func (c *dummyEnviron) StartInstance(id string) (juju.Instance, error) { | 69 func (e *dummyEnviron) StartInstance(id int) (juju.Instance, error) { |
70 » c.mu.Lock() | 70 » e.mu.Lock() |
71 » defer c.mu.Unlock() | 71 » defer e.mu.Unlock() |
72 » m := &dummyInstance{ | 72 » i := &dummyInstance{ |
73 » » name: fmt.Sprintf("%s-%d", c.baseName, c.n), | 73 » » name: fmt.Sprintf("%s-%d", e.baseName, c.n), |
74 » » id: id, | |
75 } | 74 } |
76 » c.machines[m.id] = m | 75 » e.instances[i.name] = i |
77 » c.n++ | 76 » e.n++ |
78 » return m, nil | 77 » return i, nil |
79 } | 78 } |
80 | 79 |
81 func (c *dummyEnviron) StopInstances(ms []juju.Instance) error { | 80 func (e *dummyEnviron) StopInstances(is []juju.Instance) error { |
82 » c.mu.Lock() | 81 » e.mu.Lock() |
83 » defer c.mu.Unlock() | 82 » defer e.mu.Unlock() |
84 » for _, m := range ms { | 83 » for _, i := range is { |
85 » » delete(c.machines, m.(*dummyInstance).id) | 84 » » delete(e.instances, i.(*dummyInstance).name) |
86 } | 85 } |
87 return nil | 86 return nil |
88 } | 87 } |
89 | 88 |
90 func (c *dummyEnviron) Instances() ([]juju.Instance, error) { | 89 func (e *dummyEnviron) Instances() ([]juju.Instance, error) { |
91 » c.mu.Lock() | 90 » e.mu.Lock() |
92 » defer c.mu.Unlock() | 91 » defer e.mu.Unlock() |
93 » var ms []juju.Instance | 92 » var is []juju.Instance |
94 » for _, m := range c.machines { | 93 » for _, i := range e.instances { |
95 » » ms = append(ms, m) | 94 » » is = append(is, i) |
96 } | 95 } |
97 » return ms, nil | 96 » return is, nil |
98 } | 97 } |
LEFT | RIGHT |