From cb30f3a566b73c78c3ea666acfcd3f288098684a Mon Sep 17 00:00:00 2001 From: "k.kedron" Date: Thu, 5 Mar 2020 13:12:27 +0100 Subject: 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 Change-Id: I02169eb7d0e3e90851ba1811536d1712c3b4145f --- .../src/main/resources/scripts/sdcBePy/__init__.py | 0 .../resources/scripts/sdcBePy/common/__init__.py | 0 .../resources/scripts/sdcBePy/common/bColors.py | 8 + .../resources/scripts/sdcBePy/common/helpers.py | 3 + .../resources/scripts/sdcBePy/common/logger.py | 83 +++++++++++ .../scripts/sdcBePy/common/normative/__init__.py | 0 .../scripts/sdcBePy/common/normative/main.py | 16 ++ .../sdcBePy/common/normative/toscaElements.py | 107 ++++++++++++++ .../scripts/sdcBePy/common/normative/toscaTypes.py | 121 ++++++++++++++++ .../resources/scripts/sdcBePy/common/sdcBeProxy.py | 117 +++++++++++++++ .../resources/scripts/sdcBePy/tosca/__init__.py | 0 .../scripts/sdcBePy/tosca/data/beConfig.json | 5 + .../sdcBePy/tosca/data/onapTypesToUpgrade.json | 9 ++ .../scripts/sdcBePy/tosca/data/typesToUpgrade.json | 4 + .../scripts/sdcBePy/tosca/imports/__init__.py | 0 .../resources/scripts/sdcBePy/tosca/imports/run.py | 42 ++++++ .../sdcBePy/tosca/imports/runGenericNormative.py | 37 +++++ .../sdcBePy/tosca/imports/runNormativeElement.py | 71 +++++++++ .../sdcBePy/tosca/imports/runNormativeType.py | 56 +++++++ .../main/resources/scripts/sdcBePy/tosca/main.py | 56 +++++++ .../scripts/sdcBePy/tosca/models/__init__.py | 0 .../tosca/models/normativeElementCandidate.py | 18 +++ .../sdcBePy/tosca/models/normativeElementsList.py | 76 ++++++++++ .../sdcBePy/tosca/models/normativeToUpdateList.py | 60 ++++++++ .../sdcBePy/tosca/models/normativeTypeCandidate.py | 14 ++ .../sdcBePy/tosca/models/normativeTypesList.py | 161 +++++++++++++++++++++ .../scripts/sdcBePy/tosca/upgrade/__init__.py | 0 .../resources/scripts/sdcBePy/tosca/upgrade/run.py | 59 ++++++++ .../sdcBePy/tosca/upgrade/runUpgradeNormative.py | 59 ++++++++ .../resources/scripts/sdcBePy/users/__init__.py | 0 .../scripts/sdcBePy/users/data/users.json | 44 ++++++ .../main/resources/scripts/sdcBePy/users/run.py | 64 ++++++++ 32 files changed, 1290 insertions(+) create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/__init__.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/common/__init__.py create mode 100755 catalog-be/src/main/resources/scripts/sdcBePy/common/bColors.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/common/helpers.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/common/logger.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/common/normative/__init__.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/common/normative/main.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaElements.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaTypes.py create mode 100755 catalog-be/src/main/resources/scripts/sdcBePy/common/sdcBeProxy.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/__init__.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/data/beConfig.json create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/data/onapTypesToUpgrade.json create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/data/typesToUpgrade.json create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/__init__.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/run.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/runGenericNormative.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/runNormativeElement.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/runNormativeType.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/main.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/__init__.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeElementCandidate.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeElementsList.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeToUpdateList.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeTypeCandidate.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeTypesList.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/upgrade/__init__.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/upgrade/run.py create mode 100644 catalog-be/src/main/resources/scripts/sdcBePy/tosca/upgrade/runUpgradeNormative.py create mode 100755 catalog-be/src/main/resources/scripts/sdcBePy/users/__init__.py create mode 100755 catalog-be/src/main/resources/scripts/sdcBePy/users/data/users.json create mode 100755 catalog-be/src/main/resources/scripts/sdcBePy/users/run.py (limited to 'catalog-be/src/main/resources/scripts/sdcBePy') diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/__init__.py b/catalog-be/src/main/resources/scripts/sdcBePy/__init__.py new file mode 100644 index 0000000000..e69de29bb2 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 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=, default http ] ' +# '[-i | --ip=] [-p | ' +# '--port= ] [-u | --user= ] ') 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 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() diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/__init__.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/data/beConfig.json b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/data/beConfig.json new file mode 100644 index 0000000000..f8c740b94f --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/data/beConfig.json @@ -0,0 +1,5 @@ +{ + "beHost": "localhost", + "bePort": "8080", + "adminUser": "jh0003" +} \ No newline at end of file diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/data/onapTypesToUpgrade.json b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/data/onapTypesToUpgrade.json new file mode 100644 index 0000000000..cbe581acd2 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/data/onapTypesToUpgrade.json @@ -0,0 +1,9 @@ +{ + "nfv": [ + "allottedResource" + ], + "onap": [ + ], + "sol":[ + ] +} \ No newline at end of file diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/data/typesToUpgrade.json b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/data/typesToUpgrade.json new file mode 100644 index 0000000000..5dd20011c0 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/data/typesToUpgrade.json @@ -0,0 +1,4 @@ +{ + "heat": [], + "normative": [] +} \ No newline at end of file diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/__init__.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/run.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/run.py new file mode 100644 index 0000000000..4d4eeaff6e --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/run.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +import os +import sys + +import sdcBePy.common.logger as logger +from sdcBePy.common.normative.main import process_element_list, process_type_list +from sdcBePy.common.sdcBeProxy import SdcBeProxy +from sdcBePy.tosca.main import get_args, usage +from sdcBePy.tosca.models.normativeElementsList import get_normative_element_candidate_list, \ + get_normative_element_with_metadata_list +from sdcBePy.tosca.models.normativeTypesList import get_normative_type_candidate_list + + +def main(): + scheme, be_host, be_port, admin_user, update_version, debug = get_args() + + if debug is False: + print('Disabling debug mode') + logger.debugFlag = debug + + try: + sdc_be_proxy = SdcBeProxy(be_host, be_port, scheme, admin_user, debug) + except AttributeError: + usage() + sys.exit(3) + + # use to run script form this dir (not like the command) + # base_file_location = os.getcwd() + "/../../../../import/tosca/" + base_file_location = os.getcwd() + os.path.sep + logger.debug("working directory =" + base_file_location) + + process_element_list(get_normative_element_candidate_list(base_file_location), sdc_be_proxy) + process_type_list(get_normative_type_candidate_list(base_file_location), sdc_be_proxy, update_version) + process_element_list(get_normative_element_with_metadata_list(base_file_location), sdc_be_proxy) + + logger.log("Script end ->", "All normatives imported successfully!") + logger.error_and_exit(0, None) + + +if __name__ == "__main__": + main() diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/runGenericNormative.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/runGenericNormative.py new file mode 100644 index 0000000000..424c0ca7c5 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/runGenericNormative.py @@ -0,0 +1,37 @@ +import os +import sys +from argparse import ArgumentParser + +from sdcBePy.common.normative.toscaTypes import process_and_create_normative_types +from sdcBePy.tosca.main import usage, get_args +from sdcBePy.tosca.models.normativeTypeCandidate import NormativeTypeCandidate + + +def get_normative_prams(): + parser = ArgumentParser() + + path = os.path.dirname(__file__) + parser.add_argument('--location', default=path + os.path.sep) + parser.add_argument('--element', "-e", required=True) + + args = parser.parse_args() + + return args.location, [args.element] + + +def main(): + scheme, be_host, be_port, admin_user, _, debug = get_args() + + candidate = NormativeTypeCandidate(*get_normative_prams()) + try: + process_and_create_normative_types(candidate, + scheme, be_host, be_port, admin_user, + debug=debug, + exit_on_success=True) + except AttributeError: + usage() + sys.exit(3) + + +if __name__ == '__main__': + main() diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/runNormativeElement.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/runNormativeElement.py new file mode 100644 index 0000000000..1bdb1504b2 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/runNormativeElement.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 + +import sys + +from sdcBePy.common.normative.toscaElements import process_and_create_normative_element +from sdcBePy.tosca.main import get_args, usage +from sdcBePy.tosca.models.normativeElementsList import get_capability, get_data, get_relationship, \ + get_interface_lifecycle, get_categories, get_group, get_policy, get_annotation + + +def run(candidate): + scheme, be_host, be_port, admin_user, _, debug = get_args() + try: + process_and_create_normative_element(candidate, + scheme, be_host, be_port, admin_user, + debug=debug, + exit_on_success=True) + except AttributeError: + usage() + sys.exit(3) + + +def run_import_data(): + data_candidate = get_data() + run(data_candidate) + + +def run_import_capabilities(): + capability_candidate = get_capability() + run(capability_candidate) + + +def run_import_relationship(): + relationship_candidate = get_relationship() + run(relationship_candidate) + + +def run_import_interface_lifecycle(): + interface_lifecycle_candidate = get_interface_lifecycle() + run(interface_lifecycle_candidate) + + +def run_import_categories(): + categories_candidate = get_categories() + run(categories_candidate) + + +def run_import_group(): + group_candidate = get_group() + run(group_candidate) + + +def run_import_policy(): + policy_candidate = get_policy() + run(policy_candidate) + + +def run_import_annotation(): + annotation_candidate = get_annotation() + run(annotation_candidate) + + +if __name__ == '__main__': + run_import_data() + # run_import_capabilities() + # run_import_relationship() + # run_import_interface_lifecycle() + # run_import_categories() + # run_import_group() + # run_import_policy() + # run_import_annotation() diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/runNormativeType.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/runNormativeType.py new file mode 100644 index 0000000000..ee4e05b21a --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/imports/runNormativeType.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +import sys + +from sdcBePy.common.normative.toscaTypes import process_and_create_normative_types +from sdcBePy.tosca.main import get_args, usage +from sdcBePy.tosca.models.normativeTypesList import get_normative, get_heat, get_nfv, get_onap, get_sol + + +def run(candidate, exit_on_success=True): + scheme, be_host, be_port, admin_user, update_version, debug = get_args() + try: + process_and_create_normative_types(candidate, + scheme, + be_host, + be_port, + admin_user, + update_version=update_version, + debug=debug, + exit_on_success=exit_on_success) + except AttributeError: + usage() + sys.exit(3) + + +def run_import_normative(): + normative_candidate = get_normative() + run(normative_candidate) + + +def run_import_heat(): + heat_candidate = get_heat() + run(heat_candidate) + + +def run_import_nfv(): + nfv_candidate = get_nfv() + run(nfv_candidate) + + +def run_import_onap(): + onap_candidate = get_onap() + run(onap_candidate) + + +def run_import_sol(): + sol_candidate = get_sol() + run(sol_candidate) + + +if __name__ == '__main__': + run_import_normative() + # run_import_heat() + # run_import_nfv() + # run_import_onap() + # run_import_sol() diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/main.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/main.py new file mode 100644 index 0000000000..2535ba6d77 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/main.py @@ -0,0 +1,56 @@ +import json +import os +import sys +from argparse import ArgumentParser + + +def usage(): + print(sys.argv[0], + '[-s | --scheme= ] [-i | --ip=] [-p | ' + '--port= ] [-u | --user= ] [-d | ' + '--debug=] [-v | --updateVersion=]') + + +def load_be_config(conf_path): + with open(conf_path, 'r') as f: + return json.load(f) + + +def parse_param(): + parser = ArgumentParser() + + path = os.path.dirname(__file__) + parser.add_argument('--conf', default=os.path.join(path, 'data', 'beConfig.json')) + + parser.add_argument('--ip', "-i") + parser.add_argument('--port', "-p") + parser.add_argument('--adminUser', "-a") + parser.add_argument('--https', action='store_true') + parser.add_argument('--updateVersion', action='store_false') + parser.add_argument('--debug', action='store_true') + + args, _ = parser.parse_known_args() + + return [args.conf, 'https' if args.https else 'http', + args.ip, args.port, args.adminUser, args.updateVersion, + args.debug] + + +def get_args(): + print('Number of arguments:', len(sys.argv), 'arguments.') + + conf_path, scheme, be_host, be_port, admin_user, update_version, debug = parse_param() + defaults = load_be_config(conf_path) + + # Use defaults if param not provided by the user + if be_host is None: + be_host = defaults["beHost"] + if be_port is None: + be_port = defaults["bePort"] + if admin_user is None: + admin_user = defaults["adminUser"] + + print('scheme =', scheme, ',be host =', be_host, ', be port =', be_port, ', user =', admin_user, + ', debug =', debug, ', update_version =', update_version) + + return scheme, be_host, be_port, admin_user, update_version, debug diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/__init__.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeElementCandidate.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeElementCandidate.py new file mode 100644 index 0000000000..96526ce82a --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeElementCandidate.py @@ -0,0 +1,18 @@ +from sdcBePy.common.helpers import check_arguments_not_none + + +class NormativeElementCandidate: + + def __init__(self, file_dir, url_suffix, + element_name, element_from_name, with_metadata=False): + if not check_arguments_not_none(file_dir, url_suffix, element_name, element_from_name): + raise AttributeError("The file_dir, url_suffix, element_name, element_from_name are missing") + + self.file_dir = file_dir + self.url_suffix = url_suffix + self.element_name = element_name + self.element_form_name = element_from_name + self.with_metadata = with_metadata + + def get_parameters(self): + return self.file_dir, self.url_suffix, self.element_name, self.element_form_name, self.with_metadata diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeElementsList.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeElementsList.py new file mode 100644 index 0000000000..cfef95d427 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeElementsList.py @@ -0,0 +1,76 @@ +from sdcBePy.tosca.models.normativeElementCandidate import NormativeElementCandidate + + +def get_normative_element_candidate_list(base_file_location): + return [ + get_data(base_file_location), + get_capability(base_file_location), + get_relationship(base_file_location), + get_interface_lifecycle(base_file_location), + get_categories(base_file_location) + ] + + +def get_normative_element_with_metadata_list(base_file_location): + return [ + get_group(base_file_location), + get_policy(base_file_location) + ] + + +def get_data(base_file_location="/"): + return NormativeElementCandidate(base_file_location + "data-types/", + "/sdc2/rest/v1/catalog/uploadType/datatypes", + "dataTypes", + "dataTypesZip") + + +def get_capability(base_file_location="/"): + return NormativeElementCandidate(base_file_location + "capability-types/", + "/sdc2/rest/v1/catalog/uploadType/capability", + "capabilityTypes", + "capabilityTypeZip") + + +def get_relationship(base_file_location="/"): + return NormativeElementCandidate(base_file_location + "relationship-types/", + "/sdc2/rest/v1/catalog/uploadType/relationship", + "relationshipTypes", + "relationshipTypeZip") + + +def get_interface_lifecycle(base_file_location="../../../import/tosca/"): + return NormativeElementCandidate(base_file_location + "interface-lifecycle-types/", + "/sdc2/rest/v1/catalog/uploadType/interfaceLifecycle", + "interfaceLifecycleTypes", + "interfaceLifecycleTypeZip") + + +def get_categories(base_file_location="/"): + return NormativeElementCandidate(base_file_location + "categories/", + "/sdc2/rest/v1/catalog/uploadType/categories", + "categoryTypes", + "categoriesZip") + + +def get_group(base_file_location="/"): + return NormativeElementCandidate(base_file_location + "group-types/", + "/sdc2/rest/v1/catalog/uploadType/grouptypes", + "groupTypes", + "groupTypesZip", + with_metadata=True) + + +def get_policy(base_file_location="/"): + return NormativeElementCandidate(base_file_location + "policy-types/", + "/sdc2/rest/v1/catalog/uploadType/policytypes", + "policyTypes", + "policyTypesZip", + with_metadata=True) + + +def get_annotation(base_file_location="/"): + return NormativeElementCandidate(base_file_location + "annotation-types", + "/sdc2/rest/v1/catalog/uploadType/annotationtypes", + "annotationTypesZip", + "annotationTypes") diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeToUpdateList.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeToUpdateList.py new file mode 100644 index 0000000000..a2830185d5 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeToUpdateList.py @@ -0,0 +1,60 @@ +import json + +from sdcBePy.tosca.models.normativeTypeCandidate import NormativeTypeCandidate + + +class TypesToUpdate: + + def __init__(self, files): + self.types_list = {} + self.load_files(files) + + def load_files(self, files): + for file in files: + with open(file, 'r') as stream: + _types = json.load(stream) + for type_key, type_value in _types.items(): + self.types_list[type_key] = type_value + + def get_type(self, key): + return self.types_list[key] + + +def get_heat_and_normative_to_update_list(types, base_file_location): + return [ + get_heat(types, base_file_location), + get_normative(types, base_file_location) + ] + + +def get_nfv_onap_sol_to_update_list(types, base_file_location): + return [ + get_nfv(types, base_file_location), + get_onap(types, base_file_location), + get_sol(types, base_file_location) + ] + + +def get_heat(types, base_location="/"): + return NormativeTypeCandidate(base_location + "heat-types/", + types.get_type("heat")) + + +def get_normative(types, base_location="/"): + return NormativeTypeCandidate(base_location + "normative-types/", + types.get_type("normative")) + + +def get_nfv(types, base_location="/"): + return NormativeTypeCandidate(base_location + "nfv-types/", + types.get_type("nfv")) + + +def get_onap(types, base_location="/"): + return NormativeTypeCandidate(base_location + "onap-types/", + types.get_type("onap")) + + +def get_sol(types, base_location="/"): + return NormativeTypeCandidate(base_location + "sol-types/", + types.get_type("sol")) diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeTypeCandidate.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeTypeCandidate.py new file mode 100644 index 0000000000..929ec4f864 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeTypeCandidate.py @@ -0,0 +1,14 @@ +from sdcBePy.common.helpers import check_arguments_not_none + + +class NormativeTypeCandidate: + + def __init__(self, file_dir, normative_types_list): + if not check_arguments_not_none(file_dir, normative_types_list): + raise AttributeError("The file_dir, normative_types_list are missing") + + self.file_dir = file_dir + self.normative_types_list = normative_types_list + + def get_parameters(self): + return self.file_dir, self.normative_types_list diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeTypesList.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeTypesList.py new file mode 100644 index 0000000000..eea25436b3 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/models/normativeTypesList.py @@ -0,0 +1,161 @@ +from sdcBePy.tosca.models.normativeTypeCandidate import NormativeTypeCandidate + + +def get_normative_type_candidate_list(base_file_location): + return [ + get_normative(base_file_location), + get_heat(base_file_location), + get_nfv(base_file_location), + get_onap(base_file_location), + get_sol(base_file_location) + ] + + +def get_normative(base_file_location="/"): + return NormativeTypeCandidate(base_file_location + "normative-types/", + ["root", + "compute", + "softwareComponent", + "webServer", + "webApplication", + "DBMS", + "database", + "objectStorage", + "blockStorage", + "containerRuntime", + "containerApplication", + "loadBalancer", + "port", "network"]) + + +def get_heat(base_file_location="/"): + return NormativeTypeCandidate(base_file_location + "heat-types/", + ["globalNetwork", + "globalPort", + "globalCompute", + "volume", + "cinderVolume", + "contrailVirtualNetwork", + "neutronNet", + "neutronPort", + "novaServer", + "extVl", + "internalVl", + "extCp", + "vl", + "eline", + "abstractSubstitute", + "Generic_VFC", + "Generic_VF", + "Generic_CR", + "Generic_PNF", + "Generic_Service", + "contrailNetworkRules", + "contrailPort", + "portMirroring", + "serviceProxy", + "contrailV2NetworkRules", + "contrailV2VirtualNetwork", + "securityRules", + "contrailAbstractSubstitute", + "contrailCompute", + "contrailV2VirtualMachineInterface", + "subInterface", + "contrailV2VLANSubInterface", + "multiFlavorVFC", + "vnfConfiguration", + "extCp2", + "extNeutronCP", + "extContrailCP", + "portMirroringByPolicy", + "forwardingPath", + "configuration", + "VRFObject", + "extVirtualMachineInterfaceCP", + "VLANNetworkReceptor", + "VRFEntry", + "subInterfaceV2", + "contrailV2VLANSubInterfaceV2", + "fabricConfiguration"]) + + +def get_nfv(base_file_location="/"): + return NormativeTypeCandidate(base_file_location + "nfv-types/", + ["underlayVpn", + "overlayTunnel", + "genericNeutronNet", + "allottedResource", + "extImageFile", + "extLocalStorage", + "extZteCP", + "extZteVDU", + "extZteVL", + "NSD", + "VDU", + "vduCompute", + "Cp", + "vduVirtualStorage", + "vduVirtualBlockStorage", + "vduVirtualFileStorage", + "vduVirtualObjectStorage", + "vduVirtualStorage", + "vnfVirtualLink", + "vnfExtCp", + "vduCp", + "VNF", + "PonUni", + "OltNni", + "OntNni"]) + + +def get_onap(base_file_location="/"): + return NormativeTypeCandidate(base_file_location + "onap-types/", + # Add desired type names to the list + []) + + +def get_sol(base_file_location="/"): + return NormativeTypeCandidate(base_file_location + "onap-types/", + # Add desired type names to the list + []) + + +def get_heat1707(base_file_location="/"): + return NormativeTypeCandidate(base_file_location + "heat-types/", + ["volume", + "cinderVolume", + "extVl", + "extCp", + "Generic_VFC", + "Generic_VF", + "Generic_PNF", + "Generic_Service", + "globalPort", + "globalNetwork", + "contrailV2VirtualMachineInterface", + "contrailV2VLANSubInterface", + "contrailPort", + "contrailV2VirtualNetwork", + "contrailVirtualNetwork", + "neutronNet", + "neutronPort", + "multiFlavorVFC", + "vnfConfiguration"]) + + +def get_heat1702_3537(base_file_location="/"): + return NormativeTypeCandidate(base_file_location + "heat-types/", + ["contrailPort", + "contrailV2VirtualMachineInterface", + "neutronPort", + "contrailCompute", + "novaServer", + "contrailV2VirtualNetwork", + "contrailVirtualNetwork", + "neutronNet"]) + + +def get_heat_version(base_file_location="/"): + return NormativeTypeCandidate(base_file_location + "heat_types/", + ["contrailV2VirtualMachineInterface", + "neutronPort"]) diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/upgrade/__init__.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/upgrade/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/upgrade/run.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/upgrade/run.py new file mode 100644 index 0000000000..6d90a1c085 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/upgrade/run.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 + +import os +import sys + +from sdcBePy.common import logger +from sdcBePy.common.logger import error_and_exit +from sdcBePy.common.normative.main import process_element_list, process_type_list +from sdcBePy.common.sdcBeProxy import SdcBeProxy +from sdcBePy.tosca.main import get_args, usage +from sdcBePy.tosca.models.normativeElementsList import get_normative_element_candidate_list, \ + get_normative_element_with_metadata_list +from sdcBePy.tosca.models.normativeToUpdateList import TypesToUpdate, get_heat_and_normative_to_update_list, \ + get_nfv_onap_sol_to_update_list + + +def main(): + scheme, be_host, be_port, admin_user, _, debug = get_args() + + update_version = True + update_onap_version = False + + if debug is False: + print('Disabling debug mode') + logger.debugFlag = debug + + try: + sdc_be_proxy = SdcBeProxy(be_host, be_port, scheme, admin_user, debug=debug) + except AttributeError: + usage() + sys.exit(3) + + # use to run script form this dir (not like the command) + # base_file_location = os.getcwd() + "/../../../../import/tosca/" + base_file_location = os.getcwd() + "/" + logger.debug("working directory =" + base_file_location) + process_element_list(get_normative_element_candidate_list(base_file_location), sdc_be_proxy) + process_element_list(get_normative_element_with_metadata_list(base_file_location), sdc_be_proxy) + + all_types = get_all_types() + + heat_and_normative_list = get_heat_and_normative_to_update_list(all_types, base_file_location) + process_type_list(heat_and_normative_list, sdc_be_proxy, update_version) + + nfv_onap_sol_list = get_nfv_onap_sol_to_update_list(all_types, base_file_location) + process_type_list(nfv_onap_sol_list, sdc_be_proxy, update_onap_version) + + logger.log("Updating end ->", "All normatives updated successfully!") + error_and_exit(0, None) + + +def get_all_types(): + path = os.path.dirname(__file__) + return TypesToUpdate([path + "/../data/typesToUpgrade.json", + path + "/../data/onapTypesToUpgrade.json"]) + + +if __name__ == "__main__": + main() diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/tosca/upgrade/runUpgradeNormative.py b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/upgrade/runUpgradeNormative.py new file mode 100644 index 0000000000..4b0d0cc8a2 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/tosca/upgrade/runUpgradeNormative.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 + +from sdcBePy.tosca.imports.runNormativeType import run +from sdcBePy.tosca.models.normativeToUpdateList import get_heat, get_normative, get_nfv, get_onap, get_sol +from sdcBePy.tosca.models.normativeTypesList import get_heat1707, get_heat1702_3537, get_heat_version +from sdcBePy.tosca.upgrade.run import get_all_types + +all_types = get_all_types() + + +def run_upgrade_heat(): + normative_candidate = get_heat(all_types) + run(normative_candidate) + + +def run_upgrade_normative(): + normative_candidate = get_normative(all_types) + run(normative_candidate) + + +def run_upgrade_nfv(): + normative_candidate = get_nfv(all_types) + run(normative_candidate) + + +def run_upgrade_onap(): + normative_candidate = get_onap(all_types) + run(normative_candidate) + + +def run_upgrade_sol(): + normative_candidate = get_sol(all_types) + run(normative_candidate) + + +def run_upgrade_heat1707(): + normative_candidate = get_heat1707() + run(normative_candidate) + + +def run_upgrade_heat1707_3537(): + normative_candidate = get_heat1702_3537() + run(normative_candidate) + + +def run_upgrade_heat_version(): + normative_candidate = get_heat_version() + run(normative_candidate) + + +if __name__ == '__main__': + run_upgrade_heat() + # run_upgrade_normative() + # run_upgrade_nfv() + # run_upgrade_onap() + # run_upgrade_sol() + # run_upgrade_heat1707() + # run_upgrade_heat1707_3537() + # run_upgrade_heat_version() diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/users/__init__.py b/catalog-be/src/main/resources/scripts/sdcBePy/users/__init__.py new file mode 100755 index 0000000000..e69de29bb2 diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/users/data/users.json b/catalog-be/src/main/resources/scripts/sdcBePy/users/data/users.json new file mode 100755 index 0000000000..9ce2be4dd3 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/users/data/users.json @@ -0,0 +1,44 @@ +[ + { + "userId": "demo", + "firstName": "demo", + "lastName": "demo", + "role": "ADMIN", + "email": "demo@openecomp.org" + }, + { + "userId": "op0001", + "firstName": "Oper", + "lastName": "P", + "role": "OPS", + "email": "op0001@openecomp.org" + }, + { + "userId": "gv0001", + "firstName": "Giuseppe", + "lastName": "Verdi", + "role": "GOVERNOR", + "email": "gv0001@openecomp.org" + }, + { + "userId": "jh0003", + "firstName": "Jimmy", + "lastName": "Hendrix", + "role": "Admin", + "email": "jh0003@openecomp.org" + }, + { + "userId": "jm0007", + "firstName": "Joni", + "lastName": "Mitchell", + "role": "TESTER", + "email": "jm0007@openecomp.org" + }, + { + "userId": "cs0008", + "firstName": "Carlos", + "lastName": "Santana", + "role": "DESIGNER", + "email": "cs0008r@openecomp.org" + } +] \ No newline at end of file diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/users/run.py b/catalog-be/src/main/resources/scripts/sdcBePy/users/run.py new file mode 100755 index 0000000000..b933afe7ca --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/users/run.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 + +import json +import os +import time +from argparse import ArgumentParser + +from sdcBePy.common.bColors import BColors +from sdcBePy.common.healthCheck import check_backend, RETRY_ATTEMPTS +from sdcBePy.common.sdcBeProxy import SdcBeProxy + +colors = BColors() + + +def load_users(conf_path): + with open(conf_path, 'r') as f: + return json.load(f) + + +def be_user_init(be_ip, be_port, protocol, conf_path): + sdc_be_proxy = SdcBeProxy(be_ip, be_port, protocol) + if check_backend(sdc_be_proxy, RETRY_ATTEMPTS): + users = load_users(conf_path) + for user in users: + if sdc_be_proxy.check_user(user['userId']) != 200: + result = sdc_be_proxy.create_user(user['firstName'], + user['lastName'], + user['userId'], + user['email'], + user['role']) + if result == 201: + print('[INFO]: ' + user['userId'] + + ' created, result: [' + str(result) + ']') + else: + print('[ERROR]: ' + colors.FAIL + user['userId'] + colors.END_C + + ' error creating , result: [' + str(result) + ']') + else: + print('[INFO]: ' + user['userId'] + ' already exists') + else: + print('[ERROR]: ' + time.strftime('%Y/%m/%d %H:%M:%S') + colors.FAIL + + 'Backend is DOWN :-(' + colors.END_C) + raise Exception("Cannot communicate with the backend!") + + +def get_args(): + parser = ArgumentParser() + + parser.add_argument('-i', '--ip', required=True) + parser.add_argument('-p', '--port', required=True) + parser.add_argument('--https', action='store_true') + path = os.path.dirname(__file__) + parser.add_argument('--conf', default=os.path.join(path, 'data', 'users.json')) + + args = parser.parse_args() + + return [args.ip, args.port, 'https' if args.https else 'http', args.conf] + + +def main(): + be_user_init(*get_args()) + + +if __name__ == "__main__": + main() -- cgit 1.2.3-korg