Noam Slomianko has uploaded a new change for review. Change subject: Change Discover to return description and regex ......................................................................
Change Discover to return description and regex Discover used to return only the name of the module, now it returns the description and regex of each function type changed tests to reflect the new return value Signed-off-by: Noam Slomianko <nslom...@redhat.com> Change-Id: Ibe4e6a44302636c40c96453d433caf09fc5bda94 --- M plugins/dummy.py M src/loader.py M src/request_handler.py M src/request_handler_test.py M src/utils.py M tests/java/src/main/java/org/ovirt/schedulerproxy/SchedulerProxy.java M tests/java/src/test/java/org/ovirt/schedulerproxy/SchedulerProxyTest.java 7 files changed, 101 insertions(+), 34 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-scheduler-proxy refs/changes/76/17576/1 diff --git a/plugins/dummy.py b/plugins/dummy.py index 57b8d12..e2e16d0 100755 --- a/plugins/dummy.py +++ b/plugins/dummy.py @@ -20,11 +20,12 @@ # ''' -This dummy sample is the most simple filter, it will return all ID's of all the VM's passed over +This dummy sample is the most simple example it will return all ID's of all the Hosts's passed over ''' # do not remove this import, the ovirtsdk is not going to work without it from ovirtsdk.xml import params +from ovirtsdk import api import ovirtsdk.infrastructure.brokers @@ -54,6 +55,13 @@ print filterClassInstance.filter(hosts, vm, args) +def describeFilter(): + description = "This is a simple filter that returns all given host ID" + #this filter requires no regex + custom_properties_regex = "" + return description, custom_properties_regex + + #Files can hold all three supported functions (filterFucntion,scoreFunction,balanceFunction) class SampleScore(): def __init__(self): @@ -63,6 +71,15 @@ hostScores = [] #use hosts IDs and VM ID to call the Rest API and make a decision + ''' + Sample code: + #take from configuration + api = API(url='http://host:port', username='user@domain', password='password') + for id in hosts: + hostObject = api.hosts.get(query='id=' + id) + + ... etc + ''' for hostID in hosts: #Do work hostScores.append((hostID, 50)) @@ -73,6 +90,13 @@ def scoreFunction(hosts, vm, args): scoreClassInstance = SampleScore() print scoreClassInstance.score(hosts, vm, args) + + +def describeScore(): + description = "This is a simple score function that returns all given host ID with score 50" + #this score function requires no regex + custom_properties_regex = "" + return description, custom_properties_regex class SampleBalance(): @@ -86,4 +110,12 @@ def balanceFunction(hosts, args): balanceInstance = SampleBalance() - print balanceInstance.balance(hosts, args) \ No newline at end of file + print balanceInstance.balance(hosts, args) + + +def describeBalance(): + description = "This is a fake balance function that returns always return the " \ + "guid 33333333-3333-3333-3333-333333333333" + #this score function requires no regex + custom_properties_regex = "" + return description, custom_properties_regex \ No newline at end of file diff --git a/src/loader.py b/src/loader.py index 385f5f5..d995d12 100644 --- a/src/loader.py +++ b/src/loader.py @@ -15,12 +15,24 @@ mod = __import__(name) retValue = (name,) if hasattr(mod, _utils.FILTER): - retValue += (_utils.FILTER,) + if hasattr(mod, _utils.FILTER_DESCRIPTION): + description, custom_properties_map = getattr(mod, _utils.FILTER_DESCRIPTION)() + retValue += ((_utils.FILTER, description, custom_properties_map),) + else: + retValue += ((_utils.FILTER, "", ""),) if hasattr(mod, _utils.SCORE): - retValue += (_utils.SCORE,) + if hasattr(mod, _utils.SCORE_DESCRIPTION): + description, custom_properties_map = getattr(mod, _utils.SCORE_DESCRIPTION)() + retValue += ((_utils.SCORE, description, custom_properties_map),) + else: + retValue += ((_utils.SCORE, "", ""),) if hasattr(mod, _utils.BALANCE): - retValue += (_utils.BALANCE,) + if hasattr(mod, _utils.BALANCE_DESCRIPTION): + description, custom_properties_map = getattr(mod, _utils.BALANCE_DESCRIPTION)() + retValue += ((_utils.BALANCE, description, custom_properties_map),) + else: + retValue += ((_utils.BALANCE, "", ""),) print retValue diff --git a/src/request_handler.py b/src/request_handler.py index c552917..e65edbd 100644 --- a/src/request_handler.py +++ b/src/request_handler.py @@ -36,9 +36,9 @@ def __init__(self, pluginDir, analyzerDir): self._pluginDir = pluginDir self._analyzerDir = analyzerDir - self._filters = set() - self._scores = set() - self._balancers = set() + self._filters = dict() + self._scores = dict() + self._balancers = dict() self._utils = utils() self.loadModules() @@ -70,20 +70,20 @@ availableFunctions = runner.getResults() moduleName = availableFunctions[0] - if self._utils.FILTER in availableFunctions: - self._filters.add(moduleName) - if self._utils.SCORE in availableFunctions: - self._scores.add(moduleName) - - if self._utils.BALANCE in availableFunctions: - self._balancers.add(moduleName) + for functionName, description, custom_properties_map in availableFunctions[1:]: + if self._utils.FILTER == functionName: + self._filters[moduleName] = (description, custom_properties_map) + elif self._utils.SCORE == functionName: + self._scores[moduleName] = (description, custom_properties_map) + elif self._utils.BALANCE == functionName: + self._balancers[moduleName] = (description, custom_properties_map) def discover(self): #temporary? return { - "filters": list(self._filters), - "scores": list(self._scores), - "balance": list(self._balancers)} + "filters": self._filters, + "scores": self._scores, + "balance": self._balancers} def run_filters(self, filters, hostIDs, vmID, properties_map): #Intersects the results from the filters diff --git a/src/request_handler_test.py b/src/request_handler_test.py index 1ad7188..a3e6eb9 100644 --- a/src/request_handler_test.py +++ b/src/request_handler_test.py @@ -30,7 +30,12 @@ executor = RequestHandler(os.path.join(os.getcwd(), 'plugins'), os.path.join(os.getcwd(), 'src')) ret = executor.discover() - assert ret == {'balance': ['dummy'], - 'filters': ['dummy'], - 'scores': ['dummy']} + assert ret == {'balance': + {'dummy': ('This is a fake balance function that returns ' + 'always return the guid 33333333-3333-3333-3333-333333333333', '')}, + 'filters': + {'dummy': ('This is a simple filter that returns all given host ID', '')}, + 'scores': + {'dummy': ('This is a simple score function that returns ' + 'all given host ID with score 50', '')}} pass diff --git a/src/utils.py b/src/utils.py index c932088..5d8a3ff 100644 --- a/src/utils.py +++ b/src/utils.py @@ -26,8 +26,11 @@ pass FILTER = 'filterFunction' + FILTER_DESCRIPTION = 'describeFilter' SCORE = 'scoreFunction' + SCORE_DESCRIPTION = 'describeScore' BALANCE = 'balanceFunction' + BALANCE_DESCRIPTION = 'describeBalance' LOADER_MODULE = 'loader' LOADER_FUNC = 'analyze' diff --git a/tests/java/src/main/java/org/ovirt/schedulerproxy/SchedulerProxy.java b/tests/java/src/main/java/org/ovirt/schedulerproxy/SchedulerProxy.java index a473b38..482307c 100644 --- a/tests/java/src/main/java/org/ovirt/schedulerproxy/SchedulerProxy.java +++ b/tests/java/src/main/java/org/ovirt/schedulerproxy/SchedulerProxy.java @@ -20,25 +20,30 @@ client.setConfig(config); } - public HashMap<String, List<String>> discover() throws XmlRpcException { + public HashMap<String, HashMap<String, String[]>> discover() throws XmlRpcException { Object execute = client.execute("discover", new Object[] {}); return parseDiscover(execute); } - private HashMap<String, List<String>> parseDiscover(Object result){ + private HashMap<String, HashMap<String, String[]>> parseDiscover(Object result){ if (result == null || ! (result instanceof HashMap)){ System.out.println("discover error"); return null; } - HashMap<String, Object[]> castedResult = (HashMap<String, Object[]>)result; + HashMap<String, HashMap<String, Object[]>> castedResult = (HashMap<String, HashMap<String, Object[]>>)result; //Its a list of host IDs - HashMap<String, List<String>> retValue = new HashMap<String, List<String>>(); - for (String key : castedResult.keySet()) { - List<String> values = new LinkedList<String>(); - for (Object o : castedResult.get(key)) { - values.add((String)o); + HashMap<String, HashMap<String, String[]>> retValue = new HashMap<String, HashMap<String, String[]>>(); + for (String keyType : castedResult.keySet()) { + HashMap<String, Object[]> typeMap = castedResult.get(keyType); + HashMap<String, String[]> newTypeMap = new HashMap<String, String[]>(); + for (String keyModuleName : typeMap.keySet()) { + String[] keys = new String[2]; + for (int i = 0; i < 2; i++) { + keys[i] = (String)typeMap.get(keyModuleName)[i]; + } + newTypeMap.put(keyModuleName, keys); } - retValue.put(key, values); + retValue.put(keyType, newTypeMap); } return retValue; } diff --git a/tests/java/src/test/java/org/ovirt/schedulerproxy/SchedulerProxyTest.java b/tests/java/src/test/java/org/ovirt/schedulerproxy/SchedulerProxyTest.java index a0608a9..d0c8ff1 100644 --- a/tests/java/src/test/java/org/ovirt/schedulerproxy/SchedulerProxyTest.java +++ b/tests/java/src/test/java/org/ovirt/schedulerproxy/SchedulerProxyTest.java @@ -18,6 +18,9 @@ static String[] HOST_ARRAY = new String[] { HOST_ID1, HOST_ID2 }; static String VM_ID = "33333333-3333-3333-3333-333333333333"; static String BALANCE_RESULT = "33333333-3333-3333-3333-333333333333"; + static String FILTER_DESCRIPTION = "This is a simple filter that returns all given host ID"; + static String SCORE_DESCRIPTION = "This is a simple score function that returns all given host ID with score 50"; + static String BALANCE_DESCRIPTION = "This is a fake balance function that returns always return the guid 33333333-3333-3333-3333-333333333333"; SchedulerProxy proxy; @@ -28,19 +31,24 @@ @Test public void testDiscover() throws XmlRpcException { - HashMap<String, List<String>> result = proxy.discover(); + HashMap<String, HashMap<String, String[]>> result = proxy.discover(); + System.out.println(result); assertTrue(result.containsKey("filters")); - assertTrue(result.get("filters").contains(FILE_NAME)); + assertTrue(result.get("filters").containsKey((FILE_NAME))); + assertTrue(result.get("filters").get(FILE_NAME)[0].equals(FILTER_DESCRIPTION)); assertTrue(result.containsKey("scores")); - assertTrue(result.get("scores").contains(FILE_NAME)); + assertTrue(result.get("scores").containsKey((FILE_NAME))); + assertTrue(result.get("scores").get(FILE_NAME)[0].equals(SCORE_DESCRIPTION)); assertTrue(result.containsKey("filters")); - assertTrue(result.get("balance").contains(FILE_NAME)); + assertTrue(result.get("balance").containsKey((FILE_NAME))); + assertTrue(result.get("balance").get(FILE_NAME)[0].equals(BALANCE_DESCRIPTION)); } @Test public void testFilter() throws XmlRpcException { List<String> result = proxy.filter(new String[] { FILE_NAME }, HOST_ARRAY, VM_ID, ""); + System.out.println(result); assertTrue(result.size() == 2); assertTrue(result.contains(HOST_ID1)); assertTrue(result.contains(HOST_ID2)); @@ -49,6 +57,7 @@ @Test public void testScore() throws XmlRpcException { HashMap<String,Integer> result = proxy.score(new String[] { FILE_NAME }, new Integer[] { 2 }, HOST_ARRAY, VM_ID, ""); + System.out.println(result); assertTrue(result.size() == 2); assertTrue(result.get(HOST_ID1) == 100); assertTrue(result.get(HOST_ID2) == 100); @@ -57,6 +66,7 @@ @Test public void testBalance() throws XmlRpcException { String result = proxy.balance(FILE_NAME, HOST_ARRAY, ""); + System.out.println(result); assertTrue(result.equals(BALANCE_RESULT)); } } \ No newline at end of file -- To view, visit http://gerrit.ovirt.org/17576 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ibe4e6a44302636c40c96453d433caf09fc5bda94 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-scheduler-proxy Gerrit-Branch: master Gerrit-Owner: Noam Slomianko <nslom...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches