OSC Network Meter

Implement Neutron feature of Metering into
OpenStack Client.

Meter Rules will be implemented in seperate
patchset.

Partially Implements: blueprint neutron-client-metering

Change-Id: Ie82d42759504cbdb1c991c5183c1f0adf59e60fe
This commit is contained in:
Ankur Gupta 2016-09-23 14:59:51 -05:00
parent 0948aa6aeb
commit 0fb1378c6c
8 changed files with 745 additions and 0 deletions

View File

@ -0,0 +1,91 @@
=============
network meter
=============
A **network meter** allows operators to measure
traffic for a specific IP range. The following commands
are specific to the L3 metering extension.
Network v2
network meter create
--------------------
Create network meter
.. program:: network meter create
.. code:: bash
openstack network meter create
[--project <project> [--project-domain <project-domain>]]
[--description <description>]
[--share | --no-share]
<name>
.. option:: --project <project>
Owner's project (name of ID)
*Network version 2 only*
.. option:: --description <description>
Description of meter
*Network version 2 only*
.. option:: --share
Share the meter between projects
.. option:: --no-share
Do not share the meter between projects (Default)
.. _network_meter_create:
.. describe:: <name>
New meter name
network meter delete
--------------------
Delete network meter(s)
.. program:: network meter delete
.. code:: bash
openstack network meter delete
<meter> [<meter> ...]
.. _network_meter_delete:
.. describe:: <meter>
Meter(s) to delete (name or ID)
network meter list
------------------
List network meters
.. program:: network meter list
.. code:: bash
openstack network meter list
network meter show
------------------
Show network meter
.. program:: network meter show
.. code:: bash
openstack network meter show
<meter>
.. _network_meter_show:
.. describe:: <meter>
Meter to display (name or ID)

View File

@ -111,6 +111,7 @@ referring to both Compute and Volume quotas.
* ``module``: (**Internal**) - installed Python modules in the OSC process
* ``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 rbac``: (**Network**) - an RBAC policy for network resources
* ``network qos policy``: (**Network**) - a QoS policy for network resources
* ``network qos rule type``: (**Network**) - list of QoS available rule types

View File

@ -0,0 +1,190 @@
# 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.
#
"""Metering Label 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 = {
'is_shared': 'shared',
'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.description is not None:
attrs['description'] = parsed_args.description
if parsed_args.project is not None and 'project' in parsed_args:
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
if parsed_args.share:
attrs['shared'] = True
if parsed_args.no_share:
attrs['shared'] = False
if parsed_args.name is not None:
attrs['name'] = parsed_args.name
return attrs
# TODO(ankur-gupta-f): Use the SDK resource mapped attribute names once the
# OSC minimum requirements include SDK 1.0.
class CreateMeter(command.ShowOne):
_description = _("Create network meter")
def get_parser(self, prog_name):
parser = super(CreateMeter, self).get_parser(prog_name)
parser.add_argument(
'--description',
metavar='<description>',
help=_("Create description for meter")
)
parser.add_argument(
'--project',
metavar='<project>',
help=_("Owner's project (name or ID)")
)
identity_common.add_project_domain_option_to_parser(parser)
share_group = parser.add_mutually_exclusive_group()
share_group.add_argument(
'--share',
action='store_true',
default=None,
help=_("Share meter between projects")
)
share_group.add_argument(
'--no-share',
action='store_true',
help=_("Do not share meter between projects")
)
parser.add_argument(
'name',
metavar='<name>',
help=_('Name of meter'),
)
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.network
attrs = _get_attrs(self.app.client_manager, parsed_args)
obj = client.create_metering_label(**attrs)
display_columns, columns = _get_columns(obj)
data = utils.get_item_properties(obj, columns, formatters={})
return (display_columns, data)
# TODO(ankur-gupta-f): Use the SDK resource mapped attribute names once the
# OSC minimum requirements include SDK 1.0.
class DeleteMeter(command.Command):
_description = _("Delete network meter")
def get_parser(self, prog_name):
parser = super(DeleteMeter, self).get_parser(prog_name)
parser.add_argument(
'meter',
metavar='<meter>',
nargs='+',
help=_('Meter to delete (name or ID)')
)
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.network
result = 0
for meter in parsed_args.meter:
try:
obj = client.find_metering_label(meter, ignore_missing=False)
client.delete_metering_label(obj)
except Exception as e:
result += 1
LOG.error(_("Failed to delete meter with "
"ID '%(meter)s': %(e)s"),
{"meter": meter, "e": e})
if result > 0:
total = len(parsed_args.meter)
msg = (_("%(result)s of %(total)s meters failed "
"to delete.") % {"result": result, "total": total})
raise exceptions.CommandError(msg)
class ListMeter(command.Lister):
_description = _("List network meters")
def take_action(self, parsed_args):
client = self.app.client_manager.network
columns = (
'id',
'name',
'description',
'shared',
)
column_headers = (
'ID',
'Name',
'Description',
'Shared',
)
data = client.metering_labels()
return (column_headers,
(utils.get_item_properties(
s, columns,
) for s in data))
class ShowMeter(command.ShowOne):
_description = _("Show network meter")
def get_parser(self, prog_name):
parser = super(ShowMeter, self).get_parser(prog_name)
parser.add_argument(
'meter',
metavar='<meter>',
help=_('Meter to display (name or ID)')
)
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.network
obj = client.find_metering_label(parsed_args.meter,
ignore_missing=False)
display_columns, columns = _get_columns(obj)
data = utils.get_item_properties(obj, columns)
return display_columns, data

View File

@ -0,0 +1,102 @@
# 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 TestMeter(base.TestCase):
"""Functional tests for network meter."""
# NOTE(dtroyer): Do not normalize the setup and teardown of the resource
# creation and deletion. Little is gained when each test
# has its own needs and there are collisions when running
# tests in parallel.
@classmethod
def setUpClass(cls):
# Set up some regex for matching below
cls.re_name = re.compile("name\s+\|\s+([^|]+?)\s+\|")
cls.re_shared = re.compile("shared\s+\|\s+(\S+)")
cls.re_description = re.compile("description\s+\|\s+([^|]+?)\s+\|")
def test_meter_delete(self):
"""Test create, delete multiple"""
name1 = uuid.uuid4().hex
name2 = uuid.uuid4().hex
raw_output = self.openstack(
'network meter create ' + name1,
)
self.assertEqual(
name1,
re.search(self.re_name, raw_output).group(1),
)
# Check if default shared values
self.assertEqual(
'False',
re.search(self.re_shared, raw_output).group(1)
)
raw_output = self.openstack(
'network meter create ' + name2,
)
self.assertEqual(
name2,
re.search(self.re_name, raw_output).group(1),
)
raw_output = self.openstack(
'network meter delete ' + name1 + ' ' + name2,
)
self.assertOutput('', raw_output)
def test_meter_list(self):
"""Test create, list filters, delete"""
name1 = uuid.uuid4().hex
raw_output = self.openstack(
'network meter create --description Test1 --share ' + name1,
)
self.addCleanup(self.openstack, 'network meter delete ' + name1)
self.assertEqual(
'Test1',
re.search(self.re_description, raw_output).group(1),
)
self.assertEqual(
'True',
re.search(self.re_shared, raw_output).group(1),
)
name2 = uuid.uuid4().hex
raw_output = self.openstack(
'network meter create --description Test2 --no-share ' + name2,
)
self.addCleanup(self.openstack, 'network meter delete ' + name2)
self.assertEqual(
'Test2',
re.search(self.re_description, raw_output).group(1),
)
self.assertEqual(
'False',
re.search(self.re_shared, raw_output).group(1),
)
raw_output = self.openstack('network meter list')
self.assertIsNotNone(re.search(name1 + "\s+\|\s+Test1", raw_output))
self.assertIsNotNone(re.search(name2 + "\s+\|\s+Test2", raw_output))

View File

@ -1258,6 +1258,51 @@ class FakeFloatingIP(object):
return mock.Mock(side_effect=floating_ips)
class FakeNetworkMeter(object):
"""Fake network meter"""
@staticmethod
def create_one_meter(attrs=None):
"""Create metering pool"""
attrs = attrs or {}
meter_attrs = {
'id': 'meter-id-' + uuid.uuid4().hex,
'name': 'meter-name-' + uuid.uuid4().hex,
'description': 'meter-description-' + uuid.uuid4().hex,
'tenant_id': 'project-id-' + uuid.uuid4().hex,
'shared': False
}
meter_attrs.update(attrs)
meter = fakes.FakeResource(
info=copy.deepcopy(meter_attrs),
loaded=True)
meter.project_id = meter_attrs['tenant_id']
return meter
@staticmethod
def create_meter(attrs=None, count=2):
"""Create multiple meters"""
meters = []
for i in range(0, count):
meters.append(FakeNetworkMeter.
create_one_meter(attrs))
return meters
@staticmethod
def get_meter(meter=None, count=2):
"""Get a list of meters"""
if meter is None:
meter = (FakeNetworkMeter.
create_meter(count))
return mock.Mock(side_effect=meter)
class FakeSubnetPool(object):
"""Fake one or more subnet pools."""

View File

@ -0,0 +1,304 @@
# 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 meter
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 TestMeter(network_fakes.TestNetworkV2):
def setUp(self):
super(TestMeter, 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 TestCreateMeter(TestMeter):
project = identity_fakes_v3.FakeProject.create_one_project()
domain = identity_fakes_v3.FakeDomain.create_one_domain()
new_meter = (
network_fakes.FakeNetworkMeter.
create_one_meter()
)
columns = (
'description',
'id',
'name',
'project_id',
'shared',
)
data = (
new_meter.description,
new_meter.id,
new_meter.name,
new_meter.project_id,
new_meter.shared,
)
def setUp(self):
super(TestCreateMeter, self).setUp()
self.network.create_metering_label = mock.Mock(
return_value=self.new_meter)
self.projects_mock.get.return_value = self.project
self.cmd = meter.CreateMeter(self.app, self.namespace)
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_meter.name,
]
verifylist = [
('name', self.new_meter.name),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = (self.cmd.take_action(parsed_args))
self.network.create_metering_label.assert_called_once_with(
**{'name': self.new_meter.name}
)
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data)
def test_create_all_options(self):
arglist = [
"--description", self.new_meter.description,
"--project", self.new_meter.project_id,
"--project-domain", self.domain.name,
"--share",
self.new_meter.name,
]
verifylist = [
('description', self.new_meter.description),
('name', self.new_meter.name),
('project', self.new_meter.project_id),
('project_domain', self.domain.name),
('share', True),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = (self.cmd.take_action(parsed_args))
self.network.create_metering_label.assert_called_once_with(
**{'description': self.new_meter.description,
'name': self.new_meter.name,
'tenant_id': self.project.id,
'shared': True, }
)
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data)
class TestDeleteMeter(TestMeter):
def setUp(self):
super(TestDeleteMeter, self).setUp()
self.meter_list = \
network_fakes.FakeNetworkMeter.create_meter(count=2)
self.network.delete_metering_label = mock.Mock(return_value=None)
self.network.find_metering_label = network_fakes \
.FakeNetworkMeter.get_meter(
meter=self.meter_list
)
self.cmd = meter.DeleteMeter(self.app, self.namespace)
def test_delete_one_meter(self):
arglist = [
self.meter_list[0].name,
]
verifylist = [
('meter', [self.meter_list[0].name]),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
self.network.delete_metering_label.assert_called_once_with(
self.meter_list[0]
)
self.assertIsNone(result)
def test_delete_multiple_meters(self):
arglist = []
for n in self.meter_list:
arglist.append(n.id)
verifylist = [
('meter', arglist),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
calls = []
for n in self.meter_list:
calls.append(call(n))
self.network.delete_metering_label.assert_has_calls(calls)
self.assertIsNone(result)
def test_delete_multiple_meter_exception(self):
arglist = [
self.meter_list[0].id,
'xxxx-yyyy-zzzz',
self.meter_list[1].id,
]
verifylist = [
('meter', arglist),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
return_find = [
self.meter_list[0],
exceptions.NotFound('404'),
self.meter_list[1],
]
self.network.find_meter = mock.Mock(side_effect=return_find)
ret_delete = [
None,
exceptions.NotFound('404'),
]
self.network.delete_metering_label = mock.Mock(side_effect=ret_delete)
self.assertRaises(exceptions.CommandError, self.cmd.take_action,
parsed_args)
calls = [
call(self.meter_list[0]),
call(self.meter_list[1]),
]
self.network.delete_metering_label.assert_has_calls(calls)
class TestListMeter(TestMeter):
meter_list = \
network_fakes.FakeNetworkMeter.create_meter(count=2)
columns = (
'ID',
'Name',
'Description',
'Shared',
)
data = []
for meters in meter_list:
data.append((
meters.id,
meters.name,
meters.description,
meters.shared,
))
def setUp(self):
super(TestListMeter, self).setUp()
self.network.metering_labels = mock.Mock(
return_value=self.meter_list
)
self.cmd = meter.ListMeter(self.app, self.namespace)
def test_meter_list(self):
arglist = []
verifylist = []
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)
self.network.metering_labels.assert_called_with()
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data))
class TestShowMeter(TestMeter):
new_meter = (
network_fakes.FakeNetworkMeter.
create_one_meter()
)
columns = (
'description',
'id',
'name',
'project_id',
'shared',
)
data = (
new_meter.description,
new_meter.id,
new_meter.name,
new_meter.project_id,
new_meter.shared,
)
def setUp(self):
super(TestShowMeter, self).setUp()
self.cmd = meter.ShowMeter(self.app, self.namespace)
self.network.find_metering_label = \
mock.Mock(return_value=self.new_meter)
def test_show_no_options(self):
arglist = []
verifylist = []
self.assertRaises(tests_utils.ParserException, self.check_parser,
self.cmd, arglist, verifylist)
def test_meter_show_option(self):
arglist = [
self.new_meter.name,
]
verifylist = [
('meter', self.new_meter.name),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)
self.network.find_metering_label.assert_called_with(
self.new_meter.name, ignore_missing=False
)
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data)

View File

@ -0,0 +1,7 @@
---
features:
- |
Add support for network metering commands:
``network meter create``, ``network meter delete``,
``network meter show``, ``network meter list``
[Blueprint :oscbp:`neutron-client-metering`]

View File

@ -355,6 +355,11 @@ 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