def list_floating_ips(self, req, tenant_id): ''' list all floating ip ''' result, headers = self._nova_request(req) auth_token = req.context.auth_tok try: for floating_ip in result['floating_ips']: project_id = floating_ip['project_id'] if project_id is not None: tenant = ops_api.get_tenant(project_id, auth_token) project_name = tenant['name'] floating_ip.update(project_name=project_name) except KeyError: LOG.exception(_("list floating ip error.")) raise exc.HTTPFailedDependency(_("Nova method deprecated.")) except exc.HTTPNotFound: LOG.error(_("project_id %s not found.") % project_id) raise exc.HTTPNotFound(_("project_id %s not found for" " floating ip %s.") % (project_id, floating_ip)) pools = result.get('pools', None) ips = result.get('floating_ips', None) ips = list_filter.filter_floating_ips(req, ips) ips = list_sort.sort_floating_ips(req, ips) if pools is not None: return dict(pools=pools, floating_ips=ips) else: return dict(floating_ips=ips)
def test_floating_ips_unicode(self): def generate_floating_ips(ip): ips = { "ip": ip } return ips servers = [] fixed_ips = [u"10.120.40.40", u"10.120.41.40", u"10.120.42.80", u"10.120.42.78"] for fixed_ip in fixed_ips: servers.append(generate_floating_ips(fixed_ip)) req = webob.Request.blank( '/sort?umbrella_sort_key=ip&umbrella_sort_dir=desc') result = list_sort.sort_floating_ips(req, servers) expected = [generate_floating_ips(fixed_ips[2]), generate_floating_ips(fixed_ips[3]), generate_floating_ips(fixed_ips[1]), generate_floating_ips(fixed_ips[0])] self.assertEqual(result, expected)
def test_sort_floating_ips(self): floating_ips = [{ "instance_name": "bd", "ip": "10.120.240.225", "instance_id":"82fbc5f8-e60f-440a-a9a6-a5ab32cd2f68", "project_id": "dc32392af0ae4a098fb7235760077fa6", "project_name": "admin", "type": "public", "id": 1, "pool": "nova2" }, { "instance_name": "de", "ip": "10.120.241.224", "instance_id":"82fbc5f8-e60f-440a-a9a6-a5ab32cd2f68", "project_id": "abcd", "project_name": "Project_abcd", "type": "public", "id": 1, "pool": "nova1" }, { "instance_name": "ab", "ip": "10.120.240.222", "instance_id":"82fbc5f8-e60f-440a-a9a6-a5ab32cd2f68", "project_id": "dc32392af0ae4a098fb7235760077fa6", "project_name": "admin", "type": "private", "id": 1, "pool": "nova" }] req = webob.Request.blank( '/sort?umbrella_sort_key=tenantid') result = list_sort.sort_floating_ips(req, floating_ips) self.assertEqual(result, [floating_ips[1], floating_ips[0], floating_ips[2]]) req = webob.Request.blank( '/sort?umbrella_sort_key=tenant_name') result = list_sort.sort_floating_ips(req, floating_ips) self.assertEqual(result, [floating_ips[0], floating_ips[2], floating_ips[1]]) req = webob.Request.blank( '/sort?umbrella_sort_key=ip') result = list_sort.sort_floating_ips(req, floating_ips) self.assertEqual(result, [floating_ips[2], floating_ips[0], floating_ips[1]]) req = webob.Request.blank( '/sort?umbrella_sort_key=type') result = list_sort.sort_floating_ips(req, floating_ips) self.assertEqual(result, [floating_ips[2], floating_ips[0], floating_ips[1]]) req = webob.Request.blank( '/sort?umbrella_sort_key=pool') result = list_sort.sort_floating_ips(req, floating_ips) self.assertEqual(result, [floating_ips[2], floating_ips[1], floating_ips[0]]) req = webob.Request.blank( '/sort?umbrella_sort_key=instance_name') result = list_sort.sort_floating_ips(req, floating_ips) self.assertEqual(result, [floating_ips[2], floating_ips[0], floating_ips[1]]) req = webob.Request.blank( '/sort?umbrella_sort_key=instance_name&umbrella_sort_dir=desc') result = list_sort.sort_floating_ips(req, floating_ips) self.assertEqual(result, [floating_ips[1], floating_ips[0], floating_ips[2]])