Merge "Support CLI changes for QoS (2/2)."
This commit is contained in:
107
neutronclient/neutron/v2_0/qos/bandwidth_limit_rule.py
Normal file
107
neutronclient/neutron/v2_0/qos/bandwidth_limit_rule.py
Normal file
@@ -0,0 +1,107 @@
|
||||
# Copyright 2015 Huawei Technologies India Pvt Ltd, Inc.
|
||||
# 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.
|
||||
#
|
||||
|
||||
|
||||
from neutronclient.common import exceptions
|
||||
from neutronclient.i18n import _
|
||||
from neutronclient.neutron import v2_0 as neutronv20
|
||||
from neutronclient.neutron.v2_0.qos import rule as qos_rule
|
||||
|
||||
|
||||
BANDWIDTH_LIMIT_RULE_RESOURCE = 'bandwidth_limit_rule'
|
||||
|
||||
|
||||
def add_bandwidth_limit_arguments(parser):
|
||||
parser.add_argument(
|
||||
'--max-kbps',
|
||||
help=_('max bandwidth in kbps.'))
|
||||
parser.add_argument(
|
||||
'--max-burst-kbps',
|
||||
help=_('max burst bandwidth in kbps.'))
|
||||
|
||||
|
||||
def update_bandwidth_limit_args2body(parsed_args, body):
|
||||
max_kbps = parsed_args.max_kbps
|
||||
max_burst_kbps = parsed_args.max_burst_kbps
|
||||
if not (max_kbps or max_burst_kbps):
|
||||
raise exceptions.CommandError(_("Must provide max_kbps"
|
||||
" or max_burst_kbps option."))
|
||||
neutronv20.update_dict(parsed_args, body,
|
||||
['max_kbps', 'max_burst_kbps', 'tenant_id'])
|
||||
|
||||
|
||||
class CreateQoSBandwidthLimitRule(qos_rule.QosRuleMixin,
|
||||
neutronv20.CreateCommand):
|
||||
"""Create a qos bandwidth limit rule."""
|
||||
|
||||
resource = BANDWIDTH_LIMIT_RULE_RESOURCE
|
||||
|
||||
def add_known_arguments(self, parser):
|
||||
super(CreateQoSBandwidthLimitRule, self).add_known_arguments(parser)
|
||||
add_bandwidth_limit_arguments(parser)
|
||||
|
||||
def args2body(self, parsed_args):
|
||||
body = {}
|
||||
update_bandwidth_limit_args2body(parsed_args, body)
|
||||
return {'bandwidth_limit_rule': body}
|
||||
|
||||
|
||||
class ListQoSBandwidthLimitRules(qos_rule.QosRuleMixin,
|
||||
neutronv20.ListCommand):
|
||||
"""List all qos bandwidth limit rules belonging to the specified policy."""
|
||||
|
||||
resource = BANDWIDTH_LIMIT_RULE_RESOURCE
|
||||
|
||||
_formatters = {}
|
||||
pagination_support = True
|
||||
sorting_support = True
|
||||
|
||||
def args2body(self, parsed_args):
|
||||
body = {}
|
||||
qos_rule.update_policy_args2body(parsed_args, body)
|
||||
return {'qos_rule': body}
|
||||
|
||||
|
||||
class ShowQoSBandwidthLimitRule(qos_rule.QosRuleMixin, neutronv20.ShowCommand):
|
||||
"""Show information about the given qos bandwidth limit rule."""
|
||||
|
||||
resource = BANDWIDTH_LIMIT_RULE_RESOURCE
|
||||
allow_names = False
|
||||
|
||||
|
||||
class UpdateQoSBandwidthLimitRule(qos_rule.QosRuleMixin,
|
||||
neutronv20.UpdateCommand):
|
||||
"""Update the given qos bandwidth limit rule."""
|
||||
|
||||
resource = BANDWIDTH_LIMIT_RULE_RESOURCE
|
||||
allow_names = False
|
||||
|
||||
def add_known_arguments(self, parser):
|
||||
super(UpdateQoSBandwidthLimitRule, self).add_known_arguments(parser)
|
||||
add_bandwidth_limit_arguments(parser)
|
||||
|
||||
def args2body(self, parsed_args):
|
||||
body = {}
|
||||
update_bandwidth_limit_args2body(parsed_args, body)
|
||||
return {'bandwidth_limit_rule': body}
|
||||
|
||||
|
||||
class DeleteQoSBandwidthLimitRule(qos_rule.QosRuleMixin,
|
||||
neutronv20.DeleteCommand):
|
||||
"""Delete a given qos bandwidth limit rule."""
|
||||
|
||||
resource = BANDWIDTH_LIMIT_RULE_RESOURCE
|
||||
allow_names = False
|
||||
@@ -14,6 +14,8 @@
|
||||
# under the License.
|
||||
#
|
||||
|
||||
import os
|
||||
|
||||
from neutronclient.i18n import _
|
||||
from neutronclient.neutron import v2_0 as neutronv20
|
||||
|
||||
@@ -72,6 +74,14 @@ class ShowQoSPolicy(neutronv20.ShowCommand):
|
||||
resource = 'policy'
|
||||
shadow_resource = 'qos_policy'
|
||||
|
||||
def format_output_data(self, data):
|
||||
rules = []
|
||||
for rule in data['policy'].get('rules', []):
|
||||
rules.append("%s (type: %s)" % (rule['id'], rule['type']))
|
||||
data['policy']['rules'] = os.linesep.join(rules)
|
||||
|
||||
super(ShowQoSPolicy, self).format_output_data(data)
|
||||
|
||||
|
||||
class CreateQoSPolicy(neutronv20.CreateCommand):
|
||||
"""Create a qos policy."""
|
||||
|
||||
58
neutronclient/neutron/v2_0/qos/rule.py
Normal file
58
neutronclient/neutron/v2_0/qos/rule.py
Normal file
@@ -0,0 +1,58 @@
|
||||
# Copyright 2015 Huawei Technologies India Pvt Ltd, Inc.
|
||||
# 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.
|
||||
#
|
||||
|
||||
|
||||
from neutronclient.i18n import _
|
||||
from neutronclient.neutron import v2_0 as neutronv20
|
||||
from neutronclient.neutron.v2_0.qos import policy as qos_policy
|
||||
|
||||
|
||||
def add_policy_argument(parser):
|
||||
parser.add_argument(
|
||||
'policy', metavar='QOS_POLICY',
|
||||
help=_('ID or name of the QoS policy.'))
|
||||
|
||||
|
||||
def add_rule_argument(parser):
|
||||
parser.add_argument(
|
||||
'rule', metavar='QOS_RULE',
|
||||
help=_('ID of the QoS rule.'))
|
||||
|
||||
|
||||
def update_policy_args2body(parsed_args, body):
|
||||
neutronv20.update_dict(parsed_args, body, ['policy'])
|
||||
|
||||
|
||||
def update_rule_args2body(parsed_args, body):
|
||||
neutronv20.update_dict(parsed_args, body, ['rule'])
|
||||
|
||||
|
||||
class QosRuleMixin(object):
|
||||
def add_known_arguments(self, parser):
|
||||
add_policy_argument(parser)
|
||||
|
||||
def set_extra_attrs(self, parsed_args):
|
||||
self.parent_id = qos_policy.get_qos_policy_id(self.get_client(),
|
||||
parsed_args.policy)
|
||||
|
||||
|
||||
class ListQoSRuleTypes(neutronv20.ListCommand):
|
||||
"""List available qos rule types."""
|
||||
|
||||
resource = 'rule_type'
|
||||
shadow_resource = 'qos_rule_type'
|
||||
pagination_support = True
|
||||
sorting_support = True
|
||||
@@ -72,7 +72,9 @@ from neutronclient.neutron.v2_0.nsx import networkgateway
|
||||
from neutronclient.neutron.v2_0.nsx import qos_queue
|
||||
from neutronclient.neutron.v2_0 import policyprofile
|
||||
from neutronclient.neutron.v2_0 import port
|
||||
from neutronclient.neutron.v2_0.qos import bandwidth_limit_rule
|
||||
from neutronclient.neutron.v2_0.qos import policy as qos_policy
|
||||
from neutronclient.neutron.v2_0.qos import rule as qos_rule
|
||||
from neutronclient.neutron.v2_0 import quota
|
||||
from neutronclient.neutron.v2_0 import rbac
|
||||
from neutronclient.neutron.v2_0 import router
|
||||
@@ -378,6 +380,22 @@ COMMAND_V2 = {
|
||||
'qos-policy-create': qos_policy.CreateQoSPolicy,
|
||||
'qos-policy-update': qos_policy.UpdateQoSPolicy,
|
||||
'qos-policy-delete': qos_policy.DeleteQoSPolicy,
|
||||
'qos-bandwidth-limit-rule-create': (
|
||||
bandwidth_limit_rule.CreateQoSBandwidthLimitRule
|
||||
),
|
||||
'qos-bandwidth-limit-rule-show': (
|
||||
bandwidth_limit_rule.ShowQoSBandwidthLimitRule
|
||||
),
|
||||
'qos-bandwidth-limit-rule-list': (
|
||||
bandwidth_limit_rule.ListQoSBandwidthLimitRules
|
||||
),
|
||||
'qos-bandwidth-limit-rule-update': (
|
||||
bandwidth_limit_rule.UpdateQoSBandwidthLimitRule
|
||||
),
|
||||
'qos-bandwidth-limit-rule-delete': (
|
||||
bandwidth_limit_rule.DeleteQoSBandwidthLimitRule
|
||||
),
|
||||
'qos-available-rule-types': qos_rule.ListQoSRuleTypes,
|
||||
}
|
||||
|
||||
COMMANDS = {'2.0': COMMAND_V2}
|
||||
|
||||
146
neutronclient/tests/unit/qos/test_cli20_rule.py
Normal file
146
neutronclient/tests/unit/qos/test_cli20_rule.py
Normal file
@@ -0,0 +1,146 @@
|
||||
# Copyright 2015 Huawei Technologies India Pvt Ltd.
|
||||
# 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 sys
|
||||
|
||||
from neutronclient.neutron.v2_0.qos import bandwidth_limit_rule as bw_rule
|
||||
from neutronclient.neutron.v2_0.qos import rule as qos_rule
|
||||
from neutronclient.tests.unit import test_cli20
|
||||
|
||||
|
||||
class CLITestV20QoSRuleJSON(test_cli20.CLITestV20Base):
|
||||
def setUp(self):
|
||||
super(CLITestV20QoSRuleJSON, self).setUp()
|
||||
self.res = 'bandwidth_limit_rule'
|
||||
self.cmd_res = 'qos_bandwidth_limit_rule'
|
||||
self.ress = 'bandwidth_limit_rules'
|
||||
self.cmd_ress = 'qos_bandwidth_limit_rules'
|
||||
|
||||
def test_create_bandwidth_limit_rule_with_max_kbps(self):
|
||||
cmd = bw_rule.CreateQoSBandwidthLimitRule(test_cli20.MyApp(sys.stdout),
|
||||
None)
|
||||
my_id = 'my-id'
|
||||
max_kbps = '1337'
|
||||
policy_id = 'policy_id'
|
||||
args = ['--max-kbps', max_kbps, policy_id]
|
||||
position_names = ['max_kbps']
|
||||
position_values = [max_kbps]
|
||||
self._test_create_resource(self.res, cmd, '', my_id, args,
|
||||
position_names, position_values,
|
||||
cmd_resource=self.cmd_res,
|
||||
parent_id=policy_id)
|
||||
|
||||
def test_create_bandwidth_limit_rule_with_max_burst_kbps(self):
|
||||
cmd = bw_rule.CreateQoSBandwidthLimitRule(test_cli20.MyApp(sys.stdout),
|
||||
None)
|
||||
my_id = 'my-id'
|
||||
max_burst_kbps = '1337'
|
||||
policy_id = 'policy_id'
|
||||
args = ['--max-burst-kbps', max_burst_kbps, policy_id]
|
||||
position_names = ['max_burst_kbps']
|
||||
position_values = [max_burst_kbps]
|
||||
self._test_create_resource(self.res, cmd, '', my_id, args,
|
||||
position_names, position_values,
|
||||
cmd_resource=self.cmd_res,
|
||||
parent_id=policy_id)
|
||||
|
||||
def test_create_bandwidth_limit_rule_with_all_params(self):
|
||||
cmd = bw_rule.CreateQoSBandwidthLimitRule(test_cli20.MyApp(sys.stdout),
|
||||
None)
|
||||
my_id = 'my-id'
|
||||
max_kbps = '1337'
|
||||
max_burst_kbps = '1337'
|
||||
policy_id = 'policy_id'
|
||||
args = ['--max-kbps', max_kbps,
|
||||
'--max-burst-kbps', max_burst_kbps,
|
||||
policy_id]
|
||||
position_names = ['max_kbps', 'max_burst_kbps']
|
||||
position_values = [max_kbps, max_burst_kbps]
|
||||
self._test_create_resource(self.res, cmd, '', my_id, args,
|
||||
position_names, position_values,
|
||||
cmd_resource=self.cmd_res,
|
||||
parent_id=policy_id)
|
||||
|
||||
def test_update_bandwidth_limit_rule_with_max_kbps(self):
|
||||
cmd = bw_rule.UpdateQoSBandwidthLimitRule(test_cli20.MyApp(sys.stdout),
|
||||
None)
|
||||
my_id = 'my-id'
|
||||
max_kbps = '1337'
|
||||
policy_id = 'policy_id'
|
||||
args = ['--max-kbps', max_kbps, my_id, policy_id]
|
||||
self._test_update_resource(self.res, cmd, my_id, args,
|
||||
{'max_kbps': max_kbps, },
|
||||
cmd_resource=self.cmd_res,
|
||||
parent_id=policy_id)
|
||||
|
||||
def test_update_bandwidth_limit_rule_with_max_burst_kbps(self):
|
||||
cmd = bw_rule.UpdateQoSBandwidthLimitRule(test_cli20.MyApp(sys.stdout),
|
||||
None)
|
||||
my_id = 'my-id'
|
||||
max_burst_kbps = '1337'
|
||||
policy_id = 'policy_id'
|
||||
args = ['--max-burst-kbps', max_burst_kbps,
|
||||
my_id, policy_id]
|
||||
self._test_update_resource(self.res, cmd, my_id, args,
|
||||
{'max_burst_kbps': max_burst_kbps},
|
||||
cmd_resource=self.cmd_res,
|
||||
parent_id=policy_id)
|
||||
|
||||
def test_update_bandwidth_limit_rule_with_all_params(self):
|
||||
cmd = bw_rule.UpdateQoSBandwidthLimitRule(test_cli20.MyApp(sys.stdout),
|
||||
None)
|
||||
my_id = 'my-id'
|
||||
max_kbps = '1337'
|
||||
max_burst_kbps = '1337'
|
||||
policy_id = 'policy_id'
|
||||
args = ['--max-kbps', max_kbps,
|
||||
'--max-burst-kbps', max_burst_kbps,
|
||||
my_id, policy_id]
|
||||
self._test_update_resource(self.res, cmd, my_id, args,
|
||||
{'max_kbps': max_kbps,
|
||||
'max_burst_kbps': max_burst_kbps},
|
||||
cmd_resource=self.cmd_res,
|
||||
parent_id=policy_id)
|
||||
|
||||
def test_delete_bandwidth_limit_rule(self):
|
||||
cmd = bw_rule.DeleteQoSBandwidthLimitRule(test_cli20.MyApp(sys.stdout),
|
||||
None)
|
||||
my_id = 'my-id'
|
||||
policy_id = 'policy_id'
|
||||
args = [my_id, policy_id]
|
||||
self._test_delete_resource(self.res, cmd, my_id, args,
|
||||
cmd_resource=self.cmd_res,
|
||||
parent_id=policy_id)
|
||||
|
||||
def test_show_bandwidth_limit_rule(self):
|
||||
cmd = bw_rule.ShowQoSBandwidthLimitRule(test_cli20.MyApp(sys.stdout),
|
||||
None)
|
||||
policy_id = 'policy_id'
|
||||
args = [self.test_id, policy_id]
|
||||
self._test_show_resource(self.res, cmd, self.test_id, args,
|
||||
[], cmd_resource=self.cmd_res,
|
||||
parent_id=policy_id)
|
||||
|
||||
def test_list_qos_rule_types(self):
|
||||
"""qos_rule_types."""
|
||||
resources = 'rule_types'
|
||||
cmd_resources = 'qos_rule_types'
|
||||
response_contents = [{'type': 'bandwidth_limit'}]
|
||||
cmd = qos_rule.ListQoSRuleTypes(test_cli20.MyApp(sys.stdout),
|
||||
None)
|
||||
self._test_list_resources(resources, cmd, True,
|
||||
response_contents=response_contents,
|
||||
cmd_resources=cmd_resources)
|
||||
@@ -48,7 +48,7 @@ non_admin_status_resources = ['subnet', 'floatingip', 'security_group',
|
||||
'metering_label_rule', 'net_partition',
|
||||
'fox_socket', 'subnetpool',
|
||||
'rbac_policy', 'address_scope',
|
||||
'policy']
|
||||
'policy', 'bandwidth_limit_rule']
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
|
||||
@@ -433,6 +433,10 @@ class Client(ClientBase):
|
||||
rbac_policy_path = "/rbac-policies/%s"
|
||||
qos_policies_path = "/qos/policies"
|
||||
qos_policy_path = "/qos/policies/%s"
|
||||
qos_bandwidth_limit_rules_path = "/qos/policies/%s/bandwidth_limit_rules"
|
||||
qos_bandwidth_limit_rule_path = "/qos/policies/%s/bandwidth_limit_rules/%s"
|
||||
qos_rule_types_path = "/qos/rule-types"
|
||||
qos_rule_type_path = "/qos/rule-types/%s"
|
||||
|
||||
# API has no way to report plurals, so we have to hard code them
|
||||
EXTED_PLURALS = {'routers': 'router',
|
||||
@@ -468,6 +472,8 @@ class Client(ClientBase):
|
||||
'address_scopes': 'address_scope',
|
||||
'qos_policies': 'qos_policy',
|
||||
'policies': 'policy',
|
||||
'bandwidth_limit_rules': 'bandwidth_limit_rule',
|
||||
'rule_types': 'rule_type',
|
||||
}
|
||||
|
||||
@APIParamsCall
|
||||
@@ -1693,6 +1699,44 @@ class Client(ClientBase):
|
||||
"""Deletes the specified qos policy."""
|
||||
return self.delete(self.qos_policy_path % qos_policy)
|
||||
|
||||
@APIParamsCall
|
||||
def list_qos_rule_types(self, retrieve_all=True, **_params):
|
||||
"""List available qos rule types."""
|
||||
return self.list('rule_types', self.qos_rule_types_path,
|
||||
retrieve_all, **_params)
|
||||
|
||||
@APIParamsCall
|
||||
def list_bandwidth_limit_rules(self, policy_id,
|
||||
retrieve_all=True, **_params):
|
||||
"""Fetches a list of all qos rules for the given policy."""
|
||||
return self.list('bandwidth_limit_rules',
|
||||
self.qos_bandwidth_limit_rules_path % policy_id,
|
||||
retrieve_all, **_params)
|
||||
|
||||
@APIParamsCall
|
||||
def show_bandwidth_limit_rule(self, rule, policy, body=None):
|
||||
"""Creates a new bandwidth limit rule."""
|
||||
return self.get(self.qos_bandwidth_limit_rule_path %
|
||||
(policy, rule), body=body)
|
||||
|
||||
@APIParamsCall
|
||||
def create_bandwidth_limit_rule(self, policy, body=None):
|
||||
"""Creates a new bandwidth limit rule."""
|
||||
return self.post(self.qos_bandwidth_limit_rules_path % policy,
|
||||
body=body)
|
||||
|
||||
@APIParamsCall
|
||||
def update_bandwidth_limit_rule(self, rule, policy, body=None):
|
||||
"""Updates a bandwidth limit rule."""
|
||||
return self.put(self.qos_bandwidth_limit_rule_path %
|
||||
(policy, rule), body=body)
|
||||
|
||||
@APIParamsCall
|
||||
def delete_bandwidth_limit_rule(self, rule, policy):
|
||||
"""Deletes a bandwidth limit rule."""
|
||||
return self.delete(self.qos_bandwidth_limit_rule_path %
|
||||
(policy, rule))
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
"""Initialize a new client for the Neutron v2.0 API."""
|
||||
super(Client, self).__init__(**kwargs)
|
||||
|
||||
Reference in New Issue
Block a user