| 1 |
# All Rights Reserved 2018 |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import logging |
| 16 |
|
| 17 |
from osc_lib.cli import identity as identity_utils |
| 18 |
from osc_lib.command import command |
| 19 |
from osc_lib import exceptions |
| 20 |
from osc_lib import utils as osc_utils |
| 21 |
from osc_lib.utils import columns as column_util |
| 22 |
|
| 23 |
from neutronclient.osc import utils as nc_osc_utils |
| 24 |
|
| 25 |
from networking_l2gw._i18n import _ |
| 26 |
from networking_l2gw.l2gatewayclient.osc import sdk_objects |
| 27 |
|
| 28 |
|
| 29 |
LOG = logging.getLogger(__name__) |
| 30 |
|
| 31 |
L2_GATEWAY_CONNECTION = 'l2_gateway_connection' |
| 32 |
L2_GATEWAY_CONNECTIONS = '%ss' % L2_GATEWAY_CONNECTION |
| 33 |
|
| 34 |
path = 'l2-gateway-connections' |
| 35 |
object_path = '/%s' % path |
| 36 |
resource_path = '/%s/%%s' % path |
| 37 |
|
| 38 |
_attr_map = ( |
| 39 |
('id', 'ID', column_util.LIST_BOTH), |
| 40 |
('tenant_id', 'Tenant', column_util.LIST_LONG_ONLY), |
| 41 |
('l2_gateway_id', 'L2 GateWay ID', column_util.LIST_BOTH), |
| 42 |
('network_id', 'Network ID', column_util.LIST_BOTH), |
| 43 |
('segmentation_id', 'Segmentation ID', column_util.LIST_BOTH), |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
class CreateL2gwConnection(command.ShowOne): |
| 48 |
_description = _("Create l2gateway-connection") |
| 49 |
|
| 50 |
def retrieve_ids(self, client, args): |
| 51 |
gateway_id = client._find(sdk_objects.L2gw, args.gateway_name).id |
| 52 |
network_id = client.find_network(args.network).id |
| 53 |
return gateway_id, network_id |
| 54 |
|
| 55 |
def get_parser(self, prog_name): |
| 56 |
parser = super().get_parser(prog_name) |
| 57 |
parser.add_argument('gateway_name', metavar='<GATEWAY-NAME/UUID>', |
| 58 |
help=_('Descriptive name for logical gateway.')) |
| 59 |
parser.add_argument( |
| 60 |
'network', metavar='<NETWORK-NAME/UUID>', |
| 61 |
help=_('Network name or uuid.')) |
| 62 |
parser.add_argument( |
| 63 |
'--default-segmentation-id', |
| 64 |
dest='seg_id', |
| 65 |
help=_('default segmentation-id that will ' |
| 66 |
'be applied to the interfaces for which ' |
| 67 |
'segmentation id was not specified ' |
| 68 |
'in l2-gateway-create command.')) |
| 69 |
return parser |
| 70 |
|
| 71 |
def take_action(self, parsed_args): |
| 72 |
client = self.app.client_manager.network |
| 73 |
(gateway_id, network_id) = self.retrieve_ids(client, parsed_args) |
| 74 |
body = {'l2_gateway_id': gateway_id, 'network_id': network_id} |
| 75 |
if parsed_args.seg_id: |
| 76 |
body['segmentation_id'] = parsed_args.seg_id |
| 77 |
obj = client._create(sdk_objects.L2gwConnection, **body) |
| 78 |
display_columns, columns = nc_osc_utils._get_columns(obj) |
| 79 |
data = osc_utils.get_dict_properties(obj, columns) |
| 80 |
return display_columns, data |
| 81 |
|
| 82 |
|
| 83 |
class ListL2gwConnection(command.Lister): |
| 84 |
_description = _("List l2gateway-connections") |
| 85 |
|
| 86 |
def get_parser(self, prog_name): |
| 87 |
parser = super().get_parser(prog_name) |
| 88 |
identity_utils.add_project_owner_option_to_parser(parser) |
| 89 |
|
| 90 |
return parser |
| 91 |
|
| 92 |
def take_action(self, parsed_args): |
| 93 |
client = self.app.client_manager.network |
| 94 |
params = {} |
| 95 |
if parsed_args.project is not None: |
| 96 |
project_id = identity_utils.find_project( |
| 97 |
self.app.client_manager.identity, |
| 98 |
parsed_args.project, |
| 99 |
parsed_args.project_domain, |
| 100 |
).id |
| 101 |
params['tenant_id'] = project_id |
| 102 |
objs = client._list(sdk_objects.L2gwConnection, |
| 103 |
retrieve_all=True, params=params) |
| 104 |
headers, columns = column_util.get_column_definitions( |
| 105 |
_attr_map, long_listing=True) |
| 106 |
return (headers, (osc_utils.get_dict_properties( |
| 107 |
s, columns) for s in objs)) |
| 108 |
|
| 109 |
|
| 110 |
class ShowL2gwConnection(command.ShowOne): |
| 111 |
_description = _("Show information of a given l2gateway-connection") |
| 112 |
|
| 113 |
def get_parser(self, prog_name): |
| 114 |
parser = super().get_parser(prog_name) |
| 115 |
parser.add_argument( |
| 116 |
L2_GATEWAY_CONNECTION, |
| 117 |
metavar="<L2_GATEWAY_CONNECTION>", |
| 118 |
help=_("ID of l2_gateway_connection to look up."), |
| 119 |
) |
| 120 |
return parser |
| 121 |
|
| 122 |
def take_action(self, parsed_args): |
| 123 |
client = self.app.client_manager.network |
| 124 |
id = client._find(sdk_objects.L2gwConnection, |
| 125 |
parsed_args.l2_gateway_connection) |
| 126 |
obj = client._get(sdk_objects.L2gwConnection, id) |
| 127 |
display_columns, columns = nc_osc_utils._get_columns(obj) |
| 128 |
data = osc_utils.get_dict_properties(obj, columns) |
| 129 |
return display_columns, data |
| 130 |
|
| 131 |
|
| 132 |
class DeleteL2gwConnection(command.Command): |
| 133 |
_description = _("Delete a given l2gateway-connection") |
| 134 |
|
| 135 |
def get_parser(self, prog_name): |
| 136 |
parser = super().get_parser(prog_name) |
| 137 |
parser.add_argument( |
| 138 |
L2_GATEWAY_CONNECTIONS, |
| 139 |
metavar="<L2_GATEWAY_CONNECTIONS>", |
| 140 |
nargs="+", |
| 141 |
help=_("ID(s) of l2_gateway_connections(s) to delete."), |
| 142 |
) |
| 143 |
return parser |
| 144 |
|
| 145 |
def take_action(self, parsed_args): |
| 146 |
client = self.app.client_manager.network |
| 147 |
fails = 0 |
| 148 |
for id_or_name in parsed_args.l2_gateway_connections: |
| 149 |
try: |
| 150 |
id = client._find(sdk_objects.L2gwConnection, id_or_name).id |
| 151 |
client._delete(sdk_objects.L2gwConnection, id) |
| 152 |
LOG.warning("L2 Gateaway Connection %(id)s deleted", |
| 153 |
{'id': id}) |
| 154 |
except Exception as e: |
| 155 |
fails += 1 |
| 156 |
LOG.error("Failed to delete L2 Gateway Connection with name " |
| 157 |
"or ID '%(id_or_name)s': %(e)s", |
| 158 |
{'id_or_name': id_or_name, 'e': e}) |
| 159 |
if fails > 0: |
| 160 |
msg = (_("Failed to delete %(fails)s of %(total)s L2 Gateway " |
| 161 |
"Connection.") % |
| 162 |
{'fails': fails, 'total': len( |
| 163 |
parsed_args.l2_gateway_connections)}) |
| 164 |
raise exceptions.CommandError(msg) |