~themue/juju-core/053-env-more-script-friendly

« back to all changes in this revision

Viewing changes to state/unit.go

  • Committer: Dimiter Naydenov
  • Date: 2013-07-29 15:15:41 UTC
  • mto: This revision was merged to the branch mainline in revision 1565.
  • Revision ID: dimiter.naydenov@canonical.com-20130729151541-zm8murwo9u7mtsdu
names: new package

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.