forked from juju/juju
/
unit.go
84 lines (74 loc) · 1.96 KB
/
unit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package firewaller
import (
"github.com/juju/errors"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/apiserver/params"
)
// Unit represents a juju unit as seen by a firewaller worker.
type Unit struct {
st *State
tag names.UnitTag
life params.Life
}
// Name returns the name of the unit.
func (u *Unit) Name() string {
return u.tag.Id()
}
// Tag returns the unit tag.
func (u *Unit) Tag() names.UnitTag {
return u.tag
}
// Life returns the unit's life cycle value.
func (u *Unit) Life() params.Life {
return u.life
}
// Refresh updates the cached local copy of the unit's data.
func (u *Unit) Refresh() error {
life, err := u.st.life(u.tag)
if err != nil {
return err
}
u.life = life
return nil
}
// Application returns the application.
func (u *Unit) Application() (*Application, error) {
serviceName, err := names.UnitApplication(u.Name())
if err != nil {
return nil, err
}
applicationTag := names.NewApplicationTag(serviceName)
service := &Application{
st: u.st,
tag: applicationTag,
}
// Call Refresh() immediately to get the up-to-date
// life and other needed locally cached fields.
if err := service.Refresh(); err != nil {
return nil, err
}
return service, nil
}
// AssignedMachine returns the tag of this unit's assigned machine (if
// any), or a CodeNotAssigned error.
func (u *Unit) AssignedMachine() (names.MachineTag, error) {
var results params.StringResults
args := params.Entities{
Entities: []params.Entity{{Tag: u.tag.String()}},
}
emptyTag := names.NewMachineTag("")
err := u.st.facade.FacadeCall("GetAssignedMachine", args, &results)
if err != nil {
return emptyTag, err
}
if len(results.Results) != 1 {
return emptyTag, errors.Errorf("expected 1 result, got %d", len(results.Results))
}
result := results.Results[0]
if result.Error != nil {
return emptyTag, result.Error
}
return names.ParseMachineTag(result.Result)
}