diff options
author | k.kedron <k.kedron@partner.samsung.com> | 2020-03-05 13:12:27 +0100 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2020-06-24 06:01:11 +0000 |
commit | cb30f3a566b73c78c3ea666acfcd3f288098684a (patch) | |
tree | 38cd1acb83ddde2b927485611d130bbeec3c1a4b /catalog-be/src/main/resources/scripts/sdcBePy/common/normative | |
parent | bdbfc2e460ccb561c3e174260b2908b974996d4f (diff) |
Refactoring the sdc-BE-init python scripts
Deeper refactoring of python script:
- create the python package with init script
- support for python 3.x
- reuse code
- new design
- support for .json conf file
- update the docker chef script
Issue-ID: SDC-2784
Signed-off-by: Krystian Kedron <k.kedron@partner.samsung.com>
Change-Id: I02169eb7d0e3e90851ba1811536d1712c3b4145f
Diffstat (limited to 'catalog-be/src/main/resources/scripts/sdcBePy/common/normative')
4 files changed, 244 insertions, 0 deletions
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" |