3 from StringIO import StringIO
6 from importCommon import *
9 ################################################################################################################################################
11 ################################################################################################################################################
13 def createZipFromYml(ymlFile, zipFile):
14 zip = zipfile.ZipFile(zipFile, 'w', zipfile.ZIP_DEFLATED)
16 zip.write(ymlFile, os.path.basename(ymlFile))
19 def createUserNormativeType(scheme, beHost, bePort, adminUser, fileDir, ELEMENT_NAME):
22 log("in create normative type ", ELEMENT_NAME)
23 debug("userId", adminUser)
24 debug("fileDir", fileDir)
29 url = scheme + '://' + beHost + ':' + bePort + '/sdc2/rest/v1/catalog/upload/multipart'
33 adminHeader = 'USER_ID: ' + adminUser
34 #c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader])
35 c.setopt(pycurl.HTTPHEADER, [adminHeader])
37 ymlFile = fileDir + ELEMENT_NAME + "/normative-types-new-" + ELEMENT_NAME + ".yml"
38 zipFile = fileDir + ELEMENT_NAME + "/normative-types-new-" + ELEMENT_NAME + ".zip"
43 CURRENT_JSON_FILE=fileDir + ELEMENT_NAME + "/" + ELEMENT_NAME + ".json"
44 debug(CURRENT_JSON_FILE)
45 jsonFile = open(CURRENT_JSON_FILE)
47 debug("before load json")
48 json_data = json.load(jsonFile, strict=False)
51 jsonAsStr = json.dumps(json_data)
53 send = [('resourceMetadata', jsonAsStr), ('resourceZip', (pycurl.FORM_FILE, path))]
55 c.setopt(pycurl.HTTPPOST, send)
57 c.setopt(c.WRITEFUNCTION, buffer.write)
59 c.setopt(c.SSL_VERIFYPEER, 0)
63 #print("Before get response code")
64 httpRes = c.getinfo(c.RESPONSE_CODE)
66 debug("http response=", httpRes)
67 #print('Status: ' + str(responseCode))
68 debug(buffer.getvalue())
71 return (ELEMENT_NAME, httpRes, buffer.getvalue())
73 except Exception as inst:
74 print("ERROR=" + str(inst))
75 return (ELEMENT_NAME, None, None)
79 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> ] [-l <directory base location> | --location=<directory base location>] [-e <element name> | --element=<element name>]'
80 print "----------------- Example -------------------"
81 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"
84 print 'Number of arguments:', len(sys.argv), 'arguments.'
95 opts, args = getopt.getopt(argv,"i:p:u:d:l:e:h:s:",["ip=","port=","user=","location=","element=", "debug=","scheme="])
96 except getopt.GetoptError:
98 errorAndExit(2, 'Invalid input')
100 for opt, arg in opts:
105 elif opt in ("-i", "--ip"):
107 elif opt in ("-p", "--port"):
109 elif opt in ("-u", "--user"):
111 elif opt in ("-l", "--location"):
113 elif opt in ("-e", "--element"):
115 elif opt in ("-s", "--scheme"):
117 elif opt in ("-d", "--debug"):
119 debugf = bool(arg.lower() == "true" or arg.lower() == "yes")
121 print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', user =', adminUser
123 if ( beHost == None ):
128 print 'set debug mode to ' + str(debugf)
129 importCommon.debugFlag = debugf
131 if (location == None):
132 print 'Missing file location'
136 if (element == None):
137 print 'Missing element name. E.g. root, compute, ...'
141 #pathdir = os.path.dirname(os.path.realpath(sys.argv[0]))
143 #baseFileLocation = pathdir + "/../../../import/tosca/"
144 #fileDir = baseFileLocation + "user-normative-types/"
146 #normativeType = "root1"
148 result = createUserNormativeType(scheme, beHost, bePort, adminUser, location, element)
149 #result = createUserNormativeType(beHost, bePort, adminUser, fileDir, normativeType)
150 print "---------------------------------------"
151 print "{0:30} | {1:6}".format(result[0], result[1])
152 print "---------------------------------------"
154 if ( result[1] == None or result[1] not in [200, 201] ) :
155 print "Failed creating normative type " + element + ". " + str(result[1])
156 errorAndExit(1, None)
158 errorAndExit(0, None)
160 if __name__ == "__main__":