Skip to content

Commit

Permalink
Add resource_filter support to gvm.protocols.gmpv8:create_tag
Browse files Browse the repository at this point in the history
  • Loading branch information
qha committed Apr 23, 2019
1 parent a488ba5 commit d73b1aa
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 30 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Split getting a single preference by name from `get_preferences` method into
`get_preference` [PR 85](https://github.com/greenbone/python-gvm/pull/85)
* Added an explicit `create_container_task` method [PR 108](https://github.com/greenbone/python-gvm/pull/108)
* Added Gmpv8 version of create_tag with plural resource_ids parameter
* Added Gmpv8 version of create_tag with resource_filter parameter and
plural resource_ids parameter [PR 115](https://github.com/greenbone/python-gvm/pull/115)
* Added Gmpv8 version of modify_tag with resource_action parameter,
resource_filter parameter, plural resource_ids parameter [PR 115](https://github.com/greenbone/python-gvm/pull/115)

Expand Down
54 changes: 31 additions & 23 deletions gvm/protocols/gmpv8.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,52 +394,60 @@ def modify_credential(
return self._send_xml_command(cmd)

def create_tag(
self,
name,
resource_ids,
resource_type,
*,
value=None,
comment=None,
active=None
self,
name,
resource_type,
*,
resource_filter=None,
resource_ids=None,
value=None,
comment=None,
active=None
):
"""Create a tag.
Arguments:
name (str): Name of the tag. A full tag name consisting of
namespace and predicate e.g. `foo:bar`
resource_ids (list): IDs of the resources the tag is to be
attached to
namespace and predicate e.g. `foo:bar`.
resource_type (str): Entity type the tag is to be attached
to
value (str, optional): Value associated with the tag
comment (str, optional): Comment for the tag
to.
resource_filter (str, optional) Filter term to select
resources the tag is to be attached to. Either
resource_filter or resource_ids must be provided.
resource_ids (list, optional): IDs of the resources the
tag is to be attached to. Either resource_filter or
resource_ids must be provided.
value (str, optional): Value associated with the tag.
comment (str, optional): Comment for the tag.
active (boolean, optional): Whether the tag should be
active
active.
Returns:
The response. See :py:meth:`send_command` for details.
"""
if not name:
raise RequiredArgument("create_tag requires name argument")

if not resource_filter and not resource_ids:
raise RequiredArgument(
"create_tag requires name argument")

if not resource_ids:
raise RequiredArgument(
"create_tag requires resource_ids argument")
"create_tag requires resource_filter or resource_ids argument"
)

if not resource_type:
raise RequiredArgument(
"create_tag requires resource_type argument")
raise RequiredArgument("create_tag requires resource_type argument")

cmd = XmlCommand('create_tag')
cmd.add_element('name', name)

_xmlresources = cmd.add_element("resources")
for resource_id in resource_ids:
if resource_filter is not None:
_xmlresources.set_attribute("filter", resource_filter)

for resource_id in resource_ids or []:
_xmlresources.add_element(
"resource", attrs={"id": str(resource_id)}
)

_xmlresources.add_element("type", resource_type)

if comment:
Expand Down
66 changes: 60 additions & 6 deletions tests/protocols/gmpv8/test_create_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,68 @@ def test_create_tag_missing_name(self):
name='', resource_ids=['foo'], resource_type='task'
)

def test_create_tag_missing_resource_id(self):
def test_create_tag_missing_resource_filter_and_ids(self):
with self.assertRaises(RequiredArgument):
self.gmp.create_tag(
name='foo', resource_ids=None, resource_type='task'
name='foo',
resource_type='task',
resource_filter=None,
resource_ids=None
)

with self.assertRaises(RequiredArgument):
self.gmp.create_tag(
name='foo', resource_ids=[], resource_type='task'
name='foo',
resource_type='task',
resource_filter=None,
resource_ids=[]
)

with self.assertRaises(RequiredArgument):
self.gmp.create_tag(
name='foo',
resource_type='task'
)

def test_create_tag_missing_resource_type(self):
with self.assertRaises(RequiredArgument):
self.gmp.create_tag(
name='foo', resource_ids=['foo'], resource_type=None
name='foo',
resource_type=None,
resource_filter=None,
resource_ids=['foo']
)

with self.assertRaises(RequiredArgument):
self.gmp.create_tag(
name='foo',
resource_type=None,
resource_filter="name=foo",
resource_ids=None
)

with self.assertRaises(RequiredArgument):
self.gmp.create_tag(
name='foo', resource_ids=['foo'], resource_type=''
name='foo',
resource_type='',
resource_ids=['foo']
)

def test_create_tag(self):
def test_create_tag_with_resource_filter(self):
self.gmp.create_tag(
name='foo', resource_filter='name=foo', resource_type='task'
)

self.connection.send.has_been_called_with(
'<create_tag>'
'<name>foo</name>'
'<resources filter="name=foo">'
'<type>task</type>'
'</resources>'
'</create_tag>'
)

def test_create_tag_with_resource_ids(self):
self.gmp.create_tag(
name='foo', resource_ids=['foo'], resource_type='task'
)
Expand All @@ -77,6 +116,21 @@ def test_create_tag(self):
'</create_tag>'
)

self.gmp.create_tag(
name='foo', resource_ids=['foo', 'bar'], resource_type='task'
)

self.connection.send.has_been_called_with(
'<create_tag>'
'<name>foo</name>'
'<resources>'
'<resource id="foo"/>'
'<resource id="bar"/>'
'<type>task</type>'
'</resources>'
'</create_tag>'
)

def test_create_tag_with_comment(self):
self.gmp.create_tag(
name='foo',
Expand Down

0 comments on commit d73b1aa

Please sign in to comment.