From 1e2a1c99b2e486032c0488a1b33eec16b5db2f7f Mon Sep 17 00:00:00 2001 From: jgstew Date: Wed, 3 May 2023 13:40:17 -0400 Subject: [PATCH] added import bes function. Moved validator function. --- .vscode/settings.json | 7 ++-- src/besapi/besapi.py | 74 +++++++++++++++++++++++++++++-------------- src/bescli/bescli.py | 9 ++++++ 3 files changed, 65 insertions(+), 25 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 6290b00..1c52634 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,10 +3,13 @@ "python.analysis.diagnosticSeverityOverrides": { "reportMissingImports": "information" }, - "python.formatting.provider": "black", + "python.formatting.provider": "none", "python.autoComplete.addBrackets": true, "editor.formatOnSave": true, "git.autofetch": true, "python.analysis.completeFunctionParens": true, - "python.linting.enabled": true + "python.linting.enabled": true, + "[python]": { + "editor.defaultFormatter": "ms-python.black-formatter" + } } diff --git a/src/besapi/besapi.py b/src/besapi/besapi.py index e8d8588..9204bef 100644 --- a/src/besapi/besapi.py +++ b/src/besapi/besapi.py @@ -150,6 +150,30 @@ def parse_bes_modtime(string_datetime): # ) +def validate_xsd(doc): + """validate results using XML XSDs""" + try: + xmldoc = etree.fromstring(doc) + except BaseException: + return False + + for xsd in ["BES.xsd", "BESAPI.xsd", "BESActionSettings.xsd"]: + xmlschema_doc = etree.parse(resource_filename(__name__, "schemas/%s" % xsd)) + + # one schema may throw an error while another will validate + try: + xmlschema = etree.XMLSchema(xmlschema_doc) + except etree.XMLSchemaParseError as err: + # this should only error if the XSD itself is malformed + besapi_logger.error("ERROR with `%s`: %s", xsd, err) + raise err + + if xmlschema.validate(xmldoc): + return True + + return False + + def get_bes_conn_using_config_file(conf_file=None): """ read connection values from config file @@ -313,12 +337,12 @@ def session_relevance_array(self, relevance, **kwargs): if "no such child: Answer" in str(err): try: result.append("ERROR: " + rel_result.besobj.Query.Error.text) - except AttributeError as err: - if "no such child: Error" in str(err): + except AttributeError as err2: + if "no such child: Error" in str(err2): result.append(" Nothing returned, but no error.") besapi_logger.info("Query did not return any results") else: - besapi_logger.error("%s\n%s", err, rel_result.text) + besapi_logger.error("%s\n%s", err2, rel_result.text) raise else: raise @@ -430,6 +454,29 @@ def set_current_site_path(self, site_path): self.site_path = site_path return self.site_path + def import_bes_to_site(self, bes_file_path, site_path=None): + """import bes file to site""" + + if not os.access(bes_file_path, os.R_OK): + besapi_logger.error(f"{bes_file_path} is not readable") + raise FileNotFoundError(f"{bes_file_path} is not readable") + + site_path = self.get_current_site_path(site_path) + + self.validate_site_path(site_path, True, True) + + with open(bes_file_path, "rb") as f: + content = f.read() + + # validate BES File contents: + if validate_xsd(content): + # TODO: validate write access to site? + # https://developer.bigfix.com/rest-api/api/import.html + result = self.post(f"import/{site_path}", content) + return result + else: + besapi_logger.error(f"{bes_file_path} is not valid") + def create_site_from_file(self, bes_file_path, site_type="custom"): """create new site""" xml_parsed = etree.parse(bes_file_path) @@ -908,26 +955,7 @@ def besjson(self): def validate_xsd(self, doc): """validate results using XML XSDs""" - try: - xmldoc = etree.fromstring(doc) - except BaseException: - return False - - for xsd in ["BES.xsd", "BESAPI.xsd", "BESActionSettings.xsd"]: - xmlschema_doc = etree.parse(resource_filename(__name__, "schemas/%s" % xsd)) - - # one schema may throw an error while another will validate - try: - xmlschema = etree.XMLSchema(xmlschema_doc) - except etree.XMLSchemaParseError as err: - # this should only error if the XSD itself is malformed - besapi_logger.error("ERROR with `%s`: %s", xsd, err) - raise err - - if xmlschema.validate(xmldoc): - return True - - return False + return validate_xsd(doc) def xmlparse_text(self, text): """parse response text as xml""" diff --git a/src/bescli/bescli.py b/src/bescli/bescli.py index d5533c0..9bc5dd3 100644 --- a/src/bescli/bescli.py +++ b/src/bescli/bescli.py @@ -358,6 +358,15 @@ def do_export_all_sites(self, statement=None): """export site contents to current folder""" self.bes_conn.export_all_sites(verbose=False) + complete_import_bes = Cmd.path_complete + + def do_import_bes(self, statement): + """import bes file""" + + self.poutput(f"Import file: {statement.args}") + + self.poutput(self.bes_conn.import_bes_to_site(str(statement.args))) + complete_upload = Cmd.path_complete def do_upload(self, file_path):