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(pycurl.SSL_VERIFYPEER, 0)
60 c.setopt(pycurl.SSL_VERIFYHOST, 0)
64 #print("Before get response code")
65 httpRes = c.getinfo(c.RESPONSE_CODE)
67 debug("http response=", httpRes)
68 #print('Status: ' + str(responseCode))
69 debug(buffer.getvalue())
72 return (ELEMENT_NAME, httpRes, buffer.getvalue())
74 except Exception as inst:
75 print("ERROR=" + str(inst))
76 return (ELEMENT_NAME, None, None)
80 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>]'
81 print "----------------- Example -------------------"
82 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"
85 print 'Number of arguments:', len(sys.argv), 'arguments.'
96 opts, args = getopt.getopt(argv,"i:p:u:d:l:e:h:s:",["ip=","port=","user=","location=","element=", "debug=","scheme="])
97 except getopt.GetoptError:
99 error_and_exit(2, 'Invalid input')
101 for opt, arg in opts:
106 elif opt in ("-i", "--ip"):
108 elif opt in ("-p", "--port"):
110 elif opt in ("-u", "--user"):
112 elif opt in ("-l", "--location"):
114 elif opt in ("-e", "--element"):
116 elif opt in ("-s", "--scheme"):
118 elif opt in ("-d", "--debug"):
120 debugf = bool(arg.lower() == "true" or arg.lower() == "yes")
122 print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', user =', adminUser
124 if ( beHost == None ):
129 print 'set debug mode to ' + str(debugf)
130 importCommon.debugFlag = debugf
132 if (location == None):
133 print 'Missing file location'
137 if (element == None):
138 print 'Missing element name. E.g. root, compute, ...'
142 #pathdir = os.path.dirname(os.path.realpath(sys.argv[0]))
144 #baseFileLocation = pathdir + "/../../../import/tosca/"
145 #fileDir = baseFileLocation + "user-normative-types/"
147 #normativeType = "root1"
149 result = createUserNormativeType(scheme, beHost, bePort, adminUser, location, element)
150 #result = createUserNormativeType(beHost, bePort, adminUser, fileDir, normativeType)
151 print "---------------------------------------"
152 print "{0:30} | {1:6}".format(result[0], result[1])
153 print "---------------------------------------"
155 if ( result[1] == None or result[1] not in [200, 201] ) :
156 print "Failed creating normative type " + element + ". " + str(result[1])
157 error_and_exit(1, None)
159 error_and_exit(0, None)
161 if __name__ == "__main__":