| LEFT | RIGHT |
| 1 package mstate | 1 package mstate |
| 2 | 2 |
| 3 import ( | 3 import ( |
| 4 "errors" | 4 "errors" |
| 5 "fmt" | 5 "fmt" |
| 6 "labix.org/v2/mgo/bson" | 6 "labix.org/v2/mgo/bson" |
| 7 "launchpad.net/juju-core/charm" | 7 "launchpad.net/juju-core/charm" |
| 8 "launchpad.net/juju-core/mstate/life" | |
| 9 "strconv" | 8 "strconv" |
| 10 ) | 9 ) |
| 11 | 10 |
| 12 // Service represents the state of a service. | 11 // Service represents the state of a service. |
| 13 type Service struct { | 12 type Service struct { |
| 14 st *State | 13 st *State |
| 15 name string | 14 name string |
| 16 } | 15 } |
| 17 | 16 |
| 18 // serviceDoc represents the internal state of a service in MongoDB. | 17 // serviceDoc represents the internal state of a service in MongoDB. |
| 19 type serviceDoc struct { | 18 type serviceDoc struct { |
| 20 » Name string `bson:"_id"` | 19 » Name string `bson:"_id"` |
| 21 » CharmURL *charm.URL | 20 » CharmURL *charm.URL |
| 22 » LifeCycle life.Cycle | 21 » Life Life |
| 23 } | 22 } |
| 24 | 23 |
| 25 // Name returns the service name. | 24 // Name returns the service name. |
| 26 func (s *Service) Name() string { | 25 func (s *Service) Name() string { |
| 27 return s.name | 26 return s.name |
| 28 } | 27 } |
| 29 | 28 |
| 30 // CharmURL returns the charm URL this service is supposed to use. | 29 // CharmURL returns the charm URL this service is supposed to use. |
| 31 func (s *Service) CharmURL() (url *charm.URL, err error) { | 30 func (s *Service) CharmURL() (url *charm.URL, err error) { |
| 32 sdoc := &serviceDoc{} | 31 sdoc := &serviceDoc{} |
| 33 » sel := bson.D{{"_id", s.name}, {"lifecycle", life.Alive}} | 32 » sel := bson.D{{"_id", s.name}, {"life", Alive}} |
| 34 err = s.st.services.Find(sel).One(sdoc) | 33 err = s.st.services.Find(sel).One(sdoc) |
| 35 if err != nil { | 34 if err != nil { |
| 36 return nil, fmt.Errorf("can't get the charm URL of service %q: %
v", s, err) | 35 return nil, fmt.Errorf("can't get the charm URL of service %q: %
v", s, err) |
| 37 } | 36 } |
| 38 return sdoc.CharmURL, nil | 37 return sdoc.CharmURL, nil |
| 39 } | 38 } |
| 40 | 39 |
| 41 // SetCharmURL changes the charm URL for the service. | 40 // SetCharmURL changes the charm URL for the service. |
| 42 func (s *Service) SetCharmURL(url *charm.URL) (err error) { | 41 func (s *Service) SetCharmURL(url *charm.URL) (err error) { |
| 43 change := bson.D{{"$set", bson.D{{"charmurl", url}}}} | 42 change := bson.D{{"$set", bson.D{{"charmurl", url}}}} |
| (...skipping 27 matching lines...) Expand all Loading... |
| 71 name := s.name + "/" + strconv.Itoa(id) | 70 name := s.name + "/" + strconv.Itoa(id) |
| 72 return name, nil | 71 return name, nil |
| 73 } | 72 } |
| 74 | 73 |
| 75 // addUnit adds the named unit, which is part of unitSet. | 74 // addUnit adds the named unit, which is part of unitSet. |
| 76 func (s *Service) addUnit(name string, principal string) (*Unit, error) { | 75 func (s *Service) addUnit(name string, principal string) (*Unit, error) { |
| 77 udoc := unitDoc{ | 76 udoc := unitDoc{ |
| 78 Name: name, | 77 Name: name, |
| 79 Service: s.name, | 78 Service: s.name, |
| 80 Principal: principal, | 79 Principal: principal, |
| 81 » » LifeCycle: life.Alive, | 80 » » Life: Alive, |
| 82 } | 81 } |
| 83 err := s.st.units.Insert(udoc) | 82 err := s.st.units.Insert(udoc) |
| 84 if err != nil { | 83 if err != nil { |
| 85 return nil, fmt.Errorf("can't add unit to service %q", s) | 84 return nil, fmt.Errorf("can't add unit to service %q", s) |
| 86 } | 85 } |
| 87 return newUnit(s.st, &udoc), nil | 86 return newUnit(s.st, &udoc), nil |
| 88 } | 87 } |
| 89 | 88 |
| 90 // AddUnit adds a new principal unit to the service. | 89 // AddUnit adds a new principal unit to the service. |
| 91 func (s *Service) AddUnit() (unit *Unit, err error) { | 90 func (s *Service) AddUnit() (unit *Unit, err error) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 117 return nil, errors.New("a subordinate unit must be added to a pr
incipal unit") | 116 return nil, errors.New("a subordinate unit must be added to a pr
incipal unit") |
| 118 } | 117 } |
| 119 name, err := s.newUnitName() | 118 name, err := s.newUnitName() |
| 120 if err != nil { | 119 if err != nil { |
| 121 return nil, fmt.Errorf("can't add unit to service %q: %v", err) | 120 return nil, fmt.Errorf("can't add unit to service %q: %v", err) |
| 122 } | 121 } |
| 123 return s.addUnit(name, principal.Name()) | 122 return s.addUnit(name, principal.Name()) |
| 124 } | 123 } |
| 125 | 124 |
| 126 // RemoveUnit removes the given unit from s. | 125 // RemoveUnit removes the given unit from s. |
| 127 func (s *Service) RemoveUnit(unit *Unit) (err error) { | 126 func (s *Service) RemoveUnit(unit *Unit) error { |
| 128 sel := bson.D{ | 127 sel := bson.D{ |
| 129 {"_id", unit.Name()}, | 128 {"_id", unit.Name()}, |
| 130 {"service", s.name}, | 129 {"service", s.name}, |
| 131 » » {"lifecycle", life.Alive}, | 130 » » {"life", Alive}, |
| 132 } | 131 } |
| 133 » change := bson.D{{"$set", bson.D{{"lifecycle", life.Dying}, {"machineid"
, nil}}}} | 132 » change := bson.D{{"$set", bson.D{{"life", Dying}, {"machineid", nil}}}} |
| 134 » err = s.st.units.Update(sel, change) | 133 » err := s.st.units.Update(sel, change) |
| 135 if err != nil { | 134 if err != nil { |
| 136 return fmt.Errorf("can't remove unit %q: %v", unit, err) | 135 return fmt.Errorf("can't remove unit %q: %v", unit, err) |
| 137 } | 136 } |
| 138 return nil | 137 return nil |
| 139 } | 138 } |
| 140 | 139 |
| 141 func (s *Service) unitDoc(name string) (*unitDoc, error) { | 140 func (s *Service) unitDoc(name string) (*unitDoc, error) { |
| 142 udoc := &unitDoc{} | 141 udoc := &unitDoc{} |
| 143 sel := bson.D{ | 142 sel := bson.D{ |
| 144 {"_id", name}, | 143 {"_id", name}, |
| 145 {"service", s.name}, | 144 {"service", s.name}, |
| 146 » » {"lifecycle", life.Alive}, | 145 » » {"life", Alive}, |
| 147 } | 146 } |
| 148 err := s.st.units.Find(sel).One(udoc) | 147 err := s.st.units.Find(sel).One(udoc) |
| 149 if err != nil { | 148 if err != nil { |
| 150 return nil, err | 149 return nil, err |
| 151 } | 150 } |
| 152 return udoc, nil | 151 return udoc, nil |
| 153 } | 152 } |
| 154 | 153 |
| 155 // Unit returns the service's unit with name. | 154 // Unit returns the service's unit with name. |
| 156 func (s *Service) Unit(name string) (*Unit, error) { | 155 func (s *Service) Unit(name string) (*Unit, error) { |
| 157 udoc, err := s.unitDoc(name) | 156 udoc, err := s.unitDoc(name) |
| 158 if err != nil { | 157 if err != nil { |
| 159 return nil, fmt.Errorf("can't get unit %q from service %q: %v",
name, s.name, err) | 158 return nil, fmt.Errorf("can't get unit %q from service %q: %v",
name, s.name, err) |
| 160 } | 159 } |
| 161 return newUnit(s.st, udoc), nil | 160 return newUnit(s.st, udoc), nil |
| 162 } | 161 } |
| 163 | 162 |
| 164 // AllUnits returns all units of the service. | 163 // AllUnits returns all units of the service. |
| 165 func (s *Service) AllUnits() (units []*Unit, err error) { | 164 func (s *Service) AllUnits() (units []*Unit, err error) { |
| 166 docs := []unitDoc{} | 165 docs := []unitDoc{} |
| 167 » sel := bson.D{{"service", s.name}, {"lifecycle", life.Alive}} | 166 » sel := bson.D{{"service", s.name}, {"life", Alive}} |
| 168 err = s.st.units.Find(sel).All(&docs) | 167 err = s.st.units.Find(sel).All(&docs) |
| 169 if err != nil { | 168 if err != nil { |
| 170 return nil, fmt.Errorf("can't get all units from service %q: %v"
, err) | 169 return nil, fmt.Errorf("can't get all units from service %q: %v"
, err) |
| 171 } | 170 } |
| 172 for i := range docs { | 171 for i := range docs { |
| 173 units = append(units, newUnit(s.st, &docs[i])) | 172 units = append(units, newUnit(s.st, &docs[i])) |
| 174 } | 173 } |
| 175 return units, nil | 174 return units, nil |
| 176 } | 175 } |
| LEFT | RIGHT |