{"action":"created","comment":{"url":"https://api.github.com/repos/juju/charmstore/pulls/comments/58732872","id":58732872,"diff_hunk":"@@ -0,0 +1,256 @@\n+// Copyright 2016 Canonical Ltd.\n+// Licensed under the AGPLv3, see LICENCE file for details.\n+\n+package charmstore // import \"gopkg.in/juju/charmstore.v5-unstable/internal/charmstore\"\n+import (\n+\t\"fmt\"\n+\t\"io\"\n+\t\"sort\"\n+\t\"time\"\n+\n+\t\"gopkg.in/errgo.v1\"\n+\t\"gopkg.in/juju/charm.v6-unstable\"\n+\t\"gopkg.in/juju/charmrepo.v2-unstable/csclient/params\"\n+\t\"gopkg.in/mgo.v2\"\n+\t\"gopkg.in/mgo.v2/bson\"\n+\n+\t\"gopkg.in/juju/charmstore.v5-unstable/internal/mongodoc\"\n+)\n+\n+// newResourceQuery returns a mongo query doc that will retrieve the\n+// given named resource and revision associated with the given charm URL.\n+// If revision is < 0, all revisions of the resource will be selected by\n+// the query.\n+func newResourceQuery(url *charm.URL, name string, revision int) bson.D {\n+\tquery := make(bson.D, 2, 3)\n+\tquery[0] = bson.DocElem{\"baseurl\", mongodoc.BaseURL(url)}\n+\tquery[1] = bson.DocElem{\"name\", name}\n+\tif revision >= 0 {\n+\t\tquery = append(query, bson.DocElem{\"revision\", revision})\n+\t}\n+\treturn query\n+}\n+\n+// sortResources sorts the provided resource docs, The resources are\n+// sorted first by URL then by name and finally by revision.\n+func sortResources(resources []*mongodoc.Resource) {\n+\tsort.Sort(resourcesByName(resources))\n+}\n+\n+type resourcesByName []*mongodoc.Resource\n+\n+func (sorted resourcesByName) Len() int      { return len(sorted) }\n+func (sorted resourcesByName) Swap(i, j int) { sorted[i], sorted[j] = sorted[j], sorted[i] }\n+func (sorted resourcesByName) Less(i, j int) bool {\n+\tr0, r1 := sorted[i], sorted[j]\n+\tif *r0.BaseURL != *r1.BaseURL {\n+\t\treturn r0.BaseURL.String() < r1.BaseURL.String()\n+\t}\n+\tif r0.Name != r1.Name {\n+\t\treturn r0.Name < r1.Name\n+\t}\n+\treturn r0.Revision < r1.Revision\n+}\n+\n+var resourceNotFound = errgo.Newf(\"resource not found\")\n+\n+// ListResources returns the set of resources for the charm. If the\n+// unpublished channel is specified then set is composed of the latest\n+// revision for each resource. Otherwise it holds the revisions declared\n+// when the charm/channel pair was published.\n+func (s *Store) ListResources(entity *mongodoc.Entity, channel params.Channel) ([]*mongodoc.Resource, error) {\n+\tif entity.URL.Series == \"bundle\" {\n+\t\treturn nil, errgo.Newf(\"bundles do not have resources\")\n+\t}\n+\tif entity.CharmMeta == nil {\n+\t\treturn nil, errgo.Newf(\"entity missing charm metadata\")\n+\t}\n+\tbaseEntity, err := s.FindBaseEntity(entity.URL, FieldSelector(\"channelresources\"))\n+\tif err != nil {\n+\t\treturn nil, errgo.Mask(err)\n+\t}\n+\tvar docs []*mongodoc.Resource\n+\tfor name, meta := range entity.CharmMeta.Resources {\n+\t\tdoc, err := s.latestResource(baseEntity, channel, name)\n+\t\tif errgo.Cause(err) == resourceNotFound {\n+\t\t\t// Convert the \"not found\" error into placeholder. We do\n+\t\t\t// this instead of pre-populating the database with\n+\t\t\t// placeholders. This keeps the machinery simpler while\n+\t\t\t// still correct.\n+\t\t\tdoc = &mongodoc.Resource{\n+\t\t\t\tBaseURL: baseEntity.URL,\n+\t\t\t\tName:    meta.Name,\n+\t\t\t\t// Revision, Fingerprint, etc. are not set.\n+\t\t\t}\n+\t\t} else if err != nil {\n+\t\t\treturn nil, errgo.Notef(err, \"failed to get resource %q\", name)\n+\t\t}\n+\t\tdocs = append(docs, doc)\n+\t}\n+\tsortResources(docs)\n+\treturn docs, nil\n+}\n+\n+// ResourceBlob holds the information specific to a single resource blob.\n+type ResourceBlob struct {\n+\tio.Reader\n+\n+\t// Hash is the SHA-384 checksum of the blob.\n+\tHash string\n+\n+\t// Size is the size of the blob in bytes.\n+\tSize int64\n+}\n+\n+// TODO(ericsnow) We will need Store.nextResourceRevision() to get the\n+// value to pass to addResource().\n+\n+func (s *Store) addResource(entity *mongodoc.Entity, name string, blob ResourceBlob, newRevision int) error {\n+\tif !charmHasResource(entity.CharmMeta, name) {\n+\t\treturn errgo.Newf(\"charm does not have resource %q\", name)\n+\t}\n+\n+\tblobName, err := s.storeResource(blob)\n+\tif err != nil {\n+\t\treturn errgo.Mask(err, errgo.Is(resourceNotFound))\n+\t}\n+\n+\tdoc := &mongodoc.Resource{\n+\t\tBaseURL:    entity.BaseURL,\n+\t\tName:       name,\n+\t\tRevision:   newRevision,\n+\t\tBlobHash:   blob.Hash,\n+\t\tSize:       blob.Size,\n+\t\tBlobName:   blobName,\n+\t\tUploadTime: time.Now().UTC(),\n+\t}\n+\tif s.insertResource(doc); err != nil {\n+\t\tif err := s.BlobStore.Remove(blobName); err != nil {\n+\t\t\tlogger.Errorf(\"cannot remove blob %s after error: %v\", blobName, err)\n+\t\t}\n+\t\treturn errgo.Mask(err)\n+\t}\n+\treturn nil\n+}\n+\n+func (s *Store) insertResource(doc *mongodoc.Resource) error {\n+\tif err := doc.Validate(); err != nil {\n+\t\treturn errgo.Mask(err)\n+\t}\n+\terr := s.DB.Resources().Insert(doc)\n+\tif err != nil {\n+\t\tif mgo.IsDup(err) {\n+\t\t\treturn errgo.WithCausef(nil, params.ErrDuplicateUpload, \"\")\n+\t\t}\n+\t\treturn errgo.Notef(err, \"cannot insert resource\")\n+\t}\n+\treturn nil\n+}\n+\n+func (s *Store) storeResource(blob ResourceBlob) (string, error) {\n+\tname := bson.NewObjectId().Hex()\n+\t// TODO(ericsnow) We will finish this in a follow-up patch.\n+\treturn name, nil\n+}\n+\n+func (s *Store) setResource(entity *mongodoc.Entity, channel params.Channel, name string, revision int) error {","path":"internal/charmstore/resources.go","position":156,"original_position":156,"commit_id":"9cf7a25bba0d984ed0e04c7fa477579ca3a7c2d7","original_commit_id":"9cf7a25bba0d984ed0e04c7fa477579ca3a7c2d7","user":{"login":"rogpeppe","id":66491,"avatar_url":"https://avatars.githubusercontent.com/u/66491?v=3","gravatar_id":"","url":"https://api.github.com/users/rogpeppe","html_url":"https://github.com/rogpeppe","followers_url":"https://api.github.com/users/rogpeppe/followers","following_url":"https://api.github.com/users/rogpeppe/following{/other_user}","gists_url":"https://api.github.com/users/rogpeppe/gists{/gist_id}","starred_url":"https://api.github.com/users/rogpeppe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rogpeppe/subscriptions","organizations_url":"https://api.github.com/users/rogpeppe/orgs","repos_url":"https://api.github.com/users/rogpeppe/repos","events_url":"https://api.github.com/users/rogpeppe/events{/privacy}","received_events_url":"https://api.github.com/users/rogpeppe/received_events","type":"User","site_admin":false},"body":"// setResource adds or updates the resource for the given entity,\r\n// associating the resource with the given name and revision\r\n// with the given channel.\r\n\r\n?","created_at":"2016-04-06T16:02:58Z","updated_at":"2016-04-06T16:02:58Z","html_url":"https://github.com/juju/charmstore/pull/594#discussion_r58732872","pull_request_url":"https://api.github.com/repos/juju/charmstore/pulls/594","_links":{"self":{"href":"https://api.github.com/repos/juju/charmstore/pulls/comments/58732872"},"html":{"href":"https://github.com/juju/charmstore/pull/594#discussion_r58732872"},"pull_request":{"href":"https://api.github.com/repos/juju/charmstore/pulls/594"}}},"pull_request":{"url":"https://api.github.com/repos/juju/charmstore/pulls/594","id":65487462,"html_url":"https://github.com/juju/charmstore/pull/594","diff_url":"https://github.com/juju/charmstore/pull/594.diff","patch_url":"https://github.com/juju/charmstore/pull/594.patch","issue_url":"https://api.github.com/repos/juju/charmstore/issues/594","number":594,"state":"open","locked":false,"title":"resources store methods list","user":{"login":"mhilton","id":1136018,"avatar_url":"https://avatars.githubusercontent.com/u/1136018?v=3","gravatar_id":"","url":"https://api.github.com/users/mhilton","html_url":"https://github.com/mhilton","followers_url":"https://api.github.com/users/mhilton/followers","following_url":"https://api.github.com/users/mhilton/following{/other_user}","gists_url":"https://api.github.com/users/mhilton/gists{/gist_id}","starred_url":"https://api.github.com/users/mhilton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhilton/subscriptions","organizations_url":"https://api.github.com/users/mhilton/orgs","repos_url":"https://api.github.com/users/mhilton/repos","events_url":"https://api.github.com/users/mhilton/events{/privacy}","received_events_url":"https://api.github.com/users/mhilton/received_events","type":"User","site_admin":false},"body":"This is an updated version of #583 ","created_at":"2016-04-06T15:34:12Z","updated_at":"2016-04-06T16:02:58Z","closed_at":null,"merged_at":null,"merge_commit_sha":"25bd68a0ba0ddc2bf262b4eb6e46a2679b6427ab","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/juju/charmstore/pulls/594/commits","review_comments_url":"https://api.github.com/repos/juju/charmstore/pulls/594/comments","review_comment_url":"https://api.github.com/repos/juju/charmstore/pulls/comments{/number}","comments_url":"https://api.github.com/repos/juju/charmstore/issues/594/comments","statuses_url":"https://api.github.com/repos/juju/charmstore/statuses/9cf7a25bba0d984ed0e04c7fa477579ca3a7c2d7","head":{"label":"mhilton:ericsnow-resources-store-methods-list","ref":"ericsnow-resources-store-methods-list","sha":"9cf7a25bba0d984ed0e04c7fa477579ca3a7c2d7","user":{"login":"mhilton","id":1136018,"avatar_url":"https://avatars.githubusercontent.com/u/1136018?v=3","gravatar_id":"","url":"https://api.github.com/users/mhilton","html_url":"https://github.com/mhilton","followers_url":"https://api.github.com/users/mhilton/followers","following_url":"https://api.github.com/users/mhilton/following{/other_user}","gists_url":"https://api.github.com/users/mhilton/gists{/gist_id}","starred_url":"https://api.github.com/users/mhilton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhilton/subscriptions","organizations_url":"https://api.github.com/users/mhilton/orgs","repos_url":"https://api.github.com/users/mhilton/repos","events_url":"https://api.github.com/users/mhilton/events{/privacy}","received_events_url":"https://api.github.com/users/mhilton/received_events","type":"User","site_admin":false},"repo":{"id":24229515,"name":"juju-charmstore","full_name":"mhilton/juju-charmstore","owner":{"login":"mhilton","id":1136018,"avatar_url":"https://avatars.githubusercontent.com/u/1136018?v=3","gravatar_id":"","url":"https://api.github.com/users/mhilton","html_url":"https://github.com/mhilton","followers_url":"https://api.github.com/users/mhilton/followers","following_url":"https://api.github.com/users/mhilton/following{/other_user}","gists_url":"https://api.github.com/users/mhilton/gists{/gist_id}","starred_url":"https://api.github.com/users/mhilton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhilton/subscriptions","organizations_url":"https://api.github.com/users/mhilton/orgs","repos_url":"https://api.github.com/users/mhilton/repos","events_url":"https://api.github.com/users/mhilton/events{/privacy}","received_events_url":"https://api.github.com/users/mhilton/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mhilton/juju-charmstore","description":"The charm store server.","fork":true,"url":"https://api.github.com/repos/mhilton/juju-charmstore","forks_url":"https://api.github.com/repos/mhilton/juju-charmstore/forks","keys_url":"https://api.github.com/repos/mhilton/juju-charmstore/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mhilton/juju-charmstore/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mhilton/juju-charmstore/teams","hooks_url":"https://api.github.com/repos/mhilton/juju-charmstore/hooks","issue_events_url":"https://api.github.com/repos/mhilton/juju-charmstore/issues/events{/number}","events_url":"https://api.github.com/repos/mhilton/juju-charmstore/events","assignees_url":"https://api.github.com/repos/mhilton/juju-charmstore/assignees{/user}","branches_url":"https://api.github.com/repos/mhilton/juju-charmstore/branches{/branch}","tags_url":"https://api.github.com/repos/mhilton/juju-charmstore/tags","blobs_url":"https://api.github.com/repos/mhilton/juju-charmstore/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mhilton/juju-charmstore/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mhilton/juju-charmstore/git/refs{/sha}","trees_url":"https://api.github.com/repos/mhilton/juju-charmstore/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mhilton/juju-charmstore/statuses/{sha}","languages_url":"https://api.github.com/repos/mhilton/juju-charmstore/languages","stargazers_url":"https://api.github.com/repos/mhilton/juju-charmstore/stargazers","contributors_url":"https://api.github.com/repos/mhilton/juju-charmstore/contributors","subscribers_url":"https://api.github.com/repos/mhilton/juju-charmstore/subscribers","subscription_url":"https://api.github.com/repos/mhilton/juju-charmstore/subscription","commits_url":"https://api.github.com/repos/mhilton/juju-charmstore/commits{/sha}","git_commits_url":"https://api.github.com/repos/mhilton/juju-charmstore/git/commits{/sha}","comments_url":"https://api.github.com/repos/mhilton/juju-charmstore/comments{/number}","issue_comment_url":"https://api.github.com/repos/mhilton/juju-charmstore/issues/comments{/number}","contents_url":"https://api.github.com/repos/mhilton/juju-charmstore/contents/{+path}","compare_url":"https://api.github.com/repos/mhilton/juju-charmstore/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mhilton/juju-charmstore/merges","archive_url":"https://api.github.com/repos/mhilton/juju-charmstore/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mhilton/juju-charmstore/downloads","issues_url":"https://api.github.com/repos/mhilton/juju-charmstore/issues{/number}","pulls_url":"https://api.github.com/repos/mhilton/juju-charmstore/pulls{/number}","milestones_url":"https://api.github.com/repos/mhilton/juju-charmstore/milestones{/number}","notifications_url":"https://api.github.com/repos/mhilton/juju-charmstore/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mhilton/juju-charmstore/labels{/name}","releases_url":"https://api.github.com/repos/mhilton/juju-charmstore/releases{/id}","deployments_url":"https://api.github.com/repos/mhilton/juju-charmstore/deployments","created_at":"2014-09-19T12:48:28Z","updated_at":"2015-11-09T16:16:46Z","pushed_at":"2016-04-06T15:31:21Z","git_url":"git://github.com/mhilton/juju-charmstore.git","ssh_url":"git@github.com:mhilton/juju-charmstore.git","clone_url":"https://github.com/mhilton/juju-charmstore.git","svn_url":"https://github.com/mhilton/juju-charmstore","homepage":null,"size":4269,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"v4"}},"base":{"label":"juju:v5-unstable","ref":"v5-unstable","sha":"25fa4366948e1946ec32cd6176b18297ea45e673","user":{"login":"juju","id":4604548,"avatar_url":"https://avatars.githubusercontent.com/u/4604548?v=3","gravatar_id":"","url":"https://api.github.com/users/juju","html_url":"https://github.com/juju","followers_url":"https://api.github.com/users/juju/followers","following_url":"https://api.github.com/users/juju/following{/other_user}","gists_url":"https://api.github.com/users/juju/gists{/gist_id}","starred_url":"https://api.github.com/users/juju/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/juju/subscriptions","organizations_url":"https://api.github.com/users/juju/orgs","repos_url":"https://api.github.com/users/juju/repos","events_url":"https://api.github.com/users/juju/events{/privacy}","received_events_url":"https://api.github.com/users/juju/received_events","type":"Organization","site_admin":false},"repo":{"id":20720417,"name":"charmstore","full_name":"juju/charmstore","owner":{"login":"juju","id":4604548,"avatar_url":"https://avatars.githubusercontent.com/u/4604548?v=3","gravatar_id":"","url":"https://api.github.com/users/juju","html_url":"https://github.com/juju","followers_url":"https://api.github.com/users/juju/followers","following_url":"https://api.github.com/users/juju/following{/other_user}","gists_url":"https://api.github.com/users/juju/gists{/gist_id}","starred_url":"https://api.github.com/users/juju/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/juju/subscriptions","organizations_url":"https://api.github.com/users/juju/orgs","repos_url":"https://api.github.com/users/juju/repos","events_url":"https://api.github.com/users/juju/events{/privacy}","received_events_url":"https://api.github.com/users/juju/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/juju/charmstore","description":"The charm store server.","fork":false,"url":"https://api.github.com/repos/juju/charmstore","forks_url":"https://api.github.com/repos/juju/charmstore/forks","keys_url":"https://api.github.com/repos/juju/charmstore/keys{/key_id}","collaborators_url":"https://api.github.com/repos/juju/charmstore/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/juju/charmstore/teams","hooks_url":"https://api.github.com/repos/juju/charmstore/hooks","issue_events_url":"https://api.github.com/repos/juju/charmstore/issues/events{/number}","events_url":"https://api.github.com/repos/juju/charmstore/events","assignees_url":"https://api.github.com/repos/juju/charmstore/assignees{/user}","branches_url":"https://api.github.com/repos/juju/charmstore/branches{/branch}","tags_url":"https://api.github.com/repos/juju/charmstore/tags","blobs_url":"https://api.github.com/repos/juju/charmstore/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/juju/charmstore/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/juju/charmstore/git/refs{/sha}","trees_url":"https://api.github.com/repos/juju/charmstore/git/trees{/sha}","statuses_url":"https://api.github.com/repos/juju/charmstore/statuses/{sha}","languages_url":"https://api.github.com/repos/juju/charmstore/languages","stargazers_url":"https://api.github.com/repos/juju/charmstore/stargazers","contributors_url":"https://api.github.com/repos/juju/charmstore/contributors","subscribers_url":"https://api.github.com/repos/juju/charmstore/subscribers","subscription_url":"https://api.github.com/repos/juju/charmstore/subscription","commits_url":"https://api.github.com/repos/juju/charmstore/commits{/sha}","git_commits_url":"https://api.github.com/repos/juju/charmstore/git/commits{/sha}","comments_url":"https://api.github.com/repos/juju/charmstore/comments{/number}","issue_comment_url":"https://api.github.com/repos/juju/charmstore/issues/comments{/number}","contents_url":"https://api.github.com/repos/juju/charmstore/contents/{+path}","compare_url":"https://api.github.com/repos/juju/charmstore/compare/{base}...{head}","merges_url":"https://api.github.com/repos/juju/charmstore/merges","archive_url":"https://api.github.com/repos/juju/charmstore/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/juju/charmstore/downloads","issues_url":"https://api.github.com/repos/juju/charmstore/issues{/number}","pulls_url":"https://api.github.com/repos/juju/charmstore/pulls{/number}","milestones_url":"https://api.github.com/repos/juju/charmstore/milestones{/number}","notifications_url":"https://api.github.com/repos/juju/charmstore/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/juju/charmstore/labels{/name}","releases_url":"https://api.github.com/repos/juju/charmstore/releases{/id}","deployments_url":"https://api.github.com/repos/juju/charmstore/deployments","created_at":"2014-06-11T09:53:37Z","updated_at":"2016-01-11T17:48:35Z","pushed_at":"2016-04-06T15:34:12Z","git_url":"git://github.com/juju/charmstore.git","ssh_url":"git@github.com:juju/charmstore.git","clone_url":"https://github.com/juju/charmstore.git","svn_url":"https://github.com/juju/charmstore","homepage":null,"size":6016,"stargazers_count":7,"watchers_count":7,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":21,"mirror_url":null,"open_issues_count":28,"forks":21,"open_issues":28,"watchers":7,"default_branch":"v5-unstable"}},"_links":{"self":{"href":"https://api.github.com/repos/juju/charmstore/pulls/594"},"html":{"href":"https://github.com/juju/charmstore/pull/594"},"issue":{"href":"https://api.github.com/repos/juju/charmstore/issues/594"},"comments":{"href":"https://api.github.com/repos/juju/charmstore/issues/594/comments"},"review_comments":{"href":"https://api.github.com/repos/juju/charmstore/pulls/594/comments"},"review_comment":{"href":"https://api.github.com/repos/juju/charmstore/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/juju/charmstore/pulls/594/commits"},"statuses":{"href":"https://api.github.com/repos/juju/charmstore/statuses/9cf7a25bba0d984ed0e04c7fa477579ca3a7c2d7"}}}}