diff options
| author | Naveed Massjouni <naveedm9@gmail.com> | 2012-02-14 19:46:19 +0000 |
|---|---|---|
| committer | Naveed Massjouni <naveedm9@gmail.com> | 2012-02-18 00:35:28 +0000 |
| commit | 0e1ceb4d753f2b4cd3b6aaee5e3f68caaf424c57 (patch) | |
| tree | fb68c89c50f9f8d80c745b314cb12e3ac8a46bb3 /nova | |
| parent | 63c0677b0fe937ab565322cd719c3bbb39ff0a56 (diff) | |
| download | nova-0e1ceb4d753f2b4cd3b6aaee5e3f68caaf424c57.tar.gz nova-0e1ceb4d753f2b4cd3b6aaee5e3f68caaf424c57.tar.xz nova-0e1ceb4d753f2b4cd3b6aaee5e3f68caaf424c57.zip | |
Setting access ip values on server create.
Change-Id: Id67c5e9d7bb00227e88ecbc2ffb0e772fcd69066
Diffstat (limited to 'nova')
| -rw-r--r-- | nova/compute/api.py | 1 | ||||
| -rw-r--r-- | nova/compute/manager.py | 29 | ||||
| -rw-r--r-- | nova/flags.py | 7 | ||||
| -rw-r--r-- | nova/tests/test_compute.py | 33 |
4 files changed, 67 insertions, 3 deletions
diff --git a/nova/compute/api.py b/nova/compute/api.py index bf03cb10b..edadf1978 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -565,6 +565,7 @@ class API(base.Base): "admin_password": admin_password, "injected_files": injected_files, "requested_networks": requested_networks, + "is_first_time": True, "filter_properties": filter_properties}}) def _check_create_policies(self, context, availability_zone, diff --git a/nova/compute/manager.py b/nova/compute/manager.py index 74f9af3f9..f31a0d2e4 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -405,6 +405,7 @@ class ComputeManager(manager.SchedulerDependentManager): requested_networks=None, injected_files=[], admin_password=None, + is_first_time=False, **kwargs): """Launch a new instance with specified options.""" context = context.elevated() @@ -425,6 +426,10 @@ class ComputeManager(manager.SchedulerDependentManager): with utils.save_and_reraise_exception(): self._deallocate_network(context, instance) + if (is_first_time and not instance['access_ip_v4'] + and not instance['access_ip_v6']): + self._update_access_ip(context, instance, network_info) + self._notify_about_instance_usage(instance, "create.end", network_info=network_info) @@ -440,6 +445,30 @@ class ComputeManager(manager.SchedulerDependentManager): with utils.save_and_reraise_exception(): self._set_instance_error_state(context, instance_uuid) + def _update_access_ip(self, context, instance, nw_info): + """Update the access ip values for a given instance. + + If FLAGS.default_access_ip_network_name is set, this method will + grab the corresponding network and set the access ip values + accordingly. Note that when there are multiple ips to choose from, + an arbitrary one will be chosen. + """ + + network_name = FLAGS.default_access_ip_network_name + if not network_name: + return + + update_info = {} + for vif in nw_info: + if vif['network']['label'] == network_name: + for ip in vif.fixed_ips(): + if ip['version'] == 4: + update_info['access_ip_v4'] = ip['address'] + if ip['version'] == 6: + update_info['access_ip_v6'] = ip['address'] + if update_info: + self.db.instance_update(context, instance.uuid, update_info) + def _check_instance_not_already_created(self, context, instance): """Ensure an instance with the same name is not already present.""" if self.driver.instance_exists(instance['name']): diff --git a/nova/flags.py b/nova/flags.py index e97d08f50..42169eafd 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -454,7 +454,10 @@ global_opts = [ help='The volume API class to use'), cfg.StrOpt('security_group_handler', default='nova.network.quantum.sg.NullSecurityGroupHandler', - help='security group handler class') - ] + help='security group handler class'), + cfg.StrOpt('default_access_ip_network_name', + default=None, + help='Name of network to use to set access ips for instances'), +] FLAGS.register_opts(global_opts) diff --git a/nova/tests/test_compute.py b/nova/tests/test_compute.py index 48f16d7b4..582846c91 100644 --- a/nova/tests/test_compute.py +++ b/nova/tests/test_compute.py @@ -192,7 +192,7 @@ class BaseTestCase(test.TestCase): class ComputeTestCase(BaseTestCase): def setUp(self): - def fake_get_nw_info(cls, ctxt, instance): + def fake_get_nw_info(cls, ctxt, instance, *args, **kwargs): self.assertTrue(ctxt.is_admin) return fake_network.fake_get_instance_nw_info(self.stubs, 1, 1, spectacular=True) @@ -200,6 +200,8 @@ class ComputeTestCase(BaseTestCase): super(ComputeTestCase, self).setUp() self.stubs.Set(nova.network.API, 'get_instance_nw_info', fake_get_nw_info) + self.stubs.Set(nova.network.API, 'allocate_for_instance', + fake_get_nw_info) def test_wrap_instance_fault(self): inst_uuid = "fake_uuid" @@ -271,6 +273,35 @@ class ComputeTestCase(BaseTestCase): finally: db.instance_destroy(self.context, instance['id']) + def test_default_access_ip(self): + self.flags(default_access_ip_network_name='test1', stub_network=False) + instance = self._create_fake_instance() + + try: + self.compute.run_instance(self.context, instance['uuid'], + is_first_time=True) + instances = db.instance_get_all(context.get_admin_context()) + instance = instances[0] + + self.assertEqual(instance.access_ip_v4, '192.168.1.100') + self.assertEqual(instance.access_ip_v6, '2001:db8:0:1::1') + finally: + db.instance_destroy(self.context, instance['id']) + + def test_no_default_access_ip(self): + instance = self._create_fake_instance() + + try: + self.compute.run_instance(self.context, instance['uuid'], + is_first_time=True) + instances = db.instance_get_all(context.get_admin_context()) + instance = instances[0] + + self.assertFalse(instance.access_ip_v4) + self.assertFalse(instance.access_ip_v6) + finally: + db.instance_destroy(self.context, instance['id']) + def _assert_state(self, state_dict): """assert the instance is in the state defined by state_dict""" instances = db.instance_get_all(context.get_admin_context()) |
