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