summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/resources/scripts/sdcBePy/common
diff options
context:
space:
mode:
Diffstat (limited to 'catalog-be/src/main/resources/scripts/sdcBePy/common')
-rw-r--r--catalog-be/src/main/resources/scripts/sdcBePy/common/__init__.py0
-rwxr-xr-xcatalog-be/src/main/resources/scripts/sdcBePy/common/bColors.py8
-rw-r--r--catalog-be/src/main/resources/scripts/sdcBePy/common/helpers.py3
-rw-r--r--catalog-be/src/main/resources/scripts/sdcBePy/common/logger.py83
-rw-r--r--catalog-be/src/main/resources/scripts/sdcBePy/common/normative/__init__.py0
-rw-r--r--catalog-be/src/main/resources/scripts/sdcBePy/common/normative/main.py16
-rw-r--r--catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaElements.py107
-rw-r--r--catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaTypes.py121
-rwxr-xr-xcatalog-be/src/main/resources/scripts/sdcBePy/common/sdcBeProxy.py117
9 files changed, 455 insertions, 0 deletions
diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/common/__init__.py b/catalog-be/src/main/resources/scripts/sdcBePy/common/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/catalog-be/src/main/resources/scripts/sdcBePy/common/__init__.py
diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/common/bColors.py b/catalog-be/src/main/resources/scripts/sdcBePy/common/bColors.py
new file mode 100755
index 0000000000..a14458bbec
--- /dev/null
+++ b/catalog-be/src/main/resources/scripts/sdcBePy/common/bColors.py
@@ -0,0 +1,8 @@
+
+class BColors:
+
+ HEADER = '\033[95m'
+ WARNING = '\033[93m'
+ FAIL = '\033[91m'
+ END_C = '\033[0m'
+ BOLD = '\033[1m'
diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/common/helpers.py b/catalog-be/src/main/resources/scripts/sdcBePy/common/helpers.py
new file mode 100644
index 0000000000..0e6db5efbe
--- /dev/null
+++ b/catalog-be/src/main/resources/scripts/sdcBePy/common/helpers.py
@@ -0,0 +1,3 @@
+
+def check_arguments_not_none(*args):
+ return None not in args
diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/common/logger.py b/catalog-be/src/main/resources/scripts/sdcBePy/common/logger.py
new file mode 100644
index 0000000000..e2e434fcff
--- /dev/null
+++ b/catalog-be/src/main/resources/scripts/sdcBePy/common/logger.py
@@ -0,0 +1,83 @@
+import sys
+
+debugFlag = True
+
+
+def join_strings(lst):
+ return ''.join([str(string) for string in lst])
+
+
+def debug(desc, *args):
+ if debugFlag:
+ print(desc, join_strings(args))
+
+
+def log(desc, arg):
+ print(desc, arg)
+
+
+def error_and_exit(error_code, error_desc):
+ if error_code > 0:
+ print("status={0}. {1}".format(error_code, '' if not error_desc else error_desc))
+ else:
+ print("status={0}".format(error_code))
+ sys.exit(error_code)
+
+
+def print_name_and_return_code(name, code, with_line=True):
+ if _strings_correct(name, code):
+ if with_line:
+ print("----------------------------------------")
+ print("{0:30} | {1:6}".format(name, code))
+ if with_line:
+ print("----------------------------------------")
+ else:
+ print("name of the item or return code from request is none -> error occurred!!")
+
+
+def _strings_correct(*strings):
+ results = [(string is not None and string != "") for string in strings]
+ return all(results) is True
+
+# def parse_cmd_line_params(argv):
+# print('Number of arguments:', len(sys.argv), 'arguments.')
+#
+# opts = []
+#
+# be_host = 'localhost'
+# be_port = '8080'
+# admin_user = 'jh0003'
+# scheme = 'http'
+#
+# try:
+# opts, args = getopt.getopt(argv, "i:p:u:h:s:", ["ip=", "port=", "user=", "scheme="])
+# except getopt.GetoptError:
+# usage()
+# error_and_exit(2, 'Invalid input')
+#
+# for opt, arg in opts:
+# # print opt, arg
+# if opt == '-h':
+# usage()
+# sys.exit(3)
+# elif opt in ("-i", "--ip"):
+# be_host = arg
+# elif opt in ("-p", "--port"):
+# be_port = arg
+# elif opt in ("-u", "--user"):
+# admin_user = arg
+# elif opt in ("-s", "--scheme"):
+# scheme = arg
+#
+# print('scheme =', scheme, ', be host =', be_host, ', be port =', be_port, ', user =', admin_user)
+#
+# if be_host is None:
+# usage()
+# sys.exit(3)
+# return scheme, be_host, be_port, admin_user
+#
+#
+# def usage():
+# 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> ] ')
diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/__init__.py b/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/__init__.py
diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/main.py b/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/main.py
new file mode 100644
index 0000000000..a30d46d4d5
--- /dev/null
+++ b/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/main.py
@@ -0,0 +1,16 @@
+from sdcBePy.common.normative.toscaElements import process_and_create_normative_element
+from sdcBePy.common.normative.toscaTypes import process_and_create_normative_types
+
+
+def process_element_list(normative_elements_list, sdc_be_proxy):
+ for normative_element in normative_elements_list:
+ process_and_create_normative_element(normative_element,
+ sdc_be_proxy=sdc_be_proxy)
+
+
+def process_type_list(normative_type_list, sdc_be_proxy, update_version):
+ for normative_type in normative_type_list:
+ process_and_create_normative_types(normative_type,
+ sdc_be_proxy=sdc_be_proxy,
+ update_version=update_version)
+
diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaElements.py b/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaElements.py
new file mode 100644
index 0000000000..1d4e734351
--- /dev/null
+++ b/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaElements.py
@@ -0,0 +1,107 @@
+import json
+import zipfile
+
+import pycurl
+
+from sdcBePy.common.logger import debug, log, print_name_and_return_code, error_and_exit
+from sdcBePy.common.sdcBeProxy import SdcBeProxy
+
+
+def process_and_create_normative_element(normative_element,
+ scheme=None, be_host=None, be_port=None, admin_user=None, sdc_be_proxy=None,
+ debug=False,
+ exit_on_success=False):
+ if sdc_be_proxy is None:
+ sdc_be_proxy = SdcBeProxy(be_host, be_port, scheme, admin_user, debug=debug)
+
+ file_dir, url_suffix, element_name, element_from_name, with_metadata = normative_element.get_parameters()
+ _create_normative_element(sdc_be_proxy,
+ file_dir,
+ url_suffix,
+ element_name,
+ element_from_name,
+ with_metadata,
+ exit_on_success)
+
+
+def _create_normative_element(sdc_be_proxy, file_dir,
+ url_suffix, element_name, element_form_name, with_metadata=False,
+ exit_on_success=False):
+ result = _send_request(sdc_be_proxy,
+ file_dir,
+ url_suffix,
+ element_name,
+ element_form_name,
+ with_metadata)
+ print_and_check_result(result, exit_on_success)
+
+
+def _send_request(sdc_be_proxy, file_dir, url_suffix, element_name,
+ element_form_name,
+ with_metadata=False):
+ try:
+ log("create normative element ", element_name)
+
+ type_file_name = file_dir + element_name
+ multi_part_form_data = _create_multipart_form_data(element_form_name, type_file_name, with_metadata,
+ element_name)
+
+ http_res = sdc_be_proxy.post_file(url_suffix, multi_part_form_data)
+ if http_res is not None:
+ debug("http response =", http_res)
+ debug("response buffer", str(sdc_be_proxy.con.buffer.getvalue(), "UTF-8"))
+ # c.close()
+ return element_name, http_res, sdc_be_proxy.con.buffer.getvalue()
+
+ except Exception as inst:
+ print("ERROR=" + str(inst))
+ return element_name, None, None
+
+
+def _create_multipart_form_data(element_form_name, type_file_name, with_metadata, element_name):
+ tosca_type_zip_part = _create_zip_file_multi_part(element_form_name, type_file_name, element_name)
+ multi_part_form_data = [tosca_type_zip_part]
+ if with_metadata:
+ metadata_type_part = _create_metadata_multipart(type_file_name)
+ multi_part_form_data.append(metadata_type_part)
+ debug(multi_part_form_data)
+ return multi_part_form_data
+
+
+def _create_metadata_multipart(type_file_name):
+ metadata = _create_json_metadata_str(type_file_name)
+ return "toscaTypeMetadata", metadata
+
+
+def _create_zip_file_multi_part(element_form_name, type_file_name, element_name):
+ zf = zipfile.ZipFile(type_file_name + ".zip", "w")
+ zf.write(type_file_name + '.yml', element_name + '.yml')
+ zf.close()
+
+ tosca_type_zip_path = type_file_name + ".zip"
+ tosca_type_zip_part = (element_form_name, (pycurl.FORM_FILE, tosca_type_zip_path))
+ return tosca_type_zip_part
+
+
+def _create_json_metadata_str(file_name):
+ type_metadata_json_file = file_name + ".json"
+ debug(type_metadata_json_file)
+ json_file = open(type_metadata_json_file)
+
+ debug("before load json")
+ json_data = json.load(json_file, strict=False)
+ debug(json_data)
+
+ return json.dumps(json_data)
+
+
+def print_and_check_result(result, exit_on_success):
+ if result is not None:
+ print_name_and_return_code(result[0], result[1])
+ if result[1] is None or result[1] not in [200, 201, 409]:
+ error_and_exit(1, "Failed to create the normatives elements!!")
+ else:
+ if exit_on_success is True:
+ error_and_exit(0, "All normatives elements created successfully!!")
+ else:
+ error_and_exit(1, "Results is None!!")
diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaTypes.py b/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaTypes.py
new file mode 100644
index 0000000000..48b20ccb1e
--- /dev/null
+++ b/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaTypes.py
@@ -0,0 +1,121 @@
+import json
+import zipfile
+
+import pycurl
+
+from sdcBePy.common.logger import print_name_and_return_code, error_and_exit, log, debug
+from sdcBePy.common.sdcBeProxy import SdcBeProxy
+
+
+def process_and_create_normative_types(normative_type,
+ scheme=None, be_host=None, be_port=None, admin_user=None,
+ sdc_be_proxy=None,
+ update_version=False,
+ debug=False,
+ exit_on_success=False):
+ if sdc_be_proxy is None:
+ sdc_be_proxy = SdcBeProxy(be_host, be_port, scheme, admin_user, debug=debug)
+
+ file_dir, normative_type_list = normative_type.get_parameters()
+
+ results = _create_normatives_type(file_dir, sdc_be_proxy, normative_type_list, update_version)
+ print_and_check_results(results, update_version, exit_on_success)
+
+
+def print_and_check_results(results, update_version, exit_on_success=False):
+ if results is not None:
+ if len(results) == 0:
+ return
+ print("----------------------------------------")
+ for result in results:
+ print_name_and_return_code(result[0], result[1], with_line=False)
+ print("----------------------------------------")
+ check_results_and_exit(results, update_version, exit_on_success)
+ else:
+ error_and_exit(1, "results is none -> error occurred!!")
+
+
+def check_results_and_exit(results, update_version, exit_on_success):
+ if not _results_ok(results, _get_response_code(update_version)):
+ error_and_exit(1, "Failed to create the normatives types !!")
+ else:
+ if exit_on_success:
+ error_and_exit(0, "All normatives types created successfully!!")
+
+
+def _create_normatives_type(file_dir, sdc_be_proxy, types, update_version):
+ results = []
+ response_codes = _get_response_code(update_version)
+ for normativeType in types:
+ result = _send_request(sdc_be_proxy, file_dir, normativeType, update_version)
+ results.append(result)
+ if result[1] is None or result[1] not in response_codes:
+ print("Failed creating normative type " + normativeType + ". " + str(result[1]))
+ return results
+
+
+def _send_request(sdc_be_proxy, file_dir, element_name, update_version):
+ try:
+ log("create normative type ", element_name)
+ debug("userId", sdc_be_proxy.con.user_header)
+ debug("fileDir", file_dir)
+
+ url = '/sdc2/rest/v1/catalog/upload/multipart'
+ if update_version is not None:
+ url += '?createNewVersion=' + _boolean_to_string(update_version)
+
+ send = _create_send_body(file_dir, element_name)
+
+ debug(send)
+ httpRes = sdc_be_proxy.post_file(url, send)
+ if httpRes is not None:
+ debug("http response=", httpRes)
+ debug(sdc_be_proxy.con.buffer.getvalue())
+
+ return element_name, httpRes, sdc_be_proxy.con.buffer.getvalue()
+
+ except Exception as inst:
+ print("ERROR=" + str(inst))
+ return element_name, None, None
+
+
+def _create_send_body(file_dir, element_name):
+ yml_path = file_dir + element_name + "/" + element_name + ".yml"
+ path = file_dir + element_name + "/" + element_name + ".zip"
+
+ zf = zipfile.ZipFile(path, "w")
+ zf.write(yml_path, element_name + '.yml')
+ zf.close()
+
+ debug(path)
+ current_json_file = file_dir + element_name + "/" + element_name + ".json"
+
+ jsonFile = open(current_json_file)
+
+ debug("before load json")
+ json_data = json.load(jsonFile, strict=False)
+ debug(json_data)
+
+ jsonAsStr = json.dumps(json_data)
+
+ return [('resourceMetadata', jsonAsStr), ('resourceZip', (pycurl.FORM_FILE, path))]
+
+
+def _results_ok(results, response_codes):
+ for result in results:
+ if result[1] not in response_codes:
+ return False
+
+ return True
+
+
+def _get_response_code(update_version):
+ responseCodes = [200, 201]
+ if update_version is False:
+ responseCodes.append(409)
+
+ return responseCodes
+
+
+def _boolean_to_string(boolean_value):
+ return "true" if boolean_value else "false"
diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/common/sdcBeProxy.py b/catalog-be/src/main/resources/scripts/sdcBePy/common/sdcBeProxy.py
new file mode 100755
index 0000000000..48261704f7
--- /dev/null
+++ b/catalog-be/src/main/resources/scripts/sdcBePy/common/sdcBeProxy.py
@@ -0,0 +1,117 @@
+import json
+from io import BytesIO
+
+import pycurl
+
+from sdcBePy.common.helpers import check_arguments_not_none
+
+
+def get_url(ip, port, protocol):
+ return "%s://%s:%s" % (protocol, ip, port)
+
+
+class SdcBeProxy:
+
+ def __init__(self, be_ip, be_port, scheme, user_id="jh0003",
+ debug=False, connector=None):
+ if not check_arguments_not_none(be_ip, be_port, scheme, user_id):
+ raise AttributeError("The be_host, be_port, scheme or admin_user are missing")
+ url = get_url(be_ip, be_port, scheme)
+ self.con = connector if connector \
+ else CurlConnector(url, user_id, scheme=scheme, debug=debug)
+
+ def check_backend(self):
+ return self.con.get('/sdc2/rest/v1/user/jh0003')
+
+ def check_user(self, user_name):
+ return self.con.get("/sdc2/rest/v1/user/" + user_name)
+
+ def create_user(self, first_name, last_name, user_id, email, role):
+ return self.con.post('/sdc2/rest/v1/user', json.dumps({
+ 'firstName': first_name,
+ 'lastName': last_name,
+ 'userId': user_id,
+ 'email': email,
+ 'role': role
+ }))
+
+ def post_file(self, path, multi_part_form_data):
+ return self.con.post_file(path, multi_part_form_data)
+
+
+class CurlConnector:
+ CONTENT_TYPE_HEADER = "Content-Type: application/json"
+ ACCEPT_HEADER = "Accept: application/json; charset=UTF-8"
+
+ def __init__(self, url, user_id_header, buffer=None, scheme="http", debug=False):
+ self.c = pycurl.Curl()
+ self.c.setopt(pycurl.HEADER, True)
+
+ self.user_header = "USER_ID: " + user_id_header
+
+ if not debug:
+ # disable printing not necessary logs in the terminal
+ self.c.setopt(pycurl.WRITEFUNCTION, lambda x: None)
+ else:
+ self.c.setopt(pycurl.VERBOSE, 1)
+
+ if not buffer:
+ self.buffer = BytesIO()
+
+ self.url = url
+ self._check_schema(scheme)
+
+ def get(self, path):
+ try:
+ self.c.setopt(pycurl.URL, self.url + path)
+ self.c.setopt(pycurl.HTTPHEADER, [self.user_header,
+ CurlConnector.CONTENT_TYPE_HEADER,
+ CurlConnector.ACCEPT_HEADER])
+
+ self.c.perform()
+ return self.c.getinfo(pycurl.RESPONSE_CODE)
+ except pycurl.error:
+ return 111
+
+ def post(self, path, data):
+ try:
+ self.c.setopt(pycurl.URL, self.url + path)
+ self.c.setopt(pycurl.POST, 1)
+ self.c.setopt(pycurl.HTTPHEADER, [self.user_header,
+ CurlConnector.CONTENT_TYPE_HEADER,
+ CurlConnector.ACCEPT_HEADER])
+
+ self.c.setopt(pycurl.POSTFIELDS, data)
+
+ self.c.perform()
+ self.c.setopt(pycurl.POST, 0)
+
+ return self.c.getinfo(pycurl.RESPONSE_CODE)
+ except pycurl.error:
+ return 111
+
+ def post_file(self, path, post_body, buffer=None):
+ try:
+ self.c.setopt(pycurl.URL, self.url + path)
+ self.c.setopt(pycurl.POST, 1)
+ self.c.setopt(pycurl.HTTPHEADER, [self.user_header])
+
+ self.c.setopt(pycurl.HTTPPOST, post_body)
+
+ write = self.buffer.write if not buffer else buffer.write
+ self.c.setopt(pycurl.WRITEFUNCTION, write)
+
+ self.c.perform()
+ self.c.setopt(pycurl.POST, 0)
+
+ return self.c.getinfo(pycurl.RESPONSE_CODE)
+ except pycurl.error:
+ return 111
+
+ def _check_schema(self, scheme):
+ if scheme == 'https':
+ self.c.setopt(pycurl.SSL_VERIFYPEER, 0)
+ self.c.setopt(pycurl.SSL_VERIFYHOST, 0)
+
+ def __del__(self):
+ self.c.close()