Merge lp:~dimitern/juju-core/102-state-endpoint-refactoring into lp:~go-bot/juju-core/trunk

Proposed by Dimiter Naydenov
Status: Merged
Approved by: Dimiter Naydenov
Approved revision: no longer in the source branch.
Merged at revision: 1660
Proposed branch: lp:~dimitern/juju-core/102-state-endpoint-refactoring
Merge into: lp:~go-bot/juju-core/trunk
Diff against target: 278 lines (+119/-122)
4 files modified
charm/meta.go (+41/-0)
charm/meta_test.go (+78/-0)
state/endpoint.go (+0/-41)
state/endpoint_test.go (+0/-81)
To merge this branch: bzr merge lp:~dimitern/juju-core/102-state-endpoint-refactoring
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+180141@code.launchpad.net

Commit message

state;charm: Refactor state/Endpoint

Two methods of state.Endpoint are not related to state,
more to charm metadata. Endpoint embeds a charm.Relation,
so I moved IsImplicit() and ImplementedBy() from Endpoint
to charm.Relation, along with their tests.
This is necessary, so that we can avoid implementing API
calls for them in the uniter API facade, becasue the
uniter needs them.

https://codereview.appspot.com/12906044/

R=jameinel, rogpeppe

Description of the change

state;charm: Refactor state/Endpoint

Two methods of state.Endpoint are not related to state,
more to charm metadata. Endpoint embeds a charm.Relation,
so I moved IsImplicit() and ImplementedBy() from Endpoint
to charm.Relation, along with their tests.
This is necessary, so that we can avoid implementing API
calls for them in the uniter API facade, becasue the
uniter needs them.

https://codereview.appspot.com/12906044/

To post a comment you must log in.
Revision history for this message
Dimiter Naydenov (dimitern) wrote :

Reviewers: mp+180141_code.launchpad.net,

Message:
Please take a look.

Description:
state;charm: Refactor state/Endpoint

Two methods of state.Endpoint are not related to state,
more to charm metadata. Endpoint embeds a charm.Relation,
so I moved IsImplicit() and ImplementedBy() from Endpoint
to charm.Relation, along with their tests.
This is necessary, so that we can avoid implementing API
calls for them in the uniter API facade, becasue the
uniter needs them.

https://code.launchpad.net/~dimitern/juju-core/102-state-endpoint-refactoring/+merge/180141

(do not edit description out of merge proposal)

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

Affected files:
   A [revision details]
   M charm/meta.go
   M charm/meta_test.go
   M state/endpoint_test.go

Revision history for this message
Dimiter Naydenov (dimitern) wrote :
Revision history for this message
John A Meinel (jameinel) wrote :

This just moves the functions to charm.Relation which has all the
information we were operating on.

LGTM

https://codereview.appspot.com/12906044/

Revision history for this message
Roger Peppe (rogpeppe) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charm/meta.go'
2--- charm/meta.go 2013-07-09 10:32:23 +0000
3+++ charm/meta.go 2013-08-14 14:03:11 +0000
4@@ -48,6 +48,47 @@
5 Scope RelationScope
6 }
7
8+// ImplementedBy returns whether the relation is implemented by the supplied charm.
9+func (r Relation) ImplementedBy(ch Charm) bool {
10+ if r.IsImplicit() {
11+ return true
12+ }
13+ var m map[string]Relation
14+ switch r.Role {
15+ case RoleProvider:
16+ m = ch.Meta().Provides
17+ case RoleRequirer:
18+ m = ch.Meta().Requires
19+ case RolePeer:
20+ m = ch.Meta().Peers
21+ default:
22+ panic(fmt.Errorf("unknown relation role %q", r.Role))
23+ }
24+ rel, found := m[r.Name]
25+ if !found {
26+ return false
27+ }
28+ if rel.Interface == r.Interface {
29+ switch r.Scope {
30+ case ScopeGlobal:
31+ return rel.Scope != ScopeContainer
32+ case ScopeContainer:
33+ return true
34+ default:
35+ panic(fmt.Errorf("unknown relation scope %q", r.Scope))
36+ }
37+ }
38+ return false
39+}
40+
41+// IsImplicit returns whether the relation is supplied by juju itself,
42+// rather than by a charm.
43+func (r Relation) IsImplicit() bool {
44+ return (r.Name == "juju-info" &&
45+ r.Interface == "juju-info" &&
46+ r.Role == RoleProvider)
47+}
48+
49 // Meta represents all the known content that may be defined
50 // within a charm's metadata.yaml file.
51 type Meta struct {
52
53=== modified file 'charm/meta_test.go'
54--- charm/meta_test.go 2013-08-08 09:17:27 +0000
55+++ charm/meta_test.go 2013-08-14 14:03:11 +0000
56@@ -417,3 +417,81 @@
57 c.Assert(input, DeepEquals, output)
58 }
59 }
60+
61+var implementedByTests = []struct {
62+ ifce string
63+ name string
64+ role charm.RelationRole
65+ scope charm.RelationScope
66+ match bool
67+ implicit bool
68+}{
69+ {"ifce-pro", "pro", charm.RoleProvider, charm.ScopeGlobal, true, false},
70+ {"blah", "pro", charm.RoleProvider, charm.ScopeGlobal, false, false},
71+ {"ifce-pro", "blah", charm.RoleProvider, charm.ScopeGlobal, false, false},
72+ {"ifce-pro", "pro", charm.RoleRequirer, charm.ScopeGlobal, false, false},
73+ {"ifce-pro", "pro", charm.RoleProvider, charm.ScopeContainer, true, false},
74+
75+ {"juju-info", "juju-info", charm.RoleProvider, charm.ScopeGlobal, true, true},
76+ {"blah", "juju-info", charm.RoleProvider, charm.ScopeGlobal, false, false},
77+ {"juju-info", "blah", charm.RoleProvider, charm.ScopeGlobal, false, false},
78+ {"juju-info", "juju-info", charm.RoleRequirer, charm.ScopeGlobal, false, false},
79+ {"juju-info", "juju-info", charm.RoleProvider, charm.ScopeContainer, true, true},
80+
81+ {"ifce-req", "req", charm.RoleRequirer, charm.ScopeGlobal, true, false},
82+ {"blah", "req", charm.RoleRequirer, charm.ScopeGlobal, false, false},
83+ {"ifce-req", "blah", charm.RoleRequirer, charm.ScopeGlobal, false, false},
84+ {"ifce-req", "req", charm.RolePeer, charm.ScopeGlobal, false, false},
85+ {"ifce-req", "req", charm.RoleRequirer, charm.ScopeContainer, true, false},
86+
87+ {"juju-info", "info", charm.RoleRequirer, charm.ScopeContainer, true, false},
88+ {"blah", "info", charm.RoleRequirer, charm.ScopeContainer, false, false},
89+ {"juju-info", "blah", charm.RoleRequirer, charm.ScopeContainer, false, false},
90+ {"juju-info", "info", charm.RolePeer, charm.ScopeContainer, false, false},
91+ {"juju-info", "info", charm.RoleRequirer, charm.ScopeGlobal, false, false},
92+
93+ {"ifce-peer", "peer", charm.RolePeer, charm.ScopeGlobal, true, false},
94+ {"blah", "peer", charm.RolePeer, charm.ScopeGlobal, false, false},
95+ {"ifce-peer", "blah", charm.RolePeer, charm.ScopeGlobal, false, false},
96+ {"ifce-peer", "peer", charm.RoleProvider, charm.ScopeGlobal, false, false},
97+ {"ifce-peer", "peer", charm.RolePeer, charm.ScopeContainer, true, false},
98+}
99+
100+func (s *MetaSuite) TestImplementedBy(c *C) {
101+ for i, t := range implementedByTests {
102+ c.Logf("test %d", i)
103+ r := charm.Relation{
104+ Interface: t.ifce,
105+ Name: t.name,
106+ Role: t.role,
107+ Scope: t.scope,
108+ }
109+ c.Assert(r.ImplementedBy(&dummyCharm{}), Equals, t.match)
110+ c.Assert(r.IsImplicit(), Equals, t.implicit)
111+ }
112+}
113+
114+type dummyCharm struct{}
115+
116+func (c *dummyCharm) Config() *charm.Config {
117+ panic("unused")
118+}
119+
120+func (c *dummyCharm) Revision() int {
121+ panic("unused")
122+}
123+
124+func (c *dummyCharm) Meta() *charm.Meta {
125+ return &charm.Meta{
126+ Provides: map[string]charm.Relation{
127+ "pro": {Interface: "ifce-pro", Scope: charm.ScopeGlobal},
128+ },
129+ Requires: map[string]charm.Relation{
130+ "req": {Interface: "ifce-req", Scope: charm.ScopeGlobal},
131+ "info": {Interface: "juju-info", Scope: charm.ScopeContainer},
132+ },
133+ Peers: map[string]charm.Relation{
134+ "peer": {Interface: "ifce-peer", Scope: charm.ScopeGlobal},
135+ },
136+ }
137+}
138
139=== modified file 'state/endpoint.go'
140--- state/endpoint.go 2013-07-09 10:32:23 +0000
141+++ state/endpoint.go 2013-08-14 14:03:11 +0000
142@@ -46,47 +46,6 @@
143 counterpartRole(ep.Role) == other.Role
144 }
145
146-// ImplementedBy returns whether the endpoint is implemented by the supplied charm.
147-func (ep Endpoint) ImplementedBy(ch charm.Charm) bool {
148- if ep.IsImplicit() {
149- return true
150- }
151- var m map[string]charm.Relation
152- switch ep.Role {
153- case charm.RoleProvider:
154- m = ch.Meta().Provides
155- case charm.RoleRequirer:
156- m = ch.Meta().Requires
157- case charm.RolePeer:
158- m = ch.Meta().Peers
159- default:
160- panic(fmt.Errorf("unknown relation role %q", ep.Role))
161- }
162- rel, found := m[ep.Name]
163- if !found {
164- return false
165- }
166- if rel.Interface == ep.Interface {
167- switch ep.Scope {
168- case charm.ScopeGlobal:
169- return rel.Scope != charm.ScopeContainer
170- case charm.ScopeContainer:
171- return true
172- default:
173- panic(fmt.Errorf("unknown relation scope %q", ep.Scope))
174- }
175- }
176- return false
177-}
178-
179-// IsImplicit returns whether the endpoint is supplied by juju itself,
180-// rather than by a charm.
181-func (ep Endpoint) IsImplicit() bool {
182- return (ep.Name == "juju-info" &&
183- ep.Interface == "juju-info" &&
184- ep.Role == charm.RoleProvider)
185-}
186-
187 type epSlice []Endpoint
188
189 var roleOrder = map[charm.RelationRole]int{
190
191=== modified file 'state/endpoint_test.go'
192--- state/endpoint_test.go 2013-07-09 10:32:23 +0000
193+++ state/endpoint_test.go 2013-08-14 14:03:11 +0000
194@@ -77,84 +77,3 @@
195 c.Assert(ep1.CanRelateTo(ep2), Equals, false)
196 c.Assert(ep2.CanRelateTo(ep1), Equals, false)
197 }
198-
199-type dummyCharm struct{}
200-
201-func (c *dummyCharm) Config() *charm.Config {
202- panic("unused")
203-}
204-
205-func (c *dummyCharm) Revision() int {
206- panic("unused")
207-}
208-
209-func (c *dummyCharm) Meta() *charm.Meta {
210- return &charm.Meta{
211- Provides: map[string]charm.Relation{
212- "pro": {Interface: "ifce-pro", Scope: charm.ScopeGlobal},
213- },
214- Requires: map[string]charm.Relation{
215- "req": {Interface: "ifce-req", Scope: charm.ScopeGlobal},
216- "info": {Interface: "juju-info", Scope: charm.ScopeContainer},
217- },
218- Peers: map[string]charm.Relation{
219- "peer": {Interface: "ifce-peer", Scope: charm.ScopeGlobal},
220- },
221- }
222-}
223-
224-var implementedByTests = []struct {
225- ifce string
226- name string
227- role charm.RelationRole
228- scope charm.RelationScope
229- match bool
230- implicit bool
231-}{
232- {"ifce-pro", "pro", charm.RoleProvider, charm.ScopeGlobal, true, false},
233- {"blah", "pro", charm.RoleProvider, charm.ScopeGlobal, false, false},
234- {"ifce-pro", "blah", charm.RoleProvider, charm.ScopeGlobal, false, false},
235- {"ifce-pro", "pro", charm.RoleRequirer, charm.ScopeGlobal, false, false},
236- {"ifce-pro", "pro", charm.RoleProvider, charm.ScopeContainer, true, false},
237-
238- {"juju-info", "juju-info", charm.RoleProvider, charm.ScopeGlobal, true, true},
239- {"blah", "juju-info", charm.RoleProvider, charm.ScopeGlobal, false, false},
240- {"juju-info", "blah", charm.RoleProvider, charm.ScopeGlobal, false, false},
241- {"juju-info", "juju-info", charm.RoleRequirer, charm.ScopeGlobal, false, false},
242- {"juju-info", "juju-info", charm.RoleProvider, charm.ScopeContainer, true, true},
243-
244- {"ifce-req", "req", charm.RoleRequirer, charm.ScopeGlobal, true, false},
245- {"blah", "req", charm.RoleRequirer, charm.ScopeGlobal, false, false},
246- {"ifce-req", "blah", charm.RoleRequirer, charm.ScopeGlobal, false, false},
247- {"ifce-req", "req", charm.RolePeer, charm.ScopeGlobal, false, false},
248- {"ifce-req", "req", charm.RoleRequirer, charm.ScopeContainer, true, false},
249-
250- {"juju-info", "info", charm.RoleRequirer, charm.ScopeContainer, true, false},
251- {"blah", "info", charm.RoleRequirer, charm.ScopeContainer, false, false},
252- {"juju-info", "blah", charm.RoleRequirer, charm.ScopeContainer, false, false},
253- {"juju-info", "info", charm.RolePeer, charm.ScopeContainer, false, false},
254- {"juju-info", "info", charm.RoleRequirer, charm.ScopeGlobal, false, false},
255-
256- {"ifce-peer", "peer", charm.RolePeer, charm.ScopeGlobal, true, false},
257- {"blah", "peer", charm.RolePeer, charm.ScopeGlobal, false, false},
258- {"ifce-peer", "blah", charm.RolePeer, charm.ScopeGlobal, false, false},
259- {"ifce-peer", "peer", charm.RoleProvider, charm.ScopeGlobal, false, false},
260- {"ifce-peer", "peer", charm.RolePeer, charm.ScopeContainer, true, false},
261-}
262-
263-func (s *EndpointSuite) TestImplementedBy(c *C) {
264- for i, t := range implementedByTests {
265- c.Logf("test %d", i)
266- ep := state.Endpoint{
267- ServiceName: "x",
268- Relation: charm.Relation{
269- Interface: t.ifce,
270- Name: t.name,
271- Role: t.role,
272- Scope: t.scope,
273- },
274- }
275- c.Assert(ep.ImplementedBy(&dummyCharm{}), Equals, t.match)
276- c.Assert(ep.IsImplicit(), Equals, t.implicit)
277- }
278-}

Subscribers

People subscribed via source and target branches

to status/vote changes: