| 1 |
# Copyright 2016-2017 FUJITSU LIMITED |
| 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 |
import logging |
| 17 |
|
| 18 |
from osc_lib.command import command |
| 19 |
from osc_lib import exceptions |
| 20 |
from osc_lib import utils |
| 21 |
from osc_lib.utils import columns as column_util |
| 22 |
|
| 23 |
from neutronclient._i18n import _ |
| 24 |
from neutronclient.osc import utils as osc_utils |
| 25 |
from neutronclient.osc.v2.fwaas import constants as const |
| 26 |
from neutronclient.osc.v2 import utils as v2_utils |
| 27 |
|
| 28 |
|
| 29 |
LOG = logging.getLogger(__name__) |
| 30 |
|
| 31 |
_formatters = { |
| 32 |
'admin_state_up': v2_utils.AdminStateColumn, |
| 33 |
} |
| 34 |
|
| 35 |
_attr_map_dict = { |
| 36 |
'id': 'ID', |
| 37 |
'name': 'Name', |
| 38 |
'ingress_firewall_policy_id': 'Ingress Policy ID', |
| 39 |
'egress_firewall_policy_id': 'Egress Policy ID', |
| 40 |
'description': 'Description', |
| 41 |
'status': 'Status', |
| 42 |
'ports': 'Ports', |
| 43 |
'admin_state_up': 'State', |
| 44 |
'shared': 'Shared', |
| 45 |
'tenant_id': 'Project', |
| 46 |
'project_id': 'Project', |
| 47 |
} |
| 48 |
|
| 49 |
_attr_map = ( |
| 50 |
('id', 'ID', column_util.LIST_BOTH), |
| 51 |
('name', 'Name', column_util.LIST_BOTH), |
| 52 |
('ingress_firewall_policy_id', 'Ingress Policy ID', column_util.LIST_BOTH), |
| 53 |
('egress_firewall_policy_id', 'Egress Policy ID', column_util.LIST_BOTH), |
| 54 |
('description', 'Description', column_util.LIST_LONG_ONLY), |
| 55 |
('status', 'Status', column_util.LIST_LONG_ONLY), |
| 56 |
('ports', 'Ports', column_util.LIST_LONG_ONLY), |
| 57 |
('admin_state_up', 'State', column_util.LIST_LONG_ONLY), |
| 58 |
('shared', 'Shared', column_util.LIST_LONG_ONLY), |
| 59 |
('tenant_id', 'Project', column_util.LIST_LONG_ONLY), |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
def _get_common_parser(parser): |
| 64 |
parser.add_argument( |
| 65 |
'--name', |
| 66 |
help=_('Name for the firewall group')) |
| 67 |
parser.add_argument( |
| 68 |
'--description', |
| 69 |
metavar='<description>', |
| 70 |
help=_('Description of the firewall group')) |
| 71 |
ingress_group = parser.add_mutually_exclusive_group() |
| 72 |
ingress_group.add_argument( |
| 73 |
'--ingress-firewall-policy', |
| 74 |
metavar='<ingress-firewall-policy>', |
| 75 |
dest='ingress_firewall_policy', |
| 76 |
help=_('Ingress firewall policy (name or ID)')) |
| 77 |
ingress_group.add_argument( |
| 78 |
'--no-ingress-firewall-policy', |
| 79 |
dest='no_ingress_firewall_policy', |
| 80 |
action='store_true', |
| 81 |
help=_('Detach ingress firewall policy from the firewall group')) |
| 82 |
egress_group = parser.add_mutually_exclusive_group() |
| 83 |
egress_group.add_argument( |
| 84 |
'--egress-firewall-policy', |
| 85 |
metavar='<egress-firewall-policy>', |
| 86 |
dest='egress_firewall_policy', |
| 87 |
help=_('Egress firewall policy (name or ID)')) |
| 88 |
egress_group.add_argument( |
| 89 |
'--no-egress-firewall-policy', |
| 90 |
dest='no_egress_firewall_policy', |
| 91 |
action='store_true', |
| 92 |
help=_('Detach egress firewall policy from the firewall group')) |
| 93 |
shared_group = parser.add_mutually_exclusive_group() |
| 94 |
|
| 95 |
shared_group.add_argument( |
| 96 |
'--share', |
| 97 |
action='store_true', |
| 98 |
help=_('Share the firewall group to be used in all projects ' |
| 99 |
'(by default, it is restricted to be used by the ' |
| 100 |
'current project).')) |
| 101 |
shared_group.add_argument( |
| 102 |
'--no-share', |
| 103 |
action='store_true', |
| 104 |
help=_('Restrict use of the firewall group to the ' |
| 105 |
'current project')) |
| 106 |
admin_group = parser.add_mutually_exclusive_group() |
| 107 |
admin_group.add_argument( |
| 108 |
'--enable', |
| 109 |
action='store_true', |
| 110 |
help=_('Enable firewall group')) |
| 111 |
admin_group.add_argument( |
| 112 |
'--disable', |
| 113 |
action='store_true', |
| 114 |
help=_('Disable firewall group')) |
| 115 |
return parser |
| 116 |
|
| 117 |
|
| 118 |
def _get_common_attrs(client_manager, parsed_args, is_create=True): |
| 119 |
attrs = {} |
| 120 |
client = client_manager.network |
| 121 |
|
| 122 |
if is_create: |
| 123 |
if 'project' in parsed_args and parsed_args.project is not None: |
| 124 |
attrs['tenant_id'] = osc_utils.find_project( |
| 125 |
client_manager.identity, |
| 126 |
parsed_args.project, |
| 127 |
parsed_args.project_domain, |
| 128 |
).id |
| 129 |
if (parsed_args.ingress_firewall_policy and |
| 130 |
parsed_args.no_ingress_firewall_policy): |
| 131 |
attrs['ingress_firewall_policy_id'] = client.find_firewall_policy( |
| 132 |
parsed_args.ingress_firewall_policy)['id'] |
| 133 |
elif parsed_args.ingress_firewall_policy: |
| 134 |
attrs['ingress_firewall_policy_id'] = client.find_firewall_policy( |
| 135 |
parsed_args.ingress_firewall_policy)['id'] |
| 136 |
elif parsed_args.no_ingress_firewall_policy: |
| 137 |
attrs['ingress_firewall_policy_id'] = None |
| 138 |
if (parsed_args.egress_firewall_policy and |
| 139 |
parsed_args.no_egress_firewall_policy): |
| 140 |
attrs['egress_firewall_policy_id'] = client.find_firewall_policy( |
| 141 |
parsed_args.egress_firewall_policy)['id'] |
| 142 |
elif parsed_args.egress_firewall_policy: |
| 143 |
attrs['egress_firewall_policy_id'] = client.find_firewall_policy( |
| 144 |
parsed_args.egress_firewall_policy)['id'] |
| 145 |
elif parsed_args.no_egress_firewall_policy: |
| 146 |
attrs['egress_firewall_policy_id'] = None |
| 147 |
if parsed_args.share: |
| 148 |
attrs['shared'] = True |
| 149 |
if parsed_args.no_share: |
| 150 |
attrs['shared'] = False |
| 151 |
if parsed_args.enable: |
| 152 |
attrs['admin_state_up'] = True |
| 153 |
if parsed_args.disable: |
| 154 |
attrs['admin_state_up'] = False |
| 155 |
if parsed_args.name: |
| 156 |
attrs['name'] = str(parsed_args.name) |
| 157 |
if parsed_args.description: |
| 158 |
attrs['description'] = str(parsed_args.description) |
| 159 |
if parsed_args.port and parsed_args.no_port: |
| 160 |
attrs['ports'] = sorted([client.find_port( |
| 161 |
p)['id'] for p in set(parsed_args.port)]) |
| 162 |
elif parsed_args.port: |
| 163 |
ports = [] |
| 164 |
for p in set(parsed_args.port): |
| 165 |
ports.append(client.find_port(p)['id']) |
| 166 |
if not is_create: |
| 167 |
ports += client.find_firewall_group( |
| 168 |
parsed_args.firewall_group)['ports'] |
| 169 |
attrs['ports'] = sorted(set(ports)) |
| 170 |
elif parsed_args.no_port: |
| 171 |
attrs['ports'] = [] |
| 172 |
return attrs |
| 173 |
|
| 174 |
|
| 175 |
class CreateFirewallGroup(command.ShowOne): |
| 176 |
_description = _("Create a new firewall group") |
| 177 |
|
| 178 |
def get_parser(self, prog_name): |
| 179 |
parser = super(CreateFirewallGroup, self).get_parser(prog_name) |
| 180 |
_get_common_parser(parser) |
| 181 |
osc_utils.add_project_owner_option_to_parser(parser) |
| 182 |
port_group = parser.add_mutually_exclusive_group() |
| 183 |
port_group.add_argument( |
| 184 |
'--port', |
| 185 |
metavar='<port>', |
| 186 |
action='append', |
| 187 |
help=_('Port(s) (name or ID) to apply firewall group. This ' |
| 188 |
'option can be repeated')) |
| 189 |
port_group.add_argument( |
| 190 |
'--no-port', |
| 191 |
dest='no_port', |
| 192 |
action='store_true', |
| 193 |
help=_('Detach all port from the firewall group')) |
| 194 |
return parser |
| 195 |
|
| 196 |
def take_action(self, parsed_args): |
| 197 |
client = self.app.client_manager.network |
| 198 |
attrs = _get_common_attrs(self.app.client_manager, parsed_args) |
| 199 |
obj = client.create_firewall_group(**attrs) |
| 200 |
display_columns, columns = utils.get_osc_show_columns_for_sdk_resource( |
| 201 |
obj, _attr_map_dict, ['location', 'tenant_id']) |
| 202 |
data = utils.get_dict_properties(obj, columns, formatters=_formatters) |
| 203 |
return (display_columns, data) |
| 204 |
|
| 205 |
|
| 206 |
class DeleteFirewallGroup(command.Command): |
| 207 |
_description = _("Delete firewall group(s)") |
| 208 |
|
| 209 |
def get_parser(self, prog_name): |
| 210 |
parser = super(DeleteFirewallGroup, self).get_parser(prog_name) |
| 211 |
parser.add_argument( |
| 212 |
const.FWG, |
| 213 |
metavar='<firewall-group>', |
| 214 |
nargs='+', |
| 215 |
help=_('Firewall group(s) to delete (name or ID)')) |
| 216 |
return parser |
| 217 |
|
| 218 |
def take_action(self, parsed_args): |
| 219 |
client = self.app.client_manager.network |
| 220 |
result = 0 |
| 221 |
for fwg in parsed_args.firewall_group: |
| 222 |
try: |
| 223 |
fwg_id = client.find_firewall_group(fwg)['id'] |
| 224 |
client.delete_firewall_group(fwg_id) |
| 225 |
except Exception as e: |
| 226 |
result += 1 |
| 227 |
LOG.error(_("Failed to delete firewall group with " |
| 228 |
"name or ID '%(firewall_group)s': %(e)s"), |
| 229 |
{const.FWG: fwg, 'e': e}) |
| 230 |
|
| 231 |
if result > 0: |
| 232 |
total = len(parsed_args.firewall_group) |
| 233 |
msg = (_("%(result)s of %(total)s firewall group(s) " |
| 234 |
"failed to delete.") % {'result': result, 'total': total}) |
| 235 |
raise exceptions.CommandError(msg) |
| 236 |
|
| 237 |
|
| 238 |
class ListFirewallGroup(command.Lister): |
| 239 |
_description = _("List firewall groups") |
| 240 |
|
| 241 |
def get_parser(self, prog_name): |
| 242 |
parser = super(ListFirewallGroup, self).get_parser(prog_name) |
| 243 |
parser.add_argument( |
| 244 |
'--long', |
| 245 |
action='store_true', |
| 246 |
help=_("List additional fields in output") |
| 247 |
) |
| 248 |
return parser |
| 249 |
|
| 250 |
def take_action(self, parsed_args): |
| 251 |
client = self.app.client_manager.network |
| 252 |
obj = client.firewall_groups() |
| 253 |
headers, columns = column_util.get_column_definitions( |
| 254 |
_attr_map, long_listing=parsed_args.long) |
| 255 |
return (headers, (utils.get_dict_properties( |
| 256 |
s, columns, formatters=_formatters) for s in obj)) |
| 257 |
|
| 258 |
|
| 259 |
class SetFirewallGroup(command.Command): |
| 260 |
_description = _("Set firewall group properties") |
| 261 |
|
| 262 |
def get_parser(self, prog_name): |
| 263 |
parser = super(SetFirewallGroup, self).get_parser(prog_name) |
| 264 |
_get_common_parser(parser) |
| 265 |
parser.add_argument( |
| 266 |
const.FWG, |
| 267 |
metavar='<firewall-group>', |
| 268 |
help=_('Firewall group to update (name or ID)')) |
| 269 |
parser.add_argument( |
| 270 |
'--port', |
| 271 |
metavar='<port>', |
| 272 |
action='append', |
| 273 |
help=_('Port(s) (name or ID) to apply firewall group. This ' |
| 274 |
'option can be repeated')) |
| 275 |
parser.add_argument( |
| 276 |
'--no-port', |
| 277 |
dest='no_port', |
| 278 |
action='store_true', |
| 279 |
help=_('Detach all port from the firewall group')) |
| 280 |
return parser |
| 281 |
|
| 282 |
def take_action(self, parsed_args): |
| 283 |
client = self.app.client_manager.network |
| 284 |
fwg_id = client.find_firewall_group(parsed_args.firewall_group)['id'] |
| 285 |
attrs = _get_common_attrs(self.app.client_manager, parsed_args, |
| 286 |
is_create=False) |
| 287 |
try: |
| 288 |
client.update_firewall_group(fwg_id, **attrs) |
| 289 |
except Exception as e: |
| 290 |
msg = (_("Failed to set firewall group '%(group)s': %(e)s") |
| 291 |
% {'group': parsed_args.firewall_group, 'e': e}) |
| 292 |
raise exceptions.CommandError(msg) |
| 293 |
|
| 294 |
|
| 295 |
class ShowFirewallGroup(command.ShowOne): |
| 296 |
_description = _("Display firewall group details") |
| 297 |
|
| 298 |
def get_parser(self, prog_name): |
| 299 |
parser = super(ShowFirewallGroup, self).get_parser(prog_name) |
| 300 |
parser.add_argument( |
| 301 |
const.FWG, |
| 302 |
metavar='<firewall-group>', |
| 303 |
help=_('Firewall group to show (name or ID)')) |
| 304 |
return parser |
| 305 |
|
| 306 |
def take_action(self, parsed_args): |
| 307 |
client = self.app.client_manager.network |
| 308 |
fwg_id = client.find_firewall_group(parsed_args.firewall_group)['id'] |
| 309 |
obj = client.get_firewall_group(fwg_id) |
| 310 |
display_columns, columns = utils.get_osc_show_columns_for_sdk_resource( |
| 311 |
obj, _attr_map_dict, ['location', 'tenant_id']) |
| 312 |
data = utils.get_dict_properties(obj, columns, formatters=_formatters) |
| 313 |
return (display_columns, data) |
| 314 |
|
| 315 |
|
| 316 |
class UnsetFirewallGroup(command.Command): |
| 317 |
_description = _("Unset firewall group properties") |
| 318 |
|
| 319 |
def get_parser(self, prog_name): |
| 320 |
parser = super(UnsetFirewallGroup, self).get_parser(prog_name) |
| 321 |
parser.add_argument( |
| 322 |
const.FWG, |
| 323 |
metavar='<firewall-group>', |
| 324 |
help=_('Firewall group to unset (name or ID)')) |
| 325 |
port_group = parser.add_mutually_exclusive_group() |
| 326 |
port_group.add_argument( |
| 327 |
'--port', |
| 328 |
metavar='<port>', |
| 329 |
action='append', |
| 330 |
help=_('Port(s) (name or ID) to apply firewall group. This ' |
| 331 |
'option can be repeated')) |
| 332 |
port_group.add_argument( |
| 333 |
'--all-port', |
| 334 |
action='store_true', |
| 335 |
help=_('Remove all ports for this firewall group')) |
| 336 |
parser.add_argument( |
| 337 |
'--ingress-firewall-policy', |
| 338 |
action='store_true', |
| 339 |
help=_('Ingress firewall policy (name or ID) to delete')) |
| 340 |
parser.add_argument( |
| 341 |
'--egress-firewall-policy', |
| 342 |
action='store_true', |
| 343 |
dest='egress_firewall_policy', |
| 344 |
help=_('Egress firewall policy (name or ID) to delete')) |
| 345 |
shared_group = parser.add_mutually_exclusive_group() |
| 346 |
shared_group.add_argument( |
| 347 |
'--share', |
| 348 |
action='store_true', |
| 349 |
help=_('Restrict use of the firewall group to the ' |
| 350 |
'current project')) |
| 351 |
parser.add_argument( |
| 352 |
'--enable', |
| 353 |
action='store_true', |
| 354 |
help=_('Disable firewall group')) |
| 355 |
return parser |
| 356 |
|
| 357 |
def _get_attrs(self, client, parsed_args): |
| 358 |
attrs = {} |
| 359 |
if parsed_args.ingress_firewall_policy: |
| 360 |
attrs['ingress_firewall_policy_id'] = None |
| 361 |
if parsed_args.egress_firewall_policy: |
| 362 |
attrs['egress_firewall_policy_id'] = None |
| 363 |
if parsed_args.share: |
| 364 |
attrs['shared'] = False |
| 365 |
if parsed_args.enable: |
| 366 |
attrs['admin_state_up'] = False |
| 367 |
if parsed_args.port: |
| 368 |
old = client.find_firewall_group( |
| 369 |
parsed_args.firewall_group)['ports'] |
| 370 |
new = [client.find_port(r)['id'] for r in parsed_args.port] |
| 371 |
attrs['ports'] = sorted(list(set(old) - set(new))) |
| 372 |
if parsed_args.all_port: |
| 373 |
attrs['ports'] = [] |
| 374 |
return attrs |
| 375 |
|
| 376 |
def take_action(self, parsed_args): |
| 377 |
client = self.app.client_manager.network |
| 378 |
fwg_id = client.find_firewall_group(parsed_args.firewall_group)['id'] |
| 379 |
attrs = self._get_attrs(client, parsed_args) |
| 380 |
try: |
| 381 |
client.update_firewall_group(fwg_id, **attrs) |
| 382 |
except Exception as e: |
| 383 |
msg = (_("Failed to unset firewall group '%(group)s': %(e)s") |
| 384 |
% {'group': parsed_args.firewall_group, 'e': e}) |
| 385 |
raise exceptions.CommandError(msg) |