Merge "Add meter rule to OSC"
This commit is contained in:
commit
55ea0e5b39
103
doc/source/command-objects/network-meter-rule.rst
Normal file
103
doc/source/command-objects/network-meter-rule.rst
Normal file
@ -0,0 +1,103 @@
|
||||
==================
|
||||
network meter rule
|
||||
==================
|
||||
|
||||
A **meter rule** sets the rule for
|
||||
a meter to measure traffic for a specific IP range.
|
||||
The following uses **meter** and requires the L3
|
||||
metering extension.
|
||||
|
||||
Network v2
|
||||
|
||||
network meter rule create
|
||||
-------------------------
|
||||
|
||||
Create meter rule
|
||||
|
||||
.. program:: network meter rule create
|
||||
.. code:: bash
|
||||
|
||||
openstack network meter rule create
|
||||
[--project <project> [--project-domain <project-domain>]]
|
||||
[--ingress | --egress]
|
||||
[--exclude | --include]
|
||||
--remote-ip-prefix <remote-ip-prefix>
|
||||
<meter>
|
||||
|
||||
.. option:: --project <project>
|
||||
|
||||
Owner's project (name or ID)
|
||||
|
||||
*Network version 2 only*
|
||||
|
||||
.. option:: --project-domain <project-domain>
|
||||
|
||||
Domain the project belongs to (name of ID).
|
||||
This can be used in case collisions between project names exist.
|
||||
|
||||
.. option:: --ingress
|
||||
|
||||
Rule is applied to incoming traffic (default)
|
||||
|
||||
.. option:: --egress
|
||||
|
||||
Rule is applied to outgoing traffic
|
||||
|
||||
.. option:: --exclude
|
||||
|
||||
Exclude remote_ip_prefix from count of the traffic of IP addresses
|
||||
|
||||
.. option:: --include
|
||||
|
||||
Include remote_ip_prefix into count of the traffic of IP addresses
|
||||
(default)
|
||||
|
||||
.. option:: --remote-ip-prefix <remote-ip-prefix>
|
||||
|
||||
The remote IP prefix to associate with this metering rule packet
|
||||
|
||||
.. _network_meter_rule_create:
|
||||
.. describe:: <meter>
|
||||
|
||||
Meter to associate with this meter rule (name or ID)
|
||||
|
||||
|
||||
network meter rule delete
|
||||
-------------------------
|
||||
|
||||
Delete meter rule(s)
|
||||
|
||||
.. program:: network meter rule delete
|
||||
.. code:: bash
|
||||
|
||||
openstack network meter rule delete <id> [<id> ...]
|
||||
|
||||
.. _network_meter_rule_delete:
|
||||
.. describe:: <meter-rule-id>
|
||||
|
||||
ID of meter rule(s) to delete
|
||||
|
||||
network meter rule list
|
||||
-----------------------
|
||||
|
||||
List meter rules
|
||||
|
||||
.. program:: network meter rule list
|
||||
.. code:: bash
|
||||
|
||||
openstack network meter rule list
|
||||
|
||||
network meter rule show
|
||||
-----------------------
|
||||
|
||||
Show meter rule
|
||||
|
||||
.. program:: network meter rule show
|
||||
.. code:: bash
|
||||
|
||||
openstack network meter rule show <meter-rule-id>
|
||||
|
||||
.. _network_meter_show:
|
||||
.. describe:: <meter-rule-id>
|
||||
|
||||
Meter rule to display (ID only)
|
@ -112,6 +112,7 @@ referring to both Compute and Volume quotas.
|
||||
* ``network``: (**Compute**, **Network**) - a virtual network for connecting servers and other resources
|
||||
* ``network agent``: (**Network**) - A network agent is an agent that handles various tasks used to implement virtual networks
|
||||
* ``network meter``: (**Network**) - allow traffic metering in a network
|
||||
* ``network meter rule``: (**Network**) - rules for network traffic metering
|
||||
* ``network rbac``: (**Network**) - an RBAC policy for network resources
|
||||
* ``network qos rule``: (**Network**) - a QoS rule for network resources
|
||||
* ``network qos policy``: (**Network**) - a QoS policy for network resources
|
||||
|
203
openstackclient/network/v2/network_meter_rule.py
Normal file
203
openstackclient/network/v2/network_meter_rule.py
Normal file
@ -0,0 +1,203 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#
|
||||
|
||||
"""Meter Rule Implementations"""
|
||||
|
||||
import logging
|
||||
|
||||
from osc_lib.command import command
|
||||
from osc_lib import exceptions
|
||||
from osc_lib import utils
|
||||
|
||||
from openstackclient.i18n import _
|
||||
from openstackclient.identity import common as identity_common
|
||||
from openstackclient.network import sdk_utils
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _get_columns(item):
|
||||
column_map = {
|
||||
'tenant_id': 'project_id',
|
||||
}
|
||||
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
|
||||
|
||||
|
||||
def _get_attrs(client_manager, parsed_args):
|
||||
attrs = {}
|
||||
|
||||
if parsed_args.exclude:
|
||||
attrs['excluded'] = True
|
||||
if parsed_args.include:
|
||||
attrs['excluded'] = False
|
||||
if parsed_args.ingress or not parsed_args.egress:
|
||||
attrs['direction'] = 'ingress'
|
||||
if parsed_args.egress:
|
||||
attrs['direction'] = 'egress'
|
||||
if parsed_args.remote_ip_prefix is not None:
|
||||
attrs['remote_ip_prefix'] = parsed_args.remote_ip_prefix
|
||||
if parsed_args.meter is not None:
|
||||
attrs['metering_label_id'] = parsed_args.meter
|
||||
if parsed_args.project is not None:
|
||||
identity_client = client_manager.identity
|
||||
project_id = identity_common.find_project(
|
||||
identity_client,
|
||||
parsed_args.project,
|
||||
parsed_args.project_domain,
|
||||
).id
|
||||
attrs['tenant_id'] = project_id
|
||||
|
||||
return attrs
|
||||
|
||||
|
||||
class CreateMeterRule(command.ShowOne):
|
||||
_description = _("Create a new meter rule")
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(CreateMeterRule, self).get_parser(prog_name)
|
||||
|
||||
parser.add_argument(
|
||||
'--project',
|
||||
metavar='<project>',
|
||||
help=_("Owner's project (name or ID)")
|
||||
)
|
||||
identity_common.add_project_domain_option_to_parser(parser)
|
||||
exclude_group = parser.add_mutually_exclusive_group()
|
||||
exclude_group.add_argument(
|
||||
'--exclude',
|
||||
action='store_true',
|
||||
help=_("Exclude remote IP prefix from traffic count")
|
||||
)
|
||||
exclude_group.add_argument(
|
||||
'--include',
|
||||
action='store_true',
|
||||
help=_("Include remote IP prefix from traffic count (default)")
|
||||
)
|
||||
direction_group = parser.add_mutually_exclusive_group()
|
||||
direction_group.add_argument(
|
||||
'--ingress',
|
||||
action='store_true',
|
||||
help=_("Apply rule to incoming network traffic (default)")
|
||||
)
|
||||
direction_group.add_argument(
|
||||
'--egress',
|
||||
action='store_true',
|
||||
help=_('Apply rule to outgoing network traffic')
|
||||
)
|
||||
parser.add_argument(
|
||||
'--remote-ip-prefix',
|
||||
metavar='<remote-ip-prefix>',
|
||||
required=True,
|
||||
help=_('The remote IP prefix to associate with this rule'),
|
||||
)
|
||||
parser.add_argument(
|
||||
'meter',
|
||||
metavar='<meter>',
|
||||
help=_('Label to associate with this metering rule (name or ID)'),
|
||||
)
|
||||
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
client = self.app.client_manager.network
|
||||
_meter = client.find_metering_label(parsed_args.meter,
|
||||
ignore_missing=False)
|
||||
parsed_args.meter = _meter.id
|
||||
attrs = _get_attrs(self.app.client_manager, parsed_args)
|
||||
obj = client.create_metering_label_rule(**attrs)
|
||||
display_columns, columns = _get_columns(obj)
|
||||
data = utils.get_item_properties(obj, columns, formatters={})
|
||||
|
||||
return (display_columns, data)
|
||||
|
||||
|
||||
class DeleteMeterRule(command.Command):
|
||||
_description = _("Delete meter rule(s)")
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(DeleteMeterRule, self).get_parser(prog_name)
|
||||
|
||||
parser.add_argument(
|
||||
'meter_rule_id',
|
||||
metavar='<meter-rule-id>',
|
||||
nargs='+',
|
||||
help=_('Meter rule to delete (ID only)')
|
||||
)
|
||||
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
client = self.app.client_manager.network
|
||||
result = 0
|
||||
|
||||
for id in parsed_args.meter_rule_id:
|
||||
try:
|
||||
obj = client.find_metering_label_rule(id, ignore_missing=False)
|
||||
client.delete_metering_label_rule(obj)
|
||||
except Exception as e:
|
||||
result += 1
|
||||
LOG.error(_("Failed to delete meter rule with "
|
||||
"ID '%(id)s': %(e)s"),
|
||||
{"id": id, "e": e})
|
||||
|
||||
if result > 0:
|
||||
total = len(parsed_args.meter_rule_id)
|
||||
msg = (_("%(result)s of %(total)s meter rules failed "
|
||||
"to delete.") % {"result": result, "total": total})
|
||||
raise exceptions.CommandError(msg)
|
||||
|
||||
|
||||
class ListMeterRule(command.Lister):
|
||||
_description = _("List meter rules")
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
client = self.app.client_manager.network
|
||||
|
||||
columns = (
|
||||
'id',
|
||||
'excluded',
|
||||
'direction',
|
||||
'remote_ip_prefix',
|
||||
)
|
||||
column_headers = (
|
||||
'ID',
|
||||
'Excluded',
|
||||
'Direction',
|
||||
'Remote IP Prefix',
|
||||
)
|
||||
data = client.metering_label_rules()
|
||||
return (column_headers,
|
||||
(utils.get_item_properties(
|
||||
s, columns,
|
||||
) for s in data))
|
||||
|
||||
|
||||
class ShowMeterRule(command.ShowOne):
|
||||
_description = _("Display meter rules details")
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(ShowMeterRule, self).get_parser(prog_name)
|
||||
parser.add_argument(
|
||||
'meter_rule_id',
|
||||
metavar='<meter-rule-id>',
|
||||
help=_('Meter rule (ID only)')
|
||||
)
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
client = self.app.client_manager.network
|
||||
obj = client.find_metering_label_rule(parsed_args.meter_rule_id,
|
||||
ignore_missing=False)
|
||||
display_columns, columns = _get_columns(obj)
|
||||
data = utils.get_item_properties(obj, columns)
|
||||
return display_columns, data
|
@ -0,0 +1,108 @@
|
||||
# Copyright (c) 2016, Intel Corporation.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import re
|
||||
import uuid
|
||||
|
||||
from openstackclient.tests.functional import base
|
||||
|
||||
|
||||
class TestMeterRule(base.TestCase):
|
||||
"""Functional tests for meter rule"""
|
||||
METER_NAME = uuid.uuid4().hex
|
||||
METER_ID = None
|
||||
METER_RULE_ID = None
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
# Set up some regex for matching below
|
||||
cls.re_id = re.compile("id\s+\|\s+(\S+)")
|
||||
cls.re_direction = re.compile("direction\s+\|\s+(\S+)")
|
||||
cls.re_ip_prefix = re.compile(
|
||||
"remote_ip_prefix\s+\|\s+([^|]+?)\s+\|"
|
||||
)
|
||||
cls.re_meter_id = re.compile("metering_label_id\s+\|\s+(\S+)")
|
||||
|
||||
raw_output = cls.openstack(
|
||||
'network meter create ' + cls.METER_NAME
|
||||
)
|
||||
|
||||
cls.METER_ID = re.search(cls.re_id, raw_output).group(1)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
raw_output = cls.openstack('network meter delete ' + cls.METER_ID)
|
||||
cls.assertOutput('', raw_output)
|
||||
|
||||
def test_meter_rule_delete(self):
|
||||
"""test create, delete"""
|
||||
|
||||
raw_output = self.openstack(
|
||||
'network meter rule create ' +
|
||||
'--remote-ip-prefix 10.0.0.0/8 ' +
|
||||
self.METER_ID
|
||||
)
|
||||
rule_id = re.search(self.re_id, raw_output).group(1)
|
||||
re_ip = re.search(self.re_ip_prefix, raw_output)
|
||||
|
||||
self.addCleanup(self.openstack,
|
||||
'network meter rule delete ' + rule_id)
|
||||
self.assertIsNotNone(re_ip)
|
||||
self.assertIsNotNone(rule_id)
|
||||
|
||||
def test_meter_rule_list(self):
|
||||
"""Test create, list, delete"""
|
||||
raw_output = self.openstack(
|
||||
'network meter rule create ' +
|
||||
'--remote-ip-prefix 10.0.0.0/8 ' +
|
||||
self.METER_ID
|
||||
)
|
||||
rule_id = re.search(self.re_id, raw_output).group(1)
|
||||
self.addCleanup(self.openstack,
|
||||
'network meter rule delete ' + rule_id)
|
||||
self.assertEqual(
|
||||
'10.0.0.0/8',
|
||||
re.search(self.re_ip_prefix, raw_output).group(1)
|
||||
)
|
||||
|
||||
raw_output = self.openstack('network meter rule list')
|
||||
self.assertIsNotNone(re.search(rule_id + "|\s+\|\s+\|\s+10.0.0.0/8",
|
||||
raw_output))
|
||||
|
||||
def test_meter_rule_show(self):
|
||||
"""Test create, show, delete"""
|
||||
raw_output = self.openstack(
|
||||
'network meter rule create ' +
|
||||
'--remote-ip-prefix 10.0.0.0/8 ' +
|
||||
'--egress ' +
|
||||
self.METER_ID
|
||||
)
|
||||
rule_id = re.search(self.re_id, raw_output).group(1)
|
||||
|
||||
self.assertEqual(
|
||||
'egress',
|
||||
re.search(self.re_direction, raw_output).group(1)
|
||||
)
|
||||
|
||||
raw_output = self.openstack('network meter rule show ' + rule_id)
|
||||
|
||||
self.assertEqual(
|
||||
'10.0.0.0/8',
|
||||
re.search(self.re_ip_prefix, raw_output).group(1)
|
||||
)
|
||||
self.assertIsNotNone(rule_id)
|
||||
|
||||
self.addCleanup(self.openstack,
|
||||
'network meter rule delete ' + rule_id)
|
@ -1322,6 +1322,52 @@ class FakeNetworkMeter(object):
|
||||
return mock.Mock(side_effect=meter)
|
||||
|
||||
|
||||
class FakeNetworkMeterRule(object):
|
||||
"""Fake metering rule"""
|
||||
|
||||
@staticmethod
|
||||
def create_one_rule(attrs=None):
|
||||
"""Create one meter rule"""
|
||||
attrs = attrs or {}
|
||||
|
||||
meter_rule_attrs = {
|
||||
'id': 'meter-label-rule-id-' + uuid.uuid4().hex,
|
||||
'direction': 'ingress',
|
||||
'excluded': False,
|
||||
'metering_label_id': 'meter-label-id-' + uuid.uuid4().hex,
|
||||
'remote_ip_prefix': '10.0.0.0/24',
|
||||
'tenant_id': 'project-id-' + uuid.uuid4().hex,
|
||||
}
|
||||
|
||||
meter_rule_attrs.update(attrs)
|
||||
|
||||
meter_rule = fakes.FakeResource(
|
||||
info=copy.deepcopy(meter_rule_attrs),
|
||||
loaded=True)
|
||||
|
||||
meter_rule.project_id = meter_rule_attrs['tenant_id']
|
||||
|
||||
return meter_rule
|
||||
|
||||
@staticmethod
|
||||
def create_meter_rule(attrs=None, count=2):
|
||||
"""Create multiple meter rules"""
|
||||
|
||||
meter_rules = []
|
||||
for i in range(0, count):
|
||||
meter_rules.append(FakeNetworkMeterRule.
|
||||
create_one_rule(attrs))
|
||||
return meter_rules
|
||||
|
||||
@staticmethod
|
||||
def get_meter_rule(meter_rule=None, count=2):
|
||||
"""Get a list of meter rules"""
|
||||
if meter_rule is None:
|
||||
meter_rule = (FakeNetworkMeterRule.
|
||||
create_meter_rule(count))
|
||||
return mock.Mock(side_effect=meter_rule)
|
||||
|
||||
|
||||
class FakeSubnetPool(object):
|
||||
"""Fake one or more subnet pools."""
|
||||
|
||||
|
321
openstackclient/tests/unit/network/v2/test_network_meter_rule.py
Normal file
321
openstackclient/tests/unit/network/v2/test_network_meter_rule.py
Normal file
@ -0,0 +1,321 @@
|
||||
# Copyright (c) 2016, Intel Corporation.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import mock
|
||||
from mock import call
|
||||
|
||||
from osc_lib import exceptions
|
||||
|
||||
from openstackclient.network.v2 import network_meter_rule
|
||||
from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes_v3
|
||||
from openstackclient.tests.unit.network.v2 import fakes as network_fakes
|
||||
from openstackclient.tests.unit import utils as tests_utils
|
||||
|
||||
|
||||
class TestMeterRule(network_fakes.TestNetworkV2):
|
||||
def setUp(self):
|
||||
super(TestMeterRule, self).setUp()
|
||||
self.network = self.app.client_manager.network
|
||||
self.projects_mock = self.app.client_manager.identity.projects
|
||||
self.domains_mock = self.app.client_manager.identity.domains
|
||||
|
||||
|
||||
class TestCreateMeterRule(TestMeterRule):
|
||||
project = identity_fakes_v3.FakeProject.create_one_project()
|
||||
domain = identity_fakes_v3.FakeDomain.create_one_domain()
|
||||
|
||||
new_rule = (
|
||||
network_fakes.FakeNetworkMeterRule.
|
||||
create_one_rule()
|
||||
)
|
||||
|
||||
columns = (
|
||||
'direction',
|
||||
'excluded',
|
||||
'id',
|
||||
'metering_label_id',
|
||||
'project_id',
|
||||
'remote_ip_prefix',
|
||||
)
|
||||
data = (
|
||||
new_rule.direction,
|
||||
new_rule.excluded,
|
||||
new_rule.id,
|
||||
new_rule.metering_label_id,
|
||||
new_rule.project_id,
|
||||
new_rule.remote_ip_prefix,
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
super(TestCreateMeterRule, self).setUp()
|
||||
fake_meter = network_fakes.FakeNetworkMeter.create_one_meter({
|
||||
'id': self.new_rule.metering_label_id})
|
||||
|
||||
self.network.create_metering_label_rule = mock.Mock(
|
||||
return_value=self.new_rule)
|
||||
self.projects_mock.get.return_value = self.project
|
||||
self.cmd = network_meter_rule.CreateMeterRule(self.app,
|
||||
self.namespace)
|
||||
self.network.find_metering_label = mock.Mock(
|
||||
return_value=fake_meter)
|
||||
|
||||
def test_create_no_options(self):
|
||||
arglist = []
|
||||
verifylist = []
|
||||
|
||||
self.assertRaises(tests_utils.ParserException, self.check_parser,
|
||||
self.cmd, arglist, verifylist)
|
||||
|
||||
def test_create_default_options(self):
|
||||
arglist = [
|
||||
self.new_rule.metering_label_id,
|
||||
"--remote-ip-prefix", self.new_rule.remote_ip_prefix,
|
||||
]
|
||||
verifylist = [
|
||||
('meter', self.new_rule.metering_label_id),
|
||||
('remote_ip_prefix', self.new_rule.remote_ip_prefix),
|
||||
]
|
||||
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
columns, data = (self.cmd.take_action(parsed_args))
|
||||
|
||||
self.network.create_metering_label_rule.assert_called_once_with(
|
||||
**{'direction': 'ingress',
|
||||
'metering_label_id': self.new_rule.metering_label_id,
|
||||
'remote_ip_prefix': self.new_rule.remote_ip_prefix, }
|
||||
)
|
||||
self.assertEqual(self.columns, columns)
|
||||
self.assertEqual(self.data, data)
|
||||
|
||||
def test_create_all_options(self):
|
||||
arglist = [
|
||||
"--ingress",
|
||||
"--include",
|
||||
self.new_rule.metering_label_id,
|
||||
"--remote-ip-prefix", self.new_rule.remote_ip_prefix,
|
||||
]
|
||||
verifylist = [
|
||||
('ingress', True),
|
||||
('include', True),
|
||||
('meter', self.new_rule.metering_label_id),
|
||||
('remote_ip_prefix', self.new_rule.remote_ip_prefix),
|
||||
]
|
||||
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
columns, data = (self.cmd.take_action(parsed_args))
|
||||
|
||||
self.network.create_metering_label_rule.assert_called_once_with(
|
||||
**{'direction': self.new_rule.direction,
|
||||
'excluded': self.new_rule.excluded,
|
||||
'metering_label_id': self.new_rule.metering_label_id,
|
||||
'remote_ip_prefix': self.new_rule.remote_ip_prefix, }
|
||||
)
|
||||
self.assertEqual(self.columns, columns)
|
||||
self.assertEqual(self.data, data)
|
||||
|
||||
|
||||
class TestDeleteMeterRule(TestMeterRule):
|
||||
def setUp(self):
|
||||
super(TestDeleteMeterRule, self).setUp()
|
||||
self.rule_list = \
|
||||
network_fakes.FakeNetworkMeterRule.create_meter_rule(
|
||||
count=2
|
||||
)
|
||||
self.network.delete_metering_label_rule = mock.Mock(return_value=None)
|
||||
|
||||
self.network.find_metering_label_rule = network_fakes \
|
||||
.FakeNetworkMeterRule.get_meter_rule(
|
||||
meter_rule=self.rule_list
|
||||
)
|
||||
|
||||
self.cmd = network_meter_rule.DeleteMeterRule(self.app,
|
||||
self.namespace)
|
||||
|
||||
def test_delete_one_rule(self):
|
||||
arglist = [
|
||||
self.rule_list[0].id,
|
||||
]
|
||||
verifylist = [
|
||||
('meter_rule_id', [self.rule_list[0].id]),
|
||||
]
|
||||
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
|
||||
result = self.cmd.take_action(parsed_args)
|
||||
|
||||
self.network.delete_metering_label_rule.assert_called_once_with(
|
||||
self.rule_list[0]
|
||||
)
|
||||
self.assertIsNone(result)
|
||||
|
||||
def test_delete_multiple_rules(self):
|
||||
arglist = []
|
||||
for rule in self.rule_list:
|
||||
arglist.append(rule.id)
|
||||
verifylist = [
|
||||
('meter_rule_id', arglist),
|
||||
]
|
||||
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
|
||||
result = self.cmd.take_action(parsed_args)
|
||||
|
||||
calls = []
|
||||
for rule in self.rule_list:
|
||||
calls.append(call(rule))
|
||||
self.network.delete_metering_label_rule.assert_has_calls(calls)
|
||||
self.assertIsNone(result)
|
||||
|
||||
def test_delete_multiple_rules_exception(self):
|
||||
arglist = [
|
||||
self.rule_list[0].id,
|
||||
'xxxx-yyyy-zzzz',
|
||||
self.rule_list[1].id,
|
||||
]
|
||||
verifylist = [
|
||||
('meter_rule_id', arglist),
|
||||
]
|
||||
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
|
||||
return_find = [
|
||||
self.rule_list[0],
|
||||
exceptions.NotFound('404'),
|
||||
self.rule_list[1],
|
||||
]
|
||||
self.network.find_metering_label_rule = mock.Mock(
|
||||
side_effect=return_find
|
||||
)
|
||||
|
||||
ret_delete = [
|
||||
None,
|
||||
exceptions.NotFound('404'),
|
||||
]
|
||||
self.network.delete_metering_label_rule = mock.Mock(
|
||||
side_effect=ret_delete
|
||||
)
|
||||
|
||||
self.assertRaises(exceptions.CommandError, self.cmd.take_action,
|
||||
parsed_args)
|
||||
|
||||
calls = [
|
||||
call(self.rule_list[0]),
|
||||
call(self.rule_list[1]),
|
||||
]
|
||||
self.network.delete_metering_label_rule.assert_has_calls(calls)
|
||||
|
||||
|
||||
class TestListMeterRule(TestMeterRule):
|
||||
rule_list = \
|
||||
network_fakes.FakeNetworkMeterRule.create_meter_rule(
|
||||
count=2
|
||||
)
|
||||
|
||||
columns = (
|
||||
'ID',
|
||||
'Excluded',
|
||||
'Direction',
|
||||
'Remote IP Prefix',
|
||||
)
|
||||
|
||||
data = []
|
||||
|
||||
for rule in rule_list:
|
||||
data.append((
|
||||
rule.id,
|
||||
rule.excluded,
|
||||
rule.direction,
|
||||
rule.remote_ip_prefix,
|
||||
))
|
||||
|
||||
def setUp(self):
|
||||
super(TestListMeterRule, self).setUp()
|
||||
|
||||
self.network.metering_label_rules = mock.Mock(
|
||||
return_value=self.rule_list
|
||||
)
|
||||
|
||||
self.cmd = network_meter_rule.ListMeterRule(self.app,
|
||||
self.namespace)
|
||||
|
||||
def test_rule_list(self):
|
||||
arglist = []
|
||||
verifylist = []
|
||||
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
|
||||
columns, data = self.cmd.take_action(parsed_args)
|
||||
|
||||
self.network.metering_label_rules.assert_called_with()
|
||||
self.assertEqual(self.columns, columns)
|
||||
self.assertEqual(self.data, list(data))
|
||||
|
||||
|
||||
class TestShowMeterRule(TestMeterRule):
|
||||
new_rule = (
|
||||
network_fakes.FakeNetworkMeterRule.
|
||||
create_one_rule()
|
||||
)
|
||||
|
||||
columns = (
|
||||
'direction',
|
||||
'excluded',
|
||||
'id',
|
||||
'metering_label_id',
|
||||
'project_id',
|
||||
'remote_ip_prefix',
|
||||
)
|
||||
|
||||
data = (
|
||||
new_rule.direction,
|
||||
new_rule.excluded,
|
||||
new_rule.id,
|
||||
new_rule.metering_label_id,
|
||||
new_rule.project_id,
|
||||
new_rule.remote_ip_prefix,
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
super(TestShowMeterRule, self).setUp()
|
||||
|
||||
self.cmd = network_meter_rule.ShowMeterRule(self.app,
|
||||
self.namespace)
|
||||
|
||||
self.network.find_metering_label_rule = \
|
||||
mock.Mock(return_value=self.new_rule)
|
||||
|
||||
def test_show_no_options(self):
|
||||
arglist = []
|
||||
verifylist = []
|
||||
|
||||
self.assertRaises(tests_utils.ParserException, self.check_parser,
|
||||
self.cmd, arglist, verifylist)
|
||||
|
||||
def test_label_rule_show_option(self):
|
||||
arglist = [
|
||||
self.new_rule.id,
|
||||
]
|
||||
verifylist = [
|
||||
('meter_rule_id', self.new_rule.id),
|
||||
]
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
|
||||
columns, data = self.cmd.take_action(parsed_args)
|
||||
|
||||
self.network.find_metering_label_rule.assert_called_with(
|
||||
self.new_rule.id, ignore_missing=False
|
||||
)
|
||||
self.assertEqual(self.columns, columns)
|
||||
self.assertEqual(self.data, data)
|
15
setup.cfg
15
setup.cfg
@ -355,11 +355,6 @@ openstack.network.v2 =
|
||||
|
||||
ip_floating_pool_list = openstackclient.network.v2.floating_ip_pool:ListIPFloatingPool
|
||||
|
||||
network_meter_create = openstackclient.network.v2.meter:CreateMeter
|
||||
network_meter_delete = openstackclient.network.v2.meter:DeleteMeter
|
||||
network_meter_list = openstackclient.network.v2.meter:ListMeter
|
||||
network_meter_show = openstackclient.network.v2.meter:ShowMeter
|
||||
|
||||
network_agent_delete = openstackclient.network.v2.network_agent:DeleteNetworkAgent
|
||||
network_agent_list = openstackclient.network.v2.network_agent:ListNetworkAgent
|
||||
network_agent_set = openstackclient.network.v2.network_agent:SetNetworkAgent
|
||||
@ -371,6 +366,16 @@ openstack.network.v2 =
|
||||
network_set = openstackclient.network.v2.network:SetNetwork
|
||||
network_show = openstackclient.network.v2.network:ShowNetwork
|
||||
|
||||
network_meter_create = openstackclient.network.v2.meter:CreateMeter
|
||||
network_meter_delete = openstackclient.network.v2.meter:DeleteMeter
|
||||
network_meter_list = openstackclient.network.v2.meter:ListMeter
|
||||
network_meter_show = openstackclient.network.v2.meter:ShowMeter
|
||||
|
||||
network_meter_rule_create = openstackclient.network.v2.network_meter_rule:CreateMeterRule
|
||||
network_meter_rule_delete = openstackclient.network.v2.network_meter_rule:DeleteMeterRule
|
||||
network_meter_rule_list = openstackclient.network.v2.network_meter_rule:ListMeterRule
|
||||
network_meter_rule_show = openstackclient.network.v2.network_meter_rule:ShowMeterRule
|
||||
|
||||
network_qos_policy_create = openstackclient.network.v2.network_qos_policy:CreateNetworkQosPolicy
|
||||
network_qos_policy_delete = openstackclient.network.v2.network_qos_policy:DeleteNetworkQosPolicy
|
||||
network_qos_policy_list = openstackclient.network.v2.network_qos_policy:ListNetworkQosPolicy
|
||||
|
Loading…
Reference in New Issue
Block a user