Decouple TXT Report file writing and formatting logic (6/6)
[sdc.git] / catalog-be / src / main / resources / scripts / import / tosca / importNormativeTypes.py
1 import json
2 import zipfile
3 from StringIO import StringIO
4
5 import pycurl
6
7 from importCommon import *
8
9
10 #########################################################################################################################################################################################
11 #                                                                                                                                                                                                                                                                                                                                                                       #
12 # Import all users from a given file                                                                                                                                                                                                                                                                                                    #
13 #                                                                                                                                                                                                                                                                                                                                                                               #
14 # activation :                                                                                                                                                                                                                                                                                                                                                  #
15 #       python importNormativeTypes.py [-s <scheme> | --scheme=<scheme> ] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <input file> | --ifile=<input file> ]  #
16 #                                                         [-v <true|false> | --updateversion=<true|false>]                                                                                                                                                                                                                      #
17 # shortest activation (be host = localhost, be port = 8080):                                                                                                                                                                                                                                                    #
18 #               python importNormativeTypes.py [-f <input file> | --ifile=<input file> ]                                                                                                                                                                                                        #
19 #                                                                                                                                                                                                                                                                                                                                                                       #       
20 #########################################################################################################################################################################################
21
22 def createNormativeType(scheme, be_host, be_port, admin_user, file_dir, element_name, update_version):
23     try:
24         log("in create normative type ", element_name)
25         debug("userId", admin_user)
26         debug("fileDir", file_dir)
27
28         buffer = StringIO()
29         c = pycurl.Curl()
30         if is_debug():
31             c.setopt(pycurl.VERBOSE, 1)
32
33         url = scheme + '://' + be_host + ':' + be_port + '/sdc2/rest/v1/catalog/upload/multipart'
34         if update_version is not None:
35             url += '?createNewVersion=' + update_version
36         c.setopt(c.URL, url)
37         c.setopt(c.POST, 1)
38
39         admin_header = 'USER_ID: ' + admin_user
40         # c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader])
41         c.setopt(pycurl.HTTPHEADER, [admin_header])
42
43         yml_path = file_dir + element_name + "/" + element_name + ".yml"
44         path = file_dir + element_name + "/" + element_name + ".zip"
45
46         zf = zipfile.ZipFile(path, "w")
47         zf.write(yml_path, element_name + '.yml')
48         zf.close()
49
50         debug(path)
51         current_json_file = file_dir + element_name + "/" + element_name + ".json"
52         # sed -i 's/"userId": ".*",/"userId": "'${USER_ID}'",/' ${current_json_file}
53
54         jsonFile = open(current_json_file)
55
56         debug("before load json")
57         json_data = json.load(jsonFile, strict=False)
58         debug(json_data)
59
60         json_as_str = json.dumps(json_data)
61
62         send = [('resourceMetadata', json_as_str), ('resourceZip', (pycurl.FORM_FILE, path))]
63         debug(send)
64         c.setopt(pycurl.HTTPPOST, send)
65
66         # data = json.dumps(user)
67         # c.setopt(c.POSTFIELDS, data)
68
69         if scheme == 'https':
70             c.setopt(pycurl.SSL_VERIFYPEER, 0)
71             c.setopt(pycurl.SSL_VERIFYHOST, 0)
72
73         # c.setopt(c.WRITEFUNCTION, lambda x: None)
74         c.setopt(c.WRITEFUNCTION, buffer.write)
75
76         # print("before perform")
77         c.perform()
78
79         # print("Before get response code")
80         http_res = c.getinfo(c.RESPONSE_CODE)
81         if http_res is not None:
82             debug("http response=", http_res)
83         # print('Status: ' + str(responseCode))
84         debug(buffer.getvalue())
85         c.close()
86
87         return (element_name, http_res, buffer.getvalue())
88
89     except Exception as inst:
90         print("ERROR=" + str(inst))
91         return (element_name, None, None)
92
93
94 def usage():
95     print sys.argv[0], \
96         '[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> ] [-v <true|false> | --updateversion=<true|false>]'
97
98
99 def importNormativeTypes(scheme, be_host, be_port, admin_user, file_dir, update_version):
100     normative_types = ["root", "compute", "softwareComponent", "webServer", "webApplication", "DBMS", "database",
101                        "objectStorage", "blockStorage", "containerRuntime", "containerApplication", "loadBalancer",
102                        "port", "network"]
103     # normative_types = [ "root" ]
104     response_codes = [200, 201]
105
106     if update_version == 'false':
107         response_codes = [200, 201, 409]
108
109     results = []
110     for normative_type in normative_types:
111         result = createNormativeType(scheme, be_host, be_port, admin_user, file_dir, normative_type, update_version)
112         results.append(result)
113         if result[1] is None or result[1] not in response_codes:
114             print "Failed creating normative type " + normative_type + ". " + str(result[1])
115     return results
116
117
118 def main(argv):
119     print 'Number of arguments:', len(sys.argv), 'arguments.'
120
121     be_host = 'localhost'
122     be_port = '8080'
123     admin_user = 'jh0003'
124     update_version = 'true'
125     scheme = 'http'
126
127     try:
128         opts, args = getopt.getopt(argv, "i:p:u:v:h:s:", ["ip=", "port=", "user=", "updateversion=", "scheme="])
129     except getopt.GetoptError:
130         usage()
131         error_and_exit(2, 'Invalid input')
132
133     for opt, arg in opts:
134         # print opt, arg
135         if opt == '-h':
136             usage()
137             sys.exit(3)
138         elif opt in ("-i", "--ip"):
139             be_host = arg
140         elif opt in ("-p", "--port"):
141             be_port = arg
142         elif opt in ("-u", "--user"):
143             admin_user = arg
144         elif opt in ("-s", "--scheme"):
145             scheme = arg
146         elif opt in ("-v", "--updateversion"):
147             if arg.lower() == "false" or arg.lower() == "no":
148                 update_version = 'false'
149
150     print 'scheme =', scheme, ', be host =', be_host, ', be port =', be_port, ', user =', admin_user, ', updateversion =', update_version
151
152     if be_host is None:
153         usage()
154         sys.exit(3)
155
156     results = importNormativeTypes(scheme, be_host, be_port, admin_user, "../../../import/tosca/normative-types/",
157                                    update_version)
158
159     print "-----------------------------"
160     for result in results:
161         print "{0:20} | {1:6}".format(result[0], result[1])
162     print "-----------------------------"
163
164     response_codes = [200, 201]
165
166     if update_version == 'false':
167         response_codes = [200, 201, 409]
168
169     failed_normatives = filter(lambda x: x[1] is None or x[1] not in response_codes, results)
170     if len(list(failed_normatives)) > 0:
171         error_and_exit(1, None)
172     else:
173         error_and_exit(0, None)
174
175
176 if __name__ == "__main__":
177     main(sys.argv[1:])