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
|
# Copyright 2016-2017 FUJITSU LIMITED
# All Rights Reserved
#
# 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.
import logging
from osc_lib.command import command
from osc_lib import exceptions
from osc_lib import utils
from osc_lib.utils import columns as column_util
from neutronclient._i18n import _
from neutronclient.osc import utils as osc_utils
from neutronclient.osc.v2.fwaas import constants as const
from neutronclient.osc.v2 import utils as v2_utils
LOG = logging.getLogger(__name__)
_formatters = {
'admin_state_up': v2_utils.AdminStateColumn,
}
_attr_map_dict = {
'id': 'ID',
'name': 'Name',
'ingress_firewall_policy_id': 'Ingress Policy ID',
'egress_firewall_policy_id': 'Egress Policy ID',
'description': 'Description',
'status': 'Status',
'ports': 'Ports',
'admin_state_up': 'State',
'shared': 'Shared',
'tenant_id': 'Project',
'project_id': 'Project',
}
_attr_map = (
('id', 'ID', column_util.LIST_BOTH),
('name', 'Name', column_util.LIST_BOTH),
('ingress_firewall_policy_id', 'Ingress Policy ID', column_util.LIST_BOTH),
('egress_firewall_policy_id', 'Egress Policy ID', column_util.LIST_BOTH),
('description', 'Description', column_util.LIST_LONG_ONLY),
('status', 'Status', column_util.LIST_LONG_ONLY),
('ports', 'Ports', column_util.LIST_LONG_ONLY),
('admin_state_up', 'State', column_util.LIST_LONG_ONLY),
('shared', 'Shared', column_util.LIST_LONG_ONLY),
('tenant_id', 'Project', column_util.LIST_LONG_ONLY),
)
def _get_common_parser(parser):
parser.add_argument(
'--name',
help=_('Name for the firewall group'))
parser.add_argument(
'--description',
metavar='<description>',
help=_('Description of the firewall group'))
ingress_group = parser.add_mutually_exclusive_group()
ingress_group.add_argument(
'--ingress-firewall-policy',
metavar='<ingress-firewall-policy>',
dest='ingress_firewall_policy',
help=_('Ingress firewall policy (name or ID)'))
ingress_group.add_argument(
'--no-ingress-firewall-policy',
dest='no_ingress_firewall_policy',
action='store_true',
help=_('Detach ingress firewall policy from the firewall group'))
egress_group = parser.add_mutually_exclusive_group()
egress_group.add_argument(
'--egress-firewall-policy',
metavar='<egress-firewall-policy>',
dest='egress_firewall_policy',
help=_('Egress firewall policy (name or ID)'))
egress_group.add_argument(
'--no-egress-firewall-policy',
dest='no_egress_firewall_policy',
action='store_true',
help=_('Detach egress firewall policy from the firewall group'))
shared_group = parser.add_mutually_exclusive_group()
shared_group.add_argument(
'--share',
action='store_true',
help=_('Share the firewall group to be used in all projects '
'(by default, it is restricted to be used by the '
'current project).'))
shared_group.add_argument(
'--no-share',
action='store_true',
help=_('Restrict use of the firewall group to the '
'current project'))
admin_group = parser.add_mutually_exclusive_group()
admin_group.add_argument(
'--enable',
action='store_true',
help=_('Enable firewall group'))
admin_group.add_argument(
'--disable',
action='store_true',
help=_('Disable firewall group'))
return parser
def _get_common_attrs(client_manager, parsed_args, is_create=True):
attrs = {}
client = client_manager.network
if is_create:
if 'project' in parsed_args and parsed_args.project is not None:
attrs['tenant_id'] = osc_utils.find_project(
client_manager.identity,
parsed_args.project,
parsed_args.project_domain,
).id
if (parsed_args.ingress_firewall_policy and
parsed_args.no_ingress_firewall_policy):
attrs['ingress_firewall_policy_id'] = client.find_firewall_policy(
parsed_args.ingress_firewall_policy)['id']
elif parsed_args.ingress_firewall_policy:
attrs['ingress_firewall_policy_id'] = client.find_firewall_policy(
parsed_args.ingress_firewall_policy)['id']
elif parsed_args.no_ingress_firewall_policy:
attrs['ingress_firewall_policy_id'] = None
if (parsed_args.egress_firewall_policy and
parsed_args.no_egress_firewall_policy):
attrs['egress_firewall_policy_id'] = client.find_firewall_policy(
parsed_args.egress_firewall_policy)['id']
elif parsed_args.egress_firewall_policy:
attrs['egress_firewall_policy_id'] = client.find_firewall_policy(
parsed_args.egress_firewall_policy)['id']
elif parsed_args.no_egress_firewall_policy:
attrs['egress_firewall_policy_id'] = None
if parsed_args.share:
attrs['shared'] = True
if parsed_args.no_share:
attrs['shared'] = False
if parsed_args.enable:
attrs['admin_state_up'] = True
if parsed_args.disable:
attrs['admin_state_up'] = False
if parsed_args.name:
attrs['name'] = str(parsed_args.name)
if parsed_args.description:
attrs['description'] = str(parsed_args.description)
if parsed_args.port and parsed_args.no_port:
attrs['ports'] = sorted([client.find_port(
p)['id'] for p in set(parsed_args.port)])
elif parsed_args.port:
ports = []
for p in set(parsed_args.port):
ports.append(client.find_port(p)['id'])
if not is_create:
ports += client.find_firewall_group(
parsed_args.firewall_group)['ports']
attrs['ports'] = sorted(set(ports))
elif parsed_args.no_port:
attrs['ports'] = []
return attrs
class CreateFirewallGroup(command.ShowOne):
_description = _("Create a new firewall group")
def get_parser(self, prog_name):
parser = super(CreateFirewallGroup, self).get_parser(prog_name)
_get_common_parser(parser)
osc_utils.add_project_owner_option_to_parser(parser)
port_group = parser.add_mutually_exclusive_group()
port_group.add_argument(
'--port',
metavar='<port>',
action='append',
help=_('Port(s) (name or ID) to apply firewall group. This '
'option can be repeated'))
port_group.add_argument(
'--no-port',
dest='no_port',
action='store_true',
help=_('Detach all port from the firewall group'))
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.network
attrs = _get_common_attrs(self.app.client_manager, parsed_args)
obj = client.create_firewall_group(**attrs)
display_columns, columns = utils.get_osc_show_columns_for_sdk_resource(
obj, _attr_map_dict, ['location', 'tenant_id'])
data = utils.get_dict_properties(obj, columns, formatters=_formatters)
return (display_columns, data)
class DeleteFirewallGroup(command.Command):
_description = _("Delete firewall group(s)")
def get_parser(self, prog_name):
parser = super(DeleteFirewallGroup, self).get_parser(prog_name)
parser.add_argument(
const.FWG,
metavar='<firewall-group>',
nargs='+',
help=_('Firewall group(s) to delete (name or ID)'))
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.network
result = 0
for fwg in parsed_args.firewall_group:
try:
fwg_id = client.find_firewall_group(fwg)['id']
client.delete_firewall_group(fwg_id)
except Exception as e:
result += 1
LOG.error(_("Failed to delete firewall group with "
"name or ID '%(firewall_group)s': %(e)s"),
{const.FWG: fwg, 'e': e})
if result > 0:
total = len(parsed_args.firewall_group)
msg = (_("%(result)s of %(total)s firewall group(s) "
"failed to delete.") % {'result': result, 'total': total})
raise exceptions.CommandError(msg)
class ListFirewallGroup(command.Lister):
_description = _("List firewall groups")
def get_parser(self, prog_name):
parser = super(ListFirewallGroup, self).get_parser(prog_name)
parser.add_argument(
'--long',
action='store_true',
help=_("List additional fields in output")
)
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.network
obj = client.firewall_groups()
headers, columns = column_util.get_column_definitions(
_attr_map, long_listing=parsed_args.long)
return (headers, (utils.get_dict_properties(
s, columns, formatters=_formatters) for s in obj))
class SetFirewallGroup(command.Command):
_description = _("Set firewall group properties")
def get_parser(self, prog_name):
parser = super(SetFirewallGroup, self).get_parser(prog_name)
_get_common_parser(parser)
parser.add_argument(
const.FWG,
metavar='<firewall-group>',
help=_('Firewall group to update (name or ID)'))
parser.add_argument(
'--port',
metavar='<port>',
action='append',
help=_('Port(s) (name or ID) to apply firewall group. This '
'option can be repeated'))
parser.add_argument(
'--no-port',
dest='no_port',
action='store_true',
help=_('Detach all port from the firewall group'))
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.network
fwg_id = client.find_firewall_group(parsed_args.firewall_group)['id']
attrs = _get_common_attrs(self.app.client_manager, parsed_args,
is_create=False)
try:
client.update_firewall_group(fwg_id, **attrs)
except Exception as e:
msg = (_("Failed to set firewall group '%(group)s': %(e)s")
% {'group': parsed_args.firewall_group, 'e': e})
raise exceptions.CommandError(msg)
class ShowFirewallGroup(command.ShowOne):
_description = _("Display firewall group details")
def get_parser(self, prog_name):
parser = super(ShowFirewallGroup, self).get_parser(prog_name)
parser.add_argument(
const.FWG,
metavar='<firewall-group>',
help=_('Firewall group to show (name or ID)'))
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.network
fwg_id = client.find_firewall_group(parsed_args.firewall_group)['id']
obj = client.get_firewall_group(fwg_id)
display_columns, columns = utils.get_osc_show_columns_for_sdk_resource(
obj, _attr_map_dict, ['location', 'tenant_id'])
data = utils.get_dict_properties(obj, columns, formatters=_formatters)
return (display_columns, data)
class UnsetFirewallGroup(command.Command):
_description = _("Unset firewall group properties")
def get_parser(self, prog_name):
parser = super(UnsetFirewallGroup, self).get_parser(prog_name)
parser.add_argument(
const.FWG,
metavar='<firewall-group>',
help=_('Firewall group to unset (name or ID)'))
port_group = parser.add_mutually_exclusive_group()
port_group.add_argument(
'--port',
metavar='<port>',
action='append',
help=_('Port(s) (name or ID) to apply firewall group. This '
'option can be repeated'))
port_group.add_argument(
'--all-port',
action='store_true',
help=_('Remove all ports for this firewall group'))
parser.add_argument(
'--ingress-firewall-policy',
action='store_true',
help=_('Ingress firewall policy (name or ID) to delete'))
parser.add_argument(
'--egress-firewall-policy',
action='store_true',
dest='egress_firewall_policy',
help=_('Egress firewall policy (name or ID) to delete'))
shared_group = parser.add_mutually_exclusive_group()
shared_group.add_argument(
'--share',
action='store_true',
help=_('Restrict use of the firewall group to the '
'current project'))
parser.add_argument(
'--enable',
action='store_true',
help=_('Disable firewall group'))
return parser
def _get_attrs(self, client, parsed_args):
attrs = {}
if parsed_args.ingress_firewall_policy:
attrs['ingress_firewall_policy_id'] = None
if parsed_args.egress_firewall_policy:
attrs['egress_firewall_policy_id'] = None
if parsed_args.share:
attrs['shared'] = False
if parsed_args.enable:
attrs['admin_state_up'] = False
if parsed_args.port:
old = client.find_firewall_group(
parsed_args.firewall_group)['ports']
new = [client.find_port(r)['id'] for r in parsed_args.port]
attrs['ports'] = sorted(list(set(old) - set(new)))
if parsed_args.all_port:
attrs['ports'] = []
return attrs
def take_action(self, parsed_args):
client = self.app.client_manager.network
fwg_id = client.find_firewall_group(parsed_args.firewall_group)['id']
attrs = self._get_attrs(client, parsed_args)
try:
client.update_firewall_group(fwg_id, **attrs)
except Exception as e:
msg = (_("Failed to unset firewall group '%(group)s': %(e)s")
% {'group': parsed_args.firewall_group, 'e': e})
raise exceptions.CommandError(msg)
|