[icinga-checkins] icinga.org: icinga-web/jmosshammer/apiIntegration: * Added queries for contact groups, finished HostDetailTests

git at icinga.org git at icinga.org
Tue Jan 18 13:14:54 CET 2011


Module: icinga-web
Branch: jmosshammer/apiIntegration
Commit: 48a6f514669b6c5cc702f2e43ff7bda9427adbce
URL:    https://git.icinga.org/?p=icinga-web.git;a=commit;h=48a6f514669b6c5cc702f2e43ff7bda9427adbce

Author: jmosshammer <jmosshammer at ws-jmosshammer.(none)>
Date:   Tue Jan 18 13:14:21 2011 +0100

* Added queries for contact groups, finished HostDetailTests

---

 .../lib/database/models/IcingaContactgroups.php    |   22 ++++++++++++++-
 .../Api/lib/database/models/IcingaContacts.php     |   23 ++++++++++++++-
 .../Api/lib/database/models/IcingaHosts.php        |   25 +++++++++++++++-
 .../models/generated/BaseIcingaContactgroups.php   |    5 +--
 .../models/generated/BaseIcingaContacts.php        |    6 +++-
 .../database/models/generated/BaseIcingaHosts.php  |   20 ++++--------
 etc/tests/tests/api/hosts/HostDetailTest.php       |   30 ++++++++++++++++---
 7 files changed, 105 insertions(+), 26 deletions(-)

diff --git a/app/modules/Api/lib/database/models/IcingaContactgroups.php b/app/modules/Api/lib/database/models/IcingaContactgroups.php
index 3a9bd9b..edc3cda 100644
--- a/app/modules/Api/lib/database/models/IcingaContactgroups.php
+++ b/app/modules/Api/lib/database/models/IcingaContactgroups.php
@@ -12,5 +12,25 @@
  */
 class IcingaContactgroups extends BaseIcingaContactgroups
 {
+	public function __get($field) {
+		switch($field) {
+			case 'hosts':
+				$hosts = $this->getHosts();
+				$this->set("hosts",$hosts);
+				return $hosts;
+				break;	
+		}
+		return parent::__get($field);
+	}
 
-}
\ No newline at end of file
+	public function getHosts() {
+		return Doctrine_Query::create()
+				->select("h.*")
+				->from("IcingaHosts h")
+				->innerJoin("h.contactgroups cg ON cg.contactgroup_object_id = "
+					.$this->contactgroup_object_id." AND cg.instance_id = ".$this->instance_id.
+					" AND cg.host_id = h.host_id")
+				->execute(null,Doctrine_Core::HYDRATE_RECORD);
+	}
+
+}
diff --git a/app/modules/Api/lib/database/models/IcingaContacts.php b/app/modules/Api/lib/database/models/IcingaContacts.php
index 80599da..6dae8d8 100644
--- a/app/modules/Api/lib/database/models/IcingaContacts.php
+++ b/app/modules/Api/lib/database/models/IcingaContacts.php
@@ -12,5 +12,26 @@
  */
 class IcingaContacts extends BaseIcingaContacts
 {
+	public function __get($field) {
+		switch($field) {
+			case 'hosts':
+				$hosts = $this->getHosts();
+				$this->set("hosts",$hosts);
+				return $hosts;
+				break;	
+		}
+		return parent::__get($field);
+	}
 
-}
\ No newline at end of file
+	public function getHosts() {
+		return Doctrine_Query::create()
+				->select("h.*")
+				->from("IcingaHosts h")
+				->innerJoin("h.contacts cg ON cg.contact_object_id = "
+					.$this->contact_object_id." AND cg.insance_id = ".$this->instance_id.
+					" AND cg.host_id = h.host_id")
+				->execute(null,Doctrine_Core::HYDRATE_RECORD);
+	}
+
+
+}
diff --git a/app/modules/Api/lib/database/models/IcingaHosts.php b/app/modules/Api/lib/database/models/IcingaHosts.php
index be180c3..1612210 100644
--- a/app/modules/Api/lib/database/models/IcingaHosts.php
+++ b/app/modules/Api/lib/database/models/IcingaHosts.php
@@ -15,13 +15,34 @@ class IcingaHosts extends BaseIcingaHosts
 	public function __get($method)  {
 		switch($method) {
 			case  'contacts':
-				echo $method;
-				die();
+				$contacts = $this->getContacts();					
+				$this->set("contacts",$contacts);
+				return $contacts;
 				break;
 			case 'contactgroups':
+				$contactgroups = $this->getContactgroups();
+				$this->set("contactgroups",$contactgroups);
+				return $contactgroups;
 				break;
 		}
 		return parent::__get($method);
 	} 	
+	public function getContacts() {
+		return Doctrine_Query::create()
+			->select("c.*")
+			->from("IcingaContacts c")
+			->innerJoin("c.hosts hc ON hc.host_id = ".$this->host_id
+				." AND hc.instance_id = ".$this->instance_id." AND hc.contact_object_id = c.contact_object_id")
+			->execute(null,Doctrine_Core::HYDRATE_RECORD);
 	
+	}
+	
+	public function getContactgroups() {
+		return Doctrine_Query::create()
+			->select("c.*")
+			->from("IcingaContactgroups c")
+			->innerJoin("c.hosts hc ON hc.host_id = ".$this->host_id
+				." AND hc.instance_id = ".$this->instance_id." AND hc.contactgroup_object_id = c.contactgroup_object_id")
+			->execute(null,Doctrine_Core::HYDRATE_RECORD);	
+	}		
 }
diff --git a/app/modules/Api/lib/database/models/generated/BaseIcingaContactgroups.php b/app/modules/Api/lib/database/models/generated/BaseIcingaContactgroups.php
index bd5762c..6457b84 100644
--- a/app/modules/Api/lib/database/models/generated/BaseIcingaContactgroups.php
+++ b/app/modules/Api/lib/database/models/generated/BaseIcingaContactgroups.php
@@ -79,10 +79,9 @@ abstract class BaseIcingaContactgroups extends Doctrine_Record
 		'local' => 'instance_id',
 		'foreign' => 'instance_id'			
 	));
-	$this->hasMany('IcingaHosts as hosts', array(
+	$this->hasMany('IcingaHostContactgroups as hosts', array(
 		'local' => 'contactgroup_object_id',
-		'foreign' => 'host_id',
-		'refclass' => 'IcingaHostContactgroups'
+		'foreign' => 'contactgroup_object_id',	
 	));	
 	$this->hasMany('IcingaContacts as members', array(
 		'local' => 'contactgroup_object_id',
diff --git a/app/modules/Api/lib/database/models/generated/BaseIcingaContacts.php b/app/modules/Api/lib/database/models/generated/BaseIcingaContacts.php
index b00ae6a..06a29d2 100644
--- a/app/modules/Api/lib/database/models/generated/BaseIcingaContacts.php
+++ b/app/modules/Api/lib/database/models/generated/BaseIcingaContacts.php
@@ -272,7 +272,11 @@ abstract class BaseIcingaContacts extends Doctrine_Record
 
     public function setUp()
     {
-        $this->hasOne('IcingaInstances as instance', array(
+        $this->hasMany('IcingaHostContacts as hosts',array(
+		'local' => 'contact_object_id',
+		'foreign' => 'contact_object_id'
+	));
+	$this->hasOne('IcingaInstances as instance', array(
 		'local' => 'instance_id',
 		'foreign' => 'instance_id'			
 	));
diff --git a/app/modules/Api/lib/database/models/generated/BaseIcingaHosts.php b/app/modules/Api/lib/database/models/generated/BaseIcingaHosts.php
index c1c221b..aa3de6d 100644
--- a/app/modules/Api/lib/database/models/generated/BaseIcingaHosts.php
+++ b/app/modules/Api/lib/database/models/generated/BaseIcingaHosts.php
@@ -681,23 +681,17 @@ abstract class BaseIcingaHosts extends Doctrine_Record
 	    'local' => 'host_object_id',
 	    'foreign' => 'host_object_id'
 	));
-/**	
-*	IDOUtils messes around with ids (Why the heck is it host_id here? 
-*	All other fields have host_object_id) and Doctrine doesn't like that,
-*	so this will be added in the main class
-*/
-/**
-	$this->hasMany("IcingaContacts as contacts", array(
+// Retrieved by custom finders
+
+	$this->hasMany("IcingaHostContacts as contacts", array(
 	    'local' => 'host_id',
-	    'foreign' => 'contact_object_id',
-	    'refclass' => 'IcingaHostContacts'
+	    'foreign' => 'host_id',
 	));
-	$this->hasMany("IcingaContactgroups as contactgroups", array(
+	$this->hasMany("IcingaHostContactgroups as contactgroups", array(
 	    'local' => 'host_id',
-	    'foreign' => 'contactgroup_object_id',
-	    'refclass' => 'IcingaHostContactgroups'
+	    'foreign' => 'host_id', 
 	));
-*/
+
 	$this->hasMany("IcingaStatehistory as stateHistory", array(
 	    'local' => 'host_object_id',
 	    'foreign' => 'object_id'
diff --git a/etc/tests/tests/api/hosts/HostDetailTest.php b/etc/tests/tests/api/hosts/HostDetailTest.php
index e4c4808..b52e835 100644
--- a/etc/tests/tests/api/hosts/HostDetailTest.php
+++ b/etc/tests/tests/api/hosts/HostDetailTest.php
@@ -206,7 +206,7 @@ class HostDetailTest extends PHPUnit_Framework_TestCase {
 	**/
 	public function testGetContacts() {
 		$host = $this->hostProvider();
-		$this->assertFalse(is_null($host->contacts->toArray()));
+		$this->assertFalse(is_null($host->contacts),"Contacts for host couldn't be retrieved, returned null");
 
 		info("********** DB Fixture upgrade needed - no host contacts available \n");
 		$this->markTestIncomplete("DB fixture doesn't allow proper testing of this object\n");
@@ -217,20 +217,40 @@ class HostDetailTest extends PHPUnit_Framework_TestCase {
 	**/
 	public function testGetContactgroups() {
 		$host = $this->hostProvider();
-		print_r($host->contactgroups->toArray());
-		die();
+		$this->assertFalse(is_null($host->contactgroups), "Contactgroups for host couldn't be retrieved, returned null");
+		$this->assertEquals($host->contactgroups->count(),1,"Expected 1 contactgroup to be returned");
+		$this->assertFalse(is_null($host->contactgroups->getFirst()->hosts),1,"Couldn't retrieve hosts for contactgroups" );
+		$found = false;
+		foreach($host->contactgroups->getFirst()->hosts as $_host) {
+			if($host == $_host) {
+				$found = true;
+				break;
+			}
+		}
+		$this->assertTrue($found, "Couldn't find host in host list of contactgroup");
+		success("Retrieving contactgroups succeeded\n");	
 	}
 	
+	/**
+	*   @depends HostDetailTest::testGetHost
+	**/
 	public function testGetHostChecks() {
 		$host = $this->hostProvider();
-		$host->checks->toArray();
 		$this->assertFalse(is_null($host->checks));
 		info("********** DB Fixture upgrade needed - no hostchecks available \n");
 		$this->markTestIncomplete("DB fixture doesn't allow proper testing of this object");
 	}	
 	
+		
+	/**
+	*   @depends HostDetailTest::testGetHost
+	**/
 	public function testGetServices() {
-		$this->fail("Not implemented");
+		$host = $this->hostProvider();
+		$this->assertFalse(is_null($host->services));
+		$this->assertEquals($host->services->count(),2);
+		$this->assertTrue($host->services->getFirst()->host == $host,"Returned service doesn't belong to host");	
+		success("Retrieving Services from host succeeded\n");
 	}
 
 }





More information about the icinga-checkins mailing list