summaryrefslogtreecommitdiff
path: root/openstackclient/network
diff options
context:
space:
mode:
authorTerry Howe <terrylhowe@gmail.com>2014-02-13 08:07:51 -0700
committerTerry Howe <terrylhowe@gmail.com>2014-06-24 15:00:25 -0600
commit8f59524c3e1ff3f34474d0cfcd217bc9fe0cbfc1 (patch)
treecbacaf79a662ea9d16231d5c40d7cc59f827abef /openstackclient/network
parentadf9349c40683eb53886365f6df65b8d8515e52c (diff)
downloadpython-openstackclient-8f59524c3e1ff3f34474d0cfcd217bc9fe0cbfc1.tar.gz
Network CRUD
bp/neutron https://wiki.openstack.org/wiki/OpenStackClient/Commands#Network_2 Change-Id: I89ee083154afa544b03587e84becace36d9d522a
Diffstat (limited to 'openstackclient/network')
-rw-r--r--openstackclient/network/__init__.py0
-rw-r--r--openstackclient/network/client.py61
-rw-r--r--openstackclient/network/common.py43
-rw-r--r--openstackclient/network/v2_0/__init__.py0
-rw-r--r--openstackclient/network/v2_0/network.py236
5 files changed, 340 insertions, 0 deletions
diff --git a/openstackclient/network/__init__.py b/openstackclient/network/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/openstackclient/network/__init__.py
diff --git a/openstackclient/network/client.py b/openstackclient/network/client.py
new file mode 100644
index 00000000..3c87e135
--- /dev/null
+++ b/openstackclient/network/client.py
@@ -0,0 +1,61 @@
+# 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 logging
+
+from openstackclient.common import utils
+
+
+LOG = logging.getLogger(__name__)
+
+DEFAULT_NETWORK_API_VERSION = '2.0'
+API_VERSION_OPTION = 'os_network_api_version'
+API_NAME = "network"
+API_VERSIONS = {
+ "2.0": "neutronclient.v2_0.client.Client",
+}
+
+
+def make_client(instance):
+ """Returns an network service client."""
+ network_client = utils.get_client_class(
+ API_NAME,
+ instance._api_version[API_NAME],
+ API_VERSIONS)
+ if not instance._url:
+ instance._url = instance.get_endpoint_for_service_type("network")
+ return network_client(
+ username=instance._username,
+ tenant_name=instance._project_name,
+ password=instance._password,
+ region_name=instance._region_name,
+ auth_url=instance._auth_url,
+ endpoint_url=instance._url,
+ token=instance._token,
+ insecure=instance._insecure,
+ ca_cert=instance._cacert,
+ )
+
+
+def build_option_parser(parser):
+ """Hook to add global options"""
+ parser.add_argument(
+ '--os-network-api-version',
+ metavar='<network-api-version>',
+ default=utils.env(
+ 'OS_NETWORK_API_VERSION',
+ default=DEFAULT_NETWORK_API_VERSION),
+ help='Network API version, default=' +
+ DEFAULT_NETWORK_API_VERSION +
+ ' (Env: OS_NETWORK_API_VERSION)')
+ return parser
diff --git a/openstackclient/network/common.py b/openstackclient/network/common.py
new file mode 100644
index 00000000..5ba44f7b
--- /dev/null
+++ b/openstackclient/network/common.py
@@ -0,0 +1,43 @@
+# 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.
+#
+
+from openstackclient.common import exceptions
+
+
+def find(client, resource, resources, name_or_id):
+ """Find a network resource
+
+ :param client: network client
+ :param resource: name of the resource
+ :param resources: plural name of resource
+ :param name_or_id: name or id of resource user is looking for
+
+ For example:
+ n = find(netclient, 'network', 'networks', 'matrix')
+ """
+ list_method = getattr(client, "list_%s" % resources)
+ # Search for by name
+ data = list_method(name=name_or_id, fields='id')
+ info = data[resources]
+ if len(info) == 1:
+ return info[0]['id']
+ if len(info) > 1:
+ msg = "More than one %s exists with the name '%s'."
+ raise exceptions.CommandError(msg % (resource, name_or_id))
+ # Search for by id
+ data = list_method(id=name_or_id, fields='id')
+ info = data[resources]
+ if len(info) == 1:
+ return info[0]['id']
+ msg = "No %s with a name or ID of '%s' exists." % (resource, name_or_id)
+ raise exceptions.CommandError(msg)
diff --git a/openstackclient/network/v2_0/__init__.py b/openstackclient/network/v2_0/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/openstackclient/network/v2_0/__init__.py
diff --git a/openstackclient/network/v2_0/network.py b/openstackclient/network/v2_0/network.py
new file mode 100644
index 00000000..c0c25e71
--- /dev/null
+++ b/openstackclient/network/v2_0/network.py
@@ -0,0 +1,236 @@
+# 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
+from openstackclient.network import common
+
+
+def filters(data):
+ if 'subnets' in data:
+ data['subnets'] = utils.format_list(data['subnets'])
+ return data
+
+
+class CreateNetwork(show.ShowOne):
+ """Create a network"""
+
+ log = logging.getLogger(__name__ + '.CreateNetwork')
+
+ def get_parser(self, prog_name):
+ parser = super(CreateNetwork, self).get_parser(prog_name)
+ parser.add_argument(
+ 'name', metavar='<network_name>',
+ help='Name of network to create')
+ admin_group = parser.add_mutually_exclusive_group()
+ admin_group.add_argument(
+ '--admin-state-up',
+ dest='admin_state', action='store_true',
+ default=True, help='Set Admin State Up')
+ admin_group.add_argument(
+ '--admin-state-down',
+ dest='admin_state', action='store_false',
+ help='Set Admin State Down')
+ share_group = parser.add_mutually_exclusive_group()
+ share_group.add_argument(
+ '--share',
+ dest='shared', action='store_true',
+ default=None,
+ help='Share the network across tenants')
+ share_group.add_argument(
+ '--no-share',
+ dest='shared', action='store_false',
+ help='Do not share the network across tenants')
+ 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:
+ data = filters(data)
+ 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
+ return {'network': body}
+
+
+class DeleteNetwork(command.Command):
+
+ log = logging.getLogger(__name__ + '.DeleteNetwork')
+
+ def get_parser(self, prog_name):
+ parser = super(DeleteNetwork, self).get_parser(prog_name)
+ parser.add_argument(
+ 'identifier',
+ metavar="<network>",
+ help=("Name or identifier of network to delete")
+ )
+ 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)
+ delete_method = getattr(client, "delete_network")
+ delete_method(_id)
+ 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',
+ help='ID of the DHCP agent')
+ parser.add_argument(
+ '--long',
+ action='store_true',
+ default=False,
+ help='Long listing',
+ )
+ return parser
+
+ def take_action(self, parsed_args):
+ self.log.debug('take_action(%s)' % parsed_args)
+ client = self.app.client_manager.network
+ if parsed_args.dhcp:
+ list_method = getattr(client, 'list_networks_on_dhcp_agent')
+ resources = 'networks_on_dhcp_agent'
+ report_filter = {'dhcp_agent': parsed_args.dhcp}
+ data = list_method(**report_filter)[resources]
+ else:
+ list_method = getattr(client, "list_networks")
+ report_filter = {}
+ if parsed_args.external:
+ report_filter = {'router:external': True}
+ data = list_method(**report_filter)['networks']
+ columns = len(data) > 0 and sorted(data[0].keys()) or []
+ if parsed_args.columns:
+ list_columns = parsed_args.columns
+ else:
+ list_columns = ['id', 'name', 'subnets']
+ if not parsed_args.long and not parsed_args.dhcp:
+ columns = [x for x in list_columns if x in columns]
+ formatters = {'subnets': utils.format_list}
+ return (columns,
+ (utils.get_dict_properties(s, columns, formatters=formatters)
+ for s in data))
+
+
+class SetNetwork(command.Command):
+
+ 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>",
+ help=("Name or identifier of network to set")
+ )
+ admin_group = parser.add_mutually_exclusive_group()
+ admin_group.add_argument(
+ '--admin-state-up',
+ dest='admin_state', action='store_true',
+ default=None,
+ help='Set Admin State Up')
+ admin_group.add_argument(
+ '--admin-state-down',
+ dest='admin_state', action='store_false',
+ help='Set Admin State Down')
+ parser.add_argument(
+ '--name',
+ metavar='<network_name>',
+ help='New name for the network')
+ share_group = parser.add_mutually_exclusive_group()
+ share_group.add_argument(
+ '--share',
+ dest='shared', action='store_true',
+ default=None,
+ help='Share the network across tenants')
+ share_group.add_argument(
+ '--no-share',
+ dest='shared', action='store_false',
+ help='Do not share the network across tenants')
+ 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 == {}:
+ raise exceptions.CommandError("Nothing specified to be set")
+ update_method = getattr(client, "update_network")
+ update_method(_id, {'network': body})
+ return
+
+
+class ShowNetwork(show.ShowOne):
+
+ 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>",
+ help=("Name or identifier of network to show")
+ )
+ 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)
+ show_method = getattr(client, "show_network")
+ data = show_method(_id)['network']
+ data = filters(data)
+ return zip(*sorted(six.iteritems(data)))