Sync Integ to Master
[sdc.git] / catalog-be / src / main / resources / scripts / import / tosca / importNormativeElements.py
1 import pycurl
2 import sys, getopt
3 from StringIO import StringIO
4 import json
5 import copy
6 from importCommon import *
7 #################################################################################################################################################################################
8 #                                                                                                                                                                                                                                                                                                                                                       #
9 # Import all users from a given file                                                                                                                                                                                                                                                                                    #
10 #                                                                                                                                                                                                                                                                                                                                                               #
11 # activation :                                                                                                                                                                                                                                                                                                                                  #
12 #       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> ]   #
13 #                                                                                                                                                                                                                                                                                                                                                               #
14 # shortest activation (be host = localhost, be port = 8080):                                                                                                                                                                                                                                    #
15 #               python importUsers.py [-f <input file> | --ifile=<input file> ]                                                                                                                                                                                                         #
16 #                                                                                                                                                                                                                                                                                                                                                       #
17 #################################################################################################################################################################################
18
19 def createNormativeElement(scheme, beHost, bePort, adminUser, fileDir, urlSuffix, ELEMENT_NAME, elementFormName):
20         
21         try:
22                 log("in create normative element ", ELEMENT_NAME)
23
24                 buffer = StringIO()
25                 c = pycurl.Curl()
26
27                 url = scheme + '://' + beHost + ':' + bePort + urlSuffix
28                 c.setopt(c.URL, url)
29                 c.setopt(c.POST, 1)             
30
31                 adminHeader = 'USER_ID: ' + adminUser
32                 #c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader])
33                 c.setopt(pycurl.HTTPHEADER, [adminHeader])
34
35                         
36                 path = fileDir + "/" + ELEMENT_NAME + ".zip"
37                 debug(path)
38
39                 send = [(elementFormName, (pycurl.FORM_FILE, path))]
40                 debug(send)
41                 c.setopt(pycurl.HTTPPOST, send)         
42
43                 #data = json.dumps(user)
44                 #c.setopt(c.POSTFIELDS, data)   
45
46                 #c.setopt(c.WRITEFUNCTION, lambda x: None)
47                 c.setopt(c.WRITEFUNCTION, buffer.write)
48
49                 if scheme == 'https':
50                         c.setopt(c.SSL_VERIFYPEER, 0)
51
52                 #print("before perform")        
53                 res = c.perform()
54         
55                 #print("Before get response code")      
56                 httpRes = c.getinfo(c.RESPONSE_CODE)
57                 if (httpRes != None):
58                         debug("http response=", httpRes)
59                 #print('Status: ' + str(responseCode))
60                 debug("response buffer", buffer.getvalue())
61                 c.close()
62
63                 return (ELEMENT_NAME, httpRes, buffer.getvalue())
64
65         except Exception as inst:
66                 print("ERROR=" + str(inst))
67                 return (ELEMENT_NAME, None, None)                               
68
69