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
|
# Copyright 2017-2018 FUJTISU 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 copy
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 oslo_log import log as logging
from neutronclient._i18n import _
from neutronclient.common import utils as nc_utils
from neutronclient.osc import utils as osc_utils
from neutronclient.osc.v2.fwaas import constants as fwaas_const
LOG = logging.getLogger(__name__)
_attr_map = (
('id', 'ID', column_util.LIST_BOTH),
('description', 'Description', column_util.LIST_LONG_ONLY),
('enabled', 'Enabled', column_util.LIST_BOTH),
('name', 'Name', column_util.LIST_BOTH),
('target_id', 'Target', column_util.LIST_LONG_ONLY),
('project_id', 'Project', column_util.LIST_LONG_ONLY),
('resource_id', 'Resource', column_util.LIST_LONG_ONLY),
('resource_type', 'Type', column_util.LIST_BOTH),
('event', 'Event', column_util.LIST_LONG_ONLY),
('summary', 'Summary', column_util.LIST_SHORT_ONLY),
)
_attr_map_for_loggable = (
('type', 'Supported types', column_util.LIST_BOTH),
)
NET_LOG = 'network_log'
def _get_common_parser(parser):
parser.add_argument(
'--description',
metavar='<description>',
help=_('Description of the network log'))
enable_group = parser.add_mutually_exclusive_group()
enable_group.add_argument(
'--enable',
action='store_true',
help=_('Enable this log (default is disabled)'))
enable_group.add_argument(
'--disable',
action='store_true',
help=_('Disable this log'))
return parser
def _get_common_attrs(client_manager, parsed_args, is_create=True):
attrs = {}
client = client_manager.neutronclient
if is_create:
if 'project' in parsed_args and parsed_args.project is not None:
attrs['project_id'] = osc_utils.find_project(
client_manager.identity,
parsed_args.project,
parsed_args.project_domain,
).id
resource_type = parsed_args.resource_type
attrs['resource_type'] = resource_type
if parsed_args.resource:
cmd_resource = None
if resource_type == fwaas_const.FWG:
cmd_resource = fwaas_const.CMD_FWG
attrs['resource_id'] = client.find_resource(
resource_type,
parsed_args.resource,
cmd_resource=cmd_resource)['id']
if parsed_args.target:
# NOTE(yushiro) Currently, we're supporting only port
attrs['target_id'] = client.find_resource(
'port', parsed_args.target)['id']
if parsed_args.event:
attrs['event'] = parsed_args.event
if parsed_args.enable:
attrs['enabled'] = True
if parsed_args.disable:
attrs['enabled'] = False
if parsed_args.name:
attrs['name'] = parsed_args.name
if parsed_args.description:
attrs['description'] = parsed_args.description
return attrs
class CreateNetworkLog(command.ShowOne):
_description = _("Create a new network log")
def get_parser(self, prog_name):
parser = super(CreateNetworkLog, self).get_parser(prog_name)
_get_common_parser(parser)
osc_utils.add_project_owner_option_to_parser(parser)
parser.add_argument(
'name',
metavar='<name>',
help=_('Name for the network log'))
parser.add_argument(
'--event',
metavar='{ALL,ACCEPT,DROP}',
choices=['ALL', 'ACCEPT', 'DROP'],
type=nc_utils.convert_to_uppercase,
help=_('An event to store with log'))
# NOTE(yushiro) '--resource-type' is managed by following command:
# "openstack network loggable resources list". Therefore, this option
# shouldn't have "choices" like ['security_group', 'firewall_group']
parser.add_argument(
'--resource-type',
metavar='<resource-type>',
required=True,
type=nc_utils.convert_to_lowercase,
help=_('Network log type(s). '
'You can see supported type(s) with following command:\n'
'$ openstack network loggable resources list'))
parser.add_argument(
'--resource',
metavar='<resource>',
help=_('Name or ID of resource (security group or firewall group) '
'that used for logging. You can control for logging target '
'combination with --target option.'))
parser.add_argument(
'--target',
metavar='<target>',
help=_('Port (name or ID) for logging. You can control '
'for logging target combination with --resource option.'))
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.neutronclient
attrs = _get_common_attrs(self.app.client_manager, parsed_args)
obj = client.create_network_log({'log': attrs})['log']
columns, display_columns = column_util.get_columns(obj, _attr_map)
data = utils.get_dict_properties(obj, columns)
return (display_columns, data)
class DeleteNetworkLog(command.Command):
_description = _("Delete network log(s)")
def get_parser(self, prog_name):
parser = super(DeleteNetworkLog, self).get_parser(prog_name)
parser.add_argument(
'network_log',
metavar='<network-log>',
nargs='+',
help=_('Network log(s) to delete (name or ID)'))
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.neutronclient
result = 0
for log_res in parsed_args.network_log:
try:
log_id = client.find_resource(
'log', log_res, cmd_resource=NET_LOG)['id']
client.delete_network_log(log_id)
except Exception as e:
result += 1
LOG.error(_("Failed to delete network log with "
"name or ID '%(network_log)s': %(e)s"),
{'network_log': log_res, 'e': e})
if result > 0:
total = len(parsed_args.network_log)
msg = (_("%(result)s of %(total)s network log(s) "
"failed to delete") % {'result': result, 'total': total})
raise exceptions.CommandError(msg)
class ListLoggableResource(command.Lister):
_description = _("List supported loggable resources")
def get_parser(self, prog_name):
parser = super(ListLoggableResource, 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.neutronclient
obj = client.list_network_loggable_resources()['loggable_resources']
headers, columns = column_util.get_column_definitions(
_attr_map_for_loggable, long_listing=parsed_args.long)
return (headers, (utils.get_dict_properties(s, columns) for s in obj))
class ListNetworkLog(command.Lister):
_description = _("List network logs")
def get_parser(self, prog_name):
parser = super(ListNetworkLog, self).get_parser(prog_name)
parser.add_argument(
'--long',
action='store_true',
help=_("List additional fields in output")
)
# TODO(yushiro): We'll support filtering in the future.
return parser
def _extend_list(self, data, parsed_args):
ext_data = copy.deepcopy(data)
for d in ext_data:
e_prefix = 'Event: '
if d['event']:
event = e_prefix + d['event'].upper()
port = '(port) ' + d['target_id'] if d['target_id'] else ''
resource_type = d['resource_type']
if d['resource_id']:
res = '(%s) %s' % (resource_type, d['resource_id'])
else:
res = ''
t_prefix = 'Logged: '
if port and res:
t = '%s on %s' % (res, port)
else:
# Either of res and port is empty, so concatenation works fine
t = res + port
target = t_prefix + t if t else t_prefix + '(None specified)'
d['summary'] = ',\n'.join([event, target])
return ext_data
def take_action(self, parsed_args):
client = self.app.client_manager.neutronclient
obj = client.list_network_logs()['logs']
obj_extend = self._extend_list(obj, parsed_args)
headers, columns = column_util.get_column_definitions(
_attr_map, long_listing=parsed_args.long)
return (headers, (
utils.get_dict_properties(s, columns) for s in obj_extend))
class SetNetworkLog(command.Command):
_description = _("Set network log properties")
def get_parser(self, prog_name):
parser = super(SetNetworkLog, self).get_parser(prog_name)
_get_common_parser(parser)
parser.add_argument(
'network_log',
metavar='<network-log>',
help=_('Network log to set (name or ID)'))
parser.add_argument(
'--name',
metavar='<name>',
help=_('Name of the network log'))
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.neutronclient
log_id = client.find_resource(
'log', parsed_args.network_log, cmd_resource=NET_LOG)['id']
attrs = _get_common_attrs(self.app.client_manager, parsed_args,
is_create=False)
try:
client.update_network_log(log_id, {'log': attrs})
except Exception as e:
msg = (_("Failed to set network log '%(logging)s': %(e)s")
% {'logging': parsed_args.network_log, 'e': e})
raise exceptions.CommandError(msg)
class ShowNetworkLog(command.ShowOne):
_description = _("Display network log details")
def get_parser(self, prog_name):
parser = super(ShowNetworkLog, self).get_parser(prog_name)
parser.add_argument(
'network_log',
metavar='<network-log>',
help=_('Network log to show (name or ID)'))
return parser
def take_action(self, parsed_args):
client = self.app.client_manager.neutronclient
log_id = client.find_resource(
'log', parsed_args.network_log, cmd_resource=NET_LOG)['id']
obj = client.show_network_log(log_id)['log']
columns, display_columns = column_util.get_columns(obj, _attr_map)
data = utils.get_dict_properties(obj, columns)
return (display_columns, data)
|