1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
9
"launchpad.net/juju-core/instance"
10
"launchpad.net/juju-core/names"
11
"launchpad.net/juju-core/state/api/params"
12
"launchpad.net/juju-core/state/api/watcher"
15
// Unit represents a juju unit as seen by a firewaller worker.
22
// Name returns the name of the unit.
23
func (u *Unit) Name() string {
24
_, unitName, err := names.ParseTag(u.tag, names.UnitTagKind)
26
panic(fmt.Sprintf("%q is not a valid unit tag", u.tag))
31
// Life returns the unit's life cycle value.
32
func (u *Unit) Life() params.Life {
36
// Refresh updates the cached local copy of the unit's data.
37
func (u *Unit) Refresh() error {
38
life, err := u.st.life(u.tag)
46
// Watch returns a watcher for observing changes to the unit.
47
func (u *Unit) Watch() (watcher.NotifyWatcher, error) {
48
var results params.NotifyWatchResults
49
args := params.Entities{
50
Entities: []params.Entity{{Tag: u.tag}},
52
err := u.st.caller.Call("Firewaller", "", "Watch", args, &results)
56
if len(results.Results) != 1 {
57
return nil, fmt.Errorf("expected one result, got %d", len(results.Results))
59
result := results.Results[0]
60
if result.Error != nil {
61
return nil, result.Error
63
w := watcher.NewNotifyWatcher(u.st.caller, result)
67
// Service returns the service.
68
func (u *Unit) Service() (*Service, error) {
69
serviceTag := names.ServiceTag(names.UnitService(u.Name()))
74
// Call Refresh() immediately to get the up-to-date
75
// life and other needed locally cached fields.
76
err := service.Refresh()
83
// OpenedPorts returns the list of opened ports for this unit.
85
// NOTE: This differs from state.Unit.OpenedPorts() by returning
86
// an error as well, because it needs to make an API call.
87
func (u *Unit) OpenedPorts() ([]instance.Port, error) {
88
var results params.PortsResults
89
args := params.Entities{
90
Entities: []params.Entity{{Tag: u.tag}},
92
err := u.st.caller.Call("Firewaller", "", "OpenedPorts", args, &results)
96
if len(results.Results) != 1 {
97
return nil, fmt.Errorf("expected one result, got %d", len(results.Results))
99
result := results.Results[0]
100
if result.Error != nil {
101
return nil, result.Error
103
return result.Ports, nil
106
// AssignedMachine returns the tag of this unit's assigned machine (if
107
// any), or a CodeNotAssigned error.
108
func (u *Unit) AssignedMachine() (string, error) {
109
var results params.StringResults
110
args := params.Entities{
111
Entities: []params.Entity{{Tag: u.tag}},
113
err := u.st.caller.Call("Firewaller", "", "GetAssignedMachine", args, &results)
117
if len(results.Results) != 1 {
118
return "", fmt.Errorf("expected one result, got %d", len(results.Results))
120
result := results.Results[0]
121
if result.Error != nil {
122
return "", result.Error
124
return result.Result, nil