~axwalk/juju-core/trunk

« back to all changes in this revision

Viewing changes to state/unit.go

  • Committer: Tarmac
  • Author(s): Dimiter Naydenov
  • Date: 2013-07-30 18:39:16 UTC
  • mfrom: (1548.3.8 081-common-names)
  • Revision ID: tarmac-20130730183916-t2lui5ndotkzqrth
[r=dimitern] names: New package

This introduces a new juju-core/names pacakge,
which contains all name and tag related functions
shared between state and API: IsUnitName, UnitTag,
UnitNameFromTag, MachineTag, MachineIdFromTag,
IsServiceName, etc.

Because of the pacakge name, some functions were
renamed: names.IsUnit, IsService, UnitFromTag,
all refer to names.

In addition, a change was made to these two
functions: UnitNameFromTag and MachineIdFromTag.
Both of them now return (string, error), rather
than just string. The error return is used in
case the passed tag string has an invalid format.
Because of this change, some places needed slight
refactoring, otherwise no other changes where made.

https://codereview.appspot.com/12034043/

R=fwereade, rogpeppe

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
        stderrors "errors"
8
8
        "fmt"
9
9
        "sort"
10
 
        "strings"
11
10
        "time"
12
11
 
13
12
        "labix.org/v2/mgo"
19
18
        "launchpad.net/juju-core/constraints"
20
19
        "launchpad.net/juju-core/errors"
21
20
        "launchpad.net/juju-core/instance"
 
21
        "launchpad.net/juju-core/names"
22
22
        "launchpad.net/juju-core/state/api/params"
23
23
        "launchpad.net/juju-core/state/presence"
24
24
        "launchpad.net/juju-core/utils"
433
433
// the unit. If no such entity can be determined, false is returned.
434
434
func (u *Unit) DeployerTag() (string, bool) {
435
435
        if u.doc.Principal != "" {
436
 
                return UnitTag(u.doc.Principal), true
 
436
                return names.UnitTag(u.doc.Principal), true
437
437
        } else if u.doc.MachineId != "" {
438
 
                return MachineTag(u.doc.MachineId), true
 
438
                return names.MachineTag(u.doc.MachineId), true
439
439
        }
440
440
        return "", false
441
441
}
628
628
        return u.st.pwatcher.Alive(u.globalKey())
629
629
}
630
630
 
631
 
const unitTagPrefix = "unit-"
632
 
 
633
 
// UnitTag returns the tag for the
634
 
// unit with the given name.
635
 
func UnitTag(unitName string) string {
636
 
        return unitTagPrefix + strings.Replace(unitName, "/", "-", -1)
637
 
}
638
 
 
639
 
// UnitNameFromTag returns the unit name that was used to create the tag.
640
 
func UnitNameFromTag(tag string) string {
641
 
        // TODO(dimitern): Possibly change this to return (string, error),
642
 
        // so the case below can be reported.
643
 
        if !strings.HasPrefix(tag, unitTagPrefix) {
644
 
                return ""
645
 
        }
646
 
        // Strip off the "unit-" prefix.
647
 
        name := tag[len(unitTagPrefix):]
648
 
        // Put the slashes back.
649
 
        name = strings.Replace(name, "-", "/", -1)
650
 
        return name
651
 
}
652
 
 
653
631
// Tag returns a name identifying the unit that is safe to use
654
632
// as a file name.  The returned name will be different from other
655
633
// Tag values returned by any other entities from the same state.
656
634
func (u *Unit) Tag() string {
657
 
        return UnitTag(u.Name())
 
635
        return names.UnitTag(u.Name())
658
636
}
659
637
 
660
638
// WaitAgentAlive blocks until the respective agent is alive.