From 4da1666dbcf2500338573759275e1bff96d4995e Mon Sep 17 00:00:00 2001 From: ShunliZhou Date: Fri, 8 Sep 2017 09:45:19 +0800 Subject: [PATCH] Add attach network CLI Change-Id: I45a5f25db777312f3285829d407822aac8d778f2 Depends-On: Ie05b6328449406769be3f5ce02448389bea11473 Partially-Implements: blueprint network-rest-api --- setup.cfg | 1 + zunclient/api_versions.py | 2 +- zunclient/osc/plugin.py | 2 +- zunclient/osc/v1/containers.py | 31 ++++++++++++++++++++++ zunclient/tests/unit/test_shell.py | 8 +++--- zunclient/tests/unit/v1/test_containers.py | 20 ++++++++++++++ zunclient/v1/containers.py | 4 +++ zunclient/v1/containers_shell.py | 21 +++++++++++++++ 8 files changed, 83 insertions(+), 6 deletions(-) diff --git a/setup.cfg b/setup.cfg index 77f09bbe..32d7faa4 100644 --- a/setup.cfg +++ b/setup.cfg @@ -61,6 +61,7 @@ openstack.container.v1 = appcontainer_host_list = zunclient.osc.v1.hosts:ListHost appcontainer_host_show = zunclient.osc.v1.hosts:ShowHost appcontainer_network_detach = zunclient.osc.v1.containers:NetworkDetach + appcontainer_network_attach = zunclient.osc.v1.containers:NetworkAttach [build_sphinx] source-dir = doc/source diff --git a/zunclient/api_versions.py b/zunclient/api_versions.py index 8a0fa6f0..f40a8aaf 100644 --- a/zunclient/api_versions.py +++ b/zunclient/api_versions.py @@ -30,7 +30,7 @@ if not LOG.handlers: HEADER_NAME = "OpenStack-API-Version" SERVICE_TYPE = "container" -DEFAULT_API_VERSION = '1.7' +DEFAULT_API_VERSION = '1.8' _SUBSTITUTIONS = {} diff --git a/zunclient/osc/plugin.py b/zunclient/osc/plugin.py index a6f31b04..c459d761 100644 --- a/zunclient/osc/plugin.py +++ b/zunclient/osc/plugin.py @@ -22,7 +22,7 @@ LOG = logging.getLogger(__name__) DEFAULT_CONTAINER_API_VERSION = api_versions.DEFAULT_API_VERSION API_VERSION_OPTION = "os_container_api_version" API_NAME = "container" -LAST_KNOWN_API_VERSION = 7 +LAST_KNOWN_API_VERSION = 8 API_VERSIONS = { '1.%d' % i: 'zunclient.v1.client.Client' for i in range(1, LAST_KNOWN_API_VERSION + 1) diff --git a/zunclient/osc/v1/containers.py b/zunclient/osc/v1/containers.py index 0e03dd26..899ef682 100644 --- a/zunclient/osc/v1/containers.py +++ b/zunclient/osc/v1/containers.py @@ -1015,3 +1015,34 @@ class NetworkDetach(command.Command): except Exception as e: print("Detach network for container %(container)s failed: " "%(e)s" % {'container': parsed_args.container, 'e': e}) + + +class NetworkAttach(command.Command): + """Attach neutron network to specified container.""" + log = logging.getLogger(__name__ + ".NetworkAttach") + + def get_parser(self, prog_name): + parser = super(NetworkAttach, self).get_parser(prog_name) + parser.add_argument( + 'container', + metavar='', + help='ID or name of the container to attach network.') + parser.add_argument( + 'network', + metavar='', + help='The network for specified container to attach.') + return parser + + def take_action(self, parsed_args): + client = _get_client(self, parsed_args) + opts = {} + opts['container'] = parsed_args.container + opts['network'] = parsed_args.network + opts = zun_utils.remove_null_parms(**opts) + try: + client.containers.network_attach(**opts) + print("Request to attach network to container %s " + "has been accepted." % parsed_args.container) + except Exception as e: + print("Attach network to container %(container)s failed: " + "%(e)s" % {'container': parsed_args.container, 'e': e}) diff --git a/zunclient/tests/unit/test_shell.py b/zunclient/tests/unit/test_shell.py index c8dadd88..d5ef2cf5 100644 --- a/zunclient/tests/unit/test_shell.py +++ b/zunclient/tests/unit/test_shell.py @@ -246,7 +246,7 @@ class ShellTest(utils.TestCase): project_domain_id='', project_domain_name='', user_domain_id='', user_domain_name='', profile=None, endpoint_override=None, insecure=False, - version=api_versions.APIVersion('1.7')) + version=api_versions.APIVersion('1.8')) def test_main_option_region(self): self.make_env() @@ -274,7 +274,7 @@ class ShellTest(utils.TestCase): project_domain_id='', project_domain_name='', user_domain_id='', user_domain_name='', profile=None, endpoint_override=None, insecure=False, - version=api_versions.APIVersion('1.7')) + version=api_versions.APIVersion('1.8')) @mock.patch('zunclient.client.Client') def test_main_endpoint_internal(self, mock_client): @@ -288,7 +288,7 @@ class ShellTest(utils.TestCase): project_domain_id='', project_domain_name='', user_domain_id='', user_domain_name='', profile=None, endpoint_override=None, insecure=False, - version=api_versions.APIVersion('1.7')) + version=api_versions.APIVersion('1.8')) class ShellTestKeystoneV3(ShellTest): @@ -319,4 +319,4 @@ class ShellTestKeystoneV3(ShellTest): project_domain_id='', project_domain_name='Default', user_domain_id='', user_domain_name='Default', endpoint_override=None, insecure=False, profile=None, - version=api_versions.APIVersion('1.7')) + version=api_versions.APIVersion('1.8')) diff --git a/zunclient/tests/unit/v1/test_containers.py b/zunclient/tests/unit/v1/test_containers.py index 13b8495f..1596671b 100644 --- a/zunclient/tests/unit/v1/test_containers.py +++ b/zunclient/tests/unit/v1/test_containers.py @@ -317,6 +317,14 @@ fake_responses = { None, ), }, + '/v1/containers/%s/network_attach?%s' + % (CONTAINER1['id'], parse.urlencode({'network': 'neutron_network'})): + { + 'POST': ( + {}, + None, + ), + }, } @@ -657,3 +665,15 @@ class ContainerManagerTest(testtools.TestCase): ] self.assertEqual(expect, self.api.calls) self.assertTrue(containers) + + def test_containers_network_attach(self): + containers = self.mgr.network_attach( + CONTAINER1['id'], 'neutron_network') + expect = [ + ('POST', '/v1/containers/%s/network_attach?%s' + % (CONTAINER1['id'], + parse.urlencode({'network': 'neutron_network'})), + {'Content-Length': '0'}, None) + ] + self.assertEqual(expect, self.api.calls) + self.assertTrue(containers) diff --git a/zunclient/v1/containers.py b/zunclient/v1/containers.py index cff3698c..1b07626f 100644 --- a/zunclient/v1/containers.py +++ b/zunclient/v1/containers.py @@ -211,3 +211,7 @@ class ContainerManager(base.Manager): def network_detach(self, container, network): return self._action(container, '/network_detach', qparams={'network': network}) + + def network_attach(self, container, network): + return self._action(container, '/network_attach', + qparams={'network': network}) diff --git a/zunclient/v1/containers_shell.py b/zunclient/v1/containers_shell.py index 9b5aa8ca..d385a11a 100644 --- a/zunclient/v1/containers_shell.py +++ b/zunclient/v1/containers_shell.py @@ -722,3 +722,24 @@ def do_network_detach(cs, args): except Exception as e: print("Detach network from container %(container)s " "failed: %(e)s" % {'container': args.container, 'e': e}) + + +@utils.arg('container', + metavar='', + help='ID or name of the container to attach network.') +@utils.arg('network', + metavar='', + help='The neutron network that container will attach to.') +def do_network_attach(cs, args): + """Attach a network to the container.""" + opts = {} + opts['container'] = args.container + opts['network'] = args.network + opts = zun_utils.remove_null_parms(**opts) + try: + cs.containers.network_attach(**opts) + print("Request to attach network to container %s " + "has been accepted." % args.container) + except Exception as e: + print("Attach network to container %(container)s " + "failed: %(e)s" % {'container': args.container, 'e': e})