Merge "CLI changes for adding --name argument to policy rule"

This commit is contained in:
Jenkins
2015-01-22 03:45:12 +00:00
committed by Gerrit Code Review
2 changed files with 71 additions and 11 deletions

View File

@@ -20,6 +20,7 @@ import sys
from cliff import command
from cliff import lister
from cliff import show
from keystoneclient.openstack.common.apiclient import exceptions
import six
from congressclient.common import utils
@@ -36,6 +37,23 @@ def _format_rule(rule):
return formatted_string
def get_rule_id_from_name(client, parsed_args):
results = client.list_policy_rules(parsed_args.policy_name)['results']
rule_id = None
for result in results:
if result.get('name') == parsed_args.rule_id:
if rule_id is None:
rule_id = result.get('id')
else:
raise exceptions.Conflict(
"[Multiple rules with same name: %s]" %
parsed_args.rule_id)
if rule_id is None:
raise exceptions.NotFound(
"[No rule found with name: %s]" % parsed_args.rule_id)
return rule_id
class CreatePolicyRule(show.ShowOne):
"""Create a policy rule."""
@@ -51,13 +69,17 @@ class CreatePolicyRule(show.ShowOne):
'rule',
metavar="<rule>",
help="Policy rule")
parser.add_argument(
'--name', dest="rule_name",
help="Name of the policy rule")
return parser
def take_action(self, parsed_args):
self.log.debug('take_action(%s)' % parsed_args)
client = self.app.client_manager.congressclient
body = {'rule': parsed_args.rule}
if parsed_args.rule_name:
body['name'] = parsed_args.rule_name
data = client.create_policy_rule(parsed_args.policy_name, body)
return zip(*sorted(six.iteritems(data)))
@@ -75,15 +97,20 @@ class DeletePolicyRule(command.Command):
help="Name of the policy to delete")
parser.add_argument(
'rule_id',
metavar="<rule-id>",
help="ID of the policy to delete")
metavar="<rule-id/rule-name>",
help="ID/Name of the policy rule to delete")
return parser
def take_action(self, parsed_args):
self.log.debug('take_action(%s)' % parsed_args)
client = self.app.client_manager.congressclient
client.delete_policy_rule(parsed_args.policy_name,
parsed_args.rule_id)
try:
client.delete_policy_rule(parsed_args.policy_name,
parsed_args.rule_id)
except exceptions.NotFound:
rule_id = get_rule_id_from_name(client, parsed_args)
if rule_id is not None:
client.delete_policy_rule(parsed_args.policy_name, rule_id)
class ListPolicyRules(command.Command):
@@ -105,6 +132,7 @@ class ListPolicyRules(command.Command):
results = client.list_policy_rules(parsed_args.policy_name)['results']
for result in results:
print("// ID: %s" % str(result['id']))
print("// Name: %s" % str(result.get('name')))
if result['comment'] != "None" and result['comment']:
print("// %s" % str(result['comment']))
print(result['rule'])
@@ -333,15 +361,21 @@ class ShowPolicyRule(show.ShowOne):
metavar="<policy-name>",
help="Name or identifier of the policy")
parser.add_argument(
'id',
metavar="<rule>",
help="Policy rule id")
'rule_id',
metavar="<rule-id/rule-name>",
help="Policy rule id or rule name")
return parser
def take_action(self, parsed_args):
self.log.debug('take_action(%s)' % parsed_args)
client = self.app.client_manager.congressclient
data = client.show_policy_rule(parsed_args.policy_name,
parsed_args.id)
try:
data = client.show_policy_rule(parsed_args.policy_name,
parsed_args.rule_id)
except exceptions.NotFound:
rule_id = get_rule_id_from_name(client, parsed_args)
if rule_id is not None:
data = client.show_policy_rule(parsed_args.policy_name,
rule_id)
return zip(*sorted(six.iteritems(data)))

View File

@@ -90,6 +90,32 @@ class TestCreatePolicyRule(common.TestCongressBase):
('None', 'e531f2b3-3d97-42c0-b3b5-b7b6ab532018', rule)]
self.assertEqual(filtered, result)
def test_create_policy_rule_with_name(self):
policy_name = "classification"
rule = "p(x) :- q(x)"
rule_name = "classification_rule"
response = {"comment": "None",
"id": "e531f2b3-3d97-42c0-b3b5-b7b6ab532018",
"rule": rule,
"name": rule_name}
arglist = ["--name", rule_name, policy_name, rule]
verifylist = [
('policy_name', policy_name),
('rule', rule),
("rule_name", rule_name)
]
mocker = mock.Mock(return_value=response)
self.app.client_manager.congressclient.create_policy_rule = mocker
cmd = policy.CreatePolicyRule(self.app, self.namespace)
parsed_args = self.check_parser(cmd, arglist, verifylist)
result = list(cmd.take_action(parsed_args))
filtered = [('comment', 'id', 'name', 'rule'),
('None', 'e531f2b3-3d97-42c0-b3b5-b7b6ab532018', rule_name,
rule)]
self.assertEqual(filtered, result)
class TestDeletePolicyRule(common.TestCongressBase):
def test_delete_policy_rule(self):
@@ -357,7 +383,7 @@ class TestGet(common.TestCongressBase):
arglist = [policy_name, id]
verifylist = [
('policy_name', policy_name),
('id', id),
('rule_id', id),
]
mocker = mock.Mock(return_value=response)