From 3bdd5197f56e5f974ec935b40a94cc0999a2127d Mon Sep 17 00:00:00 2001 From: Stephan Preeker Date: Fri, 8 Dec 2017 14:47:04 +0100 Subject: [PATCH] T53161 all py.test things PASSES --- pillar/api/search/documents.py | 14 +++++++++++++- pillar/api/search/elastic_indexing.py | 18 +++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/pillar/api/search/documents.py b/pillar/api/search/documents.py index 0c5e870c..649a50fe 100644 --- a/pillar/api/search/documents.py +++ b/pillar/api/search/documents.py @@ -101,7 +101,16 @@ class Node(es.DocType): def create_doc_from_user_data(user_to_index): - doc_id = str(user_to_index['objectID']) + + if not user_to_index: + return + + doc_id = str(user_to_index.get('objectID', '')) + + if not doc_id: + log.error('ID missing %s', user_to_index) + return + doc = User(_id=doc_id) doc.objectID = str(user_to_index['objectID']) doc.username = user_to_index['username'] @@ -114,6 +123,9 @@ def create_doc_from_user_data(user_to_index): def create_doc_from_node_data(node_to_index): + if not node_to_index: + return + # node stuff doc_id = str(node_to_index.get('objectID', '')) diff --git a/pillar/api/search/elastic_indexing.py b/pillar/api/search/elastic_indexing.py index c57ec158..504d1767 100644 --- a/pillar/api/search/elastic_indexing.py +++ b/pillar/api/search/elastic_indexing.py @@ -1,6 +1,7 @@ import logging from elasticsearch_dsl.connections import connections +from elasticsearch.exceptions import NotFoundError from pillar import current_app from . import documents @@ -21,7 +22,14 @@ def push_updated_user(user_to_index: dict): Push an update to the Elastic index when a user item is updated. """ + if not user_to_index: + return + doc = documents.create_doc_from_user_data(user_to_index) + + if not doc: + return + log.debug('UPDATE USER %s', doc._id) doc.save() @@ -33,6 +41,9 @@ def index_node_save(node_to_index: dict): doc = documents.create_doc_from_node_data(node_to_index) + if not doc: + return + log.debug('CREATED ELK NODE DOC %s', doc._id) doc.save() @@ -40,4 +51,9 @@ def index_node_save(node_to_index: dict): def index_node_delete(delete_id: str): log.debug('NODE DELETE INDEXING %s', delete_id) - documents.Node(id=delete_id).delete() + + try: + doc = documents.Node.get(id=delete_id) + doc.delete() + except NotFoundError: + pass