| 1 |
# Copyright 2015 OpenStack Foundation. |
| 2 |
# All Rights Reserved |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
# |
| 16 |
|
| 17 |
from neutronclient._i18n import _ |
| 18 |
from neutronclient.common import utils |
| 19 |
from neutronclient.neutron import v2_0 as neutronV20 |
| 20 |
|
| 21 |
|
| 22 |
def _format_prefixes(subnetpool): |
| 23 |
try: |
| 24 |
return '\n'.join(pool for pool in subnetpool['prefixes']) |
| 25 |
except (TypeError, KeyError): |
| 26 |
return subnetpool['prefixes'] |
| 27 |
|
| 28 |
|
| 29 |
def add_updatable_arguments(parser, for_create=False): |
| 30 |
parser.add_argument( |
| 31 |
'--description', |
| 32 |
help=_('Description of subnetpool.')) |
| 33 |
parser.add_argument( |
| 34 |
'--min-prefixlen', type=int, |
| 35 |
help=_('Subnetpool minimum prefix length.')) |
| 36 |
parser.add_argument( |
| 37 |
'--max-prefixlen', type=int, |
| 38 |
help=_('Subnetpool maximum prefix length.')) |
| 39 |
parser.add_argument( |
| 40 |
'--default-prefixlen', type=int, |
| 41 |
help=_('Subnetpool default prefix length.')) |
| 42 |
parser.add_argument( |
| 43 |
'--pool-prefix', |
| 44 |
action='append', dest='prefixes', |
| 45 |
required=for_create, |
| 46 |
help=_('Subnetpool prefixes (This option can be repeated).')) |
| 47 |
utils.add_boolean_argument( |
| 48 |
parser, '--is-default', |
| 49 |
help=_('Specify whether this should be the default subnetpool ' |
| 50 |
'(True meaning default).')) |
| 51 |
|
| 52 |
|
| 53 |
def updatable_args2body(parsed_args, body): |
| 54 |
neutronV20.update_dict(parsed_args, body, |
| 55 |
['name', 'prefixes', 'default_prefixlen', |
| 56 |
'min_prefixlen', 'max_prefixlen', 'is_default', |
| 57 |
'description']) |
| 58 |
|
| 59 |
|
| 60 |
class ListSubnetPool(neutronV20.ListCommand): |
| 61 |
"""List subnetpools that belong to a given tenant.""" |
| 62 |
|
| 63 |
_formatters = {'prefixes': _format_prefixes, } |
| 64 |
resource = 'subnetpool' |
| 65 |
list_columns = ['id', 'name', 'prefixes', |
| 66 |
'default_prefixlen', 'address_scope_id', 'is_default'] |
| 67 |
pagination_support = True |
| 68 |
sorting_support = True |
| 69 |
|
| 70 |
|
| 71 |
class ShowSubnetPool(neutronV20.ShowCommand): |
| 72 |
"""Show information of a given subnetpool.""" |
| 73 |
|
| 74 |
resource = 'subnetpool' |
| 75 |
|
| 76 |
|
| 77 |
class CreateSubnetPool(neutronV20.CreateCommand): |
| 78 |
"""Create a subnetpool for a given tenant.""" |
| 79 |
|
| 80 |
resource = 'subnetpool' |
| 81 |
|
| 82 |
def add_known_arguments(self, parser): |
| 83 |
add_updatable_arguments(parser, for_create=True) |
| 84 |
parser.add_argument( |
| 85 |
'--shared', |
| 86 |
action='store_true', |
| 87 |
help=_('Set the subnetpool as shared.')) |
| 88 |
parser.add_argument( |
| 89 |
'name', |
| 90 |
metavar='NAME', |
| 91 |
help=_('Name of the subnetpool to be created.')) |
| 92 |
parser.add_argument( |
| 93 |
'--address-scope', |
| 94 |
metavar='ADDRSCOPE', |
| 95 |
help=_('ID or name of the address scope with which the subnetpool ' |
| 96 |
'is associated. Prefixes must be unique across address ' |
| 97 |
'scopes.')) |
| 98 |
|
| 99 |
def args2body(self, parsed_args): |
| 100 |
body = {'prefixes': parsed_args.prefixes} |
| 101 |
updatable_args2body(parsed_args, body) |
| 102 |
neutronV20.update_dict(parsed_args, body, ['tenant_id']) |
| 103 |
if parsed_args.shared: |
| 104 |
body['shared'] = True |
| 105 |
|
| 106 |
# Parse and update for "address-scope" option |
| 107 |
if parsed_args.address_scope: |
| 108 |
_addrscope_id = neutronV20.find_resourceid_by_name_or_id( |
| 109 |
self.get_client(), 'address_scope', |
| 110 |
parsed_args.address_scope) |
| 111 |
body['address_scope_id'] = _addrscope_id |
| 112 |
return {'subnetpool': body} |
| 113 |
|
| 114 |
|
| 115 |
class DeleteSubnetPool(neutronV20.DeleteCommand): |
| 116 |
"""Delete a given subnetpool.""" |
| 117 |
|
| 118 |
resource = 'subnetpool' |
| 119 |
|
| 120 |
|
| 121 |
class UpdateSubnetPool(neutronV20.UpdateCommand): |
| 122 |
"""Update subnetpool's information.""" |
| 123 |
|
| 124 |
resource = 'subnetpool' |
| 125 |
|
| 126 |
def add_known_arguments(self, parser): |
| 127 |
add_updatable_arguments(parser) |
| 128 |
parser.add_argument('--name', |
| 129 |
help=_('Updated name of the subnetpool.')) |
| 130 |
addrscope_args = parser.add_mutually_exclusive_group() |
| 131 |
addrscope_args.add_argument('--address-scope', |
| 132 |
metavar='ADDRSCOPE', |
| 133 |
help=_('ID or name of the address scope ' |
| 134 |
'with which the subnetpool is ' |
| 135 |
'associated. Prefixes must be ' |
| 136 |
'unique across address scopes.')) |
| 137 |
addrscope_args.add_argument('--no-address-scope', |
| 138 |
action='store_true', |
| 139 |
help=_('Detach subnetpool from the ' |
| 140 |
'address scope.')) |
| 141 |
|
| 142 |
def args2body(self, parsed_args): |
| 143 |
body = {} |
| 144 |
updatable_args2body(parsed_args, body) |
| 145 |
|
| 146 |
# Parse and update for "address-scope" option/s |
| 147 |
if parsed_args.no_address_scope: |
| 148 |
body['address_scope_id'] = None |
| 149 |
elif parsed_args.address_scope: |
| 150 |
_addrscope_id = neutronV20.find_resourceid_by_name_or_id( |
| 151 |
self.get_client(), 'address_scope', |
| 152 |
parsed_args.address_scope) |
| 153 |
body['address_scope_id'] = _addrscope_id |
| 154 |
return {'subnetpool': body} |