From 29054a5d050e4337faac29e067c515bda357204c Mon Sep 17 00:00:00 2001 From: Sumit Naiksatam Date: Mon, 2 May 2016 01:37:07 -0700 Subject: [PATCH] Fixed IP address as optional attributes for PT Change-Id: I19491ce3fc574c974fc6cc1e1858acaed3a55cd3 Partially-implements: blueprint fixed-ips-for-pt (cherry picked from commit 9fb29c985e0b305ffeaba5de772433621fd2c96f) --- gbpclient/gbp/v2_0/groupbasedpolicy.py | 42 ++++++++++++++++++- .../tests/unit/test_cli20_policytarget.py | 26 ++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/gbpclient/gbp/v2_0/groupbasedpolicy.py b/gbpclient/gbp/v2_0/groupbasedpolicy.py index 091ae72..00d1409 100644 --- a/gbpclient/gbp/v2_0/groupbasedpolicy.py +++ b/gbpclient/gbp/v2_0/groupbasedpolicy.py @@ -11,6 +11,7 @@ # under the License. # +import argparse import logging from neutronclient.common import utils as n_utils @@ -21,6 +22,13 @@ from oslo_serialization import jsonutils from gbpclient.common import utils +def _format_fixed_ips(pt): + try: + return '\n'.join([jsonutils.dumps(ip) for ip in pt['fixed_ips']]) + except (TypeError, KeyError): + return '' + + def _format_network_service_params(net_svc_policy): try: return '\n'.join([jsonutils.dumps(param) for param in @@ -42,9 +50,9 @@ class ListPolicyTarget(neutronV20.ListCommand): resource = 'policy_target' log = logging.getLogger(__name__ + '.ListPolicyTarget') - _formatters = {} + _formatters = {'fixed_ips': _format_fixed_ips, } list_columns = ['id', 'name', 'description', 'policy_target_group_id', - 'port_id'] + 'port_id', 'fixed_ips'] pagination_support = True sorting_support = True @@ -69,6 +77,14 @@ class CreatePolicyTarget(neutronV20.CreateCommand): parser.add_argument( '--policy-target-group', help=_('Policy Target Group (required argument)')) + parser.add_argument( + '--fixed-ip', metavar='subnet_id=SUBNET,ip_address=IP_ADDR', + action='append', + help=_('Desired IP and/or subnet for this Policy Target: ' + 'subnet_id=,ip_address=. ' + 'You can repeat this option.')) + parser.add_argument( + '--fixed_ip', action='append', help=argparse.SUPPRESS) parser.add_argument( '--port-id', default='', help=_('Neutron Port UUID')) @@ -91,6 +107,13 @@ class CreatePolicyTarget(neutronV20.CreateCommand): body[self.resource]['port_id'] = ( parsed_args.port_id) + ips = [] + if parsed_args.fixed_ip: + for ip_spec in parsed_args.fixed_ip: + ip_dict = n_utils.str2dict(ip_spec) + ips.append(ip_dict) + body[self.resource]['fixed_ips'] = ips + return body @@ -114,6 +137,14 @@ class UpdatePolicyTarget(neutronV20.UpdateCommand): parser.add_argument( '--name', help=_('New name of the Policy Target')) + parser.add_argument( + '--fixed-ip', metavar='subnet_id=SUBNET,ip_address=IP_ADDR', + action='append', + help=_('Desired IP and/or subnet for this Policy Target: ' + 'subnet_id=,ip_address=. ' + 'You can repeat this option.')) + parser.add_argument( + '--fixed_ip', action='append', help=argparse.SUPPRESS) def args2body(self, parsed_args): body = {self.resource: {}, } @@ -121,6 +152,13 @@ class UpdatePolicyTarget(neutronV20.UpdateCommand): neutronV20.update_dict(parsed_args, body[self.resource], ['name', 'tenant_id', 'description']) + ips = [] + if parsed_args.fixed_ip: + for ip_spec in parsed_args.fixed_ip: + ip_dict = n_utils.str2dict(ip_spec) + ips.append(ip_dict) + body[self.resource]['fixed_ips'] = ips + return body diff --git a/gbpclient/tests/unit/test_cli20_policytarget.py b/gbpclient/tests/unit/test_cli20_policytarget.py index 2895a85..debfd49 100644 --- a/gbpclient/tests/unit/test_cli20_policytarget.py +++ b/gbpclient/tests/unit/test_cli20_policytarget.py @@ -44,6 +44,17 @@ class CLITestV20PolicyTargetJSON(test_cli20.CLITestV20Base): cmd = gbp.ListPolicyTarget(test_cli20.MyApp(sys.stdout), None) self._test_list_resources(resource, cmd, True) + def test_list_policy_targets_with_fixed_ips(self): + resources = "policy_targets" + cmd = gbp.ListPolicyTarget(test_cli20.MyApp(sys.stdout), None) + fixed_ips = [{"subnet_id": "30422057-d6df-4c90-8314-aefb5e326666", + "ip_address": "10.0.0.12"}, + {"subnet_id": "30422057-d6df-4c90-8314-aefb5e326666", + "ip_address": "10.0.0.4"}] + contents = [{'name': 'name1', 'fixed_ips': fixed_ips}] + self._test_list_resources(resources, cmd, True, + response_contents=contents) + def test_show_policy_target_name(self): resource = 'policy_target' cmd = gbp.ShowPolicyTarget(test_cli20.MyApp(sys.stdout), None) @@ -58,6 +69,21 @@ class CLITestV20PolicyTargetJSON(test_cli20.CLITestV20Base): '--tags', 'a', 'b'], {'name': 'myname', 'tags': ['a', 'b'], }) + def test_update_policy_target_fixed_ip(self): + resource = 'policy_target' + cmd = gbp.UpdatePolicyTarget(test_cli20.MyApp(sys.stdout), None) + myid = 'myid' + subnet_id = 'subnet_id' + ip_addr = '123.123.123.123' + args = [myid, + '--fixed-ip', + "subnet_id=%(subnet_id)s,ip_address=%(ip_addr)s" % + {'subnet_id': subnet_id, + 'ip_addr': ip_addr}] + updated_fields = {"fixed_ips": [{'subnet_id': subnet_id, + 'ip_address': ip_addr}]} + self._test_update_resource(resource, cmd, myid, args, updated_fields) + def test_delete_policy_target_name(self): resource = 'policy_target' cmd = gbp.DeletePolicyTarget(test_cli20.MyApp(sys.stdout), None)