new: Add method to search for tags.

fix #648
pull/653/head
Raphaël Vinot 2020-11-02 12:47:40 +01:00
parent 0947e5eabe
commit a1326f2cf2
2 changed files with 25 additions and 0 deletions

View File

@ -880,6 +880,23 @@ class PyMISP:
response = self._prepare_request('POST', f'tags/delete/{tag_id}')
return self._check_json_response(response)
def search_tags(self, tag_name: str, pythonify: bool = False) -> Union[Dict, List[MISPTag]]:
"""Search a tag by name.
:param tag_name: Name (can be a part of it) to search
"""
r = self._prepare_request('GET', f'tags/index/searchall:{tag_name}')
tag_r = self._check_json_response(r)
if 'errors' in tag_r:
return tag_r
if not (self.global_pythonify or pythonify):
return tag_r['Tag']
to_return: List[MISPTag] = []
for tag in tag_r['Tag']:
t = MISPTag()
t.from_dict(**tag)
to_return.append(t)
return to_return
# ## END Tags ###
# ## BEGIN Taxonomies ###

View File

@ -1338,6 +1338,14 @@ class TestComprehensive(unittest.TestCase):
# Delete event
self.admin_misp_connector.delete_event(first)
# Search tag
# Partial search
tags = self.admin_misp_connector.search_tags(new_tag.name[:5], pythonify=True)
self.assertEqual(tags[0].name, 'this is a test tag')
# No tags found
tags = self.admin_misp_connector.search_tags('not a tag')
self.assertFalse(tags)
# Delete tag
response = self.admin_misp_connector.delete_tag(new_tag)
self.assertEqual(response['message'], 'Tag deleted.')