|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +# |
| 13 | +"""Private SNAT v3 action implementations""" |
| 14 | + |
| 15 | +import logging |
| 16 | + |
| 17 | +from osc_lib import utils |
| 18 | +from osc_lib.command import command |
| 19 | + |
| 20 | +from otcextensions.i18n import _ |
| 21 | + |
| 22 | +LOG = logging.getLogger(__name__) |
| 23 | + |
| 24 | + |
| 25 | +def _get_transit_ip_addresses(rule): |
| 26 | + return ", ".join( |
| 27 | + association.transit_ip_address |
| 28 | + for association in (rule.transit_ip_associations or []) |
| 29 | + if getattr(association, "transit_ip_address", None) |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +class ListPrivateSnatRules(command.Lister): |
| 34 | + |
| 35 | + _description = _("List private SNAT rules.") |
| 36 | + columns = ( |
| 37 | + "id", |
| 38 | + "gateway_id", |
| 39 | + "virsubnet_id", |
| 40 | + "cidr", |
| 41 | + "transit_ip_addresses", |
| 42 | + "description", |
| 43 | + "status", |
| 44 | + ) |
| 45 | + |
| 46 | + def get_parser(self, prog_name): |
| 47 | + parser = super(ListPrivateSnatRules, self).get_parser(prog_name) |
| 48 | + |
| 49 | + parser.add_argument( |
| 50 | + "--id", |
| 51 | + metavar="<id>", |
| 52 | + action="append", |
| 53 | + help=_("Specifies the SNAT rule ID. Repeat to filter by multiple values."), |
| 54 | + ) |
| 55 | + parser.add_argument( |
| 56 | + "--limit", |
| 57 | + metavar="<limit>", |
| 58 | + type=int, |
| 59 | + help=_("Specifies the number of records displayed on each page."), |
| 60 | + ) |
| 61 | + parser.add_argument( |
| 62 | + "--marker", |
| 63 | + metavar="<marker>", |
| 64 | + help=_("Specifies the start resource ID of pagination query."), |
| 65 | + ) |
| 66 | + parser.add_argument( |
| 67 | + "--page-reverse", |
| 68 | + action="store_true", |
| 69 | + help=_("Query resources on the previous page."), |
| 70 | + ) |
| 71 | + parser.add_argument( |
| 72 | + "--project-id", |
| 73 | + metavar="<project_id>", |
| 74 | + action="append", |
| 75 | + help=_("Specifies the project ID. Repeat to filter by multiple values."), |
| 76 | + ) |
| 77 | + parser.add_argument( |
| 78 | + "--description", |
| 79 | + metavar="<description>", |
| 80 | + action="append", |
| 81 | + help=_( |
| 82 | + "Provides supplementary information about the SNAT rule. " |
| 83 | + "Repeat to filter by multiple values." |
| 84 | + ), |
| 85 | + ) |
| 86 | + parser.add_argument( |
| 87 | + "--gateway-id", |
| 88 | + metavar="<gateway_id>", |
| 89 | + action="append", |
| 90 | + help=_( |
| 91 | + "Specifies the private NAT gateway ID. " |
| 92 | + "Repeat to filter by multiple values." |
| 93 | + ), |
| 94 | + ) |
| 95 | + parser.add_argument( |
| 96 | + "--cidr", |
| 97 | + metavar="<cidr>", |
| 98 | + action="append", |
| 99 | + help=_( |
| 100 | + "Specifies the CIDR block that matches the SNAT rule. " |
| 101 | + "Repeat to filter by multiple values." |
| 102 | + ), |
| 103 | + ) |
| 104 | + parser.add_argument( |
| 105 | + "--virsubnet-id", |
| 106 | + metavar="<virsubnet_id>", |
| 107 | + action="append", |
| 108 | + help=_( |
| 109 | + "Specifies the subnet ID that matches the SNAT rule. " |
| 110 | + "Repeat to filter by multiple values." |
| 111 | + ), |
| 112 | + ) |
| 113 | + parser.add_argument( |
| 114 | + "--transit-ip-id", |
| 115 | + metavar="<transit_ip_id>", |
| 116 | + action="append", |
| 117 | + help=_( |
| 118 | + "Specifies the transit IP address ID. " |
| 119 | + "Repeat to filter by multiple values." |
| 120 | + ), |
| 121 | + ) |
| 122 | + parser.add_argument( |
| 123 | + "--transit-ip-address", |
| 124 | + metavar="<transit_ip_address>", |
| 125 | + action="append", |
| 126 | + help=_( |
| 127 | + "Specifies the transit IP address. " |
| 128 | + "Repeat to filter by multiple values." |
| 129 | + ), |
| 130 | + ) |
| 131 | + parser.add_argument( |
| 132 | + "--enterprise-project-id", |
| 133 | + metavar="<enterprise_project_id>", |
| 134 | + action="append", |
| 135 | + help=_( |
| 136 | + "Specifies the enterprise project ID. " |
| 137 | + "Repeat to filter by multiple values." |
| 138 | + ), |
| 139 | + ) |
| 140 | + return parser |
| 141 | + |
| 142 | + def take_action(self, parsed_args): |
| 143 | + client = self.app.client_manager.privatenat |
| 144 | + |
| 145 | + args_list = [ |
| 146 | + "id", |
| 147 | + "limit", |
| 148 | + "marker", |
| 149 | + "page_reverse", |
| 150 | + "project_id", |
| 151 | + "description", |
| 152 | + "gateway_id", |
| 153 | + "cidr", |
| 154 | + "virsubnet_id", |
| 155 | + "transit_ip_id", |
| 156 | + "transit_ip_address", |
| 157 | + "enterprise_project_id", |
| 158 | + ] |
| 159 | + |
| 160 | + attrs = {} |
| 161 | + for arg in args_list: |
| 162 | + val = getattr(parsed_args, arg, None) |
| 163 | + |
| 164 | + if val is None: |
| 165 | + continue |
| 166 | + if isinstance(val, list) and not val: |
| 167 | + continue |
| 168 | + if isinstance(val, bool) and not val: |
| 169 | + continue |
| 170 | + |
| 171 | + attrs[arg] = val |
| 172 | + |
| 173 | + data = client.private_snat_rules(**attrs) |
| 174 | + |
| 175 | + return ( |
| 176 | + self.columns, |
| 177 | + ( |
| 178 | + utils.get_dict_properties( |
| 179 | + { |
| 180 | + "id": s.id, |
| 181 | + "gateway_id": s.gateway_id, |
| 182 | + "virsubnet_id": s.virsubnet_id, |
| 183 | + "cidr": s.cidr, |
| 184 | + "transit_ip_addresses": _get_transit_ip_addresses(s), |
| 185 | + "description": s.description, |
| 186 | + "status": s.status, |
| 187 | + }, |
| 188 | + self.columns, |
| 189 | + ) |
| 190 | + for s in data |
| 191 | + ), |
| 192 | + ) |
0 commit comments