Merge "Add attach network CLI"

This commit is contained in:
Jenkins 2017-09-14 18:36:01 +00:00 committed by Gerrit Code Review
commit f10770b8b6
8 changed files with 83 additions and 6 deletions

View File

@ -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

View File

@ -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 = {}

View File

@ -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)

View File

@ -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='<container>',
help='ID or name of the container to attach network.')
parser.add_argument(
'network',
metavar='<network>',
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})

View File

@ -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'))

View File

@ -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)

View File

@ -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})

View File

@ -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='<container>',
help='ID or name of the container to attach network.')
@utils.arg('network',
metavar='<network>',
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})