Skip to content

Commit

Permalink
Add perms2.owner to VM
Browse files Browse the repository at this point in the history
Set VMs owner to project uuid

Change-Id: Ide9cf20ff1cdd5120f21033e89566d54dc3942ab
Closes-Bug: #1781342
Closes-Bug: #1780424
  • Loading branch information
aszc-dev committed Jul 13, 2018
1 parent 98ad896 commit 3a35eff
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
15 changes: 13 additions & 2 deletions cvm/services.py
@@ -1,6 +1,8 @@
import ipaddress
import logging

from vnc_api.vnc_api import PermType2

from cvm.constants import (CONTRAIL_VM_NAME, VM_UPDATE_FILTERS,
VNC_ROOT_DOMAIN, VNC_VCENTER_PROJECT)
from cvm.models import (VirtualMachineInterfaceModel, VirtualMachineModel,
Expand Down Expand Up @@ -72,14 +74,23 @@ def _create(self, vmware_vm, vm_properties):
for vmi_model in vm_model.vmi_models:
self._database.vmis_to_update.append(vmi_model)
self._add_property_filter_for_vm(vm_model, vmware_vm, VM_UPDATE_FILTERS)
self._vnc_api_client.update_or_create_vm(vm_model.vnc_vm)
self._update_in_vnc(vm_model.vnc_vm)
logger.info('Created %s', vm_model)
self._database.save(vm_model)

def _add_property_filter_for_vm(self, vm_model, vmware_vm, filters):
property_filter = self._esxi_api_client.add_filter(vmware_vm, filters)
vm_model.property_filter = property_filter

def _update_in_vnc(self, vnc_vm):
self._add_owner_to(vnc_vm)
self._vnc_api_client.update_or_create_vm(vnc_vm)

def _add_owner_to(self, vnc_vm):
perms2 = PermType2()
perms2.set_owner(self._project.get_uuid())
vnc_vm.set_perms2(perms2)

def get_vms_from_vmware(self):
vmware_vms = self._esxi_api_client.get_all_vms()
for vmware_vm in vmware_vms:
Expand Down Expand Up @@ -119,7 +130,7 @@ def rename_vm(self, old_name, new_name):
vm_model = self._database.get_vm_model_by_name(old_name)
vm_model.rename(new_name)
if self._can_modify_in_vnc(vm_model.vnc_vm):
self._vnc_api_client.update_or_create_vm(vm_model.vnc_vm)
self._update_in_vnc(vm_model.vnc_vm)
self._database.save(vm_model)

def update_vm_models_interfaces(self, vmware_vm):
Expand Down
14 changes: 10 additions & 4 deletions tests/test_events.py
Expand Up @@ -160,7 +160,9 @@ def vm_reconfigured_update(vmware_vm_1):
@pytest.fixture()
def vnc_api_client():
vnc_client = Mock()
vnc_client.read_or_create_project.return_value = Project()
project = Project()
project.set_uuid('project-uuid')
vnc_client.read_or_create_project.return_value = project
vnc_client.create_and_read_instance_ip.side_effect = assign_ip_to_instance_ip
return vnc_client

Expand Down Expand Up @@ -243,11 +245,15 @@ def assert_vnc_vmi_state(vnc_vmi, mac_address=None, vnc_vm_uuid=None, vnc_vn_uui
assert vnc_vn_uuid in [ref['uuid'] for ref in vnc_vmi.get_virtual_network_refs()]


def assert_vnc_vm_state(vnc_vm, uuid=None, name=None):
def assert_vnc_vm_state(vnc_vm, uuid=None, name=None, display_name=None, owner=None):
if uuid is not None:
assert vnc_vm.uuid == uuid
if name is not None:
assert vnc_vm.name == name
if display_name is not None:
assert vnc_vm.display_name == display_name
if owner is not None:
assert vnc_vm.get_perms2().get_owner() == owner


def test_vm_created(vcenter_api_client, vn_model_1, vm_created_update,
Expand Down Expand Up @@ -279,7 +285,7 @@ def test_vm_created(vcenter_api_client, vn_model_1, vm_created_update,
vnc_api_client.update_or_create_vm.assert_called_once()
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')
name='12345678-1234-1234-1234-123456789012', owner='project-uuid')

# - in Database:
vm_model = database.get_vm_model_by_uuid('12345678-1234-1234-1234-123456789012')
Expand Down Expand Up @@ -361,7 +367,7 @@ def test_vm_renamed(vcenter_api_client, vn_model_1, vm_created_update,
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')
name='12345678-1234-1234-1234-123456789012', display_name='VM1-renamed')

# - in Database:
vm_model = database.get_vm_model_by_uuid('12345678-1234-1234-1234-123456789012')
Expand Down
8 changes: 7 additions & 1 deletion tests/test_services.py
Expand Up @@ -29,7 +29,7 @@ def setUp(self):
vmware_dpg,
Mock(spec=vim.Network),
])
self.vnc_client = Mock()
self.vnc_client = create_vnc_client_mock()
self.vcenter_client = create_vcenter_client_mock()
self.database = Mock()
self.database.get_vm_model_by_uuid.return_value = None
Expand Down Expand Up @@ -207,6 +207,12 @@ def test_update_same_power_state(self):
self.assertTrue(vm_model.is_powered_on)
self.assertEqual([], self.database.ports_to_update)

def test_set_vm_owner(self):
self.vm_service.update(self.vmware_vm)

vnc_vm = self.vnc_client.update_or_create_vm.call_args[0][0]
self.assertEqual('project-uuid', vnc_vm.get_perms2().get_owner())


class TestVirtualNetworkService(TestCase):

Expand Down
4 changes: 3 additions & 1 deletion tests/utils.py
Expand Up @@ -46,7 +46,9 @@ def create_vcenter_client_mock():

def create_vnc_client_mock():
vnc_client = Mock()
vnc_client.read_or_create_project.return_value = vnc_api.Project()
project = vnc_api.Project()
project.set_uuid('project-uuid')
vnc_client.read_or_create_project.return_value = project
vnc_client.read_security_group.return_value = vnc_api.SecurityGroup()
return vnc_client

Expand Down

0 comments on commit 3a35eff

Please sign in to comment.