Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete ES element. #733

Open
wants to merge 3 commits into
base: v5-unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions internal/charmstore/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,20 @@ func (si *SearchIndex) update(doc *SearchDoc) error {
return nil
}

// delete deletes an entity into elasticsearch if elasticsearch
// is configured.
func (si *SearchIndex) delete(entity *mongodoc.Entity) error {
if si == nil || si.Database == nil {
return nil
}
doc := SearchDoc{Entity: entity}
err := si.DeleteDocument(si.Index, typeName, si.getID(doc.URL))
if err != nil && err != elasticsearch.ErrNotFound {
return errgo.Mask(err)
}
return nil
}

// getID returns an ID for the elasticsearch document based on the contents of the
// mongoDB document. This is to allow elasticsearch documents to be replaced with
// updated versions when charm data is changed.
Expand Down
14 changes: 14 additions & 0 deletions internal/charmstore/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"gopkg.in/juju/charm.v6-unstable"
"gopkg.in/juju/charmrepo.v2-unstable/csclient/params"

"gopkg.in/juju/charmstore.v5-unstable/elasticsearch"
"gopkg.in/juju/charmstore.v5-unstable/internal/mongodoc"
"gopkg.in/juju/charmstore.v5-unstable/internal/router"
"gopkg.in/juju/charmstore.v5-unstable/internal/storetesting"
Expand Down Expand Up @@ -331,6 +332,19 @@ func (s *StoreSearchSuite) TestExportSearchDocument(c *gc.C) {
c.Assert(string(actual), jc.JSONEquals, doc)
}

func (s *StoreSearchSuite) TestDeleteDocument(c *gc.C) {
var entity *mongodoc.Entity
var actual json.RawMessage
err := s.store.DB.Entities().FindId("cs:~charmers/precise/wordpress-23").One(&entity)
c.Assert(err, gc.Equals, nil)
err = s.store.ES.GetDocument(s.TestIndex, typeName, s.store.ES.getID(entity.URL), &actual)
c.Assert(err, gc.Equals, nil)
err = s.store.ES.delete(entity)
c.Assert(err, gc.Equals, nil)
err = s.store.ES.GetDocument(s.TestIndex, typeName, s.store.ES.getID(entity.URL), &actual)
c.Assert(err, gc.Equals, elasticsearch.ErrNotFound)
}

var searchTests = []struct {
about string
sp SearchParams
Expand Down
10 changes: 9 additions & 1 deletion internal/charmstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,8 @@ func (s *Store) DeleteEntity(id *router.ResolvedURL) error {
baseEntity, err := s.FindBaseEntity(&id.URL, map[string]int{
"_id": 1,
"channelentities": 1,
"promulgated": 1,
"channelacls": 1,
})
if err != nil {
return errgo.Mask(err)
Expand Down Expand Up @@ -1284,7 +1286,13 @@ func (s *Store) DeleteEntity(id *router.ResolvedURL) error {
return errgo.Notef(err, "cannot remove compatibility blob %s", name)
}
}

if s.ES == nil || s.ES.Database == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to do this check in both places, just call s.ES.delete and let it return nil if its not configured.

return nil
}
err = s.ES.delete(entity)
if err != nil {
return errgo.Mask(err)
}
return nil
}

Expand Down