Fixed IP address as optional attributes for PT
Change-Id: I19491ce3fc574c974fc6cc1e1858acaed3a55cd3
Partially-implements: blueprint fixed-ips-for-pt
(cherry picked from commit 9fb29c985e
)
This commit is contained in:
@@ -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=<nid>,ip_address=<ip>. '
|
||||
'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=<nid>,ip_address=<ip>. '
|
||||
'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
|
||||
|
||||
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user