/**
  * Add a new host entry from an OpenStackNovaInstance object, an OpenStackNovaDomain object,
  * and optional puppet information. Returns null if a host already exists, or if
  * if the host additional fails. This function should be used for adding host entries
  * for instances (private DNS).
  *
  * @static
  * @param  $instance OpenStackNovaInstance
  * @param  $domain OpenStackNovaDomain
  * @param  $puppetinfo
  * @return OpenStackNovaHost
  */
 static function addHostFromInstance($instance, $domain, $puppetinfo = array())
 {
     global $wgAuth;
     global $wgOpenStackManagerLDAPInstanceBaseDN, $wgOpenStackManagerPuppetOptions;
     OpenStackNovaLdapConnection::connect();
     $hostname = $instance->getInstanceName();
     $instanceid = $instance->getInstanceId();
     $instancename = $instance->getInstanceName();
     $instanceproject = $instance->getProject();
     $project = $instance->getProject();
     $tmpip = $instance->getInstancePrivateIPs();
     if ($tmpip && isset($tmpip[0])) {
         $ip = $tmpip[0];
     } else {
         $ip = null;
     }
     $domainname = $domain->getFullyQualifiedDomainName();
     $region = $domain->getLocation();
     $fqdn = $instancename . '.' . $instanceproject . '.' . $domainname;
     $host = OpenStackNovaHost::getHostByNameAndProject($instancename, $instanceproject, $region);
     if ($host) {
         $wgAuth->printDebug("Failed to add host {$hostname} as the DNS entry already exists", NONSENSITIVE);
         return null;
     }
     $hostEntry = array();
     $hostEntry['objectclass'][] = 'dcobject';
     $hostEntry['objectclass'][] = 'dnsdomain';
     $hostEntry['objectclass'][] = 'domainrelatedobject';
     $hostEntry['dc'] = $fqdn;
     # $hostEntry['l'] = $instance->getInstanceAvailabilityZone();
     if ($ip) {
         $hostEntry['arecord'] = $ip;
     }
     $hostEntry['associateddomain'][] = $instanceid . '.' . $domainname;
     $hostEntry['associateddomain'][] = $hostname . '.' . $domainname;
     $hostEntry['associateddomain'][] = $instanceid . '.' . $project . '.' . $domainname;
     $hostEntry['associateddomain'][] = $hostname . '.' . $project . '.' . $domainname;
     $hostEntry['l'] = $domain->getLocation();
     if ($wgOpenStackManagerPuppetOptions['enabled']) {
         $hostEntry['objectclass'][] = 'puppetclient';
         foreach ($wgOpenStackManagerPuppetOptions['defaultclasses'] as $class) {
             $hostEntry['puppetclass'][] = $class;
         }
         foreach ($wgOpenStackManagerPuppetOptions['defaultvariables'] as $variable => $value) {
             $hostEntry['puppetvar'][] = $variable . '=' . $value;
         }
         if ($puppetinfo) {
             if (isset($puppetinfo['classes'])) {
                 foreach ($puppetinfo['classes'] as $class) {
                     $hostEntry['puppetclass'][] = $class;
                 }
             }
             if (isset($puppetinfo['variables'])) {
                 foreach ($puppetinfo['variables'] as $variable => $value) {
                     if ($value) {
                         $hostEntry['puppetvar'][] = $variable . '=' . $value;
                     }
                 }
             }
         }
         $hostEntry['puppetvar'][] = 'instanceproject=' . $project;
         $hostEntry['puppetvar'][] = 'instancename=' . $hostname;
     }
     $dn = 'dc=' . $fqdn . ',' . $wgOpenStackManagerLDAPInstanceBaseDN;
     $success = LdapAuthenticationPlugin::ldap_add($wgAuth->ldapconn, $dn, $hostEntry);
     if ($success) {
         $domain->updateSOA();
         $wgAuth->printDebug("Successfully added host {$fqdn}", NONSENSITIVE);
         return OpenStackNovaHost::getHostByInstanceNameAndProject($instancename, $instanceproject, $region);
     } else {
         $wgAuth->printDebug("Failed to add host {$fqdn} with dn of {$dn}", NONSENSITIVE);
         return null;
     }
 }