diff options
Diffstat (limited to 'catalog-be/src/main/resources/scripts/import/tosca/importNormativeTypes.py')
-rw-r--r-- | catalog-be/src/main/resources/scripts/import/tosca/importNormativeTypes.py | 304 |
1 files changed, 153 insertions, 151 deletions
diff --git a/catalog-be/src/main/resources/scripts/import/tosca/importNormativeTypes.py b/catalog-be/src/main/resources/scripts/import/tosca/importNormativeTypes.py index 0fcaf58ee6..889ea05b66 100644 --- a/catalog-be/src/main/resources/scripts/import/tosca/importNormativeTypes.py +++ b/catalog-be/src/main/resources/scripts/import/tosca/importNormativeTypes.py @@ -1,11 +1,12 @@ -import pycurl -import sys, getopt +import json import zipfile from StringIO import StringIO -import json -import copy + +import pycurl + from importCommon import * -import importCommon + + ######################################################################################################################################################################################### # # # Import all users from a given file # @@ -18,158 +19,159 @@ import importCommon # # ######################################################################################################################################################################################### -def createNormativeType(scheme, beHost, bePort, adminUser, fileDir, ELEMENT_NAME, updateversion): - - try: - log("in create normative type ", ELEMENT_NAME) - debug("userId", adminUser) - debug("fileDir", fileDir) - - buffer = StringIO() - c = pycurl.Curl() - if is_debug(): - c.setopt(pycurl.VERBOSE, 1) - - url = scheme + '://' + beHost + ':' + bePort + '/sdc2/rest/v1/catalog/upload/multipart' - if updateversion != None: - url += '?createNewVersion=' + updateversion - c.setopt(c.URL, url) - c.setopt(c.POST, 1) - - adminHeader = 'USER_ID: ' + adminUser - #c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader]) - c.setopt(pycurl.HTTPHEADER, [adminHeader]) - - yml_path = fileDir + ELEMENT_NAME + "/" + ELEMENT_NAME + ".yml" - path = fileDir + ELEMENT_NAME + "/" + ELEMENT_NAME + ".zip" - - zf = zipfile.ZipFile(path, "w") - zf.write(yml_path, ELEMENT_NAME + '.yml') - zf.close() - - debug(path) - CURRENT_JSON_FILE=fileDir + ELEMENT_NAME + "/" + ELEMENT_NAME + ".json" - #sed -i 's/"userId": ".*",/"userId": "'${USER_ID}'",/' ${CURRENT_JSON_FILE} - - jsonFile = open(CURRENT_JSON_FILE) - - debug("before load json") - json_data = json.load(jsonFile, strict=False) - debug(json_data) - - jsonAsStr = json.dumps(json_data) - - send = [('resourceMetadata', jsonAsStr), ('resourceZip', (pycurl.FORM_FILE, path))] - debug(send) - c.setopt(pycurl.HTTPPOST, send) - - #data = json.dumps(user) - #c.setopt(c.POSTFIELDS, data) - - if scheme == 'https': - c.setopt(pycurl.SSL_VERIFYPEER, 0) - c.setopt(pycurl.SSL_VERIFYHOST, 0) - - #c.setopt(c.WRITEFUNCTION, lambda x: None) - c.setopt(c.WRITEFUNCTION, buffer.write) - - #print("before perform") - res = c.perform() - - #print("Before get response code") - httpRes = c.getinfo(c.RESPONSE_CODE) - if (httpRes != None): - debug("http response=", httpRes) - #print('Status: ' + str(responseCode)) - debug(buffer.getvalue()) - c.close() - - return (ELEMENT_NAME, httpRes, buffer.getvalue()) - - except Exception as inst: - print("ERROR=" + str(inst)) - return (ELEMENT_NAME, None, None) +def createNormativeType(scheme, be_host, be_port, admin_user, file_dir, element_name, update_version): + try: + log("in create normative type ", element_name) + debug("userId", admin_user) + debug("fileDir", file_dir) + + buffer = StringIO() + c = pycurl.Curl() + if is_debug(): + c.setopt(pycurl.VERBOSE, 1) + + url = scheme + '://' + be_host + ':' + be_port + '/sdc2/rest/v1/catalog/upload/multipart' + if update_version is not None: + url += '?createNewVersion=' + update_version + c.setopt(c.URL, url) + c.setopt(c.POST, 1) + + admin_header = 'USER_ID: ' + admin_user + # c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader]) + c.setopt(pycurl.HTTPHEADER, [admin_header]) + + yml_path = file_dir + element_name + "/" + element_name + ".yml" + path = file_dir + element_name + "/" + element_name + ".zip" + + zf = zipfile.ZipFile(path, "w") + zf.write(yml_path, element_name + '.yml') + zf.close() + + debug(path) + current_json_file = file_dir + element_name + "/" + element_name + ".json" + # sed -i 's/"userId": ".*",/"userId": "'${USER_ID}'",/' ${current_json_file} + + jsonFile = open(current_json_file) + + debug("before load json") + json_data = json.load(jsonFile, strict=False) + debug(json_data) + + json_as_str = json.dumps(json_data) + + send = [('resourceMetadata', json_as_str), ('resourceZip', (pycurl.FORM_FILE, path))] + debug(send) + c.setopt(pycurl.HTTPPOST, send) + + # data = json.dumps(user) + # c.setopt(c.POSTFIELDS, data) + + if scheme == 'https': + c.setopt(pycurl.SSL_VERIFYPEER, 0) + c.setopt(pycurl.SSL_VERIFYHOST, 0) + + # c.setopt(c.WRITEFUNCTION, lambda x: None) + c.setopt(c.WRITEFUNCTION, buffer.write) + + # print("before perform") + c.perform() + + # print("Before get response code") + http_res = c.getinfo(c.RESPONSE_CODE) + if http_res is not None: + debug("http response=", http_res) + # print('Status: ' + str(responseCode)) + debug(buffer.getvalue()) + c.close() + + return (element_name, http_res, buffer.getvalue()) + + except Exception as inst: + print("ERROR=" + str(inst)) + return (element_name, None, None) def usage(): - 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>]' - - -def importNormativeTypes(scheme, beHost, bePort, adminUser, fileDir, updateversion): - - normativeTypes = [ "root", "compute", "softwareComponent", "webServer", "webApplication", "DBMS", "database", "objectStorage", "blockStorage", "containerRuntime", "containerApplication", "loadBalancer", "port", "network"] - #normativeTypes = [ "root" ] - responseCodes = [200, 201] - - if(updateversion == 'false'): - responseCodes = [200, 201, 409] - - results = [] - for normativeType in normativeTypes: - result = createNormativeType(scheme, beHost, bePort, adminUser, fileDir, normativeType, updateversion) - results.append(result) - if ( result[1] == None or result[1] not in responseCodes ): - print "Failed creating normative type " + normativeType + ". " + str(result[1]) - return results + 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>]' + + +def importNormativeTypes(scheme, be_host, be_port, admin_user, file_dir, update_version): + normative_types = ["root", "compute", "softwareComponent", "webServer", "webApplication", "DBMS", "database", + "objectStorage", "blockStorage", "containerRuntime", "containerApplication", "loadBalancer", + "port", "network"] + # normative_types = [ "root" ] + response_codes = [200, 201] + + if update_version == 'false': + response_codes = [200, 201, 409] + + results = [] + for normative_type in normative_types: + result = createNormativeType(scheme, be_host, be_port, admin_user, file_dir, normative_type, update_version) + results.append(result) + if result[1] is None or result[1] not in response_codes: + print "Failed creating normative type " + normative_type + ". " + str(result[1]) + return results def main(argv): - print 'Number of arguments:', len(sys.argv), 'arguments.' - - beHost = 'localhost' - bePort = '8080' - adminUser = 'jh0003' - updateversion = 'true' - scheme = 'http' - - try: - opts, args = getopt.getopt(argv,"i:p:u:v:h:s:",["ip=","port=","user=","updateversion=","scheme="]) - except getopt.GetoptError: - usage() - error_and_exit(2, 'Invalid input') - - for opt, arg in opts: - #print opt, arg - if opt == '-h': - usage() - sys.exit(3) - elif opt in ("-i", "--ip"): - beHost = arg - elif opt in ("-p", "--port"): - bePort = arg - elif opt in ("-u", "--user"): - adminUser = arg - elif opt in ("-s", "--scheme"): - scheme = arg - elif opt in ("-v", "--updateversion"): - if (arg.lower() == "false" or arg.lower() == "no"): - updateversion = 'false' - - print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', user =', adminUser, ', updateversion =', updateversion - - if ( beHost == None ): - usage() - sys.exit(3) - - results = importNormativeTypes(scheme, beHost, bePort, adminUser, "../../../import/tosca/normative-types/", updateversion) - - print "-----------------------------" - for result in results: - print "{0:20} | {1:6}".format(result[0], result[1]) - print "-----------------------------" - - responseCodes = [200, 201] - - if(updateversion == 'false'): - responseCodes = [200, 201, 409] - - failedNormatives = filter(lambda x: x[1] == None or x[1] not in responseCodes, results) - if (len(failedNormatives) > 0): - error_and_exit(1, None) - else: - error_and_exit(0, None) + print 'Number of arguments:', len(sys.argv), 'arguments.' + + be_host = 'localhost' + be_port = '8080' + admin_user = 'jh0003' + update_version = 'true' + scheme = 'http' + + try: + opts, args = getopt.getopt(argv, "i:p:u:v:h:s:", ["ip=", "port=", "user=", "updateversion=", "scheme="]) + except getopt.GetoptError: + usage() + error_and_exit(2, 'Invalid input') + + for opt, arg in opts: + # print opt, arg + if opt == '-h': + usage() + sys.exit(3) + elif opt in ("-i", "--ip"): + be_host = arg + elif opt in ("-p", "--port"): + be_port = arg + elif opt in ("-u", "--user"): + admin_user = arg + elif opt in ("-s", "--scheme"): + scheme = arg + elif opt in ("-v", "--updateversion"): + if arg.lower() == "false" or arg.lower() == "no": + update_version = 'false' + + print 'scheme =', scheme, ', be host =', be_host, ', be port =', be_port, ', user =', admin_user, ', updateversion =', update_version + + if be_host is None: + usage() + sys.exit(3) + + results = importNormativeTypes(scheme, be_host, be_port, admin_user, "../../../import/tosca/normative-types/", + update_version) + + print "-----------------------------" + for result in results: + print "{0:20} | {1:6}".format(result[0], result[1]) + print "-----------------------------" + + response_codes = [200, 201] + + if update_version == 'false': + response_codes = [200, 201, 409] + + failed_normatives = filter(lambda x: x[1] is None or x[1] not in response_codes, results) + if len(list(failed_normatives)) > 0: + error_and_exit(1, None) + else: + error_and_exit(0, None) if __name__ == "__main__": - main(sys.argv[1:]) - + main(sys.argv[1:]) |