4 from StringIO import StringIO
7 from importCommon import *
9 #########################################################################################################################################################################################
11 # Import all users from a given file #
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> ] #
19 #########################################################################################################################################################################################
21 def createNormativeType(scheme, beHost, bePort, adminUser, fileDir, ELEMENT_NAME, updateversion):
24 log("in create normative type ", ELEMENT_NAME)
25 debug("userId", adminUser)
26 debug("fileDir", fileDir)
31 c.setopt(pycurl.VERBOSE, 1)
33 url = scheme + '://' + beHost + ':' + bePort + '/sdc2/rest/v1/catalog/upload/multipart'
34 if updateversion != None:
35 url += '?createNewVersion=' + updateversion
39 adminHeader = 'USER_ID: ' + adminUser
40 #c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader])
41 c.setopt(pycurl.HTTPHEADER, [adminHeader])
43 yml_path = fileDir + ELEMENT_NAME + "/" + ELEMENT_NAME + ".yml"
44 path = fileDir + ELEMENT_NAME + "/" + ELEMENT_NAME + ".zip"
46 zf = zipfile.ZipFile(path, "w")
47 zf.write(yml_path, ELEMENT_NAME + '.yml')
51 CURRENT_JSON_FILE=fileDir + ELEMENT_NAME + "/" + ELEMENT_NAME + ".json"
52 #sed -i 's/"userId": ".*",/"userId": "'${USER_ID}'",/' ${CURRENT_JSON_FILE}
54 jsonFile = open(CURRENT_JSON_FILE)
56 debug("before load json")
57 json_data = json.load(jsonFile, strict=False)
60 jsonAsStr = json.dumps(json_data)
62 send = [('resourceMetadata', jsonAsStr), ('resourceZip', (pycurl.FORM_FILE, path))]
64 c.setopt(pycurl.HTTPPOST, send)
66 #data = json.dumps(user)
67 #c.setopt(c.POSTFIELDS, data)
70 c.setopt(pycurl.SSL_VERIFYPEER, 0)
71 c.setopt(pycurl.SSL_VERIFYHOST, 0)
73 #c.setopt(c.WRITEFUNCTION, lambda x: None)
74 c.setopt(c.WRITEFUNCTION, buffer.write)
76 #print("before perform")
79 #print("Before get response code")
80 httpRes = c.getinfo(c.RESPONSE_CODE)
82 debug("http response=", httpRes)
83 #print('Status: ' + str(responseCode))
84 debug(buffer.getvalue())
87 return (ELEMENT_NAME, httpRes, buffer.getvalue())
89 except Exception as inst:
90 print("ERROR=" + str(inst))
91 return (ELEMENT_NAME, None, None)
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>]'
98 def importNormativeTypes(scheme, beHost, bePort, adminUser, fileDir, updateversion):
100 normativeTypes = [ "root", "compute", "softwareComponent", "webServer", "webApplication", "DBMS", "database", "objectStorage", "blockStorage", "containerRuntime", "containerApplication", "loadBalancer", "port", "network"]
101 #normativeTypes = [ "root" ]
102 responseCodes = [200, 201]
104 if(updateversion == 'false'):
105 responseCodes = [200, 201, 409]
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])
117 print 'Number of arguments:', len(sys.argv), 'arguments.'
122 updateversion = 'true'
126 opts, args = getopt.getopt(argv,"i:p:u:v:h:s:",["ip=","port=","user=","updateversion=","scheme="])
127 except getopt.GetoptError:
129 error_and_exit(2, 'Invalid input')
131 for opt, arg in opts:
136 elif opt in ("-i", "--ip"):
138 elif opt in ("-p", "--port"):
140 elif opt in ("-u", "--user"):
142 elif opt in ("-s", "--scheme"):
144 elif opt in ("-v", "--updateversion"):
145 if (arg.lower() == "false" or arg.lower() == "no"):
146 updateversion = 'false'
148 print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', user =', adminUser, ', updateversion =', updateversion
150 if ( beHost == None ):
154 results = importNormativeTypes(scheme, beHost, bePort, adminUser, "../../../import/tosca/normative-types/", updateversion)
156 print "-----------------------------"
157 for result in results:
158 print "{0:20} | {1:6}".format(result[0], result[1])
159 print "-----------------------------"
161 responseCodes = [200, 201]
163 if(updateversion == 'false'):
164 responseCodes = [200, 201, 409]
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)
170 error_and_exit(0, None)
173 if __name__ == "__main__":