~rogpeppe/juju-core/039-init-agent-version

« back to all changes in this revision

Viewing changes to mstate/state.go

  • Committer: Frank Mueller
  • Author(s): Frank Mueller
  • Date: 2012-08-28 14:59:45 UTC
  • mfrom: (434.1.3 juju-core)
  • Revision ID: themue@gmail.com-20120828145945-w9xf5416s4j5yerl
mstate: integrated lifecycle into machine

Machine as the last entity now also provides the
lifecycle methods. Also reading, removing and
testing have been changed.

R=niemeyer
CC=
https://codereview.appspot.com/6496044

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
}
51
51
 
52
52
// RemoveMachine removes the machine with the the given id.
53
 
func (s *State) RemoveMachine(id int) error {
54
 
        sel := bson.D{{"_id", id}, {"life", Alive}}
55
 
        change := bson.D{{"$set", bson.D{{"life", Dying}}}}
56
 
        err := s.machines.Update(sel, change)
57
 
        if err != nil {
58
 
                return fmt.Errorf("cannot remove machine %d: %v", id, err)
 
53
func (s *State) RemoveMachine(id int) (err error) {
 
54
        defer trivial.ErrorContextf(&err, "cannot remove machine %d", id)
 
55
        m, err := s.Machine(id)
 
56
        if err != nil {
 
57
                return err
 
58
        }
 
59
        if m.doc.Life != Dead {
 
60
                panic(fmt.Errorf("machine %d is not dead", id))
 
61
        }
 
62
        sel := bson.D{
 
63
                {"_id", id},
 
64
                {"life", Dead},
 
65
        }
 
66
        err = s.machines.Remove(sel)
 
67
        if err != nil {
 
68
                return err
59
69
        }
60
70
        return nil
61
71
}
63
73
// AllMachines returns all machines in the environment.
64
74
func (s *State) AllMachines() (machines []*Machine, err error) {
65
75
        mdocs := []machineDoc{}
66
 
        sel := bson.D{{"life", Alive}}
 
76
        sel := bson.D{}
67
77
        err = s.machines.Find(sel).Select(bson.D{{"_id", 1}}).All(&mdocs)
68
78
        if err != nil {
69
79
                return nil, fmt.Errorf("cannot get all machines: %v", err)
77
87
// Machine returns the machine with the given id.
78
88
func (s *State) Machine(id int) (*Machine, error) {
79
89
        mdoc := &machineDoc{}
80
 
        sel := bson.D{{"_id", id}, {"life", Alive}}
 
90
        sel := bson.D{{"_id", id}}
81
91
        err := s.machines.Find(sel).One(mdoc)
82
92
        if err != nil {
83
93
                return nil, fmt.Errorf("cannot get machine %d: %v", id, err)