| 1 |
# Copyright 2012 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 |
import argparse |
| 18 |
|
| 19 |
from oslo_serialization import jsonutils |
| 20 |
|
| 21 |
from neutronclient._i18n import _ |
| 22 |
from neutronclient.common import exceptions |
| 23 |
from neutronclient.common import utils |
| 24 |
from neutronclient.neutron import v2_0 as neutronV20 |
| 25 |
from neutronclient.neutron.v2_0 import dns |
| 26 |
from neutronclient.neutron.v2_0.qos import policy as qos_policy |
| 27 |
|
| 28 |
|
| 29 |
def _format_fixed_ips(port): |
| 30 |
try: |
| 31 |
return '\n'.join([jsonutils.dumps(ip) for ip in port['fixed_ips']]) |
| 32 |
except (TypeError, KeyError): |
| 33 |
return '' |
| 34 |
|
| 35 |
|
| 36 |
def _add_updatable_args(parser): |
| 37 |
parser.add_argument( |
| 38 |
'--name', |
| 39 |
help=_('Name of this port.')) |
| 40 |
parser.add_argument( |
| 41 |
'--description', |
| 42 |
help=_('Description of this port.')) |
| 43 |
parser.add_argument( |
| 44 |
'--fixed-ip', metavar='subnet_id=SUBNET,ip_address=IP_ADDR', |
| 45 |
action='append', |
| 46 |
type=utils.str2dict_type(optional_keys=['subnet_id', 'ip_address']), |
| 47 |
help=_('Desired IP and/or subnet for this port: ' |
| 48 |
'subnet_id=<name_or_id>,ip_address=<ip>. ' |
| 49 |
'You can repeat this option.')) |
| 50 |
parser.add_argument( |
| 51 |
'--fixed_ip', |
| 52 |
action='append', |
| 53 |
help=argparse.SUPPRESS) |
| 54 |
parser.add_argument( |
| 55 |
'--device-id', |
| 56 |
help=_('Device ID of this port.')) |
| 57 |
parser.add_argument( |
| 58 |
'--device_id', |
| 59 |
help=argparse.SUPPRESS) |
| 60 |
parser.add_argument( |
| 61 |
'--device-owner', |
| 62 |
help=_('Device owner of this port.')) |
| 63 |
parser.add_argument( |
| 64 |
'--device_owner', |
| 65 |
help=argparse.SUPPRESS) |
| 66 |
|
| 67 |
|
| 68 |
def _updatable_args2body(parsed_args, body, client): |
| 69 |
neutronV20.update_dict(parsed_args, body, |
| 70 |
['device_id', 'device_owner', 'name', |
| 71 |
'description']) |
| 72 |
ips = [] |
| 73 |
if parsed_args.fixed_ip: |
| 74 |
for ip_spec in parsed_args.fixed_ip: |
| 75 |
if 'subnet_id' in ip_spec: |
| 76 |
subnet_name_id = ip_spec['subnet_id'] |
| 77 |
_subnet_id = neutronV20.find_resourceid_by_name_or_id( |
| 78 |
client, 'subnet', subnet_name_id) |
| 79 |
ip_spec['subnet_id'] = _subnet_id |
| 80 |
ips.append(ip_spec) |
| 81 |
if ips: |
| 82 |
body['fixed_ips'] = ips |
| 83 |
|
| 84 |
|
| 85 |
class ListPort(neutronV20.ListCommand): |
| 86 |
"""List ports that belong to a given tenant.""" |
| 87 |
|
| 88 |
resource = 'port' |
| 89 |
_formatters = {'fixed_ips': _format_fixed_ips, } |
| 90 |
list_columns = ['id', 'name', 'mac_address', 'fixed_ips'] |
| 91 |
pagination_support = True |
| 92 |
sorting_support = True |
| 93 |
|
| 94 |
|
| 95 |
class ListRouterPort(neutronV20.ListCommand): |
| 96 |
"""List ports that belong to a given tenant, with specified router.""" |
| 97 |
|
| 98 |
resource = 'port' |
| 99 |
_formatters = {'fixed_ips': _format_fixed_ips, } |
| 100 |
list_columns = ['id', 'name', 'mac_address', 'fixed_ips'] |
| 101 |
pagination_support = True |
| 102 |
sorting_support = True |
| 103 |
|
| 104 |
def get_parser(self, prog_name): |
| 105 |
parser = super(ListRouterPort, self).get_parser(prog_name) |
| 106 |
parser.add_argument( |
| 107 |
'id', metavar='ROUTER', |
| 108 |
help=_('ID or name of the router to look up.')) |
| 109 |
return parser |
| 110 |
|
| 111 |
def take_action(self, parsed_args): |
| 112 |
neutron_client = self.get_client() |
| 113 |
_id = neutronV20.find_resourceid_by_name_or_id( |
| 114 |
neutron_client, 'router', parsed_args.id) |
| 115 |
self.values_specs.append('--device_id=%s' % _id) |
| 116 |
return super(ListRouterPort, self).take_action(parsed_args) |
| 117 |
|
| 118 |
|
| 119 |
class ShowPort(neutronV20.ShowCommand): |
| 120 |
"""Show information of a given port.""" |
| 121 |
|
| 122 |
resource = 'port' |
| 123 |
|
| 124 |
|
| 125 |
class UpdatePortSecGroupMixin(object): |
| 126 |
def add_arguments_secgroup(self, parser): |
| 127 |
group_sg = parser.add_mutually_exclusive_group() |
| 128 |
group_sg.add_argument( |
| 129 |
'--security-group', metavar='SECURITY_GROUP', |
| 130 |
default=[], action='append', dest='security_groups', |
| 131 |
help=_('Security group associated with the port. You can ' |
| 132 |
'repeat this option.')) |
| 133 |
group_sg.add_argument( |
| 134 |
'--no-security-groups', |
| 135 |
action='store_true', |
| 136 |
help=_('Associate no security groups with the port.')) |
| 137 |
|
| 138 |
def _resolv_sgid(self, secgroup): |
| 139 |
return neutronV20.find_resourceid_by_name_or_id( |
| 140 |
self.get_client(), 'security_group', secgroup) |
| 141 |
|
| 142 |
def args2body_secgroup(self, parsed_args, port): |
| 143 |
if parsed_args.security_groups: |
| 144 |
port['security_groups'] = [self._resolv_sgid(sg) for sg |
| 145 |
in parsed_args.security_groups] |
| 146 |
elif parsed_args.no_security_groups: |
| 147 |
port['security_groups'] = [] |
| 148 |
|
| 149 |
|
| 150 |
class UpdateExtraDhcpOptMixin(object): |
| 151 |
def add_arguments_extradhcpopt(self, parser): |
| 152 |
group_sg = parser.add_mutually_exclusive_group() |
| 153 |
group_sg.add_argument( |
| 154 |
'--extra-dhcp-opt', |
| 155 |
default=[], |
| 156 |
action='append', |
| 157 |
dest='extra_dhcp_opts', |
| 158 |
type=utils.str2dict_type( |
| 159 |
required_keys=['opt_name'], |
| 160 |
optional_keys=['opt_value', 'ip_version']), |
| 161 |
help=_('Extra dhcp options to be assigned to this port: ' |
| 162 |
'opt_name=<dhcp_option_name>,opt_value=<value>,' |
| 163 |
'ip_version={4,6}. You can repeat this option.')) |
| 164 |
|
| 165 |
def args2body_extradhcpopt(self, parsed_args, port): |
| 166 |
ops = [] |
| 167 |
if parsed_args.extra_dhcp_opts: |
| 168 |
# the extra_dhcp_opt params (opt_name & opt_value) |
| 169 |
# must come in pairs, if there is a parm error |
| 170 |
# both must be thrown out. |
| 171 |
opt_ele = {} |
| 172 |
edo_err_msg = _("Invalid --extra-dhcp-opt option, can only be: " |
| 173 |
"opt_name=<dhcp_option_name>,opt_value=<value>," |
| 174 |
"ip_version={4,6}. " |
| 175 |
"You can repeat this option.") |
| 176 |
for opt in parsed_args.extra_dhcp_opts: |
| 177 |
opt_ele.update(opt) |
| 178 |
if ('opt_name' in opt_ele and |
| 179 |
('opt_value' in opt_ele or 'ip_version' in opt_ele)): |
| 180 |
if opt_ele.get('opt_value') == 'null': |
| 181 |
opt_ele['opt_value'] = None |
| 182 |
ops.append(opt_ele) |
| 183 |
opt_ele = {} |
| 184 |
else: |
| 185 |
raise exceptions.CommandError(edo_err_msg) |
| 186 |
|
| 187 |
if ops: |
| 188 |
port['extra_dhcp_opts'] = ops |
| 189 |
|
| 190 |
|
| 191 |
class UpdatePortAllowedAddressPair(object): |
| 192 |
"""Update Port for allowed_address_pairs""" |
| 193 |
|
| 194 |
def add_arguments_allowedaddresspairs(self, parser): |
| 195 |
group_aap = parser.add_mutually_exclusive_group() |
| 196 |
group_aap.add_argument( |
| 197 |
'--allowed-address-pair', |
| 198 |
metavar='ip_address=IP_ADDR|CIDR[,mac_address=MAC_ADDR]', |
| 199 |
default=[], |
| 200 |
action='append', |
| 201 |
dest='allowed_address_pairs', |
| 202 |
type=utils.str2dict_type( |
| 203 |
required_keys=['ip_address'], |
| 204 |
optional_keys=['mac_address']), |
| 205 |
help=_('Allowed address pair associated with the port. ' |
| 206 |
'"ip_address" parameter is required. IP address or ' |
| 207 |
'CIDR can be specified for "ip_address". ' |
| 208 |
'"mac_address" parameter is optional. ' |
| 209 |
'You can repeat this option.')) |
| 210 |
group_aap.add_argument( |
| 211 |
'--no-allowed-address-pairs', |
| 212 |
action='store_true', |
| 213 |
help=_('Associate no allowed address pairs with the port.')) |
| 214 |
|
| 215 |
def args2body_allowedaddresspairs(self, parsed_args, port): |
| 216 |
if parsed_args.allowed_address_pairs: |
| 217 |
port['allowed_address_pairs'] = parsed_args.allowed_address_pairs |
| 218 |
elif parsed_args.no_allowed_address_pairs: |
| 219 |
port['allowed_address_pairs'] = [] |
| 220 |
|
| 221 |
|
| 222 |
class CreatePort(neutronV20.CreateCommand, UpdatePortSecGroupMixin, |
| 223 |
UpdateExtraDhcpOptMixin, qos_policy.CreateQosPolicyMixin, |
| 224 |
UpdatePortAllowedAddressPair): |
| 225 |
"""Create a port for a given tenant.""" |
| 226 |
|
| 227 |
resource = 'port' |
| 228 |
|
| 229 |
def add_known_arguments(self, parser): |
| 230 |
_add_updatable_args(parser) |
| 231 |
parser.add_argument( |
| 232 |
'--admin-state-down', |
| 233 |
dest='admin_state', action='store_false', |
| 234 |
help=_('Set admin state up to false.')) |
| 235 |
parser.add_argument( |
| 236 |
'--admin_state_down', |
| 237 |
dest='admin_state', action='store_false', |
| 238 |
help=argparse.SUPPRESS) |
| 239 |
parser.add_argument( |
| 240 |
'--mac-address', |
| 241 |
help=_('MAC address of this port.')) |
| 242 |
parser.add_argument( |
| 243 |
'--mac_address', |
| 244 |
help=argparse.SUPPRESS) |
| 245 |
parser.add_argument( |
| 246 |
'--vnic-type', |
| 247 |
metavar='<direct | direct-physical | macvtap ' |
| 248 |
'| normal | baremetal | smart-nic>', |
| 249 |
choices=['direct', 'direct-physical', 'macvtap', |
| 250 |
'normal', 'baremetal', 'smart-nic'], |
| 251 |
type=utils.convert_to_lowercase, |
| 252 |
help=_('VNIC type for this port.')) |
| 253 |
parser.add_argument( |
| 254 |
'--vnic_type', |
| 255 |
choices=['direct', 'direct-physical', 'macvtap', |
| 256 |
'normal', 'baremetal', 'smart-nic'], |
| 257 |
type=utils.convert_to_lowercase, |
| 258 |
help=argparse.SUPPRESS) |
| 259 |
parser.add_argument( |
| 260 |
'--binding-profile', |
| 261 |
help=_('Custom data to be passed as binding:profile.')) |
| 262 |
parser.add_argument( |
| 263 |
'--binding_profile', |
| 264 |
help=argparse.SUPPRESS) |
| 265 |
self.add_arguments_secgroup(parser) |
| 266 |
self.add_arguments_extradhcpopt(parser) |
| 267 |
self.add_arguments_qos_policy(parser) |
| 268 |
self.add_arguments_allowedaddresspairs(parser) |
| 269 |
|
| 270 |
parser.add_argument( |
| 271 |
'network_id', metavar='NETWORK', |
| 272 |
help=_('ID or name of the network this port belongs to.')) |
| 273 |
dns.add_dns_argument_create(parser, self.resource, 'name') |
| 274 |
|
| 275 |
def args2body(self, parsed_args): |
| 276 |
client = self.get_client() |
| 277 |
_network_id = neutronV20.find_resourceid_by_name_or_id( |
| 278 |
client, 'network', parsed_args.network_id) |
| 279 |
body = {'admin_state_up': parsed_args.admin_state, |
| 280 |
'network_id': _network_id, } |
| 281 |
_updatable_args2body(parsed_args, body, client) |
| 282 |
neutronV20.update_dict(parsed_args, body, |
| 283 |
['mac_address', 'tenant_id']) |
| 284 |
if parsed_args.vnic_type: |
| 285 |
body['binding:vnic_type'] = parsed_args.vnic_type |
| 286 |
if parsed_args.binding_profile: |
| 287 |
body['binding:profile'] = jsonutils.loads( |
| 288 |
parsed_args.binding_profile) |
| 289 |
|
| 290 |
self.args2body_secgroup(parsed_args, body) |
| 291 |
self.args2body_extradhcpopt(parsed_args, body) |
| 292 |
self.args2body_qos_policy(parsed_args, body) |
| 293 |
self.args2body_allowedaddresspairs(parsed_args, body) |
| 294 |
dns.args2body_dns_create(parsed_args, body, 'name') |
| 295 |
|
| 296 |
return {'port': body} |
| 297 |
|
| 298 |
|
| 299 |
class DeletePort(neutronV20.DeleteCommand): |
| 300 |
"""Delete a given port.""" |
| 301 |
|
| 302 |
resource = 'port' |
| 303 |
|
| 304 |
|
| 305 |
class UpdatePort(neutronV20.UpdateCommand, UpdatePortSecGroupMixin, |
| 306 |
UpdateExtraDhcpOptMixin, qos_policy.UpdateQosPolicyMixin, |
| 307 |
UpdatePortAllowedAddressPair): |
| 308 |
"""Update port's information.""" |
| 309 |
|
| 310 |
resource = 'port' |
| 311 |
|
| 312 |
def add_known_arguments(self, parser): |
| 313 |
_add_updatable_args(parser) |
| 314 |
parser.add_argument( |
| 315 |
'--admin-state-up', |
| 316 |
choices=['True', 'False'], |
| 317 |
help=_('Set admin state up for the port.')) |
| 318 |
parser.add_argument( |
| 319 |
'--admin_state_up', |
| 320 |
choices=['True', 'False'], |
| 321 |
help=argparse.SUPPRESS) |
| 322 |
self.add_arguments_secgroup(parser) |
| 323 |
self.add_arguments_extradhcpopt(parser) |
| 324 |
self.add_arguments_qos_policy(parser) |
| 325 |
self.add_arguments_allowedaddresspairs(parser) |
| 326 |
dns.add_dns_argument_update(parser, self.resource, 'name') |
| 327 |
|
| 328 |
def args2body(self, parsed_args): |
| 329 |
body = {} |
| 330 |
client = self.get_client() |
| 331 |
_updatable_args2body(parsed_args, body, client) |
| 332 |
if parsed_args.admin_state_up: |
| 333 |
body['admin_state_up'] = parsed_args.admin_state_up |
| 334 |
|
| 335 |
self.args2body_secgroup(parsed_args, body) |
| 336 |
self.args2body_extradhcpopt(parsed_args, body) |
| 337 |
self.args2body_qos_policy(parsed_args, body) |
| 338 |
self.args2body_allowedaddresspairs(parsed_args, body) |
| 339 |
dns.args2body_dns_update(parsed_args, body, 'name') |
| 340 |
|
| 341 |
return {'port': body} |