OLD | NEW |
1 // Copyright 2013 Canonical Ltd. | 1 // Copyright 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 package apiserver | 4 package client |
5 | 5 |
6 import ( | 6 import ( |
7 "fmt" | 7 "fmt" |
8 "launchpad.net/juju-core/charm" | 8 "launchpad.net/juju-core/charm" |
9 "launchpad.net/juju-core/juju" | 9 "launchpad.net/juju-core/juju" |
| 10 "launchpad.net/juju-core/state" |
10 "launchpad.net/juju-core/state/api" | 11 "launchpad.net/juju-core/state/api" |
11 "launchpad.net/juju-core/state/api/params" | 12 "launchpad.net/juju-core/state/api/params" |
| 13 "launchpad.net/juju-core/state/apiserver/common" |
12 "launchpad.net/juju-core/state/statecmd" | 14 "launchpad.net/juju-core/state/statecmd" |
13 ) | 15 ) |
14 | 16 |
15 // srvClient serves client-specific API methods. | 17 type API struct { |
16 type srvClient struct { | 18 » state *state.State |
17 » root *srvRoot | 19 » auth common.Authorizer |
| 20 » resources common.ResourceRegistry |
| 21 » client *Client |
18 } | 22 } |
19 | 23 |
20 func (c *srvClient) Status() (api.Status, error) { | 24 // Client serves client-specific API methods. |
21 » ms, err := c.root.srv.state.AllMachines() | 25 type Client struct { |
| 26 » api *API |
| 27 } |
| 28 |
| 29 // NewAPI creates a new instance of the Client API. |
| 30 func NewAPI(st *state.State, resources common.ResourceRegistry, authorizer commo
n.Authorizer) *API { |
| 31 » r := &API{ |
| 32 » » state: st, |
| 33 » » auth: authorizer, |
| 34 » » resources: resources, |
| 35 » } |
| 36 » r.client = &Client{ |
| 37 » » api: r, |
| 38 » } |
| 39 » return r |
| 40 } |
| 41 |
| 42 // Client returns an object that provides access |
| 43 // to methods accessible to non-agent clients. |
| 44 func (r *API) Client(id string) (*Client, error) { |
| 45 » if !r.auth.AuthClient() { |
| 46 » » return nil, common.ErrPerm |
| 47 » } |
| 48 » if id != "" { |
| 49 » » // Safeguard id for possible future use. |
| 50 » » return nil, common.ErrBadId |
| 51 » } |
| 52 » return r.client, nil |
| 53 } |
| 54 |
| 55 func (c *Client) Status() (api.Status, error) { |
| 56 » ms, err := c.api.state.AllMachines() |
22 if err != nil { | 57 if err != nil { |
23 return api.Status{}, err | 58 return api.Status{}, err |
24 } | 59 } |
25 status := api.Status{ | 60 status := api.Status{ |
26 Machines: make(map[string]api.MachineInfo), | 61 Machines: make(map[string]api.MachineInfo), |
27 } | 62 } |
28 for _, m := range ms { | 63 for _, m := range ms { |
29 instId, _ := m.InstanceId() | 64 instId, _ := m.InstanceId() |
30 status.Machines[m.Id()] = api.MachineInfo{ | 65 status.Machines[m.Id()] = api.MachineInfo{ |
31 InstanceId: string(instId), | 66 InstanceId: string(instId), |
32 } | 67 } |
33 } | 68 } |
34 return status, nil | 69 return status, nil |
35 } | 70 } |
36 | 71 |
37 func (c *srvClient) WatchAll() (params.AllWatcherId, error) { | 72 func (c *Client) WatchAll() (params.AllWatcherId, error) { |
38 » w := c.root.srv.state.Watch() | 73 » w := c.api.state.Watch() |
39 return params.AllWatcherId{ | 74 return params.AllWatcherId{ |
40 » » AllWatcherId: c.root.resources.Register(w), | 75 » » AllWatcherId: c.api.resources.Register(w), |
41 }, nil | 76 }, nil |
42 } | 77 } |
43 | 78 |
44 // ServiceSet implements the server side of Client.ServerSet. | 79 // ServiceSet implements the server side of Client.ServerSet. |
45 func (c *srvClient) ServiceSet(p params.ServiceSet) error { | 80 func (c *Client) ServiceSet(p params.ServiceSet) error { |
46 » svc, err := c.root.srv.state.Service(p.ServiceName) | 81 » svc, err := c.api.state.Service(p.ServiceName) |
47 if err != nil { | 82 if err != nil { |
48 return err | 83 return err |
49 } | 84 } |
50 ch, _, err := svc.Charm() | 85 ch, _, err := svc.Charm() |
51 if err != nil { | 86 if err != nil { |
52 return err | 87 return err |
53 } | 88 } |
54 changes, err := ch.Config().ParseSettingsStrings(p.Options) | 89 changes, err := ch.Config().ParseSettingsStrings(p.Options) |
55 if err != nil { | 90 if err != nil { |
56 return err | 91 return err |
57 } | 92 } |
58 return svc.UpdateConfigSettings(changes) | 93 return svc.UpdateConfigSettings(changes) |
59 } | 94 } |
60 | 95 |
61 // ServiceSetYAML implements the server side of Client.ServerSetYAML. | 96 // ServiceSetYAML implements the server side of Client.ServerSetYAML. |
62 func (c *srvClient) ServiceSetYAML(p params.ServiceSetYAML) error { | 97 func (c *Client) ServiceSetYAML(p params.ServiceSetYAML) error { |
63 » svc, err := c.root.srv.state.Service(p.ServiceName) | 98 » svc, err := c.api.state.Service(p.ServiceName) |
64 if err != nil { | 99 if err != nil { |
65 return err | 100 return err |
66 } | 101 } |
67 ch, _, err := svc.Charm() | 102 ch, _, err := svc.Charm() |
68 if err != nil { | 103 if err != nil { |
69 return err | 104 return err |
70 } | 105 } |
71 changes, err := ch.Config().ParseSettingsYAML([]byte(p.Config), p.Servic
eName) | 106 changes, err := ch.Config().ParseSettingsYAML([]byte(p.Config), p.Servic
eName) |
72 if err != nil { | 107 if err != nil { |
73 return err | 108 return err |
74 } | 109 } |
75 return svc.UpdateConfigSettings(changes) | 110 return svc.UpdateConfigSettings(changes) |
76 } | 111 } |
77 | 112 |
78 // ServiceGet returns the configuration for a service. | 113 // ServiceGet returns the configuration for a service. |
79 func (c *srvClient) ServiceGet(args params.ServiceGet) (params.ServiceGetResults
, error) { | 114 func (c *Client) ServiceGet(args params.ServiceGet) (params.ServiceGetResults, e
rror) { |
80 » return statecmd.ServiceGet(c.root.srv.state, args) | 115 » return statecmd.ServiceGet(c.api.state, args) |
81 } | 116 } |
82 | 117 |
83 // Resolved implements the server side of Client.Resolved. | 118 // Resolved implements the server side of Client.Resolved. |
84 func (c *srvClient) Resolved(p params.Resolved) error { | 119 func (c *Client) Resolved(p params.Resolved) error { |
85 » unit, err := c.root.srv.state.Unit(p.UnitName) | 120 » unit, err := c.api.state.Unit(p.UnitName) |
86 if err != nil { | 121 if err != nil { |
87 return err | 122 return err |
88 } | 123 } |
89 return unit.Resolve(p.Retry) | 124 return unit.Resolve(p.Retry) |
90 } | 125 } |
91 | 126 |
92 // ServiceExpose changes the juju-managed firewall to expose any ports that | 127 // ServiceExpose changes the juju-managed firewall to expose any ports that |
93 // were also explicitly marked by units as open. | 128 // were also explicitly marked by units as open. |
94 func (c *srvClient) ServiceExpose(args params.ServiceExpose) error { | 129 func (c *Client) ServiceExpose(args params.ServiceExpose) error { |
95 » return statecmd.ServiceExpose(c.root.srv.state, args) | 130 » return statecmd.ServiceExpose(c.api.state, args) |
96 } | 131 } |
97 | 132 |
98 // ServiceUnexpose changes the juju-managed firewall to unexpose any ports that | 133 // ServiceUnexpose changes the juju-managed firewall to unexpose any ports that |
99 // were also explicitly marked by units as open. | 134 // were also explicitly marked by units as open. |
100 func (c *srvClient) ServiceUnexpose(args params.ServiceUnexpose) error { | 135 func (c *Client) ServiceUnexpose(args params.ServiceUnexpose) error { |
101 » return statecmd.ServiceUnexpose(c.root.srv.state, args) | 136 » return statecmd.ServiceUnexpose(c.api.state, args) |
102 } | 137 } |
103 | 138 |
104 var CharmStore charm.Repository = charm.Store | 139 var CharmStore charm.Repository = charm.Store |
105 | 140 |
106 // ServiceDeploy fetches the charm from the charm store and deploys it. Local | 141 // ServiceDeploy fetches the charm from the charm store and deploys it. Local |
107 // charms are not supported. | 142 // charms are not supported. |
108 func (c *srvClient) ServiceDeploy(args params.ServiceDeploy) error { | 143 func (c *Client) ServiceDeploy(args params.ServiceDeploy) error { |
109 curl, err := charm.ParseURL(args.CharmUrl) | 144 curl, err := charm.ParseURL(args.CharmUrl) |
110 if err != nil { | 145 if err != nil { |
111 return err | 146 return err |
112 } | 147 } |
113 if curl.Schema != "cs" { | 148 if curl.Schema != "cs" { |
114 return fmt.Errorf(`charm url has unsupported schema %q`, curl.Sc
hema) | 149 return fmt.Errorf(`charm url has unsupported schema %q`, curl.Sc
hema) |
115 } | 150 } |
116 if curl.Revision < 0 { | 151 if curl.Revision < 0 { |
117 return fmt.Errorf("charm url must include revision") | 152 return fmt.Errorf("charm url must include revision") |
118 } | 153 } |
119 » conn, err := juju.NewConnFromState(c.root.srv.state) | 154 » conn, err := juju.NewConnFromState(c.api.state) |
120 if err != nil { | 155 if err != nil { |
121 return err | 156 return err |
122 } | 157 } |
123 ch, err := conn.PutCharm(curl, CharmStore, false) | 158 ch, err := conn.PutCharm(curl, CharmStore, false) |
124 if err != nil { | 159 if err != nil { |
125 return err | 160 return err |
126 } | 161 } |
127 var settings charm.Settings | 162 var settings charm.Settings |
128 if len(args.ConfigYAML) > 0 { | 163 if len(args.ConfigYAML) > 0 { |
129 settings, err = ch.Config().ParseSettingsYAML([]byte(args.Config
YAML), args.ServiceName) | 164 settings, err = ch.Config().ParseSettingsYAML([]byte(args.Config
YAML), args.ServiceName) |
130 } else if len(args.Config) > 0 { | 165 } else if len(args.Config) > 0 { |
131 settings, err = ch.Config().ParseSettingsStrings(args.Config) | 166 settings, err = ch.Config().ParseSettingsStrings(args.Config) |
132 } | 167 } |
133 if err != nil { | 168 if err != nil { |
134 return err | 169 return err |
135 } | 170 } |
136 _, err = conn.DeployService(juju.DeployServiceParams{ | 171 _, err = conn.DeployService(juju.DeployServiceParams{ |
137 ServiceName: args.ServiceName, | 172 ServiceName: args.ServiceName, |
138 Charm: ch, | 173 Charm: ch, |
139 NumUnits: args.NumUnits, | 174 NumUnits: args.NumUnits, |
140 ConfigSettings: settings, | 175 ConfigSettings: settings, |
141 Constraints: args.Constraints, | 176 Constraints: args.Constraints, |
142 ForceMachineId: args.ForceMachineId, | 177 ForceMachineId: args.ForceMachineId, |
143 }) | 178 }) |
144 return err | 179 return err |
145 } | 180 } |
146 | 181 |
147 // ServiceSetCharm sets the charm for a given service. | 182 // ServiceSetCharm sets the charm for a given service. |
148 func (c *srvClient) ServiceSetCharm(args params.ServiceSetCharm) error { | 183 func (c *Client) ServiceSetCharm(args params.ServiceSetCharm) error { |
149 » service, err := c.root.srv.state.Service(args.ServiceName) | 184 » service, err := c.api.state.Service(args.ServiceName) |
150 if err != nil { | 185 if err != nil { |
151 return err | 186 return err |
152 } | 187 } |
153 curl, err := charm.ParseURL(args.CharmUrl) | 188 curl, err := charm.ParseURL(args.CharmUrl) |
154 if err != nil { | 189 if err != nil { |
155 return err | 190 return err |
156 } | 191 } |
157 if curl.Schema != "cs" { | 192 if curl.Schema != "cs" { |
158 return fmt.Errorf(`charm url has unsupported schema %q`, curl.Sc
hema) | 193 return fmt.Errorf(`charm url has unsupported schema %q`, curl.Sc
hema) |
159 } | 194 } |
160 if curl.Revision < 0 { | 195 if curl.Revision < 0 { |
161 return fmt.Errorf("charm url must include revision") | 196 return fmt.Errorf("charm url must include revision") |
162 } | 197 } |
163 » conn, err := juju.NewConnFromState(c.root.srv.state) | 198 » conn, err := juju.NewConnFromState(c.api.state) |
164 if err != nil { | 199 if err != nil { |
165 return err | 200 return err |
166 } | 201 } |
167 ch, err := conn.PutCharm(curl, CharmStore, false) | 202 ch, err := conn.PutCharm(curl, CharmStore, false) |
168 if err != nil { | 203 if err != nil { |
169 return err | 204 return err |
170 } | 205 } |
171 return service.SetCharm(ch, args.Force) | 206 return service.SetCharm(ch, args.Force) |
172 } | 207 } |
173 | 208 |
174 // AddServiceUnits adds a given number of units to a service. | 209 // AddServiceUnits adds a given number of units to a service. |
175 func (c *srvClient) AddServiceUnits(args params.AddServiceUnits) (params.AddServ
iceUnitsResults, error) { | 210 func (c *Client) AddServiceUnits(args params.AddServiceUnits) (params.AddService
UnitsResults, error) { |
176 » units, err := statecmd.AddServiceUnits(c.root.srv.state, args) | 211 » units, err := statecmd.AddServiceUnits(c.api.state, args) |
177 if err != nil { | 212 if err != nil { |
178 return params.AddServiceUnitsResults{}, err | 213 return params.AddServiceUnitsResults{}, err |
179 } | 214 } |
180 unitNames := make([]string, len(units)) | 215 unitNames := make([]string, len(units)) |
181 for i, unit := range units { | 216 for i, unit := range units { |
182 unitNames[i] = unit.String() | 217 unitNames[i] = unit.String() |
183 } | 218 } |
184 return params.AddServiceUnitsResults{Units: unitNames}, nil | 219 return params.AddServiceUnitsResults{Units: unitNames}, nil |
185 } | 220 } |
186 | 221 |
187 // DestroyServiceUnits removes a given set of service units. | 222 // DestroyServiceUnits removes a given set of service units. |
188 func (c *srvClient) DestroyServiceUnits(args params.DestroyServiceUnits) error { | 223 func (c *Client) DestroyServiceUnits(args params.DestroyServiceUnits) error { |
189 » return statecmd.DestroyServiceUnits(c.root.srv.state, args) | 224 » return statecmd.DestroyServiceUnits(c.api.state, args) |
190 } | 225 } |
191 | 226 |
192 // ServiceDestroy destroys a given service. | 227 // ServiceDestroy destroys a given service. |
193 func (c *srvClient) ServiceDestroy(args params.ServiceDestroy) error { | 228 func (c *Client) ServiceDestroy(args params.ServiceDestroy) error { |
194 » return statecmd.ServiceDestroy(c.root.srv.state, args) | 229 » return statecmd.ServiceDestroy(c.api.state, args) |
195 } | 230 } |
196 | 231 |
197 // GetServiceConstraints returns the constraints for a given service. | 232 // GetServiceConstraints returns the constraints for a given service. |
198 func (c *srvClient) GetServiceConstraints(args params.GetServiceConstraints) (pa
rams.GetServiceConstraintsResults, error) { | 233 func (c *Client) GetServiceConstraints(args params.GetServiceConstraints) (param
s.GetServiceConstraintsResults, error) { |
199 » return statecmd.GetServiceConstraints(c.root.srv.state, args) | 234 » return statecmd.GetServiceConstraints(c.api.state, args) |
200 } | 235 } |
201 | 236 |
202 // SetServiceConstraints sets the constraints for a given service. | 237 // SetServiceConstraints sets the constraints for a given service. |
203 func (c *srvClient) SetServiceConstraints(args params.SetServiceConstraints) err
or { | 238 func (c *Client) SetServiceConstraints(args params.SetServiceConstraints) error
{ |
204 » return statecmd.SetServiceConstraints(c.root.srv.state, args) | 239 » return statecmd.SetServiceConstraints(c.api.state, args) |
205 } | 240 } |
206 | 241 |
207 // AddRelation adds a relation between the specified endpoints and returns the r
elation info. | 242 // AddRelation adds a relation between the specified endpoints and returns the r
elation info. |
208 func (c *srvClient) AddRelation(args params.AddRelation) (params.AddRelationResu
lts, error) { | 243 func (c *Client) AddRelation(args params.AddRelation) (params.AddRelationResults
, error) { |
209 » return statecmd.AddRelation(c.root.srv.state, args) | 244 » return statecmd.AddRelation(c.api.state, args) |
210 } | 245 } |
211 | 246 |
212 // DestroyRelation removes the relation between the specified endpoints. | 247 // DestroyRelation removes the relation between the specified endpoints. |
213 func (c *srvClient) DestroyRelation(args params.DestroyRelation) error { | 248 func (c *Client) DestroyRelation(args params.DestroyRelation) error { |
214 » return statecmd.DestroyRelation(c.root.srv.state, args) | 249 » return statecmd.DestroyRelation(c.api.state, args) |
215 } | 250 } |
216 | 251 |
217 // CharmInfo returns information about the requested charm. | 252 // CharmInfo returns information about the requested charm. |
218 func (c *srvClient) CharmInfo(args params.CharmInfo) (api.CharmInfo, error) { | 253 func (c *Client) CharmInfo(args params.CharmInfo) (api.CharmInfo, error) { |
219 curl, err := charm.ParseURL(args.CharmURL) | 254 curl, err := charm.ParseURL(args.CharmURL) |
220 if err != nil { | 255 if err != nil { |
221 return api.CharmInfo{}, err | 256 return api.CharmInfo{}, err |
222 } | 257 } |
223 » charm, err := c.root.srv.state.Charm(curl) | 258 » charm, err := c.api.state.Charm(curl) |
224 if err != nil { | 259 if err != nil { |
225 return api.CharmInfo{}, err | 260 return api.CharmInfo{}, err |
226 } | 261 } |
227 info := api.CharmInfo{ | 262 info := api.CharmInfo{ |
228 Revision: charm.Revision(), | 263 Revision: charm.Revision(), |
229 URL: curl.String(), | 264 URL: curl.String(), |
230 Config: charm.Config(), | 265 Config: charm.Config(), |
231 Meta: charm.Meta(), | 266 Meta: charm.Meta(), |
232 } | 267 } |
233 return info, nil | 268 return info, nil |
234 } | 269 } |
235 | 270 |
236 // EnvironmentInfo returns information about the current environment (default | 271 // EnvironmentInfo returns information about the current environment (default |
237 // series and type). | 272 // series and type). |
238 func (c *srvClient) EnvironmentInfo() (api.EnvironmentInfo, error) { | 273 func (c *Client) EnvironmentInfo() (api.EnvironmentInfo, error) { |
239 » conf, err := c.root.srv.state.EnvironConfig() | 274 » conf, err := c.api.state.EnvironConfig() |
240 if err != nil { | 275 if err != nil { |
241 return api.EnvironmentInfo{}, err | 276 return api.EnvironmentInfo{}, err |
242 } | 277 } |
243 info := api.EnvironmentInfo{ | 278 info := api.EnvironmentInfo{ |
244 DefaultSeries: conf.DefaultSeries(), | 279 DefaultSeries: conf.DefaultSeries(), |
245 ProviderType: conf.Type(), | 280 ProviderType: conf.Type(), |
246 Name: conf.Name(), | 281 Name: conf.Name(), |
247 } | 282 } |
248 return info, nil | 283 return info, nil |
249 } | 284 } |
250 | 285 |
251 // GetAnnotations returns annotations about a given entity. | 286 // GetAnnotations returns annotations about a given entity. |
252 func (c *srvClient) GetAnnotations(args params.GetAnnotations) (params.GetAnnota
tionsResults, error) { | 287 func (c *Client) GetAnnotations(args params.GetAnnotations) (params.GetAnnotatio
nsResults, error) { |
253 » entity, err := c.root.srv.state.Annotator(args.Tag) | 288 » entity, err := c.api.state.Annotator(args.Tag) |
254 if err != nil { | 289 if err != nil { |
255 return params.GetAnnotationsResults{}, err | 290 return params.GetAnnotationsResults{}, err |
256 } | 291 } |
257 ann, err := entity.Annotations() | 292 ann, err := entity.Annotations() |
258 if err != nil { | 293 if err != nil { |
259 return params.GetAnnotationsResults{}, err | 294 return params.GetAnnotationsResults{}, err |
260 } | 295 } |
261 return params.GetAnnotationsResults{Annotations: ann}, nil | 296 return params.GetAnnotationsResults{Annotations: ann}, nil |
262 } | 297 } |
263 | 298 |
264 // SetAnnotations stores annotations about a given entity. | 299 // SetAnnotations stores annotations about a given entity. |
265 func (c *srvClient) SetAnnotations(args params.SetAnnotations) error { | 300 func (c *Client) SetAnnotations(args params.SetAnnotations) error { |
266 » entity, err := c.root.srv.state.Annotator(args.Tag) | 301 » entity, err := c.api.state.Annotator(args.Tag) |
267 if err != nil { | 302 if err != nil { |
268 return err | 303 return err |
269 } | 304 } |
270 return entity.SetAnnotations(args.Pairs) | 305 return entity.SetAnnotations(args.Pairs) |
271 } | 306 } |
OLD | NEW |