1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
"""Floating IP Port Forwarding action implementations"""
import logging
from osc_lib.command import command
from osc_lib import exceptions
from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.network import common
LOG = logging.getLogger(__name__)
def validate_ports_diff(ports):
if len(ports) == 0:
return 0
ports_diff = ports[-1] - ports[0]
if ports_diff < 0:
msg = _(
"The last number in port range must be"
" greater or equal to the first"
)
raise exceptions.CommandError(msg)
return ports_diff
def validate_ports_match(internal_ports, external_ports):
internal_ports_diff = validate_ports_diff(internal_ports)
external_ports_diff = validate_ports_diff(external_ports)
if internal_ports_diff != 0 and internal_ports_diff != external_ports_diff:
msg = _(
"The relation between internal and external ports does not "
"match the pattern 1:N and N:N"
)
raise exceptions.CommandError(msg)
def validate_and_assign_port_ranges(parsed_args, attrs):
internal_port_range = parsed_args.internal_protocol_port
external_port_range = parsed_args.external_protocol_port
external_ports = internal_ports = []
if external_port_range:
external_ports = list(map(int, str(external_port_range).split(':')))
if internal_port_range:
internal_ports = list(map(int, str(internal_port_range).split(':')))
validate_ports_match(internal_ports, external_ports)
for port in external_ports + internal_ports:
validate_port(port)
if internal_port_range:
if ':' in internal_port_range:
attrs['internal_port_range'] = internal_port_range
else:
attrs['internal_port'] = int(internal_port_range)
if external_port_range:
if ':' in external_port_range:
attrs['external_port_range'] = external_port_range
else:
attrs['external_port'] = int(external_port_range)
def validate_port(port):
if port <= 0 or port > 65535:
msg = _("The port number range is <1-65535>")
raise exceptions.CommandError(msg)
def _get_columns(item):
column_map = {}
hidden_columns = ['location', 'tenant_id']
return utils.get_osc_show_columns_for_sdk_resource(
item, column_map, hidden_columns
)
class CreateFloatingIPPortForwarding(
command.ShowOne, common.NeutronCommandWithExtraArgs
):
_description = _("Create floating IP port forwarding")
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
'--internal-ip-address',
required=True,
metavar='<internal-ip-address>',
help=_(
"The fixed IPv4 address of the network "
"port associated to the floating IP port forwarding"
),
)
parser.add_argument(
'--port',
metavar='<port>',
required=True,
help=_(
"The name or ID of the network port associated "
"to the floating IP port forwarding"
),
)
parser.add_argument(
'--internal-protocol-port',
metavar='<port-number>',
required=True,
help=_(
"The protocol port number "
"of the network port fixed IPv4 address "
"associated to the floating IP port forwarding"
),
)
parser.add_argument(
'--external-protocol-port',
metavar='<port-number>',
required=True,
help=_(
"The protocol port number of "
"the port forwarding's floating IP address"
),
)
parser.add_argument(
'--protocol',
metavar='<protocol>',
required=True,
help=_(
"The protocol used in the floating IP "
"port forwarding, for instance: TCP, UDP"
),
)
parser.add_argument(
'--description',
metavar='<description>',
help=_(
"A text to describe/contextualize the use of the "
"port forwarding configuration"
),
)
parser.add_argument(
'floating_ip',
metavar='<floating-ip>',
help=_(
"Floating IP that the port forwarding belongs to "
"(IP address or ID)"
),
)
return parser
def take_action(self, parsed_args):
attrs = {}
client = self.app.client_manager.network
floating_ip = client.find_ip(
parsed_args.floating_ip,
ignore_missing=False,
)
validate_and_assign_port_ranges(parsed_args, attrs)
if parsed_args.port:
port = client.find_port(parsed_args.port, ignore_missing=False)
attrs['internal_port_id'] = port.id
attrs['internal_ip_address'] = parsed_args.internal_ip_address
attrs['protocol'] = parsed_args.protocol
if parsed_args.description is not None:
attrs['description'] = parsed_args.description
attrs.update(
self._parse_extra_properties(parsed_args.extra_properties)
)
obj = client.create_floating_ip_port_forwarding(
floating_ip.id, **attrs
)
display_columns, columns = _get_columns(obj)
data = utils.get_item_properties(obj, columns)
return (display_columns, data)
class DeleteFloatingIPPortForwarding(command.Command):
_description = _("Delete floating IP port forwarding")
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
'floating_ip',
metavar='<floating-ip>',
help=_(
"Floating IP that the port forwarding belongs to "
"(IP address or ID)"
),
)
parser.add_argument(
'port_forwarding_id',
nargs="+",
metavar="<port-forwarding-id>",
help=_("The ID of the floating IP port forwarding(s) to delete"),
)
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.network
floating_ip = client.find_ip(
parsed_args.floating_ip,
ignore_missing=False,
)
result = 0
for port_forwarding_id in parsed_args.port_forwarding_id:
try:
client.delete_floating_ip_port_forwarding(
floating_ip.id,
port_forwarding_id,
ignore_missing=False,
)
except Exception as e:
result += 1
LOG.error(
_(
"Failed to delete floating IP port forwarding "
"'%(port_forwarding_id)s': %(e)s"
),
{'port_forwarding_id': port_forwarding_id, 'e': e},
)
if result > 0:
total = len(parsed_args.port_forwarding_id)
msg = _(
"%(result)s of %(total)s Port forwarding failed " "to delete."
) % {'result': result, 'total': total}
raise exceptions.CommandError(msg)
class ListFloatingIPPortForwarding(command.Lister):
_description = _("List floating IP port forwarding")
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
'floating_ip',
metavar='<floating-ip>',
help=_(
"Floating IP that the port forwarding belongs to "
"(IP address or ID)"
),
)
parser.add_argument(
'--port',
metavar='<port>',
help=_(
"Filter the list result by the ID or name of "
"the internal network port"
),
)
parser.add_argument(
'--external-protocol-port',
metavar='<port-number>',
dest='external_protocol_port',
help=_(
"Filter the list result by the "
"protocol port number of the floating IP"
),
)
parser.add_argument(
'--protocol',
metavar='protocol',
help=_("Filter the list result by the port protocol"),
)
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.network
columns = (
'id',
'internal_port_id',
'internal_ip_address',
'internal_port',
'internal_port_range',
'external_port',
'external_port_range',
'protocol',
'description',
)
headers = (
'ID',
'Internal Port ID',
'Internal IP Address',
'Internal Port',
'Internal Port Range',
'External Port',
'External Port Range',
'Protocol',
'Description',
)
query = {}
if parsed_args.port:
port = client.find_port(parsed_args.port, ignore_missing=False)
query['internal_port_id'] = port.id
external_port = parsed_args.external_protocol_port
if external_port:
if ':' in external_port:
query['external_port_range'] = external_port
else:
query['external_port'] = int(
parsed_args.external_protocol_port
)
if parsed_args.protocol is not None:
query['protocol'] = parsed_args.protocol
obj = client.find_ip(
parsed_args.floating_ip,
ignore_missing=False,
)
data = client.floating_ip_port_forwardings(obj, **query)
return (
headers,
(
utils.get_item_properties(
s,
columns,
formatters={},
)
for s in data
),
)
class SetFloatingIPPortForwarding(common.NeutronCommandWithExtraArgs):
_description = _("Set floating IP Port Forwarding Properties")
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
'floating_ip',
metavar='<floating-ip>',
help=_(
"Floating IP that the port forwarding belongs to "
"(IP address or ID)"
),
)
parser.add_argument(
'port_forwarding_id',
metavar='<port-forwarding-id>',
help=_("The ID of the floating IP port forwarding"),
)
parser.add_argument(
'--port',
metavar='<port>',
help=_(
"The ID of the network port associated to "
"the floating IP port forwarding"
),
)
parser.add_argument(
'--internal-ip-address',
metavar='<internal-ip-address>',
help=_(
"The fixed IPv4 address of the network port "
"associated to the floating IP port forwarding"
),
)
parser.add_argument(
'--internal-protocol-port',
metavar='<port-number>',
help=_(
"The TCP/UDP/other protocol port number of the "
"network port fixed IPv4 address associated to "
"the floating IP port forwarding"
),
)
parser.add_argument(
'--external-protocol-port',
metavar='<port-number>',
help=_(
"The TCP/UDP/other protocol port number of the "
"port forwarding's floating IP address"
),
)
parser.add_argument(
'--protocol',
metavar='<protocol>',
choices=['tcp', 'udp'],
help=_("The IP protocol used in the floating IP port forwarding"),
)
parser.add_argument(
'--description',
metavar='<description>',
help=_(
"A text to describe/contextualize the use of "
"the port forwarding configuration"
),
)
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.network
floating_ip = client.find_ip(
parsed_args.floating_ip,
ignore_missing=False,
)
attrs = {}
if parsed_args.port:
port = client.find_port(parsed_args.port, ignore_missing=False)
attrs['internal_port_id'] = port.id
if parsed_args.internal_ip_address:
attrs['internal_ip_address'] = parsed_args.internal_ip_address
validate_and_assign_port_ranges(parsed_args, attrs)
if parsed_args.protocol:
attrs['protocol'] = parsed_args.protocol
if parsed_args.description is not None:
attrs['description'] = parsed_args.description
attrs.update(
self._parse_extra_properties(parsed_args.extra_properties)
)
client.update_floating_ip_port_forwarding(
floating_ip.id, parsed_args.port_forwarding_id, **attrs
)
class ShowFloatingIPPortForwarding(command.ShowOne):
_description = _("Display floating IP Port Forwarding details")
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
'floating_ip',
metavar='<floating-ip>',
help=_(
"Floating IP that the port forwarding belongs to "
"(IP address or ID)"
),
)
parser.add_argument(
'port_forwarding_id',
metavar="<port-forwarding-id>",
help=_("The ID of the floating IP port forwarding"),
)
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.network
floating_ip = client.find_ip(
parsed_args.floating_ip,
ignore_missing=False,
)
obj = client.find_floating_ip_port_forwarding(
floating_ip,
parsed_args.port_forwarding_id,
ignore_missing=False,
)
display_columns, columns = _get_columns(obj)
data = utils.get_item_properties(obj, columns)
return (display_columns, data)
|