~fwereade/pyjuju/go-place-unit

« back to all changes in this revision

Viewing changes to state/unit.go

  • Committer: William Reade
  • Date: 2012-06-06 10:17:40 UTC
  • Revision ID: fwereade@gmail.com-20120606101740-a6xgj009s8tqt7v1
address review points

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
        ResolvedNoHooks    ResolvedMode = 1001
23
23
)
24
24
 
25
 
// PlacementPolicy controls what machine a unit will be assigned to.
26
 
type PlacementPolicy string
 
25
// AssignmentPolicy controls what machine a unit will be assigned to.
 
26
type AssignmentPolicy string
27
27
 
28
28
const (
29
 
        PlaceLocal      PlacementPolicy = "local"
30
 
        PlaceUnassigned PlacementPolicy = "unassigned"
 
29
        // AssignLocal indicates that all service units should be assigned to machine 0.
 
30
        AssignLocal AssignmentPolicy = "local"
 
31
        // AssignUnused indicates that every service unit should be assigned to a
 
32
        // dedicated machine, and that new machines should be launched if required.
 
33
        AssignUnused AssignmentPolicy = "unused"
31
34
)
32
35
 
33
36
// NeedsUpgrade describes if a unit needs an
237
240
        return &Machine{u.st, machineKey}, nil
238
241
}
239
242
 
240
 
// Place assigns u to a machine according to policy.
241
 
func (u *Unit) Place(policy PlacementPolicy) (err error) {
242
 
        var m *Machine
243
 
        switch policy {
244
 
        case PlaceLocal:
245
 
                if m, err = u.st.Machine(0); err != nil {
246
 
                        return
247
 
                }
248
 
        case PlaceUnassigned:
249
 
                switch _, err = u.AssignToUnusedMachine(); err {
250
 
                case noUnusedMachines:
251
 
                default:
252
 
                        return
253
 
                }
254
 
                if m, err = u.st.AddMachine(); err != nil {
255
 
                        return
256
 
                }
257
 
        default:
258
 
                panic(fmt.Errorf("unknown unit placement policy: %q", policy))
259
 
        }
260
 
        return u.AssignToMachine(m)
261
 
}
262
 
 
263
243
// UnassignFromMachine removes the assignment between this unit and
264
244
// the machine it's assigned to.
265
245
func (u *Unit) UnassignFromMachine() error {