Skip to content

Commit

Permalink
Change VMI update flow to delete/create
Browse files Browse the repository at this point in the history
Closes-Bug: #1780429
Change-Id: I4a9ff58cb8e4e1ff66118b0cf4449b4cde1cc71f
  • Loading branch information
aszc-dev committed Jul 6, 2018
1 parent 8721db4 commit 6caa822
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 33 deletions.
20 changes: 6 additions & 14 deletions cvm/clients.py
Expand Up @@ -268,22 +268,18 @@ def get_all_vms(self):
def read_vm(self, uuid):
return self.vnc_lib.virtual_machine_read(id=uuid)

def update_or_create_vmi(self, vnc_vmi):
def update_vmi(self, vnc_vmi):
try:
logger.info('Attempting to update Virtual Machine Interface %s in VNC', vnc_vmi.name)
self._update_vmi(vnc_vmi)
self.delete_vmi(vnc_vmi.get_uuid())
except NoIdError:
logger.info('Virtual Machine Interface %s not found in VNC - creating', vnc_vmi.name)
self._create_vmi(vnc_vmi)
self._create_vmi(vnc_vmi)

def _update_vmi(self, vnc_vmi):
self.vnc_lib.virtual_machine_interface_update(vnc_vmi)
def _create_vmi(self, vnc_vmi):
self.vnc_lib.virtual_machine_interface_create(vnc_vmi)
logger.info('Virtual Machine Interface %s updated in VNC', vnc_vmi.name)

def _create_vmi(self, vmi):
self.vnc_lib.virtual_machine_interface_create(vmi)
logger.info('Virtual Machine Interface %s created in VNC', vmi.display_name)

def delete_vmi(self, uuid):
vmi = self.read_vmi(uuid)
for instance_ip_ref in vmi.get_instance_ip_back_refs() or []:
Expand All @@ -306,11 +302,7 @@ def get_vmis_for_vm(self, vm_model):
return [self.read_vmi(vmi['uuid']) for vmi in vmis]

def read_vmi(self, uuid):
try:
return self.vnc_lib.virtual_machine_interface_read(id=uuid)
except NoIdError:
logger.error('Virtual Machine Interface %s not found in VNC', uuid)
return None
return self.vnc_lib.virtual_machine_interface_read(id=uuid)

def read_vn(self, fq_name):
try:
Expand Down
5 changes: 3 additions & 2 deletions cvm/services.py
Expand Up @@ -227,7 +227,7 @@ def _add_vnc_info_to(self, vmi_model):
vmi_model.security_group = self._default_security_group

def _update_in_vnc(self, vmi_model):
self._vnc_api_client.update_or_create_vmi(vmi_model.vnc_vmi)
self._vnc_api_client.update_vmi(vmi_model.vnc_vmi)

def _add_instance_ip_to(self, vmi_model):
vmi_model.construct_instance_ip()
Expand Down Expand Up @@ -261,7 +261,8 @@ def _update_ip_address(self, vmi_model, ip_address):
if not vmi_model.update_ip_address(ip_address):
return
self._add_instance_ip_to(vmi_model)
logger.info('IP address of %s updated to %s', vmi_model.display_name, ip_address)
logger.info('IP address of %s updated to %s',
vmi_model.display_name, vmi_model.vnc_instance_ip.instance_ip_address)

def _delete(self, vmi_model):
self._delete_from_vnc(vmi_model)
Expand Down
9 changes: 5 additions & 4 deletions tests/test_clients.py
Expand Up @@ -145,16 +145,17 @@ def test_update_create_new_vm(self):

def test_update_create_vmi(self):
vnc_vmi = Mock()
self.vnc_lib.virtual_machine_interface_read.side_effect = NoIdError(None)

self.vnc_client.update_or_create_vmi(vnc_vmi)
self.vnc_client.update_vmi(vnc_vmi)

self.vnc_lib.virtual_machine_interface_update.assert_called_once()
self.vnc_lib.virtual_machine_interface_create.assert_called_once()

def test_update_create_new_vmi(self):
vnc_vmi = Mock()
self.vnc_lib.virtual_machine_interface_update.side_effect = NoIdError(None)
self.vnc_lib.virtual_machine_interface_read.side_effect = NoIdError(None)

self.vnc_client.update_or_create_vmi(vnc_vmi)
self.vnc_client.update_vmi(vnc_vmi)

self.vnc_lib.virtual_machine_interface_create.assert_called_once_with(vnc_vmi)

Expand Down
18 changes: 9 additions & 9 deletions tests/test_events.py
Expand Up @@ -287,8 +287,8 @@ def test_vm_created(vcenter_api_client, vn_model_1, vm_created_update,

# Check if VMI Model has been saved properly:
# - in VNC
vnc_api_client.update_or_create_vmi.assert_called_once()
vnc_vmi = vnc_api_client.update_or_create_vmi.call_args[0][0]
vnc_api_client.update_vmi.assert_called_once()
vnc_vmi = vnc_api_client.update_vmi.call_args[0][0]
assert_vnc_vmi_state(vnc_vmi, mac_address='11:11:11:11:11:11',
vnc_vm_uuid=vnc_vm.uuid, vnc_vn_uuid=vnc_vn_1.uuid)

Expand Down Expand Up @@ -358,7 +358,7 @@ def test_vm_renamed(vcenter_api_client, vn_model_1, vm_created_update,

# Check if VM Model has been saved properly:
# - in VNC:
assert vnc_api_client.update_or_create_vmi.call_count == 2
assert vnc_api_client.update_vmi.call_count == 2
vnc_vm = vnc_api_client.update_or_create_vm.call_args[0][0]
assert_vnc_vm_state(vnc_vm, uuid='12345678-1234-1234-1234-123456789012',
name='12345678-1234-1234-1234-123456789012')
Expand All @@ -369,8 +369,8 @@ def test_vm_renamed(vcenter_api_client, vn_model_1, vm_created_update,

# Check if VMI Model has been saved properly:
# - in VNC
assert vnc_api_client.update_or_create_vmi.call_count == 2
vnc_vmi = vnc_api_client.update_or_create_vmi.call_args[0][0]
assert vnc_api_client.update_vmi.call_count == 2
vnc_vmi = vnc_api_client.update_vmi.call_args[0][0]
assert_vnc_vmi_state(vnc_vmi, mac_address='11:11:11:11:11:11', vnc_vm_uuid=vnc_vm.uuid)

# - in Database
Expand Down Expand Up @@ -455,8 +455,8 @@ def test_vm_reconfigured(vcenter_api_client, vn_model_1, vn_model_2, vm_created_

# - in VNC
vnc_api_client.delete_vmi.assert_called_once_with(vmi_model.uuid)
assert vnc_api_client.update_or_create_vmi.call_count == 2
vnc_vmi = vnc_api_client.update_or_create_vmi.call_args[0][0]
assert vnc_api_client.update_vmi.call_count == 2
vnc_vmi = vnc_api_client.update_vmi.call_args[0][0]
assert_vnc_vmi_state(vnc_vmi, mac_address='11:11:11:11:11:11', vnc_vn_uuid=vnc_vn_2.uuid)

# Check if VMI Model's Instance IP has been updated in VNC:
Expand Down Expand Up @@ -560,7 +560,7 @@ def test_contrail_vm(vcenter_api_client, vm_created_update, esxi_api_client,

# There were no calls to vnc_api
vnc_api_client.update_or_create_vm.assert_not_called()
vnc_api_client.update_or_create_vmi.assert_not_called()
vnc_api_client.update_vmi.assert_not_called()

# There were no calls to vrouter_api
vrouter_api_client.add_port.assert_not_called()
Expand Down Expand Up @@ -606,4 +606,4 @@ def test_external_ipam(vcenter_api_client, vm_created_update, esxi_api_client,
vrouter_api_client.delete_port.assert_called_once()

# The VMI itself should not be updated, since there's no new info
vnc_api_client.update_or_create_vmi.assert_called_once()
vnc_api_client.update_vmi.assert_called_once()
8 changes: 4 additions & 4 deletions tests/test_services.py
Expand Up @@ -284,7 +284,7 @@ def test_create_vmis_proper_vm_dpg(self):
self.assertEqual(self.vm_model, saved_vmi.vm_model)
self.assertEqual(self.vn_model, saved_vmi.vn_model)
self.assertIn(saved_vmi, self.database.ports_to_update)
vnc_vmi = self.vnc_client.update_or_create_vmi.call_args[0][0]
vnc_vmi = self.vnc_client.update_vmi.call_args[0][0]
self.assertIn(self.vn_model.uuid, [ref['uuid'] for ref in vnc_vmi.get_virtual_network_refs()])

def test_no_update_for_no_dpgs(self):
Expand All @@ -296,7 +296,7 @@ def test_no_update_for_no_dpgs(self):
self.vmi_service.update_vmis()

self.assertEqual(0, len(self.database.get_all_vmi_models()))
self.vnc_client.update_or_create_vmi.assert_not_called()
self.vnc_client.update_vmi.assert_not_called()
self.assertEqual([], self.database.ports_to_update)

def test_update_existing_vmi(self):
Expand Down Expand Up @@ -327,7 +327,7 @@ def test_update_existing_vmi(self):
saved_vmi = self.database.get_all_vmi_models()[0]
self.assertEqual(self.vm_model, saved_vmi.vm_model)
self.assertEqual(second_vn_model, saved_vmi.vn_model)
vnc_vmi = self.vnc_client.update_or_create_vmi.call_args[0][0]
vnc_vmi = self.vnc_client.update_vmi.call_args[0][0]
self.assertIn(second_vn_model.uuid, [ref['uuid'] for ref in vnc_vmi.get_virtual_network_refs()])
self.assertIn(saved_vmi, self.database.ports_to_update)

Expand Down Expand Up @@ -408,7 +408,7 @@ def test_rename_vmis(self):

self.assertEqual('vmi-VM Portgroup-VM-renamed', vmi_model.display_name)
self.assertEqual(0, self.vnc_client.create_and_read_instance_ip.call_count)
self.vnc_client.update_or_create_vmi.assert_called_once()
self.vnc_client.update_vmi.assert_called_once()
self.assertIn(vmi_model, self.database.ports_to_update)

def test_update_nic(self):
Expand Down

0 comments on commit 6caa822

Please sign in to comment.