summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2017-06-09 03:39:58 +0000
committerGerrit Code Review <review@openstack.org>2017-06-09 03:39:58 +0000
commit529f638c241a058993940fc9a797d9a063b29ee1 (patch)
tree3d9db79aa72b9ddcd47603a25dc049028082e74a
parente74ea7614c3e86d95a72fbc6910ddbe32231a3b5 (diff)
parent7b9a38f5a5571d7e177fb71bd6290fd5dcd31a9b (diff)
Merge "[Generic driver] Fix incompatibility with novaclient" into stable/ocatastable/ocata
-rw-r--r--manila/compute/nova.py38
-rw-r--r--manila/network/neutron/api.py31
-rw-r--r--manila/share/drivers/service_instance.py20
-rw-r--r--manila/tests/compute/test_nova.py65
-rw-r--r--manila/tests/share/drivers/test_service_instance.py108
5 files changed, 131 insertions, 131 deletions
diff --git a/manila/compute/nova.py b/manila/compute/nova.py
index 0f59db2..a38476f 100644
--- a/manila/compute/nova.py
+++ b/manila/compute/nova.py
@@ -306,39 +306,13 @@ class API(base.Base):
306 return novaclient(context).keypairs.list() 306 return novaclient(context).keypairs.list()
307 307
308 def image_list(self, context): 308 def image_list(self, context):
309 return novaclient(context).images.list() 309 client = novaclient(context)
310 if hasattr(client, 'images'):
311 # Old novaclient with 'images' API proxy
312 return client.images.list()
313 # New novaclient without 'images' API proxy
314 return client.glance.list()
310 315
311 def add_security_group_to_server(self, context, server, security_group): 316 def add_security_group_to_server(self, context, server, security_group):
312 return novaclient(context).servers.add_security_group(server, 317 return novaclient(context).servers.add_security_group(server,
313 security_group) 318 security_group)
314
315 def security_group_create(self, context, name, description=""):
316 return novaclient(context).security_groups.create(name, description)
317
318 def security_group_get(self, context, group_id):
319 return novaclient(context).security_groups.get(group_id)
320
321 def security_group_list(self, context, search_opts=None):
322 return novaclient(context).security_groups.list(search_opts)
323
324 def security_group_rule_create(self, context, parent_group_id,
325 ip_protocol=None, from_port=None,
326 to_port=None, cidr=None, group_id=None):
327 return novaclient(context).security_group_rules.create(
328 parent_group_id, ip_protocol, from_port, to_port, cidr, group_id)
329
330 def security_group_rule_delete(self, context, rule):
331 return novaclient(context).security_group_rules.delete(rule)
332
333 def fixed_ip_reserve(self, context, fixed_ip):
334 return novaclient(context).fixed_ips.reserve(fixed_ip)
335
336 def fixed_ip_unreserve(self, context, fixed_ip):
337 return novaclient(context).fixed_ips.unreserve(fixed_ip)
338
339 def fixed_ip_get(self, context, fixed_ip):
340 return _to_dict(novaclient(context).fixed_ips.get(fixed_ip))
341
342 def network_get(self, context, network_id):
343 """Return network data by its ID."""
344 return _to_dict(novaclient(context).networks.get(network_id))
diff --git a/manila/network/neutron/api.py b/manila/network/neutron/api.py
index a61d55f..6b302ce 100644
--- a/manila/network/neutron/api.py
+++ b/manila/network/neutron/api.py
@@ -372,3 +372,34 @@ class API(object):
372 except neutron_client_exc.NeutronClientException as e: 372 except neutron_client_exc.NeutronClientException as e:
373 raise exception.NetworkException(code=e.status_code, 373 raise exception.NetworkException(code=e.status_code,
374 message=e.message) 374 message=e.message)
375
376 def security_group_list(self, search_opts=None):
377 try:
378 return self.client.list_security_groups(**search_opts)
379 except neutron_client_exc.NeutronClientException as e:
380 raise exception.NetworkException(
381 code=e.status_code, message=e.message)
382
383 def security_group_create(self, name, description=""):
384 try:
385 return self.client.create_security_group(
386 {"name": name, "description": description})
387 except neutron_client_exc.NeutronClientException as e:
388 raise exception.NetworkException(
389 code=e.status_code, message=e.message)
390
391 def security_group_rule_create(self, parent_group_id,
392 ip_protocol=None, from_port=None,
393 to_port=None, cidr=None, group_id=None):
394 try:
395 return self.client.create_security_group_rule({
396 "parent_group_id": parent_group_id,
397 "ip_protocol": ip_protocol,
398 "from_port": from_port,
399 "to_port": to_port,
400 "cidr": cidr,
401 "group_id": group_id,
402 })
403 except neutron_client_exc.NeutronClientException as e:
404 raise exception.NetworkException(
405 code=e.status_code, message=e.message)
diff --git a/manila/share/drivers/service_instance.py b/manila/share/drivers/service_instance.py
index 27b671d..859972a 100644
--- a/manila/share/drivers/service_instance.py
+++ b/manila/share/drivers/service_instance.py
@@ -330,20 +330,22 @@ class ServiceInstanceManager(object):
330 LOG.warning(_LW("Name for service instance security group is not " 330 LOG.warning(_LW("Name for service instance security group is not "
331 "provided. Skipping security group step.")) 331 "provided. Skipping security group step."))
332 return None 332 return None
333 s_groups = [s for s in self.compute_api.security_group_list(context) 333 s_groups = self.network_helper.neutron_api.security_group_list({
334 if s.name == name] 334 "name": name,
335 })['security_groups']
336 s_groups = [s for s in s_groups if s['name'] == name]
335 if not s_groups: 337 if not s_groups:
336 # Creating security group 338 # Creating security group
337 if not description: 339 if not description:
338 description = "This security group is intended "\ 340 description = ("This security group is intended "
339 "to be used by share service." 341 "to be used by share service.")
340 LOG.debug("Creating security group with name '%s'.", name) 342 LOG.debug("Creating security group with name '%s'.", name)
341 sg = self.compute_api.security_group_create( 343 sg = self.network_helper.neutron_api.security_group_create(
342 context, name, description) 344 name, description)['security_group']
343 for protocol, ports in const.SERVICE_INSTANCE_SECGROUP_DATA: 345 for protocol, ports in const.SERVICE_INSTANCE_SECGROUP_DATA:
344 self.compute_api.security_group_rule_create( 346 self.network_helper.neutron_api.security_group_rule_create(
345 context, 347 context,
346 parent_group_id=sg.id, 348 parent_group_id=sg['id'],
347 ip_protocol=protocol, 349 ip_protocol=protocol,
348 from_port=ports[0], 350 from_port=ports[0],
349 to_port=ports[1], 351 to_port=ports[1],
@@ -550,7 +552,7 @@ class ServiceInstanceManager(object):
550 552
551 security_group = self._get_or_create_security_group(context) 553 security_group = self._get_or_create_security_group(context)
552 if security_group: 554 if security_group:
553 sg_id = security_group.id 555 sg_id = security_group['id']
554 LOG.debug( 556 LOG.debug(
555 "Adding security group '%(sg)s' to server '%(si)s'.", 557 "Adding security group '%(sg)s' to server '%(si)s'.",
556 dict(sg=sg_id, si=service_instance["id"])) 558 dict(sg=sg_id, si=service_instance["id"]))
diff --git a/manila/tests/compute/test_nova.py b/manila/tests/compute/test_nova.py
index 09ef9d9..83eab40 100644
--- a/manila/tests/compute/test_nova.py
+++ b/manila/tests/compute/test_nova.py
@@ -69,22 +69,11 @@ class FakeNovaClient(object):
69 def get(self, net_id): 69 def get(self, net_id):
70 return Network(net_id) 70 return Network(net_id)
71 71
72 class FixedIPs(object):
73 def get(self, fixed_ip):
74 return dict(address=fixed_ip)
75
76 def reserve(self, fixed_ip):
77 return None
78
79 def unreserve(self, fixed_ip):
80 return None
81
82 def __init__(self): 72 def __init__(self):
83 self.servers = self.Servers() 73 self.servers = self.Servers()
84 self.volumes = self.Volumes() 74 self.volumes = self.Volumes()
85 self.keypairs = self.servers 75 self.keypairs = self.servers
86 self.networks = self.Networks() 76 self.networks = self.Networks()
87 self.fixed_ips = self.FixedIPs()
88 77
89 78
90@nova.translate_server_exception 79@nova.translate_server_exception
@@ -221,6 +210,38 @@ class NovaApiTestCase(test.TestCase):
221 self.mock_object(nova, '_untranslate_server_summary_view', 210 self.mock_object(nova, '_untranslate_server_summary_view',
222 lambda server: server) 211 lambda server: server)
223 212
213 def test_image_list_novaclient_has_no_proxy(self):
214 image_list = ['fake', 'image', 'list']
215
216 class FakeGlanceClient(object):
217 def list(self):
218 return image_list
219
220 self.novaclient.glance = FakeGlanceClient()
221
222 result = self.api.image_list(self.ctx)
223
224 self.assertEqual(image_list, result)
225
226 def test_image_list_novaclient_has_proxy(self):
227 image_list1 = ['fake', 'image', 'list1']
228 image_list2 = ['fake', 'image', 'list2']
229
230 class FakeImagesClient(object):
231 def list(self):
232 return image_list1
233
234 class FakeGlanceClient(object):
235 def list(self):
236 return image_list2
237
238 self.novaclient.images = FakeImagesClient()
239 self.novaclient.glance = FakeGlanceClient()
240
241 result = self.api.image_list(self.ctx)
242
243 self.assertEqual(image_list1, result)
244
224 def test_server_create(self): 245 def test_server_create(self):
225 result = self.api.server_create(self.ctx, 'server_name', 'fake_image', 246 result = self.api.server_create(self.ctx, 'server_name', 'fake_image',
226 'fake_flavor', None, None, None) 247 'fake_flavor', None, None, None)
@@ -372,28 +393,6 @@ class NovaApiTestCase(test.TestCase):
372 self.assertEqual([{'id': 'id1'}, {'id': 'id2'}], 393 self.assertEqual([{'id': 'id1'}, {'id': 'id2'}],
373 self.api.keypair_list(self.ctx)) 394 self.api.keypair_list(self.ctx))
374 395
375 def test_fixed_ip_get(self):
376 fixed_ip = 'fake_fixed_ip'
377 result = self.api.fixed_ip_get(self.ctx, fixed_ip)
378 self.assertIsInstance(result, dict)
379 self.assertEqual(fixed_ip, result['address'])
380
381 def test_fixed_ip_reserve(self):
382 fixed_ip = 'fake_fixed_ip'
383 result = self.api.fixed_ip_reserve(self.ctx, fixed_ip)
384 self.assertIsNone(result)
385
386 def test_fixed_ip_unreserve(self):
387 fixed_ip = 'fake_fixed_ip'
388 result = self.api.fixed_ip_unreserve(self.ctx, fixed_ip)
389 self.assertIsNone(result)
390
391 def test_network_get(self):
392 net_id = 'fake_net_id'
393 net = self.api.network_get(self.ctx, net_id)
394 self.assertIsInstance(net, dict)
395 self.assertEqual(net_id, net['id'])
396
397 396
398class ToDictTestCase(test.TestCase): 397class ToDictTestCase(test.TestCase):
399 398
diff --git a/manila/tests/share/drivers/test_service_instance.py b/manila/tests/share/drivers/test_service_instance.py
index b5ab896..76e1dd9 100644
--- a/manila/tests/share/drivers/test_service_instance.py
+++ b/manila/tests/share/drivers/test_service_instance.py
@@ -97,6 +97,12 @@ class FakeNetworkHelper(service_instance.BaseNetworkhelper):
97 def NAME(self): 97 def NAME(self):
98 return service_instance.NEUTRON_NAME 98 return service_instance.NEUTRON_NAME
99 99
100 @property
101 def neutron_api(self):
102 if not hasattr(self, '_neutron_api'):
103 self._neutron_api = mock.Mock()
104 return self._neutron_api
105
100 def __init__(self, service_instance_manager): 106 def __init__(self, service_instance_manager):
101 self.get_config_option = service_instance_manager.get_config_option 107 self.get_config_option = service_instance_manager.get_config_option
102 108
@@ -356,78 +362,66 @@ class ServiceInstanceManagerTestCase(test.TestCase):
356 'service_instance_security_group') 362 'service_instance_security_group')
357 363
358 def test_security_group_name_from_config_and_sg_exist(self): 364 def test_security_group_name_from_config_and_sg_exist(self):
359 fake_secgroup = fake_compute.FakeSecurityGroup(name="fake_sg_name") 365 name = "fake_sg_name_from_config"
366 desc = "fake_sg_description"
367 fake_secgroup = {'id': 'fake_sg_id', 'name': name, 'description': desc}
360 self.mock_object(self._manager, 'get_config_option', 368 self.mock_object(self._manager, 'get_config_option',
361 mock.Mock(return_value="fake_sg_name")) 369 mock.Mock(return_value=name))
362 self.mock_object(self._manager.compute_api, 'security_group_list', 370 neutron_api = self._manager.network_helper.neutron_api
363 mock.Mock(return_value=[fake_secgroup, ])) 371 neutron_api.security_group_list.return_value = {
372 'security_groups': [fake_secgroup]}
373
364 result = self._manager._get_or_create_security_group( 374 result = self._manager._get_or_create_security_group(
365 self._manager.admin_context) 375 self._manager.admin_context)
376
366 self.assertEqual(fake_secgroup, result) 377 self.assertEqual(fake_secgroup, result)
367 self._manager.get_config_option.assert_has_calls([ 378 self._manager.get_config_option.assert_called_once_with(
368 mock.call('service_instance_security_group'), 379 'service_instance_security_group')
369 ]) 380 neutron_api.security_group_list.assert_called_once_with({"name": name})
370 self._manager.compute_api.security_group_list.assert_called_once_with(
371 self._manager.admin_context)
372 381
373 def test_security_group_creation_with_name_from_config(self): 382 @ddt.data(None, 'fake_name')
374 name = "fake_sg_name" 383 def test_security_group_creation_with_name_from_config(self, name):
384 config_name = "fake_sg_name_from_config"
375 desc = "fake_sg_description" 385 desc = "fake_sg_description"
376 fake_secgroup = fake_compute.FakeSecurityGroup(name=name, 386 fake_secgroup = {'id': 'fake_sg_id', 'name': name, 'description': desc}
377 description=desc)
378 self.mock_object(self._manager, 'get_config_option', 387 self.mock_object(self._manager, 'get_config_option',
379 mock.Mock(return_value=name)) 388 mock.Mock(return_value=name or config_name))
380 self.mock_object(self._manager.compute_api, 'security_group_list', 389 neutron_api = self._manager.network_helper.neutron_api
381 mock.Mock(return_value=[])) 390 neutron_api.security_group_list.return_value = {'security_groups': []}
382 self.mock_object(self._manager.compute_api, 'security_group_create', 391 neutron_api.security_group_create.return_value = {
383 mock.Mock(return_value=fake_secgroup)) 392 'security_group': fake_secgroup,
384 self.mock_object(self._manager.compute_api, 393 }
385 'security_group_rule_create') 394
386 result = self._manager._get_or_create_security_group( 395 result = self._manager._get_or_create_security_group(
387 context=self._manager.admin_context, 396 context=self._manager.admin_context,
388 name=None, 397 name=name,
389 description=desc, 398 description=desc,
390 ) 399 )
400
391 self.assertEqual(fake_secgroup, result) 401 self.assertEqual(fake_secgroup, result)
392 self._manager.compute_api.security_group_list.assert_called_once_with( 402 if not name:
393 self._manager.admin_context) 403 self._manager.get_config_option.assert_called_once_with(
394 self._manager.compute_api.security_group_create.\ 404 'service_instance_security_group')
395 assert_called_once_with(self._manager.admin_context, name, desc) 405 neutron_api.security_group_list.assert_called_once_with(
396 self._manager.get_config_option.assert_has_calls([ 406 {"name": name or config_name})
397 mock.call('service_instance_security_group'), 407 neutron_api.security_group_create.assert_called_once_with(
398 ]) 408 name or config_name, desc)
399
400 def test_security_group_creation_with_provided_name(self):
401 name = "fake_sg_name"
402 fake_secgroup = fake_compute.FakeSecurityGroup(name=name)
403 self.mock_object(self._manager.compute_api, 'security_group_list',
404 mock.Mock(return_value=[]))
405 self.mock_object(self._manager.compute_api, 'security_group_create',
406 mock.Mock(return_value=fake_secgroup))
407 self.mock_object(self._manager.compute_api,
408 'security_group_rule_create')
409 result = self._manager._get_or_create_security_group(
410 context=self._manager.admin_context, name=name)
411 self._manager.compute_api.security_group_list.assert_called_once_with(
412 self._manager.admin_context)
413 self._manager.compute_api.security_group_create.\
414 assert_called_once_with(
415 self._manager.admin_context, name, mock.ANY)
416 self.assertEqual(fake_secgroup, result)
417 409
418 def test_security_group_two_sg_in_list(self): 410 def test_security_group_two_sg_in_list(self):
419 name = "fake_name" 411 name = "fake_name"
420 fake_secgroup1 = fake_compute.FakeSecurityGroup(name=name) 412 fake_secgroup1 = {'id': 'fake_sg_id1', 'name': name}
421 fake_secgroup2 = fake_compute.FakeSecurityGroup(name=name) 413 fake_secgroup2 = {'id': 'fake_sg_id2', 'name': name}
422 self.mock_object(self._manager.compute_api, 'security_group_list', 414 neutron_api = self._manager.network_helper.neutron_api
423 mock.Mock(return_value=[fake_secgroup1, 415 neutron_api.security_group_list.return_value = {
424 fake_secgroup2])) 416 'security_groups': [fake_secgroup1, fake_secgroup2]}
417
425 self.assertRaises(exception.ServiceInstanceException, 418 self.assertRaises(exception.ServiceInstanceException,
426 self._manager._get_or_create_security_group, 419 self._manager._get_or_create_security_group,
427 self._manager.admin_context, 420 self._manager.admin_context,
428 name) 421 name)
429 self._manager.compute_api.security_group_list.assert_called_once_with( 422
430 self._manager.admin_context) 423 neutron_api.security_group_list.assert_called_once_with(
424 {"name": name})
431 425
432 @ddt.data( 426 @ddt.data(
433 dict(), 427 dict(),
@@ -897,7 +891,7 @@ class ServiceInstanceManagerTestCase(test.TestCase):
897 891
898 server_create = dict(id='fakeid', status='CREATING', networks=dict()) 892 server_create = dict(id='fakeid', status='CREATING', networks=dict())
899 net_name = self._manager.get_config_option("service_network_name") 893 net_name = self._manager.get_config_option("service_network_name")
900 sg = type('FakeSG', (object, ), dict(id='fakeid', name='fakename')) 894 sg = {'id': 'fakeid', 'name': 'fakename'}
901 ip_address = 'fake_ip_address' 895 ip_address = 'fake_ip_address'
902 service_image_id = 'fake_service_image_id' 896 service_image_id = 'fake_service_image_id'
903 key_data = 'fake_key_name', 'fake_key_path' 897 key_data = 'fake_key_name', 'fake_key_path'
@@ -972,7 +966,7 @@ class ServiceInstanceManagerTestCase(test.TestCase):
972 self._manager.admin_context, server_create['id']) 966 self._manager.admin_context, server_create['id'])
973 self._manager.compute_api.add_security_group_to_server.\ 967 self._manager.compute_api.add_security_group_to_server.\
974 assert_called_once_with( 968 assert_called_once_with(
975 self._manager.admin_context, server_get['id'], sg.id) 969 self._manager.admin_context, server_get['id'], sg['id'])
976 self._manager.network_helper.get_network_name.assert_has_calls([]) 970 self._manager.network_helper.get_network_name.assert_has_calls([])
977 971
978 def test___create_service_instance_neutron_no_admin_ip(self): 972 def test___create_service_instance_neutron_no_admin_ip(self):
@@ -986,7 +980,7 @@ class ServiceInstanceManagerTestCase(test.TestCase):
986 980
987 server_create = {'id': 'fakeid', 'status': 'CREATING', 'networks': {}} 981 server_create = {'id': 'fakeid', 'status': 'CREATING', 'networks': {}}
988 net_name = self._manager.get_config_option("service_network_name") 982 net_name = self._manager.get_config_option("service_network_name")
989 sg = type('FakeSG', (object, ), {'id': 'fakeid', 'name': 'fakename'}) 983 sg = {'id': 'fakeid', 'name': 'fakename'}
990 ip_address = 'fake_ip_address' 984 ip_address = 'fake_ip_address'
991 service_image_id = 'fake_service_image_id' 985 service_image_id = 'fake_service_image_id'
992 key_data = 'fake_key_name', 'fake_key_path' 986 key_data = 'fake_key_name', 'fake_key_path'
@@ -1047,7 +1041,7 @@ class ServiceInstanceManagerTestCase(test.TestCase):
1047 self._manager.admin_context, server_create['id']) 1041 self._manager.admin_context, server_create['id'])
1048 self._manager.compute_api.add_security_group_to_server.\ 1042 self._manager.compute_api.add_security_group_to_server.\
1049 assert_called_once_with( 1043 assert_called_once_with(
1050 self._manager.admin_context, server_get['id'], sg.id) 1044 self._manager.admin_context, server_get['id'], sg['id'])
1051 self._manager.network_helper.get_network_name.assert_has_calls([]) 1045 self._manager.network_helper.get_network_name.assert_has_calls([])
1052 1046
1053 @ddt.data( 1047 @ddt.data(