/
unit.go
80 lines (70 loc) · 1.81 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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package firewaller
import (
"github.com/juju/errors"
"github.com/juju/names/v4"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/core/life"
)
// Unit represents a juju unit as seen by a firewaller worker.
type Unit struct {
st *Client
tag names.UnitTag
life life.Value
}
// 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() life.Value {
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) {
appName, err := names.UnitApplication(u.Name())
if err != nil {
return nil, err
}
applicationTag := names.NewApplicationTag(appName)
app := &Application{
st: u.st,
tag: applicationTag,
}
return app, 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)
}