From cd9d2dbbce3ac038ea8846cfb59701ab6b126be7 Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Fri, 4 Aug 2017 22:48:33 +0000 Subject: [PATCH] Implement create with specified neutron net Support creating container with specified neutron net. Users can pass the uuid/name of an existing neutron network via the 'nets' parameter. If network is specified, Zun will use the specified network instead of searching one. NOTE: If both 'port' and 'network' is set via the 'nets' parameter, the 'network' will be ignored since Zun will use the network of the existing port. Change-Id: I8a0f4f7461a99c1c3bafdfd2baa5b51800292230 Partial-Implements: blueprint multiple-networks Closes-Bug: #1705154 --- zun/api/controllers/v1/containers.py | 8 +++++++- zun/network/neutron.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/zun/api/controllers/v1/containers.py b/zun/api/controllers/v1/containers.py index 40d2d81f0..603ae1292 100644 --- a/zun/api/controllers/v1/containers.py +++ b/zun/api/controllers/v1/containers.py @@ -302,13 +302,19 @@ class ContainersController(base.Controller): neutron_api = neutron.NeutronAPI(context) requested_networks = [] for net in nets: - if 'port' in net: + if net.get('port'): port = neutron_api.get_neutron_port(net['port']) neutron_api.ensure_neutron_port_usable(port) requested_networks.append({'network': port['network_id'], 'port': port['id'], 'v4-fixed-ip': '', 'v6-fixed-ip': ''}) + elif net.get('network'): + network = neutron_api.get_neutron_network(net['network']) + requested_networks.append({'network': network['id'], + 'port': '', + 'v4-fixed-ip': '', + 'v6-fixed-ip': ''}) if not requested_networks: # Find an available neutron net and create docker network by diff --git a/zun/network/neutron.py b/zun/network/neutron.py index c92efcbbc..79d30bf7e 100644 --- a/zun/network/neutron.py +++ b/zun/network/neutron.py @@ -33,6 +33,22 @@ class NeutronAPI(object): nets.sort(key=lambda x: x['created_at']) return nets[0] + def get_neutron_network(self, network): + if uuidutils.is_uuid_like(network): + networks = self.neutron.list_networks(id=network)['networks'] + else: + networks = self.neutron.list_networks(name=network)['networks'] + + if len(networks) == 0: + raise exception.NetworkNotFound(network=network) + elif len(networks) > 1: + raise exception.Conflict(_( + 'Multiple neutron networks exist with same name. ' + 'Please use the uuid instead.')) + + network = networks[0] + return network + def get_neutron_port(self, port): if uuidutils.is_uuid_like(port): ports = self.neutron.list_ports(id=port)['ports']