Initial OpenECOMP SDC commit
[sdc.git] / catalog-be / src / main / resources / scripts / import / tosca / importCategoryTypes.py
1 import pycurl
2 import sys, getopt
3 from StringIO import StringIO
4 import json
5 import copy
6 from importNormativeElements import createNormativeElement
7
8 from importCommon import *
9 ################################################################################################################################################
10 #                                                                                                                                                                                                                                                                                      #        
11 # Import all users from a given file                                                                                                                                                                                                               #
12 #                                                                                                                                                                                                                                                                                          #            
13 # activation :                                                                                                                                                                                                                                                             #
14 #       python importUsers.py [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <input file> | --ifile=<input file> ]     #
15 #                                                                                                                                                                                                                                                                                          #                    
16 # shortest activation (be host = localhost, be port = 8080):                                                                                                                                                               #                                                                                                                                   #
17 #               python importUsers.py [-f <input file> | --ifile=<input file> ]                                                                                                                                    #
18 #                                                                                                                                                                                                                                                                                      #        
19 ################################################################################################################################################
20
21 def usage():
22         print sys.argv[0], '[-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-u <user userId> | --user=<user userId> ]'
23
24
25 def importCategories(beHost, bePort, adminUser, exitOnSuccess, fileDir):
26         result = createNormativeElement(beHost, bePort, adminUser, fileDir, "/sdc2/rest/v1/catalog/uploadType/categories", "categoryTypes", "categoriesZip")
27
28         printFrameLine()
29         printNameAndReturnCode(result[0], result[1])
30         printFrameLine()
31
32         if ( result[1] == None or result[1] not in [200, 201, 409] ):
33                 errorAndExit(1, None)
34         else:
35                 if (exitOnSuccess == True):
36                         errorAndExit(0, None)
37
38 def main(argv):
39         print 'Number of arguments:', len(sys.argv), 'arguments.'
40
41         beHost = 'localhost' 
42         bePort = '8080'
43         adminUser = 'jh0003'
44
45         try:
46                 opts, args = getopt.getopt(argv,"i:p:u:h:",["ip=","port=","user="])
47         except getopt.GetoptError:
48                 usage()
49                 errorAndExit(2, 'Invalid input')
50                  
51         for opt, arg in opts:
52         #print opt, arg
53                 if opt == '-h':
54                         usage()                        
55                         sys.exit(3)
56                 elif opt in ("-i", "--ip"):
57                         beHost = arg
58                 elif opt in ("-p", "--port"):
59                         bePort = arg
60                 elif opt in ("-u", "--user"):
61                         adminUser = arg
62
63         print 'be host =',beHost,', be port =', bePort,', user =', adminUser
64         
65         if ( beHost == None ):
66                 usage()
67                 sys.exit(3)
68
69         importCategories(beHost, bePort, adminUser, True, "../../../import/tosca/categories/")
70
71
72 if __name__ == "__main__":
73         main(sys.argv[1:])
74