Sync Integ to Master
[sdc.git] / catalog-be / src / main / resources / scripts / import / tosca / importNormativeTypes.py
1 import pycurl
2 import sys, getopt
3 from StringIO import StringIO
4 import json
5 import copy
6 from importCommon import *
7 import importCommon
8 #########################################################################################################################################################################################
9 #                                                                                                                                                                                                                                                                                                                                                                       #
10 # Import all users from a given file                                                                                                                                                                                                                                                                                                    #
11 #                                                                                                                                                                                                                                                                                                                                                                               #
12 # activation :                                                                                                                                                                                                                                                                                                                                                  #
13 #       python importNormativeTypes.py [-s <scheme> | --scheme=<scheme> ] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <input file> | --ifile=<input file> ]  #
14 #                                                         [-v <true|false> | --updateversion=<true|false>]                                                                                                                                                                                                                      #
15 # shortest activation (be host = localhost, be port = 8080):                                                                                                                                                                                                                                                    #
16 #               python importNormativeTypes.py [-f <input file> | --ifile=<input file> ]                                                                                                                                                                                                        #
17 #                                                                                                                                                                                                                                                                                                                                                                       #       
18 #########################################################################################################################################################################################
19
20 def createNormativeType(scheme, beHost, bePort, adminUser, fileDir, ELEMENT_NAME, updateversion):
21         
22         try:
23                 log("in create normative type ", ELEMENT_NAME)
24                 debug("userId", adminUser)
25                 debug("fileDir", fileDir)
26                 
27                 buffer = StringIO()
28                 c = pycurl.Curl()
29
30                 url = scheme + '://' + beHost + ':' + bePort + '/sdc2/rest/v1/catalog/upload/multipart'
31                 if updateversion != None:
32                         url += '?createNewVersion=' + updateversion
33                 c.setopt(c.URL, url)
34                 c.setopt(c.POST, 1)             
35
36                 adminHeader = 'USER_ID: ' + adminUser
37                 #c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader])
38                 c.setopt(pycurl.HTTPHEADER, [adminHeader])
39
40
41                 path = fileDir + ELEMENT_NAME + "/" + ELEMENT_NAME + ".zip"
42                 debug(path)
43                 CURRENT_JSON_FILE=fileDir + ELEMENT_NAME + "/" + ELEMENT_NAME + ".json"
44                 #sed -i 's/"userId": ".*",/"userId": "'${USER_ID}'",/' ${CURRENT_JSON_FILE}
45
46                 jsonFile = open(CURRENT_JSON_FILE)
47                 
48                 debug("before load json")
49                 json_data = json.load(jsonFile, strict=False)
50                 debug(json_data)
51         
52                 jsonAsStr = json.dumps(json_data)
53
54                 send = [('resourceMetadata', jsonAsStr), ('resourceZip', (pycurl.FORM_FILE, path))]
55                 debug(send)
56                 c.setopt(pycurl.HTTPPOST, send)         
57
58                 #data = json.dumps(user)
59                 #c.setopt(c.POSTFIELDS, data)   
60
61                 if scheme == 'https':
62                         c.setopt(c.SSL_VERIFYPEER, 0)
63
64                 #c.setopt(c.WRITEFUNCTION, lambda x: None)
65                 c.setopt(c.WRITEFUNCTION, buffer.write)
66                 #print("before perform")        
67                 res = c.perform()
68         
69                 #print("Before get response code")      
70                 httpRes = c.getinfo(c.RESPONSE_CODE)
71                 if (httpRes != None):
72                         debug("http response=", httpRes)
73                 #print('Status: ' + str(responseCode))
74                 debug(buffer.getvalue())
75                 c.close()
76
77                 return (ELEMENT_NAME, httpRes, buffer.getvalue())
78
79         except Exception as inst:
80                 print("ERROR=" + str(inst))
81                 return (ELEMENT_NAME, None, None)                               
82
83
84 def usage():
85         print sys.argv[0], '[optional -s <scheme> | --scheme=<scheme>, default http] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-u <user userId> | --user=<user userId> ] [-v <true|false> | --updateversion=<true|false>]'
86
87
88 def importNormativeTypes(scheme, beHost, bePort, adminUser, fileDir, updateversion):
89         
90         normativeTypes = [ "root", "compute", "softwareComponent", "webServer", "webApplication", "DBMS", "database", "objectStorage", "blockStorage", "containerRuntime", "containerApplication", "loadBalancer", "port", "network", "allottedResource"]
91         #normativeTypes = [ "root" ]
92         responseCodes = [200, 201]
93         
94         if(updateversion == 'false'):
95                 responseCodes = [200, 201, 409]
96         
97         results = []
98         for normativeType in normativeTypes:
99                 result = createNormativeType(scheme, beHost, bePort, adminUser, fileDir, normativeType, updateversion)
100                 results.append(result)
101                 if ( result[1] == None or result[1] not in responseCodes ):
102                         print "Failed creating normative type " + normativeType + ". " + str(result[1])                                 
103         return results
104
105
106 def main(argv):
107         print 'Number of arguments:', len(sys.argv), 'arguments.'
108
109         beHost = 'localhost' 
110         bePort = '8080'
111         adminUser = 'jh0003'
112         updateversion = 'true'
113         scheme = 'http'
114
115         try:
116                 opts, args = getopt.getopt(argv,"i:p:u:v:h:s:",["ip=","port=","user=","updateversion=","scheme="])
117         except getopt.GetoptError:
118                 usage()
119                 errorAndExit(2, 'Invalid input')
120                  
121         for opt, arg in opts:
122         #print opt, arg
123                 if opt == '-h':
124                         usage()                        
125                         sys.exit(3)
126                 elif opt in ("-i", "--ip"):
127                         beHost = arg
128                 elif opt in ("-p", "--port"):
129                         bePort = arg
130                 elif opt in ("-u", "--user"):
131                         adminUser = arg
132                 elif opt in ("-s", "--scheme"):
133                         scheme = arg
134                 elif opt in ("-v", "--updateversion"):
135                         if (arg.lower() == "false" or arg.lower() == "no"):
136                                 updateversion = 'false'
137
138         print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', user =', adminUser, ', updateversion =', updateversion
139         
140         if ( beHost == None ):
141                 usage()
142                 sys.exit(3)
143
144         results = importNormativeTypes(scheme, beHost, bePort, adminUser, "../../../import/tosca/normative-types/", updateversion)
145
146         print "-----------------------------"
147         for result in results:
148                 print "{0:20} | {1:6}".format(result[0], result[1])
149         print "-----------------------------"
150         
151         responseCodes = [200, 201]
152         
153         if(updateversion == 'false'):
154                 responseCodes = [200, 201, 409]
155         
156         failedNormatives = filter(lambda x: x[1] == None or x[1] not in responseCodes, results)
157         if (len(failedNormatives) > 0):
158                 errorAndExit(1, None)
159         else:
160                 errorAndExit(0, None)
161
162
163 if __name__ == "__main__":
164         main(sys.argv[1:])
165