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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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)")
|
|
|
|
parser.add_argument(
|
|
|
|
'--domain',
|
|
|
|
metavar='<domain>',
|
|
|
|
help="Owner's domain (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
|
|
|
|
body = self.get_body(parsed_args)
|
|
|
|
create_method = getattr(client, "create_network")
|
|
|
|
data = create_method(body)['network']
|
|
|
|
if data:
|
2014-09-18 10:35:15 -05:00
|
|
|
data = _prep_network_detail(data)
|
2014-02-13 08:07:51 -07:00
|
|
|
else:
|
|
|
|
data = {'': ''}
|
|
|
|
return zip(*sorted(six.iteritems(data)))
|
|
|
|
|
|
|
|
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
|
|
|
|
if parsed_args.domain is not None:
|
|
|
|
domain = identity_common.find_domain(identity_client,
|
|
|
|
parsed_args.domain)
|
|
|
|
project_id = utils.find_resource(identity_client.projects,
|
|
|
|
parsed_args.project,
|
|
|
|
domain_id=domain.id).id
|
|
|
|
else:
|
|
|
|
project_id = utils.find_resource(identity_client.projects,
|
|
|
|
parsed_args.project).id
|
|
|
|
body['tenant_id'] = project_id
|
2014-02-13 08:07:51 -07:00
|
|
|
return {'network': body}
|
|
|
|
|
|
|
|
|
|
|
|
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(
|
|
|
|
'--dhcp',
|
2014-09-18 10:35:15 -05:00
|
|
|
metavar='<dhcp-id>',
|
|
|
|
help='DHCP agent ID')
|
2014-02-13 08:07:51 -07:00
|
|
|
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)
|
|
|
|
client = self.app.client_manager.network
|
2014-09-18 10:35:15 -05:00
|
|
|
|
2014-02-13 08:07:51 -07:00
|
|
|
if parsed_args.dhcp:
|
2014-09-18 10:35:15 -05:00
|
|
|
data = client.api.dhcp_agent_list(dhcp_id=parsed_args.dhcp)
|
|
|
|
|
|
|
|
columns = ('ID',)
|
|
|
|
column_headers = columns
|
2014-02-13 08:07:51 -07:00
|
|
|
else:
|
2014-09-18 10:35:15 -05:00
|
|
|
data = client.api.network_list(external=parsed_args.external)
|
|
|
|
|
|
|
|
if parsed_args.long:
|
|
|
|
columns = (
|
|
|
|
'ID',
|
|
|
|
'Name',
|
|
|
|
'Status',
|
|
|
|
'project_id',
|
|
|
|
'state',
|
|
|
|
'Shared',
|
|
|
|
'Subnets',
|
|
|
|
'provider:network_type',
|
|
|
|
'router_type',
|
|
|
|
)
|
|
|
|
column_headers = (
|
|
|
|
'ID',
|
|
|
|
'Name',
|
|
|
|
'Status',
|
|
|
|
'Project',
|
|
|
|
'State',
|
|
|
|
'Shared',
|
|
|
|
'Subnets',
|
|
|
|
'Network Type',
|
|
|
|
'Router Type',
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
columns = ('ID', 'Name', 'Subnets')
|
|
|
|
column_headers = columns
|
|
|
|
|
|
|
|
for d in data:
|
|
|
|
d = _prep_network_detail(d)
|
|
|
|
|
|
|
|
return (column_headers,
|
|
|
|
(utils.get_dict_properties(
|
|
|
|
s, columns,
|
|
|
|
formatters={'subnets': utils.format_list},
|
|
|
|
) 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)))
|