|
14 | 14 |
|
15 | 15 | import logging |
16 | 16 |
|
| 17 | +from osc_lib import exceptions |
17 | 18 | from osc_lib import utils |
18 | 19 | from osc_lib.command import command |
19 | 20 |
|
| 21 | +from otcextensions.common import sdk_utils |
20 | 22 | from otcextensions.i18n import _ |
21 | 23 |
|
22 | 24 | LOG = logging.getLogger(__name__) |
23 | 25 |
|
24 | 26 |
|
| 27 | +def _get_columns(item): |
| 28 | + column_map = {} |
| 29 | + return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map) |
| 30 | + |
| 31 | + |
25 | 32 | class ListPrivateDnatRules(command.Lister): |
26 | 33 |
|
27 | 34 | _description = _("List private DNAT rules.") |
@@ -240,3 +247,123 @@ def take_action(self, parsed_args): |
240 | 247 | for s in data |
241 | 248 | ), |
242 | 249 | ) |
| 250 | + |
| 251 | + |
| 252 | +class CreatePrivateDnatRule(command.ShowOne): |
| 253 | + |
| 254 | + _description = _("Create a private DNAT rule.") |
| 255 | + |
| 256 | + def get_parser(self, prog_name): |
| 257 | + parser = super(CreatePrivateDnatRule, self).get_parser(prog_name) |
| 258 | + |
| 259 | + parser.add_argument( |
| 260 | + "--gateway-id", |
| 261 | + metavar="<gateway_id>", |
| 262 | + required=True, |
| 263 | + help=_("Specifies the private NAT gateway ID."), |
| 264 | + ) |
| 265 | + parser.add_argument( |
| 266 | + "--transit-ip-id", |
| 267 | + metavar="<transit_ip_id>", |
| 268 | + required=True, |
| 269 | + help=_("Specifies the transit IP address ID."), |
| 270 | + ) |
| 271 | + parser.add_argument( |
| 272 | + "--type", |
| 273 | + metavar="<type>", |
| 274 | + choices=["COMPUTE", "VIP", "ELB", "ELBv3", "CUSTOMIZE"], |
| 275 | + help=_( |
| 276 | + "Specifies the backend resource type. " |
| 277 | + "Supported values: COMPUTE, VIP, ELB, ELBv3, CUSTOMIZE." |
| 278 | + ), |
| 279 | + ) |
| 280 | + parser.add_argument( |
| 281 | + "--network-interface-id", |
| 282 | + metavar="<network_interface_id>", |
| 283 | + help=_("Specifies the port ID of the backend resource."), |
| 284 | + ) |
| 285 | + parser.add_argument( |
| 286 | + "--private-ip-address", |
| 287 | + metavar="<private_ip_address>", |
| 288 | + help=_("Specifies the backend private IP address."), |
| 289 | + ) |
| 290 | + parser.add_argument( |
| 291 | + "--protocol", |
| 292 | + metavar="<protocol>", |
| 293 | + choices=["tcp", "udp", "any", "TCP", "UDP", "ANY"], |
| 294 | + help=_("Specifies the protocol type. " "Supported values: tcp, udp, any."), |
| 295 | + ) |
| 296 | + parser.add_argument( |
| 297 | + "--internal-service-port", |
| 298 | + metavar="<internal_service_port>", |
| 299 | + type=int, |
| 300 | + help=_("Specifies the port number of the backend resource."), |
| 301 | + ) |
| 302 | + parser.add_argument( |
| 303 | + "--transit-service-port", |
| 304 | + metavar="<transit_service_port>", |
| 305 | + type=int, |
| 306 | + help=_("Specifies the port number of the transit IP address."), |
| 307 | + ) |
| 308 | + parser.add_argument( |
| 309 | + "--description", |
| 310 | + metavar="<description>", |
| 311 | + help=_("Provides supplementary information about the DNAT rule."), |
| 312 | + ) |
| 313 | + parser.add_argument( |
| 314 | + "--enterprise-project-id", |
| 315 | + metavar="<enterprise_project_id>", |
| 316 | + help=_("Specifies the enterprise project ID."), |
| 317 | + ) |
| 318 | + return parser |
| 319 | + |
| 320 | + def _build_attrs(self, parsed_args): |
| 321 | + attrs = { |
| 322 | + "gateway_id": parsed_args.gateway_id, |
| 323 | + "transit_ip_id": parsed_args.transit_ip_id, |
| 324 | + } |
| 325 | + optional_attrs = ( |
| 326 | + "type", |
| 327 | + "protocol", |
| 328 | + "network_interface_id", |
| 329 | + "private_ip_address", |
| 330 | + "internal_service_port", |
| 331 | + "transit_service_port", |
| 332 | + "description", |
| 333 | + "enterprise_project_id", |
| 334 | + ) |
| 335 | + |
| 336 | + if parsed_args.network_interface_id and parsed_args.private_ip_address: |
| 337 | + raise exceptions.CommandError( |
| 338 | + _( |
| 339 | + "Specify either --network-interface-id or " |
| 340 | + "--private-ip-address, but not both." |
| 341 | + ) |
| 342 | + ) |
| 343 | + |
| 344 | + if not parsed_args.network_interface_id and not parsed_args.private_ip_address: |
| 345 | + raise exceptions.CommandError( |
| 346 | + _( |
| 347 | + "One of --network-interface-id or --private-ip-address " |
| 348 | + "must be specified." |
| 349 | + ) |
| 350 | + ) |
| 351 | + |
| 352 | + for key in optional_attrs: |
| 353 | + value = getattr(parsed_args, key, None) |
| 354 | + if value is not None: |
| 355 | + if key == "protocol": |
| 356 | + attrs["protocol"] = value.lower() |
| 357 | + else: |
| 358 | + attrs[key] = value |
| 359 | + |
| 360 | + return attrs |
| 361 | + |
| 362 | + def take_action(self, parsed_args): |
| 363 | + client = self.app.client_manager.privatenat |
| 364 | + attrs = self._build_attrs(parsed_args) |
| 365 | + obj = client.create_private_dnat_rule(**attrs) |
| 366 | + |
| 367 | + display_columns, columns = _get_columns(obj) |
| 368 | + data = utils.get_item_properties(obj, columns) |
| 369 | + return display_columns, data |
0 commit comments