4 from StringIO import StringIO
9 from importCommon import *
12 ################################################################################################################################################
14 ################################################################################################################################################
17 def createZipFromYml(ymlFile, zipFile):
18 zip = zipfile.ZipFile(zipFile, 'w', zipfile.ZIP_DEFLATED)
20 zip.write(ymlFile, os.path.basename(ymlFile))
24 def createUserNormativeType(scheme, be_host, be_port, admin_user, file_dir, element_name):
26 log("in create normative type ", element_name)
27 debug("userId", admin_user)
28 debug("fileDir", file_dir)
33 url = scheme + '://' + be_host + ':' + be_port + '/sdc2/rest/v1/catalog/upload/multipart'
37 admin_header = 'USER_ID: ' + admin_user
38 # c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader])
39 c.setopt(pycurl.HTTPHEADER, [admin_header])
41 ymlFile = file_dir + element_name + "/normative-types-new-" + element_name + ".yml"
42 zipFile = file_dir + element_name + "/normative-types-new-" + element_name + ".zip"
47 current_json_file = file_dir + element_name + "/" + element_name + ".json"
48 debug(current_json_file)
49 json_file = open(current_json_file)
51 debug("before load json")
52 json_data = json.load(json_file, strict=False)
55 json_as_str = json.dumps(json_data)
57 send = [('resourceMetadata', json_as_str), ('resourceZip', (pycurl.FORM_FILE, path))]
59 c.setopt(pycurl.HTTPPOST, send)
61 c.setopt(c.WRITEFUNCTION, _buffer.write)
63 c.setopt(pycurl.SSL_VERIFYPEER, 0)
64 c.setopt(pycurl.SSL_VERIFYHOST, 0)
68 # print("Before get response code")
69 http_res = c.getinfo(c.RESPONSE_CODE)
70 if http_res is not None:
71 debug("http response=", http_res)
72 # print('Status: ' + str(responseCode))
73 debug(_buffer.getvalue())
76 return element_name, http_res, _buffer.getvalue()
78 except Exception as inst:
79 print("ERROR=" + str(inst))
80 return element_name, None, None
85 '[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> ] [-l <directory base location> | --location=<directory base location>] [-e <element name> | --element=<element name>]'
86 print "----------------- Example -------------------"
87 print "python importNodeType.py -d false -l /home/vagrant/catalog-be-1604.0.2.15.6-SNAPSHOT/scripts/import/tosca/../../../import/tosca/user-normative-types/ -e root1"
91 print 'Number of arguments:', len(sys.argv), 'arguments.'
102 opts, args = getopt.getopt(argv, "i:p:u:d:l:e:h:s:",
103 ["ip=", "port=", "user=", "location=", "element=", "debug=", "scheme="])
104 except getopt.GetoptError:
106 error_and_exit(2, 'Invalid input')
108 for opt, arg in opts:
113 elif opt in ("-i", "--ip"):
115 elif opt in ("-p", "--port"):
117 elif opt in ("-u", "--user"):
119 elif opt in ("-l", "--location"):
121 elif opt in ("-e", "--element"):
123 elif opt in ("-s", "--scheme"):
125 elif opt in ("-d", "--debug"):
127 debug_f = bool(arg.lower() == "true" or arg.lower() == "yes")
129 print 'scheme =', scheme, ', be host =', be_host, ', be port =', be_port, ', user =', admin_user
135 if debug_f is not None:
136 print 'set debug mode to ' + str(debug_f)
137 importCommon.debugFlag = debug_f
140 print 'Missing file location'
145 print 'Missing element name. E.g. root, compute, ...'
149 # pathdir = os.path.dirname(os.path.realpath(sys.argv[0]))
151 # baseFileLocation = pathdir + "/../../../import/tosca/"
152 # fileDir = baseFileLocation + "user-normative-types/"
154 # normativeType = "root1"
156 result = createUserNormativeType(scheme, be_host, be_port, admin_user, location, element)
157 # result = createUserNormativeType(beHost, bePort, adminUser, fileDir, normativeType)
158 print "---------------------------------------"
159 print "{0:30} | {1:6}".format(result[0], result[1])
160 print "---------------------------------------"
162 if result[1] is None or result[1] not in [200, 201]:
163 print "Failed creating normative type " + element + ". " + str(result[1])
164 error_and_exit(1, None)
166 error_and_exit(0, None)
169 if __name__ == "__main__":