function RunImport()
{
    $taglist = isset($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
    $objectnames = $_POST['objectname'];
    global $dbxlink;
    $log = emptyLog();
    foreach ($objectnames as $objectname) {
        // FIXME: This reads the entire directory for each object. Not very efficient.
        if ($handle = opendir('./yamls/')) {
            while (false !== ($file = readdir($handle))) {
                # puppet names the files $FQDN.yaml, not PSMN
                if ($file == $objectname . ".yaml") {
                    # SPYC is not happy with the puppet header. Hence read it as string, strip the header and feed it to SPYC
                    $file_contents = file_get_contents("./yamls/{$file}");
                    $file_contents = substr($file_contents, strpos($file_contents, "\n") + 1);
                    $yaml_file_array = Spyc::YAMLLoadString($file_contents);
                    // FIXME: Is this the correct way to narrow in on an array?
                    // At this point, $yaml_file_array contains all the data from the YAML files in a indexed array.
                    $yaml_name = $yaml_file_array['name'];
                    // switch to the 2nd part of the array
                    $yaml_file_array = $yaml_file_array['parameters'];
                    // getSearchResultByField ($tname, $rcolumns, $scolumn, $terms, $ocolumn = '', $exactness = 0|1|2)
                    $object = getSearchResultByField('RackObject', array('id'), 'name', $yaml_name, '', 2);
                    if ($object) {
                        # Object exists. Do NOT modify.
                        $id = $object[0]['id'];
                        $log = mergeLogs($log, oneLiner(202, array("{$objectname} exists. No modifications!")));
                    } else {
                        // Object does not exist. Create new.
                        // Syntax: commitAddObject ($new_name, $new_label, $new_type_id, $new_asset_no, $taglist = array())
                        // Type is 4, server, by default.
                        $new_yamlobject = commitAddObject($yaml_name, '', 4, $yaml_file_array['serialnumber'], $taglist);
                        // Hardware type (i.e. ProLiant DL380 G6a), Dict Chapter ID is '11';
                        $hw_dict_key = getdict($hw = $yaml_file_array['productname'], $chapter = 11);
                        commitUpdateAttrValue($object_id = $new_yamlobject, $attr_id = '2', $value = $hw_dict_key);
                        // Operating system string, Dict Chapter ID is '13'.
                        $osrelease = $yaml_file_array['operatingsystem'] . " " . $yaml_file_array['operatingsystemrelease'];
                        $os_dict_key = getdict($hw = $osrelease, $chapter = 13);
                        commitUpdateAttrValue($object_id = $new_yamlobject, $attr_id = '4', $value = $os_dict_key);
                        /*
                                    // FIXME: The IDs should be looked up, and not preset.
                                    // Architecture. Attribute ID is '10000'.
                                    commitUpdateAttrValue ($object_id = $new_yamlobject, $attr_id = '10000', $value = $yaml_file_array['hardwareisa']);
                                    // Memory. Attribute ID is 17.
                                    commitUpdateAttrValue ($object_id = $new_yamlobject, $attr_id = '17', $value = (int)$yaml_file_array['memorysize']);
                                    // CPU. Attribute ID is 100001
                                    $cpu = $yaml_file_array['processorcount'] . " x " . $yaml_file_array['processor0'];
                                    commitUpdateAttrValue ($object_id = $new_yamlobject, $attr_id = '10001', $value = $cpu);
                        */
                        // OEM S/N 1. Attribute ID is '1'.
                        commitUpdateAttrValue($object_id = $new_yamlobject, $attr_id = '1', $value = $yaml_file_array['serialnumber']);
                        // FQDN. Attribute ID is '3'.
                        commitUpdateAttrValue($object_id = $new_yamlobject, $attr_id = '3', $value = $yaml_file_array['fqdn']);
                        // UUID. Attribute ID is '25'.
                        commitUpdateAttrValue($object_id = $new_yamlobject, $attr_id = '25', $value = $yaml_file_array['uuid']);
                        // Hypervisor. Attribute ID is '26', Dict Chapter ID is '29'.
                        // Hypervisor key does not exist in standard Puppet yaml file, added by PSMN
                        if (isset($yaml_file_array['hypervisor'])) {
                            $hv_dict_key = getdict($hw = $yaml_file_array['hypervisor'], $chapter = 29);
                            commitUpdateAttrValue($object_id = $new_yamlobject, $attr_id = '26', $value = $hv_dict_key);
                        }
                        // NICS
                        // Warning! This part only work if default Configuration is modified
                        // Go to "MainPage -> Configuration -> User Interface"
                        // Modify "AutoPorts configuration": Change "4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm" to "15 = 1*446*kvm"
                        // Ref: http://www.freelists.org/post/racktables-users/Automatic-insertions-of-servers-in-the-db,7
                        $nics = explode(',', $yaml_file_array['interfaces'], 9);
                        $count = count($nics);
                        for ($i = 0; $i < $count; $i++) {
                            switch ($nics[$i]) {
                                case "sit0":
                                    break 1;
                                case "ib0":
                                    // infiniband
                                    if (isset($yaml_file_array['ipaddress_' . $nics[$i]])) {
                                        $ip = $yaml_file_array['ipaddress_' . $nics[$i]];
                                    }
                                    // do NOT import infiniband MAC for now
                                    //                if (isset($yaml_file_array['macaddress_' . $nics[$i]]))
                                    //                {
                                    //                  $mac = $yaml_file_array['macaddress_' . $nics[$i]];
                                    //                }
                                    // Add port to object. Type 40 is 10GBase-CX4, MAC can be NULL
                                    commitAddPort($object_id = $new_yamlobject, $nics[$i], 40, 'infiniband', $mac);
                                    // Add IP to object.
                                    if (isset($ip)) {
                                        bindIpToObject($ip, $new_yamlobject, $nics[$i], 'regular');
                                    }
                                    break 1;
                                default:
                                    if (preg_match("eth", $nics[$i]) === 0) {
                                        break 1;
                                    }
                                    # this one might be bad for non-linux OSes ?
                                    if (isset($yaml_file_array['ipaddress_' . $nics[$i]])) {
                                        $ip = $yaml_file_array['ipaddress_' . $nics[$i]];
                                    }
                                    if (isset($yaml_file_array['macaddress_' . $nics[$i]])) {
                                        $mac = $yaml_file_array['macaddress_' . $nics[$i]];
                                    }
                                    // Add port to object. Type 24 is 1000Base-T
                                    commitAddPort($object_id = $new_yamlobject, $nics[$i], 24, 'Ethernet port', "{$mac}");
                                    // Add IP to object.
                                    if (isset($ip)) {
                                        bindIpToObject($ip, $new_yamlobject, $nics[$i], 'regular');
                                    }
                                    break 1;
                            }
                            unset($ip);
                            unset($mac);
                        }
                        // Create a URL for the log message.
                        $url = makeHref(array('page' => 'object', 'tab' => 'default', 'object_id' => $new_yamlobject));
                        $loginstance = "<a href=\"{$url}\">" . $objectname . "</a>";
                        $log = mergeLogs($log, oneLiner(80, array("{$loginstance}")));
                    }
                }
            }
        }
    }
    return showSuccess($log);
}
 /**
  * Unset everything before the next test.
  */
 protected function tearDown()
 {
     unset($this->api);
     emptyLog();
 }
function oneLiner($code, $args = array())
{
    $ret = emptyLog();
    $ret['m'][] = count($args) ? array('c' => $code, 'a' => $args) : array('c' => $code);
    return $ret;
}