aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/resources/scripts/import/tosca/importNormativeElements.py
blob: c28bbfb143f55559f8ef2e3ac89f8ab7dfa9da73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import pycurl
import sys, getopt
from StringIO import StringIO
import json
import copy
from importCommon import *
#################################################################################################################################################################################
#																																		       									#
# Import all users from a given file																										   									#
# 																																			   									#
# activation :																																   									#
#       python importUsers.py [-s <scheme> | --scheme=<scheme> ] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <input file> | --ifile=<input file> ]   #
#																																		  	   									#
# shortest activation (be host = localhost, be port = 8080): 																				   									#
#		python importUsers.py [-f <input file> | --ifile=<input file> ]												 				           									#
#																																		       									#
#################################################################################################################################################################################

def createNormativeElement(scheme, beHost, bePort, adminUser, fileDir, urlSuffix, ELEMENT_NAME, elementFormName):
	
	try:
		log("in create normative element ", ELEMENT_NAME)

		buffer = StringIO()
		c = pycurl.Curl()

		url = scheme + '://' + beHost + ':' + bePort + urlSuffix
		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])

			
		path = fileDir + "/" + ELEMENT_NAME + ".zip"
		debug(path)

		send = [(elementFormName, (pycurl.FORM_FILE, path))]
		debug(send)
		c.setopt(pycurl.HTTPPOST, send)		

		#data = json.dumps(user)
		#c.setopt(c.POSTFIELDS, data)	

		#c.setopt(c.WRITEFUNCTION, lambda x: None)
		c.setopt(c.WRITEFUNCTION, buffer.write)

		if scheme == 'https':
			c.setopt(c.SSL_VERIFYPEER, 0)

		#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("response buffer", buffer.getvalue())
		c.close()

		return (ELEMENT_NAME, httpRes, buffer.getvalue())

	except Exception as inst:
		print("ERROR=" + str(inst))
		return (ELEMENT_NAME, None, None)