~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/gopkg.in/juju/charmstore.v5-unstable/internal/v4/relations.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 
10
10
        "gopkg.in/errgo.v1"
11
11
        "gopkg.in/juju/charmrepo.v2-unstable/csclient/params"
12
 
        "gopkg.in/mgo.v2/bson"
13
12
 
14
13
        "gopkg.in/juju/charmstore.v5-unstable/internal/charmstore"
15
14
        "gopkg.in/juju/charmstore.v5-unstable/internal/mongodoc"
18
17
 
19
18
// GET id/meta/charm-related[?include=meta[&include=meta…]]
20
19
// https://github.com/juju/charmstore/blob/v4/docs/API.md#get-idmetacharm-related
21
 
func (h *ReqHandler) metaCharmRelated(entity *mongodoc.Entity, id *router.ResolvedURL, path string, flags url.Values, req *http.Request) (interface{}, error) {
 
20
func (h ReqHandler) metaCharmRelated(entity *mongodoc.Entity, id *router.ResolvedURL, path string, flags url.Values, req *http.Request) (interface{}, error) {
22
21
        if id.URL.Series == "bundle" {
23
22
                return nil, nil
24
23
        }
27
26
        if len(entity.CharmProvidedInterfaces)+len(entity.CharmRequiredInterfaces) == 0 {
28
27
                return &params.RelatedResponse{}, nil
29
28
        }
30
 
        q := h.Store.MatchingInterfacesQuery(entity.CharmProvidedInterfaces, entity.CharmRequiredInterfaces)
31
 
 
32
 
        fields := bson.D{
33
 
                {"_id", 1},
34
 
                {"supportedseries", 1},
35
 
                {"development", 1},
36
 
                {"charmrequiredinterfaces", 1},
37
 
                {"charmprovidedinterfaces", 1},
38
 
                {"promulgated-url", 1},
39
 
                {"promulgated-revision", 1},
40
 
        }
41
 
 
 
29
        fields := charmstore.FieldSelector(
 
30
                "supportedseries",
 
31
                "charmrequiredinterfaces",
 
32
                "charmprovidedinterfaces",
 
33
                "promulgated-url",
 
34
                "promulgated-revision",
 
35
        )
 
36
        query := h.Store.MatchingInterfacesQuery(entity.CharmProvidedInterfaces, entity.CharmRequiredInterfaces)
 
37
        iter := h.Cache.Iter(query.Sort("_id"), fields)
42
38
        var entities []*mongodoc.Entity
43
 
        if err := q.Select(fields).Sort("_id").All(&entities); err != nil {
 
39
        for iter.Next() {
 
40
                entities = append(entities, iter.Entity())
 
41
        }
 
42
        if err := iter.Err(); err != nil {
44
43
                return nil, errgo.Notef(err, "cannot retrieve the related charms")
45
44
        }
46
45
 
86
85
//           {Id: "cs:utopic/memcached-0", Meta: ...},
87
86
//       },
88
87
//   }
89
 
func (h *ReqHandler) getRelatedCharmsResponse(
 
88
func (h ReqHandler) getRelatedCharmsResponse(
90
89
        ifaces []string,
91
90
        entities []*mongodoc.Entity,
92
91
        getInterfaces entityRelatedInterfacesGetter,
93
92
        includes []string,
94
93
        req *http.Request,
95
 
) (map[string][]params.MetaAnyResponse, error) {
96
 
        results := make(map[string][]params.MetaAnyResponse, len(ifaces))
 
94
) (map[string][]params.EntityResult, error) {
 
95
        results := make(map[string][]params.EntityResult, len(ifaces))
97
96
        for _, iface := range ifaces {
98
97
                responses, err := h.getRelatedIfaceResponses(iface, entities, getInterfaces, includes, req)
99
98
                if err != nil {
106
105
        return results, nil
107
106
}
108
107
 
109
 
func (h *ReqHandler) getRelatedIfaceResponses(
 
108
func (h ReqHandler) getRelatedIfaceResponses(
110
109
        iface string,
111
110
        entities []*mongodoc.Entity,
112
111
        getInterfaces entityRelatedInterfacesGetter,
113
112
        includes []string,
114
113
        req *http.Request,
115
 
) ([]params.MetaAnyResponse, error) {
 
114
) ([]params.EntityResult, error) {
116
115
        // Build a list of responses including only entities which are related
117
116
        // to the given interface.
118
117
        usesInterface := func(e *mongodoc.Entity) bool {
131
130
        return resp, nil
132
131
}
133
132
 
134
 
func (h *ReqHandler) getMetadataForEntities(entities []*mongodoc.Entity, includes []string, req *http.Request, includeEntity func(*mongodoc.Entity) bool) ([]params.MetaAnyResponse, error) {
135
 
        response := make([]params.MetaAnyResponse, 0, len(entities))
 
133
func (h ReqHandler) getMetadataForEntities(entities []*mongodoc.Entity, includes []string, req *http.Request, includeEntity func(*mongodoc.Entity) bool) ([]params.EntityResult, error) {
 
134
        response := make([]params.EntityResult, 0, len(entities))
 
135
        for _, inc := range includes {
 
136
                if h.Router.MetaHandler(inc) == nil {
 
137
                        return nil, errgo.Newf("unrecognized metadata name %q", inc)
 
138
                }
 
139
        }
136
140
        err := expandMultiSeries(entities, func(series string, e *mongodoc.Entity) error {
137
141
                if includeEntity != nil && !includeEntity(e) {
138
142
                        return nil
142
146
                        return nil
143
147
                }
144
148
                if err != nil {
145
 
                        return errgo.Mask(err)
 
149
                        // Unfortunately it is possible to get errors here due to
 
150
                        // internal inconsistency, so rather than throwing away
 
151
                        // all the search results, we just log the error and move on.
 
152
                        logger.Errorf("cannot retrieve metadata for %v: %v", e.PreferredURL(true), err)
 
153
                        return nil
146
154
                }
147
155
                id := e.PreferredURL(true)
148
156
                id.Series = series
149
 
                response = append(response, params.MetaAnyResponse{
 
157
                response = append(response, params.EntityResult{
150
158
                        Id:   id,
151
159
                        Meta: meta,
152
160
                })
160
168
 
161
169
var errMetadataUnauthorized = errgo.Newf("metadata unauthorized")
162
170
 
163
 
func (h *ReqHandler) getMetadataForEntity(e *mongodoc.Entity, includes []string, req *http.Request) (map[string]interface{}, error) {
 
171
func (h ReqHandler) getMetadataForEntity(e *mongodoc.Entity, includes []string, req *http.Request) (map[string]interface{}, error) {
164
172
        rurl := charmstore.EntityResolvedURL(e)
165
173
        // Ignore entities that aren't readable by the current user.
166
174
        if err := h.AuthorizeEntity(rurl, req); err != nil {