Skip to content

Commit

Permalink
Stop saving VM model for ContrailVM
Browse files Browse the repository at this point in the history
Change-Id: Ic5f96b3e7b0e04c59c968a59df5230ca7954a87c
Closes-Bug: #1778540
  • Loading branch information
aszc-dev committed Jun 26, 2018
1 parent 472b884 commit 661c2c3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
12 changes: 5 additions & 7 deletions cvm/__main__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#!/usr/bin/env python

import argparse
import socket
import sys

import gevent
from gevent import monkey; monkey.patch_all()
import socket

import yaml

from pysandesh.sandesh_base import Sandesh, SandeshLevel
from cvm.sandesh.vcenter_manager.ttypes import *
from pysandesh.sandesh_base import Sandesh

import cvm.constants as const
from cvm.clients import (ESXiAPIClient, VCenterAPIClient, VNCAPIClient,
Expand All @@ -19,10 +15,12 @@
VmRenamedHandler, VmwareController)
from cvm.database import Database
from cvm.monitors import VMwareMonitor
from cvm.sandesh_handler import SandeshHandler
from cvm.services import (VirtualMachineInterfaceService,
VirtualMachineService, VirtualNetworkService,
VRouterPortService)
from cvm.sandesh_handler import SandeshHandler

gevent.monkey.patch_all()


def load_config(config_file):
Expand Down
4 changes: 0 additions & 4 deletions cvm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ def uuid(self):
def name(self):
return self.vm_properties.get('name')

@property
def is_contrail_vm(self):
return 'ContrailVM' in self.name

@property
def is_powered_on(self):
return self.vm_properties.get('runtime.powerState') == 'poweredOn'
Expand Down
14 changes: 9 additions & 5 deletions cvm/services.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import ipaddress
import logging

from cvm.constants import VLAN_ID_RANGE_END, VLAN_ID_RANGE_START
from cvm.constants import (CONTRAIL_VM_NAME, VLAN_ID_RANGE_END,
VLAN_ID_RANGE_START)
from cvm.models import (VirtualMachineInterfaceModel, VirtualMachineModel,
VirtualNetworkModel, VlanIdPool)

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def is_contrail_vm_name(name):
return CONTRAIL_VM_NAME in name


class Service(object):
def __init__(self, vnc_api_client, database, esxi_api_client=None, vcenter_api_client=None):
self._vnc_api_client = vnc_api_client
Expand Down Expand Up @@ -47,6 +52,8 @@ def __init__(self, esxi_api_client, vnc_api_client, database):

def update(self, vmware_vm):
vm_properties = self._esxi_api_client.read_vm_properties(vmware_vm)
if is_contrail_vm_name(vm_properties['name']):
return
vm_model = self._database.get_vm_model_by_uuid(vmware_vm.config.instanceUuid)
if vm_model:
self._update(vm_model, vmware_vm, vm_properties)
Expand All @@ -64,8 +71,7 @@ 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, ['guest.toolsRunningStatus', 'guest.net'])
if not vm_model.is_contrail_vm:
self._vnc_api_client.update_or_create_vm(vm_model.vnc_vm)
self._vnc_api_client.update_or_create_vm(vm_model.vnc_vm)
self._database.save(vm_model)

def _add_property_filter_for_vm(self, vm_model, filters):
Expand Down Expand Up @@ -153,8 +159,6 @@ def sync_vmis(self):
def update_vmis(self):
vmis_to_update = [vmi_model for vmi_model in self._database.vmis_to_update]
for vmi_model in vmis_to_update:
if vmi_model.vm_model.is_contrail_vm:
continue
self.update_vmis_vn(vmi_model)
self._database.vmis_to_update.remove(vmi_model)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ def test_vm_created_vlan_id(vcenter_api_client, vn_model_1, vm_created_update,

def test_contrail_vm(vcenter_api_client, vm_created_update, esxi_api_client,
vnc_api_client, contrail_vm_properties):
""" We need ContrailVM only in database. It should not be created in VNC. """
""" We don't need ContrailVM model for CVM to operate properly. """
vrouter_api_client = Mock()
database = Database()
vm_service = VirtualMachineService(esxi_api_client, vnc_api_client, database)
Expand All @@ -500,8 +500,8 @@ def test_contrail_vm(vcenter_api_client, vm_created_update, esxi_api_client,
# A new update containing VmCreatedEvent arrives and is being handled by the controller
controller.handle_update(vm_created_update)

# Check if VM Model has been saved in the database
assert len(database.get_all_vm_models()) == 1
# VM model has not been saved in the database
assert len(database.get_all_vm_models()) == 0

# There were no calls to vnc_api
vnc_api_client.update_or_create_vm.assert_not_called()
Expand Down
16 changes: 14 additions & 2 deletions tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
VirtualMachineModel, VirtualNetworkModel)
from cvm.services import (Service, VirtualMachineInterfaceService,
VirtualMachineService, VirtualNetworkService,
VRouterPortService)
VRouterPortService, is_contrail_vm_name)
from tests.utils import (create_dpg_mock, create_property_filter,
create_vcenter_client_mock, create_vmware_vm_mock,
create_vn_model, create_vnc_client_mock)
Expand Down Expand Up @@ -647,4 +647,16 @@ def test_disable_port(self):
self.port_service.sync_ports()

self.vrouter_api_client.disable_port.assert_called_once_with('fe71b44d-0654-36aa-9841-ab9b78d628c5')
self.vrouter_api_client.enable_port.assert_not_called()
self.vrouter_api_client.enable_port.assert_not_called()


class TestContrailVM(TestCase):
def test_contrail_vm_name(self):
contrail_name = 'ContrailVM-datacenter-0.0.0.0'
regular_name = 'VM1'

contrail_result = is_contrail_vm_name(contrail_name)
regular_result = is_contrail_vm_name(regular_name)

self.assertTrue(contrail_result)
self.assertFalse(regular_result)

0 comments on commit 661c2c3

Please sign in to comment.