forked from juju/juju
/
machine.go
139 lines (125 loc) · 3.65 KB
/
machine.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package machiner
import (
"github.com/juju/errors"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/api/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/network"
"github.com/juju/juju/status"
"github.com/juju/juju/watcher"
)
// Machine represents a juju machine as seen by a machiner worker.
type Machine struct {
tag names.MachineTag
life params.Life
st *State
}
// Tag returns the machine's tag.
func (m *Machine) Tag() names.Tag {
return m.tag
}
// Life returns the machine's lifecycle value.
func (m *Machine) Life() params.Life {
return m.life
}
// Refresh updates the cached local copy of the machine's data.
func (m *Machine) Refresh() error {
life, err := m.st.machineLife(m.tag)
if err != nil {
return err
}
m.life = life
return nil
}
// SetStatus sets the status of the machine.
func (m *Machine) SetStatus(status status.Status, info string, data map[string]interface{}) error {
var result params.ErrorResults
args := params.SetStatus{
Entities: []params.EntityStatusArgs{
{Tag: m.tag.String(), Status: status.String(), Info: info, Data: data},
},
}
err := m.st.facade.FacadeCall("SetStatus", args, &result)
if err != nil {
return err
}
return result.OneError()
}
// SetMachineAddresses sets the machine determined addresses of the machine.
func (m *Machine) SetMachineAddresses(addresses []network.Address) error {
var result params.ErrorResults
args := params.SetMachinesAddresses{
MachineAddresses: []params.MachineAddresses{
{Tag: m.Tag().String(), Addresses: params.FromNetworkAddresses(addresses...)},
},
}
err := m.st.facade.FacadeCall("SetMachineAddresses", args, &result)
if err != nil {
return err
}
return result.OneError()
}
// EnsureDead sets the machine lifecycle to Dead if it is Alive or
// Dying. It does nothing otherwise.
func (m *Machine) EnsureDead() error {
var result params.ErrorResults
args := params.Entities{
Entities: []params.Entity{{Tag: m.tag.String()}},
}
err := m.st.facade.FacadeCall("EnsureDead", args, &result)
if err != nil {
return err
}
return result.OneError()
}
// Watch returns a watcher for observing changes to the machine.
func (m *Machine) Watch() (watcher.NotifyWatcher, error) {
return common.Watch(m.st.facade, m.tag)
}
// Jobs returns a list of jobs for the machine.
func (m *Machine) Jobs() (*params.JobsResult, error) {
var results params.JobsResults
args := params.Entities{
Entities: []params.Entity{{Tag: m.Tag().String()}},
}
err := m.st.facade.FacadeCall("Jobs", args, &results)
if err != nil {
return nil, errors.Annotate(err, "error from FacadeCall")
}
if len(results.Results) != 1 {
return nil, errors.Errorf("expected 1 result, got %d", len(results.Results))
}
result := results.Results[0]
if result.Error != nil {
return nil, result.Error
}
return &result, nil
}
// SetObservedNetworkConfig sets the machine network config as observed on the
// machine.
func (m *Machine) SetObservedNetworkConfig(netConfig []params.NetworkConfig) error {
args := params.SetMachineNetworkConfig{
Tag: m.Tag().String(),
Config: netConfig,
}
err := m.st.facade.FacadeCall("SetObservedNetworkConfig", args, nil)
if err != nil {
return errors.Trace(err)
}
return nil
}
// SetProviderNetworkConfig sets the machine network config as seen by the
// provider.
func (m *Machine) SetProviderNetworkConfig() error {
var result params.ErrorResults
args := params.Entities{
Entities: []params.Entity{{Tag: m.tag.String()}},
}
err := m.st.facade.FacadeCall("SetProviderNetworkConfig", args, &result)
if err != nil {
return err
}
return result.OneError()
}