forked from juju/juju
/
state.go
172 lines (152 loc) · 5.02 KB
/
state.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package api
import (
"net"
"strconv"
"github.com/juju/juju/network"
"github.com/juju/juju/state/api/agent"
"github.com/juju/juju/state/api/charmrevisionupdater"
"github.com/juju/juju/state/api/deployer"
"github.com/juju/juju/state/api/environment"
"github.com/juju/juju/state/api/firewaller"
"github.com/juju/juju/state/api/keyupdater"
apilogger "github.com/juju/juju/state/api/logger"
"github.com/juju/juju/state/api/machiner"
"github.com/juju/juju/state/api/networker"
"github.com/juju/juju/state/api/params"
"github.com/juju/juju/state/api/provisioner"
"github.com/juju/juju/state/api/rsyslog"
"github.com/juju/juju/state/api/uniter"
"github.com/juju/juju/state/api/upgrader"
)
// Login authenticates as the entity with the given name and password.
// Subsequent requests on the state will act as that entity. This
// method is usually called automatically by Open. The machine nonce
// should be empty unless logging in as a machine agent.
func (st *State) Login(tag, password, nonce string) error {
var result params.LoginResult
err := st.Call("Admin", "", "Login", ¶ms.Creds{
AuthTag: tag,
Password: password,
Nonce: nonce,
}, &result)
if err == nil {
st.authTag = tag
hostPorts, err := addAddress(result.Servers, st.addr)
if err != nil {
st.Close()
return err
}
st.hostPorts = hostPorts
st.environTag = result.EnvironTag
}
return err
}
// slideAddressToFront moves the address at the location (serverIndex, addrIndex) to be
// the first address of the first server.
func slideAddressToFront(servers [][]network.HostPort, serverIndex, addrIndex int) {
server := servers[serverIndex]
hostPort := server[addrIndex]
// Move the matching address to be the first in this server
for ; addrIndex > 0; addrIndex-- {
server[addrIndex] = server[addrIndex-1]
}
server[0] = hostPort
for ; serverIndex > 0; serverIndex-- {
servers[serverIndex] = servers[serverIndex-1]
}
servers[0] = server
}
// addAddress appends a new server derived from the given
// address to servers if the address is not already found
// there.
func addAddress(servers [][]network.HostPort, addr string) ([][]network.HostPort, error) {
for i, server := range servers {
for j, hostPort := range server {
if hostPort.NetAddr() == addr {
slideAddressToFront(servers, i, j)
return servers, nil
}
}
}
host, portString, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
port, err := strconv.Atoi(portString)
if err != nil {
return nil, err
}
hostPort := network.HostPort{
Address: network.NewAddress(host, network.ScopeUnknown),
Port: port,
}
result := make([][]network.HostPort, 0, len(servers)+1)
result = append(result, []network.HostPort{hostPort})
result = append(result, servers...)
return result, nil
}
// Client returns an object that can be used
// to access client-specific functionality.
func (st *State) Client() *Client {
return &Client{st}
}
// Machiner returns a version of the state that provides functionality
// required by the machiner worker.
func (st *State) Machiner() *machiner.State {
return machiner.NewState(st)
}
// Networker returns a version of the state that provides functionality
// required by the networker worker.
func (st *State) Networker() *networker.State {
return networker.NewState(st)
}
// Provisioner returns a version of the state that provides functionality
// required by the provisioner worker.
func (st *State) Provisioner() *provisioner.State {
return provisioner.NewState(st)
}
// Uniter returns a version of the state that provides functionality
// required by the uniter worker.
func (st *State) Uniter() *uniter.State {
return uniter.NewState(st, st.authTag)
}
// Firewaller returns a version of the state that provides functionality
// required by the firewaller worker.
func (st *State) Firewaller() *firewaller.State {
return firewaller.NewState(st)
}
// Agent returns a version of the state that provides
// functionality required by the agent code.
func (st *State) Agent() *agent.State {
return agent.NewState(st)
}
// Upgrader returns access to the Upgrader API
func (st *State) Upgrader() *upgrader.State {
return upgrader.NewState(st)
}
// Deployer returns access to the Deployer API
func (st *State) Deployer() *deployer.State {
return deployer.NewState(st)
}
// Environment returns access to the Environment API
func (st *State) Environment() *environment.Facade {
return environment.NewFacade(st)
}
// Logger returns access to the Logger API
func (st *State) Logger() *apilogger.State {
return apilogger.NewState(st)
}
// KeyUpdater returns access to the KeyUpdater API
func (st *State) KeyUpdater() *keyupdater.State {
return keyupdater.NewState(st)
}
// CharmRevisionUpdater returns access to the CharmRevisionUpdater API
func (st *State) CharmRevisionUpdater() *charmrevisionupdater.State {
return charmrevisionupdater.NewState(st)
}
// Rsyslog returns access to the Rsyslog API
func (st *State) Rsyslog() *rsyslog.State {
return rsyslog.NewState(st)
}