summaryrefslogtreecommitdiffstats
path: root/asdctool/src/main/resources/es-resources/index_ops.py
diff options
context:
space:
mode:
authorMichael Lando <ml636r@att.com>2017-02-19 10:28:42 +0200
committerMichael Lando <ml636r@att.com>2017-02-19 10:51:01 +0200
commit451a3400b76511393c62a444f588a4ed15f4a549 (patch)
treee4f5873a863d1d3e55618eab48b83262f874719d /asdctool/src/main/resources/es-resources/index_ops.py
parent5abfe4e1fb5fae4bbd5fbc340519f52075aff3ff (diff)
Initial OpenECOMP SDC commit
Change-Id: I0924d5a6ae9cdc161ae17c68d3689a30d10f407b Signed-off-by: Michael Lando <ml636r@att.com>
Diffstat (limited to 'asdctool/src/main/resources/es-resources/index_ops.py')
-rw-r--r--asdctool/src/main/resources/es-resources/index_ops.py151
1 files changed, 151 insertions, 0 deletions
diff --git a/asdctool/src/main/resources/es-resources/index_ops.py b/asdctool/src/main/resources/es-resources/index_ops.py
new file mode 100644
index 0000000000..d1f3bb0021
--- /dev/null
+++ b/asdctool/src/main/resources/es-resources/index_ops.py
@@ -0,0 +1,151 @@
+import itertools
+import string
+import json
+from datetime import datetime
+from elasticsearch import Elasticsearch
+import elasticsearch
+import elasticsearch.helpers
+from elasticsearch.client import IndicesClient, CatClient
+import sys, os, getopt
+from file_utils import readFileToJson
+from config_properties import getGlobalVar
+
+
+
+def createIndex(client, indexName, createBody):
+ try:
+ print "start createIndex"
+ if (client == None):
+ client = Elasticsearch(['localhost'])
+ esIndexClient = IndicesClient(client)
+ res = deleteIndex(client, indexName)
+ if (res != 0):
+ print "operation failed"
+ return 2
+ create_res=elasticsearch.client.IndicesClient.create(esIndexClient, index=indexName, body=createBody)
+ print "create index response: ", create_res
+ if (create_res['acknowledged'] != True):
+ print "failed to create index"
+ return 1
+ else:
+ print "index ",indexName, " created successfully"
+ return 0
+ except Exception, error:
+ print "An exception was thrown!"
+ print str(error)
+ return 2
+
+
+def deleteIndex(client, indexName):
+ try:
+ print "start deleteIndex"
+ if (client == None):
+ client = Elasticsearch(['localhost'])
+ esIndexClient = IndicesClient(client)
+ isExists=elasticsearch.client.IndicesClient.exists(esIndexClient, indexName)
+ if ( isExists == True ):
+ delete_res=elasticsearch.client.IndicesClient.delete(esIndexClient, index=indexName)
+ if (delete_res['acknowledged'] != True):
+ print "failed to delete index"
+ return 1
+ else:
+ print "index ",indexName, " deleted"
+ return 0
+ else:
+ print "index not found - assume already deleted"
+ return 0
+ except Exception, error:
+ print "An exception was thrown!"
+ print str(error)
+ return 2
+
+def copyIndex(client, fromIndex, toIndex):
+ try:
+ print "start copyIndex"
+ if (client == None):
+ client = Elasticsearch(['localhost'])
+ client.indices.refresh(index=fromIndex)
+ count=client.search(fromIndex, search_type='count')
+ print "original index count: ",count
+ docNum, docErrors = elasticsearch.helpers.reindex(client, fromIndex, toIndex)
+ print "copy result: ", docNum, docErrors
+ if (docNum != count['hits']['total']):
+ print "Failed to copy all documents. expected: ", count['hits']['total'], " actual: ", docNum
+ return 1
+ # if (len(docErrors) != 0):
+ # print "copy returned with errors"
+ # print docErrors
+ # return 1
+ return 0
+ except Exception, error:
+ print "An exception was thrown!"
+ print str(error)
+ return 2
+
+
+def usage():
+ print 'USAGE: ', sys.argv[0], '-o <operation : create | delete | move> -n <indexName> -a <address> -f <mappingFile (for create)> -t <toIndex (for move operation)>'
+
+
+
+def main(argv):
+ print "start script with ", len(sys.argv), 'arguments.'
+ print "=============================================="
+
+ try:
+ opts, args = getopt.getopt(argv, "h:o:a:n:f:t:", ["operation","address","indexName","file","toIndex"])
+ except getopt.GetoptError:
+ usage()
+ sys.exit(2)
+
+ host = None
+ for opt, arg in opts:
+ print opt, arg
+ if opt == '-h':
+ usage()
+ sys.exit(2)
+ elif opt in ('-f', '--file'):
+ mapping=readFileToJson(arg)
+ elif opt in ('-a', '--address'):
+ host=arg
+ elif opt in ('-o', '--operation'):
+ operation=arg
+ elif opt in ('-n', '--indexName'):
+ indexName=arg
+ elif opt in ('-t', '--toIndex'):
+ destIndexName=arg
+
+ if (operation == None):
+ usage()
+ sys.exit(2)
+ elif (host == None):
+ print "address is mandatory argument"
+ usage()
+ sys.exit(2)
+ elif operation == 'create':
+ print "create new index ", indexName
+ client = Elasticsearch([{'host': host, 'timeout':5}] )
+ res = createIndex(client, indexName, mapping)
+
+ elif operation == 'delete':
+ print "delete index ", indexName
+ client = Elasticsearch([{'host': host, 'timeout':5}] )
+ res = deleteIndex(client, indexName)
+
+ elif operation == 'move':
+ print "move index ", indexName, " to ", destIndexName
+ client = Elasticsearch([{'host': host, 'timeout':5}] )
+ res = copyIndex(client, indexName, destIndexName)
+ else:
+ usage()
+ exit(2)
+ if res != 0:
+ print "ERROR: operation Failed"
+ exit(1)
+
+
+
+if __name__ == "__main__":
+ main(sys.argv[1:])
+
+