Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tag commands in gmpv8 and exclude tests from installation #115

Merged
merged 5 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ 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 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)

### Changed
* Aligned ALIVE_TESTS declaration with list from GSA [PR 93](https://github.com/greenbone/python-gvm/pull/93)
Expand Down
2 changes: 1 addition & 1 deletion gvm/protocols/gmpv7.py
Original file line number Diff line number Diff line change
Expand Up @@ -5269,7 +5269,7 @@ def modify_tag(
name (str, optional): Name of the tag.
value (str, optional): Value of the tag.
active (boolean, optional): Whether the tag is active.
resource_id (str, optional): IDs of the resource to which to
resource_id (str, optional): ID of the resource to which to
attach the tag. Required if resource_type is set.
resource_type (str, optional): Type of the resource to which to
attach the tag. Required if resource_id is set.
Expand Down
156 changes: 156 additions & 0 deletions gvm/protocols/gmpv8.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,159 @@ def modify_credential(
_xmlkey.add_element("public", public_key)

return self._send_xml_command(cmd)

def create_tag(
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_type (str): Entity type the tag is to be attached
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.

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 resource_filter or resource_ids argument"
)

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

cmd = XmlCommand('create_tag')
cmd.add_element('name', name)
qha marked this conversation as resolved.
Show resolved Hide resolved

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

for resource_id in resource_ids or []:
qha marked this conversation as resolved.
Show resolved Hide resolved
_xmlresources.add_element(
"resource", attrs={"id": str(resource_id)}
)
qha marked this conversation as resolved.
Show resolved Hide resolved

_xmlresources.add_element("type", resource_type)

if comment:
cmd.add_element("comment", comment)

if value:
cmd.add_element("value", value)

if active is not None:
if active:
cmd.add_element("active", "1")
else:
cmd.add_element("active", "0")

return self._send_xml_command(cmd)

def modify_tag(
self,
tag_id,
*,
comment=None,
name=None,
value=None,
active=None,
resource_action=None,
resource_type=None,
resource_filter=None,
resource_ids=None
):
"""Modifies an existing tag.

Arguments:
tag_id (str): UUID of the tag.
comment (str, optional): Comment to add to the tag.
name (str, optional): Name of the tag.
value (str, optional): Value of the tag.
active (boolean, optional): Whether the tag is active.
resource_action (str, optional) Whether to add or remove
resources instead of overwriting. One of '', 'add',
'set' or 'remove'.
resource_type (str, optional): Type of the resources to
which to attach the tag. Required if resource_filter
or resource_ids is set.
resource_filter (str, optional) Filter term to select
resources the tag is to be attached to. Required if
resource_type is set unless resource_ids is set.
resource_ids (list, optional): IDs of the resources to
which to attach the tag. Required if resource_type is
set unless resource_filter is set.

Returns:
The response. See :py:meth:`send_command` for details.
"""
if not tag_id:
raise RequiredArgument("modify_tag requires a tag_id element")

cmd = XmlCommand("modify_tag")
cmd.set_attribute("tag_id", str(tag_id))

if comment:
cmd.add_element("comment", comment)

if name:
cmd.add_element("name", name)

if value:
cmd.add_element("value", value)

if active is not None:
cmd.add_element("active", _to_bool(active))

if resource_action or resource_filter or resource_ids or resource_type:
if not resource_filter and not resource_ids:
raise RequiredArgument(
"modify_tag requires resource_filter or resource_ids "
"argument when resource_action or resource_type is set"
)

if not resource_type:
raise RequiredArgument(
"modify_tag requires resource_type argument when "
"resource_action or resource_filter or resource_ids "
"is set"
)

_xmlresources = cmd.add_element("resources")
if resource_action is not None:
_xmlresources.set_attribute("action", resource_action)

if resource_filter is not None:
qha marked this conversation as resolved.
Show resolved Hide resolved
_xmlresources.set_attribute("filter", resource_filter)

for resource_id in resource_ids or []:
qha marked this conversation as resolved.
Show resolved Hide resolved
_xmlresources.add_element(
"resource", attrs={"id": str(resource_id)}
)

_xmlresources.add_element("type", resource_type)
qha marked this conversation as resolved.
Show resolved Hide resolved

return self._send_xml_command(cmd)
202 changes: 202 additions & 0 deletions tests/protocols/gmpv8/test_create_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018 Greenbone Networks GmbH
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import unittest

from gvm.errors import RequiredArgument
from gvm.protocols.gmpv8 import Gmp

from .. import MockConnection


class GmpCreateTagTestCase(unittest.TestCase):
def setUp(self):
self.connection = MockConnection()
self.gmp = Gmp(self.connection)

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

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

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

with self.assertRaises(RequiredArgument):
self.gmp.create_tag(
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_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_type='',
resource_ids=['foo']
)

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'
)

self.connection.send.has_been_called_with(
'<create_tag>'
'<name>foo</name>'
'<resources>'
'<resource id="foo"/>'
'<type>task</type>'
'</resources>'
'</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',
resource_ids=['foo'],
resource_type='task',
comment='bar'
)

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

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

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

def test_create_tag_with_active(self):
self.gmp.create_tag(
name='foo', resource_ids=['foo'], resource_type='task', active=True
)

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

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

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


if __name__ == '__main__':
unittest.main()
Loading