2014-02-13 08:07:51 -07:00
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
|
|
|
|
"""Network action implementations"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import six
|
|
|
|
|
|
|
|
from cliff import command
|
|
|
|
from cliff import lister
|
|
|
|
from cliff import show
|
|
|
|
|
2015-12-01 16:00:28 +08:00
|
|
|
from openstack import connection
|
|
|
|
|
2014-02-13 08:07:51 -07:00
|
|
|
from openstackclient.common import exceptions
|
|
|
|
from openstackclient.common import utils
|
2015-02-16 23:21:00 -08:00
|
|
|
from openstackclient.identity import common as identity_common
|
2014-02-13 08:07:51 -07:00
|
|
|
from openstackclient.network import common
|
|
|
|
|
|
|
|
|
2015-12-01 16:00:28 +08:00
|
|
|
def _format_admin_state(item):
|
|
|
|
return 'UP' if item else 'DOWN'
|
|
|
|
|
|
|
|
|
|
|
|
def _format_router_external(item):
|
|
|
|
return 'External' if item else 'Internal'
|
|
|
|
|
|
|
|
|
|
|
|
_formatters = {
|
|
|
|
'subnets': utils.format_list,
|
|
|
|
'admin_state_up': _format_admin_state,
|
|
|
|
'router_external': _format_router_external,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def _make_client_sdk(instance):
|
|
|
|
"""Return a network proxy"""
|
|
|
|
conn = connection.Connection(authenticator=instance.session.auth)
|
|
|
|
return conn.network
|
|
|
|
|
|
|
|
|
2014-09-18 10:35:15 -05:00
|
|
|
def _prep_network_detail(net):
|
|
|
|
"""Prepare network object for output"""
|
|
|
|
if 'subnets' in net:
|
|
|
|
net['subnets'] = utils.format_list(net['subnets'])
|
|
|
|
if 'admin_state_up' in net:
|
|
|
|
net['state'] = 'UP' if net['admin_state_up'] else 'DOWN'
|
|
|
|
net.pop('admin_state_up')
|
|
|
|
if 'router:external' in net:
|
|
|
|
net['router_type'] = 'External' if net['router:external'] \
|
|
|
|
else 'Internal'
|
|
|
|
net.pop('router:external')
|
|
|
|
if 'tenant_id' in net:
|
|
|
|
net['project_id'] = net.pop('tenant_id')
|
|
|
|
return net
|
2014-02-13 08:07:51 -07:00
|
|
|
|
|
|
|
|
|
|
|
class CreateNetwork(show.ShowOne):
|
2015-01-20 16:44:50 -06:00
|
|
|
"""Create new network"""
|
2014-02-13 08:07:51 -07:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__ + '.CreateNetwork')
|
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(CreateNetwork, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
2015-01-20 16:44:50 -06:00
|
|
|
'name',
|
|
|
|
metavar='<name>',
|
|
|
|
help='New network name',
|
|
|
|
)
|
2014-02-13 08:07:51 -07:00
|
|
|
admin_group = parser.add_mutually_exclusive_group()
|
|
|
|
admin_group.add_argument(
|
2014-08-18 05:41:58 -06:00
|
|
|
'--enable',
|
|
|
|
dest='admin_state',
|
|
|
|
action='store_true',
|
2015-01-20 16:44:50 -06:00
|
|
|
default=True,
|
|
|
|
help='Enable network (default)',
|
|
|
|
)
|
2014-02-13 08:07:51 -07:00
|
|
|
admin_group.add_argument(
|
2014-08-18 05:41:58 -06:00
|
|
|
'--disable',
|
|
|
|
dest='admin_state',
|
|
|
|
action='store_false',
|
2015-01-20 16:44:50 -06:00
|
|
|
help='Disable network',
|
|
|
|
)
|
2014-02-13 08:07:51 -07:00
|
|
|
share_group = parser.add_mutually_exclusive_group()
|
|
|
|
share_group.add_argument(
|
|
|
|
'--share',
|
2015-01-20 16:44:50 -06:00
|
|
|
dest='shared',
|
|
|
|
action='store_true',
|
2014-02-13 08:07:51 -07:00
|
|
|
default=None,
|
2015-01-20 16:44:50 -06:00
|
|
|
help='Share the network between projects',
|
|
|
|
)
|
2014-02-13 08:07:51 -07:00
|
|
|
share_group.add_argument(
|
|
|
|
'--no-share',
|
2015-01-20 16:44:50 -06:00
|
|
|
dest='shared',
|
|
|
|
action='store_false',
|
|
|
|
help='Do not share the network between projects',
|
|
|
|
)
|
2015-02-16 23:21:00 -08:00
|
|
|
parser.add_argument(
|
|
|
|
'--project',
|
|
|
|
metavar='<project>',
|
|
|
|
help="Owner's project (name or ID)")
|
2015-06-26 12:07:58 +08:00
|
|
|
identity_common.add_project_domain_option_to_parser(parser)
|
2014-02-13 08:07:51 -07:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
self.log.debug('take_action(%s)' % parsed_args)
|
2015-12-04 19:48:22 +08:00
|
|
|
self.app.client_manager.network = \
|
|
|
|
_make_client_sdk(self.app.client_manager)
|
2014-02-13 08:07:51 -07:00
|
|
|
client = self.app.client_manager.network
|
|
|
|
body = self.get_body(parsed_args)
|
2015-12-04 19:48:22 +08:00
|
|
|
obj = client.create_network(**body)
|
|
|
|
columns = sorted(obj.keys())
|
|
|
|
data = utils.get_item_properties(obj, columns, formatters=_formatters)
|
|
|
|
return (tuple(columns), data)
|
2014-02-13 08:07:51 -07:00
|
|
|
|
|
|
|
def get_body(self, parsed_args):
|
|
|
|
body = {'name': str(parsed_args.name),
|
|
|
|
'admin_state_up': parsed_args.admin_state}
|
|
|
|
if parsed_args.shared is not None:
|
|
|
|
body['shared'] = parsed_args.shared
|
2015-02-16 23:21:00 -08:00
|
|
|
if parsed_args.project is not None:
|
|
|
|
identity_client = self.app.client_manager.identity
|
2015-06-26 12:07:58 +08:00
|
|
|
project_id = identity_common.find_project(
|
|
|
|
identity_client,
|
|
|
|
parsed_args.project,
|
|
|
|
parsed_args.project_domain,
|
|
|
|
).id
|
2015-02-16 23:21:00 -08:00
|
|
|
body['tenant_id'] = project_id
|
2015-12-04 19:48:22 +08:00
|
|
|
return body
|
2014-02-13 08:07:51 -07:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteNetwork(command.Command):
|
2014-12-10 11:47:54 +08:00
|
|
|
"""Delete network(s)"""
|
2014-02-13 08:07:51 -07:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__ + '.DeleteNetwork')
|
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(DeleteNetwork, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
2014-12-10 11:47:54 +08:00
|
|
|
'networks',
|
2014-02-13 08:07:51 -07:00
|
|
|
metavar="<network>",
|
2014-12-10 11:47:54 +08:00
|
|
|
nargs="+",
|
2015-01-20 16:44:50 -06:00
|
|
|
help=("Network to delete (name or ID)")
|
2014-02-13 08:07:51 -07:00
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
self.log.debug('take_action(%s)' % parsed_args)
|
|
|
|
client = self.app.client_manager.network
|
|
|
|
delete_method = getattr(client, "delete_network")
|
2014-12-10 11:47:54 +08:00
|
|
|
for network in parsed_args.networks:
|
|
|
|
_id = common.find(client, 'network', 'networks', network)
|
|
|
|
delete_method(_id)
|
2014-02-13 08:07:51 -07:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
class ListNetwork(lister.Lister):
|
|
|
|
"""List networks"""
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__ + '.ListNetwork')
|
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(ListNetwork, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'--external',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='List external networks',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--long',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
2014-09-18 10:35:15 -05:00
|
|
|
help='List additional fields in output',
|
2014-02-13 08:07:51 -07:00
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
self.log.debug('take_action(%s)' % parsed_args)
|
2015-12-01 15:40:52 +08:00
|
|
|
self.app.client_manager.network = \
|
|
|
|
_make_client_sdk(self.app.client_manager)
|
2014-02-13 08:07:51 -07:00
|
|
|
client = self.app.client_manager.network
|
2014-09-18 10:35:15 -05:00
|
|
|
|
2015-07-08 11:21:41 -06:00
|
|
|
if parsed_args.long:
|
|
|
|
columns = (
|
2015-12-01 15:40:52 +08:00
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'status',
|
|
|
|
'tenant_id',
|
|
|
|
'admin_state_up',
|
|
|
|
'shared',
|
|
|
|
'subnets',
|
|
|
|
'provider_network_type',
|
|
|
|
'router_external',
|
2015-07-08 11:21:41 -06:00
|
|
|
)
|
|
|
|
column_headers = (
|
|
|
|
'ID',
|
|
|
|
'Name',
|
|
|
|
'Status',
|
|
|
|
'Project',
|
|
|
|
'State',
|
|
|
|
'Shared',
|
|
|
|
'Subnets',
|
|
|
|
'Network Type',
|
|
|
|
'Router Type',
|
|
|
|
)
|
2014-02-13 08:07:51 -07:00
|
|
|
else:
|
2015-12-01 15:40:52 +08:00
|
|
|
columns = (
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'subnets'
|
|
|
|
)
|
|
|
|
column_headers = (
|
|
|
|
'ID',
|
|
|
|
'Name',
|
|
|
|
'Subnets',
|
|
|
|
)
|
2014-09-18 10:35:15 -05:00
|
|
|
|
2015-12-01 15:40:52 +08:00
|
|
|
if parsed_args.external:
|
|
|
|
args = {'router:external': True}
|
|
|
|
else:
|
|
|
|
args = {}
|
|
|
|
data = client.networks(**args)
|
2014-09-18 10:35:15 -05:00
|
|
|
return (column_headers,
|
2015-12-01 15:40:52 +08:00
|
|
|
(utils.get_item_properties(
|
2014-09-18 10:35:15 -05:00
|
|
|
s, columns,
|
2015-12-01 15:40:52 +08:00
|
|
|
formatters=_formatters,
|
2014-09-18 10:35:15 -05:00
|
|
|
) for s in data))
|
2014-02-13 08:07:51 -07:00
|
|
|
|
|
|
|
|
|
|
|
class SetNetwork(command.Command):
|
2014-07-17 19:20:53 -04:00
|
|
|
"""Set network properties"""
|
2014-02-13 08:07:51 -07:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__ + '.SetNetwork')
|
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(SetNetwork, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'identifier',
|
|
|
|
metavar="<network>",
|
2015-01-20 16:44:50 -06:00
|
|
|
help=("Network to modify (name or ID)")
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--name',
|
|
|
|
metavar='<name>',
|
|
|
|
help='Set network name',
|
2014-02-13 08:07:51 -07:00
|
|
|
)
|
|
|
|
admin_group = parser.add_mutually_exclusive_group()
|
|
|
|
admin_group.add_argument(
|
2014-08-18 05:41:58 -06:00
|
|
|
'--enable',
|
|
|
|
dest='admin_state',
|
|
|
|
action='store_true',
|
2015-01-20 16:44:50 -06:00
|
|
|
default=None,
|
|
|
|
help='Enable network',
|
|
|
|
)
|
2014-02-13 08:07:51 -07:00
|
|
|
admin_group.add_argument(
|
2014-08-18 05:41:58 -06:00
|
|
|
'--disable',
|
|
|
|
dest='admin_state',
|
|
|
|
action='store_false',
|
2015-01-20 16:44:50 -06:00
|
|
|
help='Disable network',
|
|
|
|
)
|
2014-02-13 08:07:51 -07:00
|
|
|
share_group = parser.add_mutually_exclusive_group()
|
|
|
|
share_group.add_argument(
|
|
|
|
'--share',
|
2015-01-20 16:44:50 -06:00
|
|
|
dest='shared',
|
|
|
|
action='store_true',
|
2014-02-13 08:07:51 -07:00
|
|
|
default=None,
|
2015-01-20 16:44:50 -06:00
|
|
|
help='Share the network between projects',
|
|
|
|
)
|
2014-02-13 08:07:51 -07:00
|
|
|
share_group.add_argument(
|
|
|
|
'--no-share',
|
2015-01-20 16:44:50 -06:00
|
|
|
dest='shared',
|
|
|
|
action='store_false',
|
|
|
|
help='Do not share the network between projects',
|
|
|
|
)
|
2014-02-13 08:07:51 -07:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
self.log.debug('take_action(%s)' % parsed_args)
|
|
|
|
client = self.app.client_manager.network
|
|
|
|
_id = common.find(client, 'network', 'networks',
|
|
|
|
parsed_args.identifier)
|
|
|
|
body = {}
|
|
|
|
if parsed_args.name is not None:
|
|
|
|
body['name'] = str(parsed_args.name)
|
|
|
|
if parsed_args.admin_state is not None:
|
|
|
|
body['admin_state_up'] = parsed_args.admin_state
|
|
|
|
if parsed_args.shared is not None:
|
|
|
|
body['shared'] = parsed_args.shared
|
|
|
|
if body == {}:
|
2014-07-09 13:40:12 -04:00
|
|
|
msg = "Nothing specified to be set"
|
|
|
|
raise exceptions.CommandError(msg)
|
2014-02-13 08:07:51 -07:00
|
|
|
update_method = getattr(client, "update_network")
|
|
|
|
update_method(_id, {'network': body})
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
class ShowNetwork(show.ShowOne):
|
2014-07-17 19:20:53 -04:00
|
|
|
"""Show network details"""
|
2014-02-13 08:07:51 -07:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__ + '.ShowNetwork')
|
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(ShowNetwork, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'identifier',
|
|
|
|
metavar="<network>",
|
2015-01-20 16:44:50 -06:00
|
|
|
help=("Network to display (name or ID)")
|
2014-02-13 08:07:51 -07:00
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
self.log.debug('take_action(%s)' % parsed_args)
|
|
|
|
client = self.app.client_manager.network
|
2014-09-18 10:35:15 -05:00
|
|
|
net = client.api.find_attr(
|
|
|
|
'networks',
|
|
|
|
parsed_args.identifier,
|
|
|
|
)
|
|
|
|
data = _prep_network_detail(net)
|
2014-02-13 08:07:51 -07:00
|
|
|
return zip(*sorted(six.iteritems(data)))
|