Merge lp:~frankban/juju-core/env-name-in-context into lp:~go-bot/juju-core/trunk

Proposed by Francesco Banconi
Status: Merged
Approved by: Francesco Banconi
Approved revision: no longer in the source branch.
Merged at revision: 2289
Proposed branch: lp:~frankban/juju-core/env-name-in-context
Merge into: lp:~go-bot/juju-core/trunk
Diff against target: 375 lines (+118/-37)
12 files modified
doc/charms-in-action.txt (+1/-0)
state/api/params/internal.go (+9/-1)
state/api/uniter/environ.go (+9/-18)
state/api/uniter/environ_test.go (+15/-7)
state/api/uniter/uniter.go (+32/-1)
state/apiserver/uniter/uniter.go (+11/-0)
state/apiserver/uniter/uniter_test.go (+13/-0)
state/environ.go (+5/-0)
state/environ_test.go (+4/-0)
worker/uniter/context.go (+7/-2)
worker/uniter/context_test.go (+7/-2)
worker/uniter/uniter.go (+5/-6)
To merge this branch: bzr merge lp:~frankban/juju-core/env-name-in-context
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+204450@code.launchpad.net

Commit message

Add the environment name to the hooks context.

Expose the human friendly environment name to the
hooks through the JUJU_ENV_NAME environment variable.

This is required e.g. by the GUI in order to give to the user
a hint about where to find the password to log in.

https://codereview.appspot.com/50090044/

Description of the change

Add the environment name to the hooks context.

Expose the human friendly environment name to the
hooks through the JUJU_ENV_NAME environment variable.

This is required e.g. by the GUI in order to give to the user
a hint about where to find the password to log in.

https://codereview.appspot.com/50090044/

To post a comment you must log in.
Revision history for this message
Francesco Banconi (frankban) wrote :

Reviewers: mp+204450_code.launchpad.net,

Message:
Please take a look.

Description:
Add the environment name to the hooks context.

Expose the human friendly environment name to the
hooks through the JUJU_ENV_NAME environment variable.

This is required e.g. by the GUI in order to give to the user
a hint about where to find the password to log in.

https://code.launchpad.net/~frankban/juju-core/env-name-in-context/+merge/204450

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/50090044/

Affected files (+113, -38 lines):
   A [revision details]
   M doc/charms-in-action.txt
   M state/api/params/internal.go
   M state/api/uniter/environ.go
   M state/api/uniter/environ_test.go
   M state/api/uniter/uniter.go
   M state/apiserver/uniter/uniter.go
   M state/apiserver/uniter/uniter_test.go
   M state/environ.go
   M state/environ_test.go
   M worker/uniter/context.go
   M worker/uniter/context_test.go
   M worker/uniter/uniter.go

Revision history for this message
Dimiter Naydenov (dimitern) wrote :

LGTM with a few suggestions below. Nice!

https://codereview.appspot.com/50090044/diff/1/state/api/params/internal.go
File state/api/params/internal.go (right):

https://codereview.appspot.com/50090044/diff/1/state/api/params/internal.go#newcode115
state/api/params/internal.go:115: // environment.
// EnvironmentResult holds the result of an API call returning a name
and UUID for an environment ?

Also s/CurrentEnvironmentResult/EnvironmentResult/ ?

https://codereview.appspot.com/50090044/diff/1/state/api/uniter/environ.go
File state/api/uniter/environ.go (left):

https://codereview.appspot.com/50090044/diff/1/state/api/uniter/environ.go#oldcode23
state/api/uniter/environ.go:23: // TODO(dimitern): 2013-09-06 bug
1221834

Can you update the bug to exclude Environ.UUID() from the list, because
with this change I think we can consider it cached.

https://codereview.appspot.com/50090044/diff/1/state/api/uniter/environ.go
File state/api/uniter/environ.go (right):

https://codereview.appspot.com/50090044/diff/1/state/api/uniter/environ.go#newcode9
state/api/uniter/environ.go:9: // Environment represents the name and
the unique identifier of an environment.
No need to change this comment I think - see other examples in
api/uniter.

https://codereview.appspot.com/50090044/diff/1/state/api/uniter/uniter.go
File state/api/uniter/uniter.go (right):

https://codereview.appspot.com/50090044/diff/1/state/api/uniter/uniter.go#newcode164
state/api/uniter/uniter.go:164: if err != nil {
you need to check result.Error as well, in addition to checking err -
see APIAddresses below.

https://codereview.appspot.com/50090044/diff/1/state/api/uniter/uniter.go#newcode173
state/api/uniter/uniter.go:173: func (st *State) environment1dot16()
(*Environment, error) {
// environment1dot16 requests just the UUID of the current environment,
when using an older apiserver that does not support CurrentEnvironment
API call. ?

https://codereview.appspot.com/50090044/

Revision history for this message
Francesco Banconi (frankban) wrote :

Please take a look.

https://codereview.appspot.com/50090044/diff/1/state/api/params/internal.go
File state/api/params/internal.go (right):

https://codereview.appspot.com/50090044/diff/1/state/api/params/internal.go#newcode115
state/api/params/internal.go:115: // environment.
On 2014/02/03 11:20:12, dimitern wrote:
> // EnvironmentResult holds the result of an API call returning a name
and UUID
> for an environment ?

> Also s/CurrentEnvironmentResult/EnvironmentResult/ ?

Done.

https://codereview.appspot.com/50090044/diff/1/state/api/uniter/environ.go
File state/api/uniter/environ.go (left):

https://codereview.appspot.com/50090044/diff/1/state/api/uniter/environ.go#oldcode23
state/api/uniter/environ.go:23: // TODO(dimitern): 2013-09-06 bug
1221834

On 2014/02/03 11:20:12, dimitern wrote:
> Can you update the bug to exclude Environ.UUID() from the list,
because with
> this change I think we can consider it cached.

Done.

https://codereview.appspot.com/50090044/diff/1/state/api/uniter/environ.go
File state/api/uniter/environ.go (right):

https://codereview.appspot.com/50090044/diff/1/state/api/uniter/environ.go#newcode9
state/api/uniter/environ.go:9: // Environment represents the name and
the unique identifier of an environment.
On 2014/02/03 11:20:12, dimitern wrote:
> No need to change this comment I think - see other examples in
api/uniter.

Done.

https://codereview.appspot.com/50090044/diff/1/state/api/uniter/uniter.go
File state/api/uniter/uniter.go (right):

https://codereview.appspot.com/50090044/diff/1/state/api/uniter/uniter.go#newcode164
state/api/uniter/uniter.go:164: if err != nil {
On 2014/02/03 11:20:12, dimitern wrote:
> you need to check result.Error as well, in addition to checking err -
see
> APIAddresses below.

Done.

https://codereview.appspot.com/50090044/diff/1/state/api/uniter/uniter.go#newcode173
state/api/uniter/uniter.go:173: func (st *State) environment1dot16()
(*Environment, error) {
On 2014/02/03 11:20:12, dimitern wrote:
> // environment1dot16 requests just the UUID of the current
environment, when
> using an older apiserver that does not support CurrentEnvironment API
call. ?

Done.

https://codereview.appspot.com/50090044/

Revision history for this message
Francesco Banconi (frankban) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'doc/charms-in-action.txt'
2--- doc/charms-in-action.txt 2013-05-27 08:03:26 +0000
3+++ doc/charms-in-action.txt 2014-02-03 11:46:53 +0000
4@@ -104,6 +104,7 @@
5 * $JUJU_CONTEXT_ID and $JUJU_AGENT_SOCKET are set (but should not be messed
6 with: the command line tools won't work without them).
7 * $JUJU_API_ADDRESSES holds a space separated list of juju API addresses.
8+ * $JUJU_ENV_NAME holds the human friendly name of the current environment.
9
10 Hook tools
11 ----------
12
13=== modified file 'state/api/params/internal.go'
14--- state/api/params/internal.go 2014-01-21 18:43:35 +0000
15+++ state/api/params/internal.go 2014-02-03 11:46:53 +0000
16@@ -96,7 +96,7 @@
17 Results []StringResult
18 }
19
20-// CharmArchiveURLResult holds a charm archive (bunle) URL, a
21+// CharmArchiveURLResult holds a charm archive (bundle) URL, a
22 // DisableSSLHostnameVerification flag or an error.
23 type CharmArchiveURLResult struct {
24 Error *Error
25@@ -111,6 +111,14 @@
26 Results []CharmArchiveURLResult
27 }
28
29+// EnvironmentResult holds the result of an API call returning a name and UUID
30+// for an environment.
31+type EnvironmentResult struct {
32+ Error *Error
33+ Name string
34+ UUID string
35+}
36+
37 // ResolvedModeResult holds a resolved mode or an error.
38 type ResolvedModeResult struct {
39 Error *Error
40
41=== modified file 'state/api/uniter/environ.go'
42--- state/api/uniter/environ.go 2013-09-06 16:46:48 +0000
43+++ state/api/uniter/environ.go 2014-02-03 11:46:53 +0000
44@@ -3,30 +3,21 @@
45
46 package uniter
47
48-import (
49- "launchpad.net/juju-core/state/api/params"
50-)
51-
52 // This module implements a subset of the interface provided by
53 // state.Environment, as needed by the uniter API.
54
55 // Environment represents the state of an environment.
56 type Environment struct {
57- st *State
58+ name string
59+ uuid string
60 }
61
62 // UUID returns the universally unique identifier of the environment.
63-//
64-// NOTE: This differs from state.Environment.UUID() by returning an
65-// error as well, because it needs to make an API call
66-//
67-// TODO(dimitern): 2013-09-06 bug 1221834
68-// Cache this after getting it once - it's immutable.
69-func (e Environment) UUID() (string, error) {
70- var result params.StringResult
71- err := e.st.caller.Call("Uniter", "", "CurrentEnvironUUID", nil, &result)
72- if err != nil {
73- return "", err
74- }
75- return result.Result, nil
76+func (e Environment) UUID() string {
77+ return e.uuid
78+}
79+
80+// Name returns the human friendly name of the environment.
81+func (e Environment) Name() string {
82+ return e.name
83 }
84
85=== modified file 'state/api/uniter/environ_test.go'
86--- state/api/uniter/environ_test.go 2013-09-06 13:54:56 +0000
87+++ state/api/uniter/environ_test.go 2014-02-03 11:46:53 +0000
88@@ -5,16 +5,26 @@
89
90 import (
91 gc "launchpad.net/gocheck"
92+
93+ "launchpad.net/juju-core/state"
94+ "launchpad.net/juju-core/state/api/uniter"
95 )
96
97 type environSuite struct {
98 uniterSuite
99+ apiEnviron *uniter.Environment
100+ stateEnviron *state.Environment
101 }
102
103 var _ = gc.Suite(&environSuite{})
104
105 func (s *environSuite) SetUpTest(c *gc.C) {
106 s.uniterSuite.SetUpTest(c)
107+ var err error
108+ s.apiEnviron, err = s.uniter.Environment()
109+ c.Assert(err, gc.IsNil)
110+ s.stateEnviron, err = s.State.Environment()
111+ c.Assert(err, gc.IsNil)
112 }
113
114 func (s *environSuite) TearDownTest(c *gc.C) {
115@@ -22,11 +32,9 @@
116 }
117
118 func (s *environSuite) TestUUID(c *gc.C) {
119- apiEnviron, err := s.uniter.Environment()
120- c.Assert(err, gc.IsNil)
121- stateEnviron, err := s.State.Environment()
122- c.Assert(err, gc.IsNil)
123- uuid, err := apiEnviron.UUID()
124- c.Assert(err, gc.IsNil)
125- c.Assert(uuid, gc.Equals, stateEnviron.UUID())
126+ c.Assert(s.apiEnviron.UUID(), gc.Equals, s.stateEnviron.UUID())
127+}
128+
129+func (s *environSuite) TestName(c *gc.C) {
130+ c.Assert(s.apiEnviron.Name(), gc.Equals, s.stateEnviron.Name())
131 }
132
133=== modified file 'state/api/uniter/uniter.go'
134--- state/api/uniter/uniter.go 2014-01-23 00:10:10 +0000
135+++ state/api/uniter/uniter.go 2014-02-03 11:46:53 +0000
136@@ -155,7 +155,38 @@
137
138 // Environment returns the environment entity.
139 func (st *State) Environment() (*Environment, error) {
140- return &Environment{st}, nil
141+ var result params.EnvironmentResult
142+ err := st.caller.Call("Uniter", "", "CurrentEnvironment", nil, &result)
143+ if params.IsCodeNotImplemented(err) {
144+ // Fall back to using the 1.16 API.
145+ return st.environment1dot16()
146+ }
147+ if err != nil {
148+ return nil, err
149+ }
150+ if err := result.Error; err != nil {
151+ return nil, err
152+ }
153+ return &Environment{
154+ name: result.Name,
155+ uuid: result.UUID,
156+ }, nil
157+}
158+
159+// environment1dot16 requests just the UUID of the current environment, when
160+// using an older API server that does not support CurrentEnvironment API call.
161+func (st *State) environment1dot16() (*Environment, error) {
162+ var result params.StringResult
163+ err := st.caller.Call("Uniter", "", "CurrentEnvironUUID", nil, &result)
164+ if err != nil {
165+ return nil, err
166+ }
167+ if err := result.Error; err != nil {
168+ return nil, err
169+ }
170+ return &Environment{
171+ uuid: result.Result,
172+ }, nil
173 }
174
175 // APIAddresses returns the list of addresses used to connect to the API.
176
177=== modified file 'state/apiserver/uniter/uniter.go'
178--- state/apiserver/uniter/uniter.go 2014-01-22 22:06:48 +0000
179+++ state/apiserver/uniter/uniter.go 2014-02-03 11:46:53 +0000
180@@ -749,6 +749,17 @@
181 return result, err
182 }
183
184+// CurrentEnvironment returns the name and UUID for the current juju environment.
185+func (u *UniterAPI) CurrentEnvironment() (params.EnvironmentResult, error) {
186+ result := params.EnvironmentResult{}
187+ env, err := u.st.Environment()
188+ if err == nil {
189+ result.Name = env.Name()
190+ result.UUID = env.UUID()
191+ }
192+ return result, err
193+}
194+
195 // ProviderType returns the provider type used by the current juju
196 // environment.
197 //
198
199=== modified file 'state/apiserver/uniter/uniter_test.go'
200--- state/apiserver/uniter/uniter_test.go 2014-01-24 12:39:48 +0000
201+++ state/apiserver/uniter/uniter_test.go 2014-02-03 11:46:53 +0000
202@@ -900,6 +900,19 @@
203 c.Assert(result, gc.DeepEquals, params.StringResult{Result: env.UUID()})
204 }
205
206+func (s *uniterSuite) TestCurrentEnvironment(c *gc.C) {
207+ env, err := s.State.Environment()
208+ c.Assert(err, gc.IsNil)
209+
210+ result, err := s.uniter.CurrentEnvironment()
211+ c.Assert(err, gc.IsNil)
212+ expected := params.EnvironmentResult{
213+ Name: env.Name(),
214+ UUID: env.UUID(),
215+ }
216+ c.Assert(result, gc.DeepEquals, expected)
217+}
218+
219 func (s *uniterSuite) addRelation(c *gc.C, first, second string) *state.Relation {
220 eps, err := s.State.InferEndpoints([]string{first, second})
221 c.Assert(err, gc.IsNil)
222
223=== modified file 'state/environ.go'
224--- state/environ.go 2014-01-08 02:46:32 +0000
225+++ state/environ.go 2014-02-03 11:46:53 +0000
226@@ -55,6 +55,11 @@
227 return e.doc.UUID
228 }
229
230+// Name returns the human friendly name of the environment.
231+func (e *Environment) Name() string {
232+ return e.doc.Name
233+}
234+
235 // Life returns whether the environment is Alive, Dying or Dead.
236 func (e *Environment) Life() Life {
237 return e.doc.Life
238
239=== modified file 'state/environ_test.go'
240--- state/environ_test.go 2013-11-19 06:43:27 +0000
241+++ state/environ_test.go 2014-02-03 11:46:53 +0000
242@@ -28,6 +28,10 @@
243 c.Assert(s.env.Tag(), gc.Equals, expected)
244 }
245
246+func (s *EnvironSuite) TestName(c *gc.C) {
247+ c.Assert(s.env.Name(), gc.Equals, "testenv")
248+}
249+
250 func (s *EnvironSuite) TestUUID(c *gc.C) {
251 uuidA := s.env.UUID()
252 c.Assert(uuidA, gc.HasLen, 36)
253
254=== modified file 'worker/uniter/context.go'
255--- worker/uniter/context.go 2014-01-28 04:58:43 +0000
256+++ worker/uniter/context.go 2014-02-03 11:46:53 +0000
257@@ -60,6 +60,9 @@
258 // uuid is the universally unique identifier of the environment.
259 uuid string
260
261+ // envName is the human friendly name of the environment.
262+ envName string
263+
264 // relationId identifies the relation for which a relation hook is
265 // executing. If it is -1, the context is not running a relation hook;
266 // otherwise, its value must be a valid key into the relations map.
267@@ -84,13 +87,14 @@
268 proxySettings osenv.ProxySettings
269 }
270
271-func NewHookContext(unit *uniter.Unit, id, uuid string, relationId int,
272- remoteUnitName string, relations map[int]*ContextRelation,
273+func NewHookContext(unit *uniter.Unit, id, uuid, envName string,
274+ relationId int, remoteUnitName string, relations map[int]*ContextRelation,
275 apiAddrs []string, serviceOwner string, proxySettings osenv.ProxySettings) (*HookContext, error) {
276 ctx := &HookContext{
277 unit: unit,
278 id: id,
279 uuid: uuid,
280+ envName: envName,
281 relationId: relationId,
282 remoteUnitName: remoteUnitName,
283 relations: relations,
284@@ -184,6 +188,7 @@
285 "JUJU_AGENT_SOCKET=" + socketPath,
286 "JUJU_UNIT_NAME=" + ctx.unit.Name(),
287 "JUJU_ENV_UUID=" + ctx.uuid,
288+ "JUJU_ENV_NAME=" + ctx.envName,
289 "JUJU_API_ADDRESSES=" + strings.Join(ctx.apiAddrs, " "),
290 }
291 if r, found := ctx.HookRelation(); found {
292
293=== modified file 'worker/uniter/context_test.go'
294--- worker/uniter/context_test.go 2014-01-23 21:57:42 +0000
295+++ worker/uniter/context_test.go 2014-02-03 11:46:53 +0000
296@@ -181,6 +181,7 @@
297 env: map[string]string{
298 "JUJU_UNIT_NAME": "u/0",
299 "JUJU_API_ADDRESSES": expectedApiAddrs,
300+ "JUJU_ENV_NAME": "test-env-name",
301 "http_proxy": "http",
302 "HTTP_PROXY": "http",
303 "https_proxy": "https",
304@@ -195,6 +196,7 @@
305 env: map[string]string{
306 "JUJU_UNIT_NAME": "u/0",
307 "JUJU_API_ADDRESSES": expectedApiAddrs,
308+ "JUJU_ENV_NAME": "test-env-name",
309 "JUJU_RELATION": "db",
310 "JUJU_RELATION_ID": "db:1",
311 "JUJU_REMOTE_UNIT": "",
312@@ -207,6 +209,7 @@
313 env: map[string]string{
314 "JUJU_UNIT_NAME": "u/0",
315 "JUJU_API_ADDRESSES": expectedApiAddrs,
316+ "JUJU_ENV_NAME": "test-env-name",
317 "JUJU_RELATION": "db",
318 "JUJU_RELATION_ID": "db:1",
319 "JUJU_REMOTE_UNIT": "r/1",
320@@ -710,8 +713,9 @@
321 _, found := s.relctxs[relid]
322 c.Assert(found, gc.Equals, true)
323 }
324- context, err := uniter.NewHookContext(s.apiUnit, "TestCtx", uuid, relid, remote,
325- s.relctxs, apiAddrs, "test-owner", proxies)
326+ context, err := uniter.NewHookContext(s.apiUnit, "TestCtx", uuid,
327+ "test-env-name", relid, remote, s.relctxs, apiAddrs, "test-owner",
328+ proxies)
329 c.Assert(err, gc.IsNil)
330 return context
331 }
332@@ -764,6 +768,7 @@
333 "JUJU_CONTEXT_ID": "TestCtx",
334 "JUJU_AGENT_SOCKET": "/path/to/socket",
335 "JUJU_UNIT_NAME": "u/0",
336+ "JUJU_ENV_NAME": "test-env-name",
337 }
338 for key, value := range expected {
339 c.Check(executionEnvironment[key], gc.Equals, value)
340
341=== modified file 'worker/uniter/uniter.go'
342--- worker/uniter/uniter.go 2014-01-28 04:58:43 +0000
343+++ worker/uniter/uniter.go 2014-02-03 11:46:53 +0000
344@@ -66,6 +66,7 @@
345 relationers map[int]*Relationer
346 relationHooks chan hook.Info
347 uuid string
348+ envName string
349
350 dataDir string
351 baseDir string
352@@ -189,10 +190,8 @@
353 if err != nil {
354 return err
355 }
356- u.uuid, err = env.UUID()
357- if err != nil {
358- return err
359- }
360+ u.uuid = env.UUID()
361+ u.envName = env.Name()
362
363 runListenerSocketPath := filepath.Join(u.baseDir, RunListenerFile)
364 logger.Debugf("starting juju-run listener on %s:%s", RunListenerNetType, runListenerSocketPath)
365@@ -341,8 +340,8 @@
366
367 // Make a copy of the proxy settings.
368 proxySettings := u.proxy
369- return NewHookContext(u.unit, hctxId, u.uuid, relationId, remoteUnitName,
370- ctxRelations, apiAddrs, ownerTag, proxySettings)
371+ return NewHookContext(u.unit, hctxId, u.uuid, u.envName, relationId,
372+ remoteUnitName, ctxRelations, apiAddrs, ownerTag, proxySettings)
373 }
374
375 func (u *Uniter) acquireHookLock(message string) (err error) {

Subscribers

People subscribed via source and target branches

to status/vote changes: