Skip to content

Commit

Permalink
added import bes function. Moved validator function.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgstew committed May 3, 2023
1 parent 6883f2e commit 1e2a1c9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 25 deletions.
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
74 changes: 51 additions & 23 deletions src/besapi/besapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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> 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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"""
Expand Down
9 changes: 9 additions & 0 deletions src/bescli/bescli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 1e2a1c9

Please sign in to comment.