Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 package state | 1 package state |
2 | 2 |
3 import ( | 3 import ( |
4 "errors" | 4 "errors" |
5 "fmt" | 5 "fmt" |
6 "labix.org/v2/mgo" | 6 "labix.org/v2/mgo" |
7 "labix.org/v2/mgo/txn" | 7 "labix.org/v2/mgo/txn" |
8 "launchpad.net/juju-core/charm" | 8 "launchpad.net/juju-core/charm" |
9 "launchpad.net/juju-core/state/presence" | 9 "launchpad.net/juju-core/state/presence" |
10 "launchpad.net/juju-core/trivial" | 10 "launchpad.net/juju-core/trivial" |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
397 // It returns the started pinger. | 397 // It returns the started pinger. |
398 func (u *Unit) SetAgentAlive() (*presence.Pinger, error) { | 398 func (u *Unit) SetAgentAlive() (*presence.Pinger, error) { |
399 p := presence.NewPinger(u.st.presence, u.globalKey()) | 399 p := presence.NewPinger(u.st.presence, u.globalKey()) |
400 err := p.Start() | 400 err := p.Start() |
401 if err != nil { | 401 if err != nil { |
402 return nil, err | 402 return nil, err |
403 } | 403 } |
404 return p, nil | 404 return p, nil |
405 } | 405 } |
406 | 406 |
407 // IsAssignedTo returns whether u is a principal assigned to machine m. | 407 // notAssignedError represents the error that a unit is not assigned. |
408 // It panics if u is not a principal unit. | 408 type notAssignedError struct { |
409 func (u *Unit) IsAssignedTo(m *Machine) bool { | 409 » msg string |
rog
2012/11/09 09:08:08
is this necessary? can't we just use AssignedMachi
aram
2012/11/09 16:50:08
It was impossible to use the previous version of A
| |
410 » if !u.IsPrincipal() { | 410 } |
411 » » panic("unit is not a principal") | 411 |
412 » } | 412 func (e *notAssignedError) Error() string { |
413 » return u.doc.MachineId != nil && *u.doc.MachineId == m.doc.Id | 413 » return e.msg |
414 } | |
415 | |
416 func notAssigned(format string, args ...interface{}) error { | |
417 » return ¬AssignedError{fmt.Sprintf(format+" is not assigned to a machi ne", args...)} | |
niemeyer
2012/11/14 23:34:08
Pre-req has a missing test? It should have a test
| |
418 } | |
419 | |
420 func IsNotAssigned(err error) bool { | |
421 » _, ok := err.(*notAssignedError) | |
422 » return ok | |
414 } | 423 } |
415 | 424 |
416 // AssignedMachineId returns the id of the assigned machine. | 425 // AssignedMachineId returns the id of the assigned machine. |
417 func (u *Unit) AssignedMachineId() (id int, err error) { | 426 func (u *Unit) AssignedMachineId() (id int, err error) { |
418 defer trivial.ErrorContextf(&err, "cannot get machine id of unit %q", u) | |
419 if u.IsPrincipal() { | 427 if u.IsPrincipal() { |
420 if u.doc.MachineId == nil { | 428 if u.doc.MachineId == nil { |
421 » » » return 0, errors.New("unit not assigned to machine") | 429 » » » return 0, notAssigned("unit %q", u) |
422 } | 430 } |
423 return *u.doc.MachineId, nil | 431 return *u.doc.MachineId, nil |
424 } | 432 } |
425 pudoc := unitDoc{} | 433 pudoc := unitDoc{} |
426 err = u.st.units.Find(D{{"_id", u.doc.Principal}}).One(&pudoc) | 434 err = u.st.units.Find(D{{"_id", u.doc.Principal}}).One(&pudoc) |
427 » if err != nil { | 435 » if err == mgo.ErrNotFound { |
428 » » return 0, err | 436 » » return 0, notFound("cannot get machine id of unit %q: principal %q %v", u, u.doc.Principal) |
437 » } | |
438 » if err != nil { | |
439 » » return 0, fmt.Errorf("cannot get machine id of unit %q: %v", u, err) | |
429 } | 440 } |
430 if pudoc.MachineId == nil { | 441 if pudoc.MachineId == nil { |
431 » » return 0, errors.New("unit not assigned to machine") | 442 » » return 0, notAssigned("unit %q", u) |
432 } | 443 } |
433 return *pudoc.MachineId, nil | 444 return *pudoc.MachineId, nil |
434 } | 445 } |
435 | 446 |
436 var ( | 447 var ( |
437 machineDeadErr = errors.New("machine is dead") | 448 machineDeadErr = errors.New("machine is dead") |
438 unitDeadErr = errors.New("unit is dead") | 449 unitDeadErr = errors.New("unit is dead") |
439 alreadyAssignedErr = errors.New("unit is already assigned to a machine") | 450 alreadyAssignedErr = errors.New("unit is already assigned to a machine") |
440 inUseErr = errors.New("machine is not unused") | 451 inUseErr = errors.New("machine is not unused") |
441 ) | 452 ) |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
665 return p1.Protocol < p2.Protocol | 676 return p1.Protocol < p2.Protocol |
666 } | 677 } |
667 return p1.Number < p2.Number | 678 return p1.Number < p2.Number |
668 } | 679 } |
669 | 680 |
670 // SortPorts sorts the given ports, first by protocol, | 681 // SortPorts sorts the given ports, first by protocol, |
671 // then by number. | 682 // then by number. |
672 func SortPorts(ports []Port) { | 683 func SortPorts(ports []Port) { |
673 sort.Sort(portSlice(ports)) | 684 sort.Sort(portSlice(ports)) |
674 } | 685 } |
LEFT | RIGHT |