| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 1 // The state package enables reading, observing, and changing | 1 // The state package enables reading, observing, and changing |
| 2 // the state stored in MongoDB of a whole environment | 2 // the state stored in MongoDB of a whole environment |
| 3 // managed by juju. | 3 // managed by juju. |
| 4 package mstate | 4 package mstate |
| 5 | 5 |
| 6 import ( | 6 import ( |
| 7 "fmt" | 7 "fmt" |
| 8 "launchpad.net/mgo" | 8 "launchpad.net/mgo" |
| 9 "launchpad.net/mgo/bson" | 9 "launchpad.net/mgo/bson" |
| 10 ) | 10 ) |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 return fmt.Errorf("can't remove machine %d", id) | 37 return fmt.Errorf("can't remove machine %d", id) |
| 38 } | 38 } |
| 39 return nil | 39 return nil |
| 40 } | 40 } |
| 41 | 41 |
| 42 // AllMachines returns all machines in the environment. | 42 // AllMachines returns all machines in the environment. |
| 43 func (s *State) AllMachines() (machines []*Machine, err error) { | 43 func (s *State) AllMachines() (machines []*Machine, err error) { |
| 44 mdocs := []machineDoc{} | 44 mdocs := []machineDoc{} |
| 45 err = s.machines.Find(nil).Select(bson.D{{"_id", 1}}).All(&mdocs) | 45 err = s.machines.Find(nil).Select(bson.D{{"_id", 1}}).All(&mdocs) |
| 46 if err != nil { | 46 if err != nil { |
| 47 » » return | 47 » » return nil, fmt.Errorf("can't get all machines: %v", err) |
|
niemeyer
2012/06/20 19:31:55
fmt.Errorf(<sensible message>, err)
aram
2012/06/21 08:28:41
Done.
| |
| 48 } | 48 } |
| 49 for _, v := range mdocs { | 49 for _, v := range mdocs { |
| 50 machines = append(machines, &Machine{st: s, id: v.Id}) | 50 machines = append(machines, &Machine{st: s, id: v.Id}) |
| 51 } | 51 } |
| 52 return | 52 return |
| 53 } | 53 } |
| 54 | 54 |
| 55 // Machine returns the machine with the given id. | 55 // Machine returns the machine with the given id. |
| 56 func (s *State) Machine(id int) (*Machine, error) { | 56 func (s *State) Machine(id int) (*Machine, error) { |
| 57 mdoc := &machineDoc{} | 57 mdoc := &machineDoc{} |
| 58 err := s.machines.Find(bson.D{{"_id", id}}).One(mdoc) | 58 err := s.machines.Find(bson.D{{"_id", id}}).One(mdoc) |
| 59 if err == mgo.NotFound { | |
| 60 return nil, fmt.Errorf("machine %d not found", id) | |
| 61 } | |
| 59 if err != nil { | 62 if err != nil { |
|
niemeyer
2012/06/20 19:31:55
if err == mgo.NotFound {
We'll need another branc
aram
2012/06/21 08:28:41
Done.
| |
| 60 » » return nil, fmt.Errorf("machine %d not found", id) | 63 » » return nil, fmt.Errorf("can't get machine %d: %v", id, err) |
| 61 } | 64 } |
| 62 return &Machine{st: s, id: mdoc.Id}, nil | 65 return &Machine{st: s, id: mdoc.Id}, nil |
| 63 } | 66 } |
| LEFT | RIGHT |