public function testDeleteUnknownId() { $this->setExpectedException('\\Centreon\\Internal\\Exception', $this->errMsg); Service::delete(9999); }
/** * Deploy services by host templates * * @param int $hostId * @param int $hostTemplateId */ public static function deployServices($hostId, $hostTemplateId = null) { static $deployedServices = array(); $aServices = array(); $db = Di::getDefault()->get('db_centreon'); //get host template $aHostTemplates = HostRepository::getTemplateChain($hostId, array(), -1); // get host services $aHostServices = HostServiceRelation::getMergedParameters(array('host_id'), array('service_id', 'service_description', 'service_alias', 'inherited'), -1, 0, null, "ASC", array('host_id' => $hostId), "OR"); // get all service templates linked to a host by its host templates $aHostServiceTemplates = array(); foreach ($aHostTemplates as $oHostTemplate) { $aHostTemplateServiceTemplates = HostTemplateServiceTemplateRelation::getMergedParameters(array('host_id'), array('service_id', 'service_description', 'service_alias'), -1, 0, null, "ASC", array('host_id' => $oHostTemplate['id']), "OR"); // Remove services with same description foreach ($aHostTemplateServiceTemplates as $oHostTemplateServiceTemplate) { if (!isset($aHostServiceTemplates[$oHostTemplateServiceTemplate['service_alias']])) { $aHostServiceTemplates[$oHostTemplateServiceTemplate['service_alias']] = $oHostTemplateServiceTemplate; } } } // get services linked to the host $aServicesDescription = array_values(array_column($aHostServices, 'service_description')); $repository = "CentreonConfiguration\\Repository\\ServiceRepository"; $repository::setRelationMap(array("service_hosts" => "\\CentreonConfiguration\\Models\\Relation\\Service\\Host", "service_parents" => "\\CentreonConfiguration\\Models\\Relation\\Service\\Serviceparents", "service_children" => "\\CentreonConfiguration\\Models\\Relation\\Service\\Servicechildren")); $repository::setObjectName("Service"); $repository::setObjectClass("\\CentreonConfiguration\\Models\\Service"); $oModel = "CentreonConfiguration\\Models\\Service"; $oSlugify = new CentreonSlugify($oModel, $repository); $db->beginTransaction(); $aTemplateServicesDescription = array_keys($aHostServiceTemplates); foreach ($aHostServices as $service) { if ($service['inherited'] == 1 && !in_array($service['service_description'], $aTemplateServicesDescription)) { Service::delete($service['service_id']); } } // create services which don't yet exist foreach ($aHostServiceTemplates as $oHostServiceTemplate) { if (!in_array($oHostServiceTemplate['service_alias'], $aServicesDescription)) { $sHostName = Host::get($hostId, 'host_name'); if (isset($sHostName['host_name'])) { $sString = $sHostName['host_name'] . " " . $oHostServiceTemplate['service_alias']; } else { $sString = $oHostServiceTemplate['service_alias']; } $sSlug = $oSlugify->slug($sString); if (!empty($oHostServiceTemplate['service_alias'])) { $sData = $oHostServiceTemplate['service_alias']; } else { $sData = $oHostServiceTemplate['service_description']; } $newService['service_slug'] = $sSlug; $newService['service_description'] = $sData; $newService['service_template_model_stm_id'] = $oHostServiceTemplate['service_id']; $newService['service_register'] = 1; $newService['service_activate'] = 1; $newService['organization_id'] = Di::getDefault()->get('organization'); $newService['inherited'] = 1; $serviceId = Service::insert($newService); HostServiceRelation::insert($hostId, $serviceId); ServiceHostTemplateRelation::insert($serviceId, $oHostServiceTemplate['host_id']); } } $db->commit(); }