| 1 |
# Copyright 2017-2018 FUJTISU 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 |
|
| 17 |
import copy |
| 18 |
|
| 19 |
from osc_lib.command import command |
| 20 |
from osc_lib import exceptions |
| 21 |
from osc_lib import utils |
| 22 |
from osc_lib.utils import columns as column_util |
| 23 |
from oslo_log import log as logging |
| 24 |
|
| 25 |
from neutronclient._i18n import _ |
| 26 |
from neutronclient.common import utils as nc_utils |
| 27 |
from neutronclient.osc import utils as osc_utils |
| 28 |
from neutronclient.osc.v2.fwaas import constants as fwaas_const |
| 29 |
|
| 30 |
LOG = logging.getLogger(__name__) |
| 31 |
|
| 32 |
_attr_map = ( |
| 33 |
('id', 'ID', column_util.LIST_BOTH), |
| 34 |
('description', 'Description', column_util.LIST_LONG_ONLY), |
| 35 |
('enabled', 'Enabled', column_util.LIST_BOTH), |
| 36 |
('name', 'Name', column_util.LIST_BOTH), |
| 37 |
('target_id', 'Target', column_util.LIST_LONG_ONLY), |
| 38 |
('project_id', 'Project', column_util.LIST_LONG_ONLY), |
| 39 |
('resource_id', 'Resource', column_util.LIST_LONG_ONLY), |
| 40 |
('resource_type', 'Type', column_util.LIST_BOTH), |
| 41 |
('event', 'Event', column_util.LIST_LONG_ONLY), |
| 42 |
('summary', 'Summary', column_util.LIST_SHORT_ONLY), |
| 43 |
) |
| 44 |
|
| 45 |
_attr_map_for_loggable = ( |
| 46 |
('type', 'Supported types', column_util.LIST_BOTH), |
| 47 |
) |
| 48 |
|
| 49 |
NET_LOG = 'network_log' |
| 50 |
|
| 51 |
|
| 52 |
def _get_common_parser(parser): |
| 53 |
parser.add_argument( |
| 54 |
'--description', |
| 55 |
metavar='<description>', |
| 56 |
help=_('Description of the network log')) |
| 57 |
enable_group = parser.add_mutually_exclusive_group() |
| 58 |
enable_group.add_argument( |
| 59 |
'--enable', |
| 60 |
action='store_true', |
| 61 |
help=_('Enable this log')) |
| 62 |
enable_group.add_argument( |
| 63 |
'--disable', |
| 64 |
action='store_true', |
| 65 |
help=_('Disable this log (default is enabled)')) |
| 66 |
return parser |
| 67 |
|
| 68 |
|
| 69 |
def _get_common_attrs(client_manager, parsed_args, is_create=True): |
| 70 |
attrs = {} |
| 71 |
client = client_manager.neutronclient |
| 72 |
|
| 73 |
if is_create: |
| 74 |
if 'project' in parsed_args and parsed_args.project is not None: |
| 75 |
attrs['project_id'] = osc_utils.find_project( |
| 76 |
client_manager.identity, |
| 77 |
parsed_args.project, |
| 78 |
parsed_args.project_domain, |
| 79 |
).id |
| 80 |
resource_type = parsed_args.resource_type |
| 81 |
attrs['resource_type'] = resource_type |
| 82 |
if parsed_args.resource: |
| 83 |
cmd_resource = None |
| 84 |
if resource_type == fwaas_const.FWG: |
| 85 |
cmd_resource = fwaas_const.CMD_FWG |
| 86 |
attrs['resource_id'] = client.find_resource( |
| 87 |
resource_type, |
| 88 |
parsed_args.resource, |
| 89 |
cmd_resource=cmd_resource)['id'] |
| 90 |
|
| 91 |
if parsed_args.target: |
| 92 |
# NOTE(yushiro) Currently, we're supporting only port |
| 93 |
attrs['target_id'] = client.find_resource( |
| 94 |
'port', parsed_args.target)['id'] |
| 95 |
if parsed_args.event: |
| 96 |
attrs['event'] = parsed_args.event |
| 97 |
if parsed_args.enable: |
| 98 |
attrs['enabled'] = True |
| 99 |
if parsed_args.disable: |
| 100 |
attrs['enabled'] = False |
| 101 |
if parsed_args.name: |
| 102 |
attrs['name'] = parsed_args.name |
| 103 |
if parsed_args.description: |
| 104 |
attrs['description'] = parsed_args.description |
| 105 |
return attrs |
| 106 |
|
| 107 |
|
| 108 |
class CreateNetworkLog(command.ShowOne): |
| 109 |
_description = _("Create a new network log") |
| 110 |
|
| 111 |
def get_parser(self, prog_name): |
| 112 |
parser = super(CreateNetworkLog, self).get_parser(prog_name) |
| 113 |
_get_common_parser(parser) |
| 114 |
osc_utils.add_project_owner_option_to_parser(parser) |
| 115 |
parser.add_argument( |
| 116 |
'name', |
| 117 |
metavar='<name>', |
| 118 |
help=_('Name for the network log')) |
| 119 |
parser.add_argument( |
| 120 |
'--event', |
| 121 |
metavar='{ALL,ACCEPT,DROP}', |
| 122 |
choices=['ALL', 'ACCEPT', 'DROP'], |
| 123 |
type=nc_utils.convert_to_uppercase, |
| 124 |
help=_('An event to store with log')) |
| 125 |
# NOTE(yushiro) '--resource-type' is managed by following command: |
| 126 |
# "openstack network loggable resources list". Therefore, this option |
| 127 |
# shouldn't have "choices" like ['security_group', 'firewall_group'] |
| 128 |
parser.add_argument( |
| 129 |
'--resource-type', |
| 130 |
metavar='<resource-type>', |
| 131 |
required=True, |
| 132 |
type=nc_utils.convert_to_lowercase, |
| 133 |
help=_('Network log type(s). ' |
| 134 |
'You can see supported type(s) with following command:\n' |
| 135 |
'$ openstack network loggable resources list')) |
| 136 |
parser.add_argument( |
| 137 |
'--resource', |
| 138 |
metavar='<resource>', |
| 139 |
help=_('Name or ID of resource (security group or firewall group) ' |
| 140 |
'that used for logging. You can control for logging target ' |
| 141 |
'combination with --target option.')) |
| 142 |
parser.add_argument( |
| 143 |
'--target', |
| 144 |
metavar='<target>', |
| 145 |
help=_('Port (name or ID) for logging. You can control ' |
| 146 |
'for logging target combination with --resource option.')) |
| 147 |
return parser |
| 148 |
|
| 149 |
def take_action(self, parsed_args): |
| 150 |
client = self.app.client_manager.neutronclient |
| 151 |
attrs = _get_common_attrs(self.app.client_manager, parsed_args) |
| 152 |
obj = client.create_network_log({'log': attrs})['log'] |
| 153 |
columns, display_columns = column_util.get_columns(obj, _attr_map) |
| 154 |
data = utils.get_dict_properties(obj, columns) |
| 155 |
return (display_columns, data) |
| 156 |
|
| 157 |
|
| 158 |
class DeleteNetworkLog(command.Command): |
| 159 |
_description = _("Delete network log(s)") |
| 160 |
|
| 161 |
def get_parser(self, prog_name): |
| 162 |
parser = super(DeleteNetworkLog, self).get_parser(prog_name) |
| 163 |
parser.add_argument( |
| 164 |
'network_log', |
| 165 |
metavar='<network-log>', |
| 166 |
nargs='+', |
| 167 |
help=_('Network log(s) to delete (name or ID)')) |
| 168 |
return parser |
| 169 |
|
| 170 |
def take_action(self, parsed_args): |
| 171 |
client = self.app.client_manager.neutronclient |
| 172 |
result = 0 |
| 173 |
for log_res in parsed_args.network_log: |
| 174 |
try: |
| 175 |
log_id = client.find_resource( |
| 176 |
'log', log_res, cmd_resource=NET_LOG)['id'] |
| 177 |
client.delete_network_log(log_id) |
| 178 |
except Exception as e: |
| 179 |
result += 1 |
| 180 |
LOG.error(_("Failed to delete network log with " |
| 181 |
"name or ID '%(network_log)s': %(e)s"), |
| 182 |
{'network_log': log_res, 'e': e}) |
| 183 |
|
| 184 |
if result > 0: |
| 185 |
total = len(parsed_args.network_log) |
| 186 |
msg = (_("%(result)s of %(total)s network log(s) " |
| 187 |
"failed to delete") % {'result': result, 'total': total}) |
| 188 |
raise exceptions.CommandError(msg) |
| 189 |
|
| 190 |
|
| 191 |
class ListLoggableResource(command.Lister): |
| 192 |
_description = _("List supported loggable resources") |
| 193 |
|
| 194 |
def get_parser(self, prog_name): |
| 195 |
parser = super(ListLoggableResource, self).get_parser(prog_name) |
| 196 |
parser.add_argument( |
| 197 |
'--long', |
| 198 |
action='store_true', |
| 199 |
help=_("List additional fields in output") |
| 200 |
) |
| 201 |
return parser |
| 202 |
|
| 203 |
def take_action(self, parsed_args): |
| 204 |
client = self.app.client_manager.neutronclient |
| 205 |
obj = client.list_network_loggable_resources()['loggable_resources'] |
| 206 |
headers, columns = column_util.get_column_definitions( |
| 207 |
_attr_map_for_loggable, long_listing=parsed_args.long) |
| 208 |
return (headers, (utils.get_dict_properties(s, columns) for s in obj)) |
| 209 |
|
| 210 |
|
| 211 |
class ListNetworkLog(command.Lister): |
| 212 |
_description = _("List network logs") |
| 213 |
|
| 214 |
def get_parser(self, prog_name): |
| 215 |
parser = super(ListNetworkLog, self).get_parser(prog_name) |
| 216 |
parser.add_argument( |
| 217 |
'--long', |
| 218 |
action='store_true', |
| 219 |
help=_("List additional fields in output") |
| 220 |
) |
| 221 |
# TODO(yushiro): We'll support filtering in the future. |
| 222 |
return parser |
| 223 |
|
| 224 |
def _extend_list(self, data, parsed_args): |
| 225 |
ext_data = copy.deepcopy(data) |
| 226 |
for d in ext_data: |
| 227 |
e_prefix = 'Event: ' |
| 228 |
if d['event']: |
| 229 |
event = e_prefix + d['event'].upper() |
| 230 |
port = '(port) ' + d['target_id'] if d['target_id'] else '' |
| 231 |
resource_type = d['resource_type'] |
| 232 |
if d['resource_id']: |
| 233 |
res = '(%s) %s' % (resource_type, d['resource_id']) |
| 234 |
else: |
| 235 |
res = '' |
| 236 |
t_prefix = 'Logged: ' |
| 237 |
if port and res: |
| 238 |
t = '%s on %s' % (res, port) |
| 239 |
else: |
| 240 |
# Either of res and port is empty, so concatenation works fine |
| 241 |
t = res + port |
| 242 |
target = t_prefix + t if t else t_prefix + '(None specified)' |
| 243 |
d['summary'] = ',\n'.join([event, target]) |
| 244 |
return ext_data |
| 245 |
|
| 246 |
def take_action(self, parsed_args): |
| 247 |
client = self.app.client_manager.neutronclient |
| 248 |
obj = client.list_network_logs()['logs'] |
| 249 |
obj_extend = self._extend_list(obj, parsed_args) |
| 250 |
headers, columns = column_util.get_column_definitions( |
| 251 |
_attr_map, long_listing=parsed_args.long) |
| 252 |
return (headers, ( |
| 253 |
utils.get_dict_properties(s, columns) for s in obj_extend)) |
| 254 |
|
| 255 |
|
| 256 |
class SetNetworkLog(command.Command): |
| 257 |
_description = _("Set network log properties") |
| 258 |
|
| 259 |
def get_parser(self, prog_name): |
| 260 |
parser = super(SetNetworkLog, self).get_parser(prog_name) |
| 261 |
_get_common_parser(parser) |
| 262 |
parser.add_argument( |
| 263 |
'network_log', |
| 264 |
metavar='<network-log>', |
| 265 |
help=_('Network log to set (name or ID)')) |
| 266 |
parser.add_argument( |
| 267 |
'--name', |
| 268 |
metavar='<name>', |
| 269 |
help=_('Name of the network log')) |
| 270 |
return parser |
| 271 |
|
| 272 |
def take_action(self, parsed_args): |
| 273 |
client = self.app.client_manager.neutronclient |
| 274 |
log_id = client.find_resource( |
| 275 |
'log', parsed_args.network_log, cmd_resource=NET_LOG)['id'] |
| 276 |
attrs = _get_common_attrs(self.app.client_manager, parsed_args, |
| 277 |
is_create=False) |
| 278 |
try: |
| 279 |
client.update_network_log(log_id, {'log': attrs}) |
| 280 |
except Exception as e: |
| 281 |
msg = (_("Failed to set network log '%(logging)s': %(e)s") |
| 282 |
% {'logging': parsed_args.network_log, 'e': e}) |
| 283 |
raise exceptions.CommandError(msg) |
| 284 |
|
| 285 |
|
| 286 |
class ShowNetworkLog(command.ShowOne): |
| 287 |
_description = _("Display network log details") |
| 288 |
|
| 289 |
def get_parser(self, prog_name): |
| 290 |
parser = super(ShowNetworkLog, self).get_parser(prog_name) |
| 291 |
parser.add_argument( |
| 292 |
'network_log', |
| 293 |
metavar='<network-log>', |
| 294 |
help=_('Network log to show (name or ID)')) |
| 295 |
return parser |
| 296 |
|
| 297 |
def take_action(self, parsed_args): |
| 298 |
client = self.app.client_manager.neutronclient |
| 299 |
log_id = client.find_resource( |
| 300 |
'log', parsed_args.network_log, cmd_resource=NET_LOG)['id'] |
| 301 |
obj = client.show_network_log(log_id)['log'] |
| 302 |
columns, display_columns = column_util.get_columns(obj, _attr_map) |
| 303 |
data = utils.get_dict_properties(obj, columns) |
| 304 |
return (display_columns, data) |