def create_port(self, tenant_id, network_id, host_id=None, subnet_id=None, fixed_ip=None, device_owner=None, device_id=None, mac_address=None, port_security_enabled=True, security_group_ids=None, dhcp_opts=None, **kwargs): try: port_req_body = {'port': {}} port_req_body['port']['network_id'] = network_id port_req_body['port']['admin_state_up'] = True port_req_body['port']['tenant_id'] = tenant_id if not port_security_enabled: port_req_body['port']['port_security_enabled'] = ( port_security_enabled) elif security_group_ids: port_req_body['port']['security_groups'] = security_group_ids if mac_address: port_req_body['port']['mac_address'] = mac_address if host_id: if not self._has_port_binding_extension(): msg = ("host_id (%(host_id)s) specified but neutron " "doesn't support port binding. Please activate the " "extension accordingly." % { "host_id": host_id }) raise exception.NetworkException(message=msg) port_req_body['port']['binding:host_id'] = host_id if dhcp_opts is not None: port_req_body['port']['extra_dhcp_opts'] = dhcp_opts if subnet_id: fixed_ip_dict = {'subnet_id': subnet_id} if fixed_ip: fixed_ip_dict.update({'ip_address': fixed_ip}) port_req_body['port']['fixed_ips'] = [fixed_ip_dict] if device_owner: port_req_body['port']['device_owner'] = device_owner if device_id: port_req_body['port']['device_id'] = device_id if kwargs: port_req_body['port'].update(kwargs) port = self.client.create_port(port_req_body).get('port', {}) return port except neutron_client_exc.NeutronClientException as e: LOG.exception('Neutron error creating port on network %s', network_id) if e.status_code == 409: raise exception.PortLimitExceeded() raise exception.NetworkException(code=e.status_code, message=e.message)
def create_port(self, tenant_id, network_id, host_id=None, subnet_id=None, fixed_ip=None, device_owner=None, device_id=None, mac_address=None, security_group_ids=None, dhcp_opts=None): try: port_req_body = {'port': {}} port_req_body['port']['network_id'] = network_id port_req_body['port']['admin_state_up'] = True port_req_body['port']['tenant_id'] = tenant_id if security_group_ids: port_req_body['port']['security_groups'] = security_group_ids if mac_address: port_req_body['port']['mac_address'] = mac_address if self._has_port_binding_extension() and host_id: port_req_body['port']['binding:host_id'] = host_id if dhcp_opts is not None: port_req_body['port']['extra_dhcp_opts'] = dhcp_opts if subnet_id: fixed_ip_dict = {'subnet_id': subnet_id} if fixed_ip: fixed_ip_dict.update({'ip_address': fixed_ip}) port_req_body['port']['fixed_ips'] = [fixed_ip_dict] if device_owner: port_req_body['port']['device_owner'] = device_owner if device_id: port_req_body['port']['device_id'] = device_id port = self.client.create_port(port_req_body).get('port', {}) return port except neutron_client_exc.NeutronClientException as e: LOG.exception(_LE('Neutron error creating port on network %s'), network_id) if e.status_code == 409: raise exception.PortLimitExceeded() raise exception.NetworkException(code=e.status_code, message=e.message)
def security_group_rule_create(self, parent_group_id, ip_protocol=None, from_port=None, to_port=None, cidr=None, group_id=None, direction="ingress"): request = { "security_group_id": parent_group_id, "protocol": ip_protocol, "remote_ip_prefix": cidr, "remote_group_id": group_id, "direction": direction } if ip_protocol != "icmp": request["port_range_min"] = from_port request["port_range_max"] = to_port try: return self.client.create_security_group_rule( {"security_group_rule": request}) except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def show_port(self, port_id): """Return the port for the client given the port id.""" try: return self.client.show_port(port_id).get('port') except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def get_subnet(self, subnet_uuid): """Get specific subnet for client.""" try: return self.client.show_subnet(subnet_uuid).get('subnet', {}) except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def _wait_for_ports_bind(self, ports, share_server): inactive_ports = [] for port in ports: port = self._neutron_api.show_port(port['id']) if (port['status'] == neutron_constants.PORT_STATUS_ERROR or ('binding:vif_type' in port and port['binding:vif_type'] == neutron_constants.VIF_TYPE_BINDING_FAILED)): msg = _("Port binding %s failed.") % port['id'] raise exception.NetworkException(msg) elif port['status'] != neutron_constants.PORT_STATUS_ACTIVE: LOG.debug( "The port %(id)s is in state %(state)s. " "Wait for active state.", { "id": port['id'], "state": port['status'] }) inactive_ports.append(port['id']) if len(inactive_ports) == 0: return msg = _("Ports are not fully bound for share server " "'%(s_id)s' (inactive ports: %(ports)s)") % { "s_id": share_server['id'], "ports": inactive_ports } raise exception.NetworkBindException(msg)
def _allocate_network(self, context, share_server, share_network, **kwargs): """Allocate network resources using one Nova network.""" allocations = [] allocation_count = kwargs.get('count', 1) if allocation_count < 1: return allocations nova_net_id = share_network.get('nova_net_id') if not nova_net_id: raise exception.NetworkException( _("'nova_net_id' is not provided with share network.")) # NOTE(vponomaryov): nova network should be taken using admin context # because several required attrs of network are available # only for admins. nova_net = self.nova_api.network_get(self.admin_context, nova_net_id) self._save_network_info(context, nova_net, share_network) ip_addresses = self._get_available_ips(context, nova_net, allocation_count) for ip_address in ip_addresses: data = { 'share_server_id': share_server['id'], 'ip_address': ip_address, 'status': constants.STATUS_ACTIVE, 'label': self.label, 'cidr': share_network['cidr'], 'gateway': share_network['gateway'], 'ip_version': share_network['ip_version'], 'segmentation_id': share_network['segmentation_id'], 'network_type': share_network['network_type'], } self.nova_api.fixed_ip_reserve(self.admin_context, ip_address) allocations.append(self.db.network_allocation_create( context, data)) return allocations
def get_network(self, network_uuid): """Get specific network for client.""" try: network = self.client.show_network(network_uuid).get('network', {}) return network except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def admin_project_id(self): if self.client.httpclient.auth_token is None: try: self.client.httpclient.authenticate() except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message) return self.client.httpclient.auth_tenant_id
def router_create(self, tenant_id, name): router_req_body = {'router': {}} router_req_body['router']['tenant_id'] = tenant_id router_req_body['router']['name'] = name try: return self.client.create_router(router_req_body).get('router', {}) except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def update_port_fixed_ips(self, port_id, fixed_ips): try: port_req_body = {'port': fixed_ips} port = self.client.update_port(port_id, port_req_body).get('port', {}) return port except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def security_group_create(self, name, description=""): try: return self.client.create_security_group({ "name": name, "description": description }) except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def router_update_routes(self, router_id, routes): try: router_req_body = {'router': routes} port = self.client.update_router(router_id, router_req_body).get( 'router', {}) return port except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def update_subnet(self, subnet_uuid, name): """Update specific subnet for client.""" subnet_req_body = {'subnet': {'name': name}} try: return self.client.update_subnet(subnet_uuid, subnet_req_body).get( 'subnet', {}) except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def network_create(self, tenant_id, name): network_req_body = {'network': {}} network_req_body['network']['tenant_id'] = tenant_id network_req_body['network']['name'] = name try: return self.client.create_network(network_req_body).get( 'network', {}) except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def send_api(self, method, params=None, request_type='post'): if params: params = json.dumps(params) url = ('http://%(hostname)s:%(port)s/%(rest)s/%(method)s' % {'hostname': self._hostname, 'port': self._port, 'rest': 'rest', 'method': method}) # header is not needed when the driver login the backend if method == 'security/token': # token won't be return to the token_pool if request_type == 'delete': header = {'X-Auth-Token': self._token_pool.pop(0)} else: header = None else: if len(self._token_pool) == 0: self.logins() token = self._token_pool.pop(0) header = {'X-Auth-Token': token} self._token_pool.append(token) response = self.do_request(request_type, url, header, params) try: code = response.get('code') if code == 0: if request_type == 'get': data = response.get('data') else: if method == 'security/token': data = response.get('data') else: data = response.get('message') data = str(data).lower() if hasattr(data, 'success'): return elif code == 301: msg = _('Token is expired') LOG.error(msg) raise exception.NetworkException(msg) else: message = response.get('message') msg = (_('Unexpected RestAPI response: %(code)d %(msg)s') % { 'code': code, 'msg': message}) LOG.error(msg) raise exception.ShareBackendException(msg) except ValueError: msg = _("Deal with response failed") raise exception.ShareBackendException(msg) return data
def router_remove_interface(self, router_id, subnet_id, port_id=None): body = {} if subnet_id: body['subnet_id'] = subnet_id if port_id: body['port_id'] = port_id try: self.client.remove_interface_router(router_id, body) except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def subnet_create(self, tenant_id, net_id, name, cidr): subnet_req_body = {'subnet': {}} subnet_req_body['subnet']['tenant_id'] = tenant_id subnet_req_body['subnet']['name'] = name subnet_req_body['subnet']['network_id'] = net_id subnet_req_body['subnet']['cidr'] = cidr subnet_req_body['subnet']['ip_version'] = 4 try: return self.client.create_subnet(subnet_req_body).get('subnet', {}) except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def _update_port_host_id(port_id, host_id): from manila import exception from manila.network.neutron import api as neutron_api from neutronclient.common import exceptions as neutron_client_exc try: port_req_body = {'port': {'binding:host_id': host_id}} port = neutron_api.API().client.update_port( port_id, port_req_body).get('port', {}) return port except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def security_group_rule_create(self, parent_group_id, ip_protocol=None, from_port=None, to_port=None, cidr=None, group_id=None): try: return self.client.create_security_group_rule({ "parent_group_id": parent_group_id, "ip_protocol": ip_protocol, "from_port": from_port, "to_port": to_port, "cidr": cidr, "group_id": group_id, }) except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def do_request(cmd, url, header, data): LOG.debug('CMD: %(cmd)s, URL: %(url)s, DATA: %(data)s', {'cmd': cmd, 'url': url, 'data': data}) if cmd == 'post': req = requests.post(url, data=data, headers=header) elif cmd == 'get': req = requests.get(url, data=data, headers=header) elif cmd == 'put': req = requests.put(url, data=data, headers=header) elif cmd == 'delete': req = requests.delete(url, data=data, headers=header) else: msg = (_('Unsupported cmd: %s') % cmd) raise exception.ShareBackendException(msg) response = req.json() code = req.status_code LOG.debug('CODE: %(code)s, RESPONSE: %(response)s', {'code': code, 'response': response}) if code != 200: msg = (_('Code: %(code)s, URL: %(url)s, Message: %(msg)s') % {'code': req.status_code, 'url': req.url, 'msg': req.text}) LOG.error(msg) raise exception.NetworkException(msg) return response
def security_group_list(self, search_opts=None): try: return self.client.list_security_groups(**search_opts) except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def delete_subnet(self, subnet_id): try: self.client.delete_subnet(subnet_id) except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def show_router(self, router_id): try: return self.client.show_router(router_id).get('router', {}) except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)
def send_api(self, method, params=None, request_type='post'): if params is not None: params = json.dumps(params) url = ('http://%(hostname)s:%(port)s/%(rest)s/%(method)s' % { 'hostname': self._hostname, 'port': self._port, 'rest': 'rest', 'method': method }) # header is not needed when the driver login the backend if method == 'security/token': # token won't be return to the token_pool if request_type == 'delete': header = {'X-Auth-Token': self._token_pool.pop(0)} else: header = None else: if len(self._token_pool) == 0: self.logins() token = self._token_pool.pop(0) header = {'X-Auth-Token': token} self._token_pool.append(token) if request_type == 'post': req = requests.post(url, data=params, headers=header) elif request_type == 'get': req = requests.get(url, data=params, headers=header) elif request_type == 'put': req = requests.put(url, data=params, headers=header) elif request_type == 'delete': req = requests.delete(url, data=params, headers=header) else: msg = 'Unsupported request_type: %s' % request_type raise exception.ShareBackendException(msg) if req.status_code != 200: msg = 'Error code: %(code)s , API: %(api)s , Message: %(msg)s'\ % {'code': req.status_code, 'api': req.url, 'msg': req.text} LOG.error(msg) raise exception.NetworkException(msg) try: response = req.json() code = response.get('code') if code == 0: if request_type == 'get': data = response.get('data') else: if method == 'security/token': data = response.get('data') else: data = response.get('message') data = str(data).lower() if hasattr(data, 'success'): return elif code == 301: msg = 'Token is out time' LOG.error(msg) raise exception.NetworkException(msg) else: message = response.get('message') # response['message'] msg = ('The RestAPI exception output:' 'Message:%s, Code:%s' % (message, code)) LOG.error(msg) raise exception.ShareBackendException(msg) except ValueError: raise exception.ShareBackendException(msg) data = None req.close() return data
def router_list(self): try: return self.client.list_routers().get('routers', {}) except neutron_client_exc.NeutronClientException as e: raise exception.NetworkException(code=e.status_code, message=e.message)