Tomer Saban has uploaded a new change for review.

Change subject: Added a few design changes
......................................................................

Added a few design changes

Created constants for URL, Username and Password for connecting to
the api.

Created constants for some more number and strings.

Changed some variables to private.

Added use of python max function.

Change-Id: I034cee176c42c3d760540bac076507b31fb43a14
Signed-off-by: Tomer Saban <[email protected]>
---
M doc/plugin_samples/even_vm_distribution.py
M doc/plugin_samples/host_memory_balance.py
M doc/plugin_samples/ksm_same_os_score.py
M doc/plugin_samples/max_vm_filter.py
M doc/plugin_samples/vm_balance.py
5 files changed, 69 insertions(+), 29 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-scheduler-proxy 
refs/changes/92/39592/1

diff --git a/doc/plugin_samples/even_vm_distribution.py 
b/doc/plugin_samples/even_vm_distribution.py
index c0adae6..7d916bb 100644
--- a/doc/plugin_samples/even_vm_distribution.py
+++ b/doc/plugin_samples/even_vm_distribution.py
@@ -6,14 +6,20 @@
 class even_vm_distribution():
     '''rank hosts by the number of running vms on them, with the least first'''
 
+    #Definitions
+    _CONNECTION_URL = 'http://host:port'
+    _CONNECTION_USERNAME = 'user@domain'
+    _CONNECTION_PASSWORD = ''
+
     properties_validation = ''
 
     def _get_connection(self):
         #open a connection to the rest api
         connection = None
         try:
-            connection = API(url='http://host:port',
-                             username='user@domain', password='')
+            connection = API(url=_CONNECTION_URL,
+                             username=_CONNECTION_USERNAME,
+                             password=_CONNECTION_PASSWORD)
         except BaseException as ex:
             #letting the external proxy know there was an error
             print >> sys.stderr, ex
diff --git a/doc/plugin_samples/host_memory_balance.py 
b/doc/plugin_samples/host_memory_balance.py
index 634f1d1..c006c2c 100644
--- a/doc/plugin_samples/host_memory_balance.py
+++ b/doc/plugin_samples/host_memory_balance.py
@@ -8,18 +8,25 @@
 
     #What are the values this module will accept, used to present
     #the user with options
+    #Definitions
+    _TO_BYTES = 1024 * 1024
+    _MINIMUM_MEMORY_DEFAULT = 500
+    _SAFE_SELECTION_DEFAULT = 'True'
+
+    _CONNECTION_URL = 'http://host:port'
+    _CONNECTION_USERNAME = 'user@domain'
+    _CONNECTION_PASSWORD = ''
+
     properties_validation = 
'minimum_host_memoryMB=[0-9]*;safe_selection=True|False'
-    TO_BYTES = 1024 * 1024
-    MINIMUM_MEMORY_DEFAULT = 500
-    SAFE_SELECTION_DEFAULT = 'True'
-    free_memory_cache = {}
+    _free_memory_cache = {}
 
     def _get_connection(self):
         #open a connection to the rest api
         connection = None
         try:
-            connection = API(url='http://host:port',
-                             username='user@domain', password='')
+            connection = API(url=_CONNECTION_URL,
+                             username=_CONNECTION_USERNAME,
+                             password=_CONNECTION_PASSWORD)
         except BaseException as ex:
             #letting the external proxy know there was an error
             print >> sys.stderr, ex
@@ -36,14 +43,14 @@
 
     def getFreeMemory(self, host):
         #getiing free memory requires a REST call, so cache results
-        if not host.id in self.free_memory_cache:
+        if not host.id in self._free_memory_cache:
             try:
-                self.free_memory_cache[host.id] = host.get_statistics().get(
+                self._free_memory_cache[host.id] = host.get_statistics().get(
                     'memory.free').get_values().get_value()[0].get_datum()
             except Exception:
-                self.free_memory_cache[host.id] = -1
+                self._free_memory_cache[host.id] = -1
 
-        return self.free_memory_cache[host.id]
+        return self._free_memory_cache[host.id]
 
     def getOverUtilizedHostAndUnderUtilizedList(self, engine_hosts, 
minimum_host_memory):
         '''return the most over utilized host,
@@ -76,8 +83,8 @@
 
             available_memory = min(available_memory,
                                    host.get_max_scheduling_memory())
-            if available_memory > maximum_vm_memory:
-                maximum_vm_memory = available_memory
+
+            maximum_vm_memory = max(maximum_vm_memory, available_memory)
 
         return maximum_vm_memory
 
@@ -123,10 +130,10 @@
 
         #get our parameters from the map
         minimum_host_memory = long(args_map.get('minimum_host_memoryMB',
-                                                self.MINIMUM_MEMORY_DEFAULT))
-        minimum_host_memory = minimum_host_memory * self.TO_BYTES
+                                                
host_memory_balance._MINIMUM_MEMORY_DEFAULT))
+        minimum_host_memory = minimum_host_memory * 
host_memory_balance._TO_BYTES
         safe = bool(args_map.get('safe_selection',
-                                 self.SAFE_SELECTION_DEFAULT))
+                                 
self.host_memory_balance._SAFE_SELECTION_DEFAULT))
 
         #get all the hosts with the given ids
         engine_hosts = self._get_hosts(hosts_ids, conn)
diff --git a/doc/plugin_samples/ksm_same_os_score.py 
b/doc/plugin_samples/ksm_same_os_score.py
index f46657f..f6b78be 100644
--- a/doc/plugin_samples/ksm_same_os_score.py
+++ b/doc/plugin_samples/ksm_same_os_score.py
@@ -8,12 +8,21 @@
 
     properties_validation = ''
 
+    #Definitions
+    _CONNECTION_URL = 'http://host:port'
+    _CONNECTION_USERNAME = 'user@domain'
+    _CONNECTION_PASSWORD = '1'
+
+    _SAME_VERSION_AND_OS_SCORE = 100
+    _SAME_OS_SCORE = 20
+
     def _get_connection(self):
         #open a connection to the rest api
         connection = None
         try:
-            connection = API(url='http://localhost:8080',
-                             username='admin@internal', password='1')
+            connection = API(url=_CONNECTION_URL,
+                             username=_CONNECTION_USERNAME,
+                             password=_CONNECTION_PASSWORD)
         except BaseException as ex:
             #letting the external proxy know there was an error
             print >> sys.stderr, ex
@@ -42,9 +51,10 @@
         for host_vm in host_vms:
                 if(vm.get_os().get_type() == host_vm.get_os().get_type()):
                     if(vm.get_os().get_version() == 
host_vm.get_os().get_version()):
-                        score += 100
+                        score += _SAME_VERSION_AND_OS_SCORE
                     else:
-                        score += 20
+                        score += _SAME_OS_SCORE
+
         return (host.id, score / len(host_vms))
 
     def do_score(self, hosts_ids, vm_id, args_map):
@@ -59,4 +69,4 @@
         for host in engine_hosts:
             host_scores.append(self.score_host(vm, host, conn))
 
-        print host_scores
\ No newline at end of file
+        print host_scores
diff --git a/doc/plugin_samples/max_vm_filter.py 
b/doc/plugin_samples/max_vm_filter.py
index 0b56750..08bfb6f 100644
--- a/doc/plugin_samples/max_vm_filter.py
+++ b/doc/plugin_samples/max_vm_filter.py
@@ -7,6 +7,14 @@
 class max_vms():
     '''returns only hosts with less running vms then the maximum'''
 
+    #Definitions
+    _DEFAULT_MAX_VM_COUNT = 100
+
+    _CONNECTION_URL = 'http://host:port'
+    _CONNECTION_USERNAME = 'user@domain'
+    _CONNECTION_PASSWORD = ''
+
+
     #What are the values this module will accept, used to present
     #the user with options
     properties_validation = 'maximum_vm_count=[0-9]*'
@@ -15,8 +23,9 @@
         #open a connection to the rest api
         connection = None
         try:
-            connection = API(url='http://host:port',
-                             username='user@domain', password='')
+            connection = API(url=_CONNECTION_URL,
+                             username=_CONNECTION_USERNAME,
+                             password=_CONNECTION_PASSWORD)
         except BaseException as ex:
             #letting the external proxy know there was an error
             print >> sys.stderr, ex
@@ -37,7 +46,7 @@
             return
 
         #get our parameters from the map
-        maximum_vm_count = int(args_map.get('maximum_vm_count', 100))
+        maximum_vm_count = int(args_map.get('maximum_vm_count', 
_DEFAULT_MAX_VM_COUNT))
         engine_hosts = self._get_hosts(hosts_ids, conn)
 
         #iterate over them and decide which to accept
diff --git a/doc/plugin_samples/vm_balance.py b/doc/plugin_samples/vm_balance.py
index 4127465..5879f03 100644
--- a/doc/plugin_samples/vm_balance.py
+++ b/doc/plugin_samples/vm_balance.py
@@ -6,6 +6,13 @@
 class vm_balance():
     '''moves a vm from a host with to many'''
 
+    #Definitions
+    _DEFAULT_MAX_VM_COUNT = 100
+    _CONNECTION_URL = 'http://host:port'
+    _CONNECTION_USERNAME = 'user@domain'
+    _CONNECTION_PASSWORD = ''
+
+
     #What are the values this module will accept, used to present
     #the user with options
     properties_validation = 'maximum_vm_count=[0-9]*'
@@ -14,8 +21,9 @@
         #open a connection to the rest api
         connection = None
         try:
-            connection = API(url='http://host:port',
-                             username='user@domain', password='')
+            connection = API(url=_CONNECTION_URL,
+                             username=_CONNECTION_USERNAME,
+                             password=_CONNECTION_PASSWORD)
         except BaseException as ex:
             #letting the external proxy know there was an error
             print >> sys.stderr, ex
@@ -36,7 +44,7 @@
             return
 
         #get our parameters from the map
-        maximum_vm_count = int(args_map.get('maximum_vm_count', 100))
+        maximum_vm_count = int(args_map.get('maximum_vm_count', 
_DEFAULT_MAX_VM_COUNT))
 
         #get all the hosts with the given ids
         engine_hosts = self._get_hosts(hosts_ids, conn)
@@ -45,7 +53,7 @@
         over_loaded_host = None
         white_listed_hosts = []
         for engine_host in engine_hosts:
-            if(engine_host):
+            if engine_host:
                 if (engine_host.summary.active < maximum_vm_count):
                     white_listed_hosts.append(engine_host.id)
                     continue


-- 
To view, visit https://gerrit.ovirt.org/39592
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I034cee176c42c3d760540bac076507b31fb43a14
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-scheduler-proxy
Gerrit-Branch: master
Gerrit-Owner: Tomer Saban <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to