-
Notifications
You must be signed in to change notification settings - Fork 1
/
unit.go
executable file
·125 lines (114 loc) · 3.15 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package firewaller
import (
"fmt"
"launchpad.net/juju-core/instance"
"launchpad.net/juju-core/names"
"launchpad.net/juju-core/state/api/params"
"launchpad.net/juju-core/state/api/watcher"
)
// Unit represents a juju unit as seen by a firewaller worker.
type Unit struct {
st *State
tag string
life params.Life
}
// Name returns the name of the unit.
func (u *Unit) Name() string {
_, unitName, err := names.ParseTag(u.tag, names.UnitTagKind)
if err != nil {
panic(fmt.Sprintf("%q is not a valid unit tag", u.tag))
}
return unitName
}
// 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
}
// Watch returns a watcher for observing changes to the unit.
func (u *Unit) Watch() (watcher.NotifyWatcher, error) {
var results params.NotifyWatchResults
args := params.Entities{
Entities: []params.Entity{{Tag: u.tag}},
}
err := u.st.caller.Call("Firewaller", "", "Watch", args, &results)
if err != nil {
return nil, err
}
if len(results.Results) != 1 {
return nil, fmt.Errorf("expected one result, got %d", len(results.Results))
}
result := results.Results[0]
if result.Error != nil {
return nil, result.Error
}
w := watcher.NewNotifyWatcher(u.st.caller, result)
return w, nil
}
// Service returns the service.
func (u *Unit) Service() (*Service, error) {
serviceTag := names.ServiceTag(names.UnitService(u.Name()))
service := &Service{
st: u.st,
tag: serviceTag,
}
// Call Refresh() immediately to get the up-to-date
// life and other needed locally cached fields.
err := service.Refresh()
if err != nil {
return nil, err
}
return service, nil
}
// OpenedPorts returns the list of opened ports for this unit.
//
// NOTE: This differs from state.Unit.OpenedPorts() by returning
// an error as well, because it needs to make an API call.
func (u *Unit) OpenedPorts() ([]instance.Port, error) {
var results params.PortsResults
args := params.Entities{
Entities: []params.Entity{{Tag: u.tag}},
}
err := u.st.caller.Call("Firewaller", "", "OpenedPorts", args, &results)
if err != nil {
return nil, err
}
if len(results.Results) != 1 {
return nil, fmt.Errorf("expected one result, got %d", len(results.Results))
}
result := results.Results[0]
if result.Error != nil {
return nil, result.Error
}
return result.Ports, nil
}
// AssignedMachine returns the tag of this unit's assigned machine (if
// any), or a CodeNotAssigned error.
func (u *Unit) AssignedMachine() (string, error) {
var results params.StringResults
args := params.Entities{
Entities: []params.Entity{{Tag: u.tag}},
}
err := u.st.caller.Call("Firewaller", "", "GetAssignedMachine", args, &results)
if err != nil {
return "", err
}
if len(results.Results) != 1 {
return "", fmt.Errorf("expected one result, got %d", len(results.Results))
}
result := results.Results[0]
if result.Error != nil {
return "", result.Error
}
return result.Result, nil
}