Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 // The uniter package implements the API interface | 4 // The uniter package implements the API interface |
5 // used by the uniter worker. | 5 // used by the uniter worker. |
6 package uniter | 6 package uniter |
7 | 7 |
8 import ( | 8 import ( |
9 "fmt" | |
10 | |
11 "launchpad.net/juju-core/charm" | 9 "launchpad.net/juju-core/charm" |
12 "launchpad.net/juju-core/names" | 10 "launchpad.net/juju-core/names" |
13 "launchpad.net/juju-core/state" | 11 "launchpad.net/juju-core/state" |
14 "launchpad.net/juju-core/state/api/params" | 12 "launchpad.net/juju-core/state/api/params" |
15 "launchpad.net/juju-core/state/apiserver/common" | 13 "launchpad.net/juju-core/state/apiserver/common" |
16 "launchpad.net/juju-core/state/watcher" | 14 "launchpad.net/juju-core/state/watcher" |
17 ) | 15 ) |
18 | 16 |
19 // UniterAPI implements the API used by the uniter worker. | 17 // UniterAPI implements the API used by the uniter worker. |
20 type UniterAPI struct { | 18 type UniterAPI struct { |
(...skipping 25 matching lines...) Expand all Loading... | |
46 } | 44 } |
47 return func(tag string) bool { | 45 return func(tag string) bool { |
48 return tag == names.ServiceTag(unit.ServiceName()) | 46 return tag == names.ServiceTag(unit.ServiceName()) |
49 }, nil | 47 }, nil |
50 } | 48 } |
51 getCanAccessUnitOrService := func() (common.AuthFunc, error) { | 49 getCanAccessUnitOrService := func() (common.AuthFunc, error) { |
52 // These errors here are ignored: they're always nil. | 50 // These errors here are ignored: they're always nil. |
53 canAccessService, _ := getCanAccessService() | 51 canAccessService, _ := getCanAccessService() |
54 canAccessUnit, _ := getCanAccessUnit() | 52 canAccessUnit, _ := getCanAccessUnit() |
55 return func(tag string) bool { | 53 return func(tag string) bool { |
56 » » » if !canAccessService(tag) { | 54 » » » return canAccessService(tag) || canAccessUnit(tag) |
rog
2013/08/13 15:14:26
return canAccessUnit(tag) || canAccessService(tag)
dimitern
2013/08/13 15:20:19
Done.
| |
57 » » » » return canAccessUnit(tag) | |
58 » » » } | |
59 » » » return true | |
60 }, nil | 55 }, nil |
61 } | 56 } |
62 return &UniterAPI{ | 57 return &UniterAPI{ |
63 LifeGetter: common.NewLifeGetter(st, getCanAccess UnitOrService), | 58 LifeGetter: common.NewLifeGetter(st, getCanAccess UnitOrService), |
64 StatusSetter: common.NewStatusSetter(st, getCanAcce ssUnit), | 59 StatusSetter: common.NewStatusSetter(st, getCanAcce ssUnit), |
65 DeadEnsurer: common.NewDeadEnsurer(st, getCanAcces sUnit), | 60 DeadEnsurer: common.NewDeadEnsurer(st, getCanAcces sUnit), |
66 AgentEntityWatcher: common.NewAgentEntityWatcher(st, reso urces, getCanAccessUnitOrService), | 61 AgentEntityWatcher: common.NewAgentEntityWatcher(st, reso urces, getCanAccessUnitOrService), |
67 st: st, | 62 st: st, |
68 auth: authorizer, | 63 auth: authorizer, |
69 resources: resources, | 64 resources: resources, |
70 getCanAccessUnit: getCanAccessUnit, | 65 getCanAccessUnit: getCanAccessUnit, |
71 getCanAccessService: getCanAccessService, | 66 getCanAccessService: getCanAccessService, |
72 getCanAccessUnitOrService: getCanAccessUnitOrService, | 67 getCanAccessUnitOrService: getCanAccessUnitOrService, |
73 }, nil | 68 }, nil |
74 } | 69 } |
75 | 70 |
76 func (u *UniterAPI) getUnitOrService(tag string) (state.Entity, error) { | |
77 kind, err := names.TagKind(tag) | |
78 if err != nil { | |
79 // Invalid tags might indicate something security related. | |
80 return nil, common.ErrPerm | |
81 } | |
82 if kind != names.UnitTagKind && kind != names.ServiceTagKind { | |
83 return nil, fmt.Errorf("%q is not a unit or a service tag", tag) | |
84 } | |
85 return u.st.FindEntity(tag) | |
86 } | |
87 | |
88 func (u *UniterAPI) getUnit(tag string) (*state.Unit, error) { | 71 func (u *UniterAPI) getUnit(tag string) (*state.Unit, error) { |
89 » entity, err := u.getUnitOrService(tag) | 72 » entity, err := u.st.FindEntity(tag) |
90 if err != nil { | 73 if err != nil { |
91 return nil, err | 74 return nil, err |
92 } | 75 } |
93 return entity.(*state.Unit), nil | 76 return entity.(*state.Unit), nil |
94 } | 77 } |
95 | 78 |
96 func (u *UniterAPI) getService(tag string) (*state.Service, error) { | 79 func (u *UniterAPI) getService(tag string) (*state.Service, error) { |
97 » entity, err := u.getUnitOrService(tag) | 80 » entity, err := u.st.FindEntity(tag) |
98 if err != nil { | 81 if err != nil { |
99 return nil, err | 82 return nil, err |
100 } | 83 } |
101 return entity.(*state.Service), nil | 84 return entity.(*state.Service), nil |
102 } | 85 } |
103 | 86 |
104 // PublicAddress returns for each given unit, a pair of the public | 87 // PublicAddress returns for each given unit, a pair of the public |
105 // address of the unit and whether it's valid. | 88 // address of the unit and whether it's valid. |
106 func (u *UniterAPI) PublicAddress(args params.Entities) (params.StringBoolResult s, error) { | 89 func (u *UniterAPI) PublicAddress(args params.Entities) (params.StringBoolResult s, error) { |
107 result := params.StringBoolResults{ | 90 result := params.StringBoolResults{ |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
299 result := params.StringBoolResults{ | 282 result := params.StringBoolResults{ |
300 Results: make([]params.StringBoolResult, len(args.Entities)), | 283 Results: make([]params.StringBoolResult, len(args.Entities)), |
301 } | 284 } |
302 canAccess, err := u.getCanAccessUnitOrService() | 285 canAccess, err := u.getCanAccessUnitOrService() |
303 if err != nil { | 286 if err != nil { |
304 return params.StringBoolResults{}, err | 287 return params.StringBoolResults{}, err |
305 } | 288 } |
306 for i, entity := range args.Entities { | 289 for i, entity := range args.Entities { |
307 err := common.ErrPerm | 290 err := common.ErrPerm |
308 if canAccess(entity.Tag) { | 291 if canAccess(entity.Tag) { |
309 » » » var unitOrService interface{} | 292 » » » var unitOrService state.Entity |
310 » » » unitOrService, err = u.getUnitOrService(entity.Tag) | 293 » » » unitOrService, err = u.st.FindEntity(entity.Tag) |
311 if err == nil { | 294 if err == nil { |
312 charmURLer := unitOrService.(interface { | 295 charmURLer := unitOrService.(interface { |
313 CharmURL() (*charm.URL, bool) | 296 CharmURL() (*charm.URL, bool) |
314 }) | 297 }) |
315 curl, ok := charmURLer.CharmURL() | 298 curl, ok := charmURLer.CharmURL() |
316 result.Results[i].Result = curl.String() | 299 result.Results[i].Result = curl.String() |
317 result.Results[i].Ok = ok | 300 result.Results[i].Ok = ok |
318 } | 301 } |
319 } | 302 } |
320 result.Results[i].Error = common.ServerError(err) | 303 result.Results[i].Error = common.ServerError(err) |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
512 // GetCharmBundleSha256 | 495 // GetCharmBundleSha256 |
513 // RelationSetDying | 496 // RelationSetDying |
514 // RelationIsImplicit | 497 // RelationIsImplicit |
515 // GetRelation | 498 // GetRelation |
516 // GetRelationSettings | 499 // GetRelationSettings |
517 // RelationEnterScope | 500 // RelationEnterScope |
518 // RelationLeaveScope | 501 // RelationLeaveScope |
519 // WatchRelation | 502 // WatchRelation |
520 // EndpointImplementedBy | 503 // EndpointImplementedBy |
521 // EnvironUUID | 504 // EnvironUUID |
LEFT | RIGHT |