Merge lp:~niemeyer/pyjuju/go-final-formula-meta into lp:pyjuju/go

Proposed by Gustavo Niemeyer
Status: Merged
Approved by: Kapil Thangavelu
Approved revision: 10
Merge reported by: Gustavo Niemeyer
Merged at revision: not available
Proposed branch: lp:~niemeyer/pyjuju/go-final-formula-meta
Merge into: lp:pyjuju/go
Prerequisite: lp:~niemeyer/pyjuju/go-rename-short-types
Diff against target: 330 lines (+172/-5)
16 files modified
formula/formula.go (+36/-0)
formula/formula_test.go (+30/-5)
formula/testrepo/dummy/.ignored (+1/-0)
formula/testrepo/dummy/config.yaml (+5/-0)
formula/testrepo/dummy/metadata.yaml (+7/-0)
formula/testrepo/dummy/src/hello.c (+7/-0)
formula/testrepo/mysql/metadata.yaml (+7/-0)
formula/testrepo/mysql2/metadata.yaml (+11/-0)
formula/testrepo/new/metadata.yaml (+7/-0)
formula/testrepo/old/metadata.yaml (+7/-0)
formula/testrepo/riak/metadata.yaml (+13/-0)
formula/testrepo/varnish/metadata.yaml (+7/-0)
formula/testrepo/varnish2/hooks/install (+3/-0)
formula/testrepo/varnish2/metadata.yaml (+7/-0)
formula/testrepo/wordpress/config.yaml (+3/-0)
formula/testrepo/wordpress/metadata.yaml (+21/-0)
To merge this branch: bzr merge lp:~niemeyer/pyjuju/go-final-formula-meta
Reviewer Review Type Date Requested Status
Kapil Thangavelu (community) Approve
William Reade (community) Approve
Review via email: mp+73304@code.launchpad.net

Description of the change

This branch adds support for parsing relations in the metadata.yaml file, completing support for metadata parsing in Go.

To post a comment you must log in.
Revision history for this message
William Reade (fwereade) wrote :

+1. As you know, I fret about duplication, but I agree that test data is not really where we need to worry ;).

review: Approve
Revision history for this message
Kapil Thangavelu (hazmat) wrote :

i learned some new golang stuff from this branch. looks good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'formula/formula.go'
2--- formula/formula.go 2011-08-29 21:58:23 +0000
3+++ formula/formula.go 2011-08-29 21:58:23 +0000
4@@ -34,11 +34,24 @@
5 return
6 }
7
8+// Relation represents a single relation defined in the formula
9+// metadata.yaml file.
10+type Relation struct {
11+ Interface string
12+ Optional bool
13+ Limit int
14+}
15+
16+// Meta represents all the known content that may be defined
17+// within a formula's metadata.yaml file.
18 type Meta struct {
19 Name string
20 Revision int
21 Summary string
22 Description string
23+ Provides map[string]Relation
24+ Requires map[string]Relation
25+ Peers map[string]Relation
26 }
27
28 // ReadMeta reads a metadata.yaml file and returns its representation.
29@@ -74,9 +87,32 @@
30 meta.Revision = int(m["revision"].(int64))
31 meta.Summary = m["summary"].(string)
32 meta.Description = m["description"].(string)
33+ meta.Provides = parseRelations(m["provides"])
34+ meta.Requires = parseRelations(m["requires"])
35+ meta.Peers = parseRelations(m["peers"])
36 return
37 }
38
39+func parseRelations(relations interface{}) map[string]Relation {
40+ if relations == nil {
41+ return nil
42+ }
43+ result := make(map[string]Relation)
44+ for name, rel := range relations.(schema.MapType) {
45+ relMap := rel.(schema.MapType)
46+ relation := Relation{}
47+ relation.Interface = relMap["interface"].(string)
48+ relation.Optional = relMap["optional"].(bool)
49+ if relMap["limit"] != nil {
50+ // Schema defaults to int64, but we know
51+ // the int range should be more than enough.
52+ relation.Limit = int(relMap["limit"].(int64))
53+ }
54+ result[name.(string)] = relation
55+ }
56+ return result
57+}
58+
59 // Schema coercer that expands the interface shorthand notation.
60 // A consistent format is easier to work with than considering the
61 // potential difference everywhere.
62
63=== modified file 'formula/formula_test.go'
64--- formula/formula_test.go 2011-08-29 21:58:23 +0000
65+++ formula/formula_test.go 2011-08-29 21:58:23 +0000
66@@ -38,10 +38,12 @@
67 c.Assert(err, Matches, `Missing formula revision: "local:foo-x"`)
68 }
69
70-const dummyMeta = "testrepo/dummy/metadata.yaml"
71+func repoMeta(name string) (path string) {
72+ return filepath.Join("testrepo", name, "metadata.yaml")
73+}
74
75 func (s *S) TestReadMeta(c *C) {
76- meta, err := formula.ReadMeta(dummyMeta)
77+ meta, err := formula.ReadMeta(repoMeta("dummy"))
78 c.Assert(err, IsNil)
79 c.Assert(meta.Name, Equals, "dummy")
80 c.Assert(meta.Revision, Equals, 1)
81@@ -51,7 +53,7 @@
82 }
83
84 func (s *S) TestParseMeta(c *C) {
85- data, err := ioutil.ReadFile(dummyMeta)
86+ data, err := ioutil.ReadFile(repoMeta("dummy"))
87 c.Assert(err, IsNil)
88
89 meta, err := formula.ParseMeta(data)
90@@ -64,7 +66,7 @@
91 }
92
93 func (s *S) TestMetaHeader(c *C) {
94- yaml := ReadYaml(dummyMeta)
95+ yaml := ReadYaml(repoMeta("dummy"))
96 yaml["ensemble"] = "foo"
97 data := DumpYaml(yaml)
98
99@@ -73,7 +75,7 @@
100 }
101
102 func (s *S) TestMetaErrorWithPath(c *C) {
103- yaml := ReadYaml(dummyMeta)
104+ yaml := ReadYaml(repoMeta("dummy"))
105 yaml["ensemble"] = "foo"
106 data := DumpYaml(yaml)
107
108@@ -89,6 +91,29 @@
109 c.Assert(err, Matches, `/.*/mymeta\.yaml: ensemble: expected "formula", got "foo"`)
110 }
111
112+func (s *S) TestParseMetaRelations(c *C) {
113+ meta, err := formula.ReadMeta(repoMeta("mysql"))
114+ c.Assert(err, IsNil)
115+ c.Assert(meta.Provides["server"], Equals, formula.Relation{Interface: "mysql"})
116+ c.Assert(meta.Requires, IsNil)
117+ c.Assert(meta.Peers, IsNil)
118+
119+ meta, err = formula.ReadMeta(repoMeta("riak"))
120+ c.Assert(err, IsNil)
121+ c.Assert(meta.Provides["endpoint"], Equals, formula.Relation{Interface: "http"})
122+ c.Assert(meta.Provides["admin"], Equals, formula.Relation{Interface: "http"})
123+ c.Assert(meta.Peers["ring"], Equals, formula.Relation{Interface: "riak", Limit: 1})
124+ c.Assert(meta.Requires, IsNil)
125+
126+ meta, err = formula.ReadMeta(repoMeta("wordpress"))
127+ c.Assert(err, IsNil)
128+ c.Assert(meta.Provides["url"], Equals, formula.Relation{Interface: "http"})
129+ c.Assert(meta.Requires["db"], Equals, formula.Relation{Interface: "mysql", Limit: 1})
130+ c.Assert(meta.Requires["cache"], Equals, formula.Relation{Interface: "varnish", Limit: 2, Optional: true})
131+ c.Assert(meta.Peers, IsNil)
132+
133+}
134+
135 // Test rewriting of a given interface specification into long form.
136 //
137 // InterfaceExpander uses `coerce` to do one of two things:
138
139=== added directory 'formula/testrepo'
140=== added directory 'formula/testrepo/dummy'
141=== added file 'formula/testrepo/dummy/.ignored'
142--- formula/testrepo/dummy/.ignored 1970-01-01 00:00:00 +0000
143+++ formula/testrepo/dummy/.ignored 2011-08-29 21:58:23 +0000
144@@ -0,0 +1,1 @@
145+#
146\ No newline at end of file
147
148=== added directory 'formula/testrepo/dummy/build'
149=== added file 'formula/testrepo/dummy/build/ignored'
150=== added file 'formula/testrepo/dummy/config.yaml'
151--- formula/testrepo/dummy/config.yaml 1970-01-01 00:00:00 +0000
152+++ formula/testrepo/dummy/config.yaml 2011-08-29 21:58:23 +0000
153@@ -0,0 +1,5 @@
154+options:
155+ title: {default: My Title, description: A descriptive title used for the service., type: str}
156+ outlook: {description: No default outlook., type: str}
157+ username: {default: admin001, description: The name of the initial account (given admin permissions)., type: regex, validator: '\w{8}'}
158+ skill-level: {description: A number indicating skill., type: int}
159
160=== added directory 'formula/testrepo/dummy/empty'
161=== added file 'formula/testrepo/dummy/metadata.yaml'
162--- formula/testrepo/dummy/metadata.yaml 1970-01-01 00:00:00 +0000
163+++ formula/testrepo/dummy/metadata.yaml 2011-08-29 21:58:23 +0000
164@@ -0,0 +1,7 @@
165+ensemble: formula
166+name: dummy
167+revision: 1
168+summary: "That's a dummy formula."
169+description: |
170+ This is a longer description which
171+ potentially contains multiple lines.
172
173=== added directory 'formula/testrepo/dummy/src'
174=== added file 'formula/testrepo/dummy/src/hello.c'
175--- formula/testrepo/dummy/src/hello.c 1970-01-01 00:00:00 +0000
176+++ formula/testrepo/dummy/src/hello.c 2011-08-29 21:58:23 +0000
177@@ -0,0 +1,7 @@
178+#include <stdio.h>
179+
180+main()
181+{
182+ printf ("Hello World!\n");
183+ return 0;
184+}
185
186=== added directory 'formula/testrepo/mysql'
187=== added file 'formula/testrepo/mysql/metadata.yaml'
188--- formula/testrepo/mysql/metadata.yaml 1970-01-01 00:00:00 +0000
189+++ formula/testrepo/mysql/metadata.yaml 2011-08-29 21:58:23 +0000
190@@ -0,0 +1,7 @@
191+ensemble: formula
192+name: mysql
193+revision: 1
194+summary: "Database engine"
195+description: "A pretty popular database"
196+provides:
197+ server: mysql
198
199=== added directory 'formula/testrepo/mysql2'
200=== added file 'formula/testrepo/mysql2/metadata.yaml'
201--- formula/testrepo/mysql2/metadata.yaml 1970-01-01 00:00:00 +0000
202+++ formula/testrepo/mysql2/metadata.yaml 2011-08-29 21:58:23 +0000
203@@ -0,0 +1,11 @@
204+ensemble: formula
205+name: mysql2
206+revision: 1
207+summary: "Database engine"
208+description: "A pretty popular database"
209+provides:
210+ prod:
211+ interface: mysql
212+ dev:
213+ interface: mysql
214+ limit: 2
215
216=== added directory 'formula/testrepo/new'
217=== added file 'formula/testrepo/new/metadata.yaml'
218--- formula/testrepo/new/metadata.yaml 1970-01-01 00:00:00 +0000
219+++ formula/testrepo/new/metadata.yaml 2011-08-29 21:58:23 +0000
220@@ -0,0 +1,7 @@
221+ensemble: formula
222+name: sample
223+revision: 2
224+summary: "That's a sample formula."
225+description: |
226+ This is a longer description which
227+ potentially contains multiple lines.
228
229=== added directory 'formula/testrepo/old'
230=== added file 'formula/testrepo/old/metadata.yaml'
231--- formula/testrepo/old/metadata.yaml 1970-01-01 00:00:00 +0000
232+++ formula/testrepo/old/metadata.yaml 2011-08-29 21:58:23 +0000
233@@ -0,0 +1,7 @@
234+ensemble: formula
235+name: sample
236+revision: 1
237+summary: "That's a sample formula."
238+description: |
239+ This is a longer description which
240+ potentially contains multiple lines.
241
242=== added directory 'formula/testrepo/riak'
243=== added file 'formula/testrepo/riak/metadata.yaml'
244--- formula/testrepo/riak/metadata.yaml 1970-01-01 00:00:00 +0000
245+++ formula/testrepo/riak/metadata.yaml 2011-08-29 21:58:23 +0000
246@@ -0,0 +1,13 @@
247+ensemble: formula
248+name: riak
249+revision: 7
250+summary: "K/V storage engine"
251+description: "Scalable K/V Store in Erlang with Clocks :-)"
252+provides:
253+ endpoint:
254+ interface: http
255+ admin:
256+ interface: http
257+peers:
258+ ring:
259+ interface: riak
260
261=== added directory 'formula/testrepo/varnish'
262=== added file 'formula/testrepo/varnish/metadata.yaml'
263--- formula/testrepo/varnish/metadata.yaml 1970-01-01 00:00:00 +0000
264+++ formula/testrepo/varnish/metadata.yaml 2011-08-29 21:58:23 +0000
265@@ -0,0 +1,7 @@
266+ensemble: formula
267+name: varnish
268+revision: 1
269+summary: "Database engine"
270+description: "Another popular database"
271+provides:
272+ webcache: varnish
273
274=== added directory 'formula/testrepo/varnish2'
275=== added directory 'formula/testrepo/varnish2/hooks'
276=== added file 'formula/testrepo/varnish2/hooks/install'
277--- formula/testrepo/varnish2/hooks/install 1970-01-01 00:00:00 +0000
278+++ formula/testrepo/varnish2/hooks/install 2011-08-29 21:58:23 +0000
279@@ -0,0 +1,3 @@
280+#!/bin/bash
281+
282+echo hello world
283\ No newline at end of file
284
285=== added file 'formula/testrepo/varnish2/metadata.yaml'
286--- formula/testrepo/varnish2/metadata.yaml 1970-01-01 00:00:00 +0000
287+++ formula/testrepo/varnish2/metadata.yaml 2011-08-29 21:58:23 +0000
288@@ -0,0 +1,7 @@
289+ensemble: formula
290+name: varnish
291+revision: 1
292+summary: "Database engine"
293+description: "Another popular database"
294+provides:
295+ webcache: varnish
296
297=== added directory 'formula/testrepo/wordpress'
298=== added file 'formula/testrepo/wordpress/config.yaml'
299--- formula/testrepo/wordpress/config.yaml 1970-01-01 00:00:00 +0000
300+++ formula/testrepo/wordpress/config.yaml 2011-08-29 21:58:23 +0000
301@@ -0,0 +1,3 @@
302+options:
303+ blog-title: {default: My Title, description: A descriptive title used for the blog., type: str}
304+
305
306=== added file 'formula/testrepo/wordpress/metadata.yaml'
307--- formula/testrepo/wordpress/metadata.yaml 1970-01-01 00:00:00 +0000
308+++ formula/testrepo/wordpress/metadata.yaml 2011-08-29 21:58:23 +0000
309@@ -0,0 +1,21 @@
310+ensemble: formula
311+name: wordpress
312+revision: 3
313+summary: "Blog engine"
314+description: "A pretty popular blog engine"
315+provides:
316+ url:
317+ interface: http
318+ limit:
319+ optional: false
320+requires:
321+ db:
322+ interface: mysql
323+ limit: 1
324+ optional: false
325+ cache:
326+ interface: varnish
327+ limit: 2
328+ optional: true
329+
330+

Subscribers

People subscribed via source and target branches