@sapeurorca You can find host ID’s by name and parse it how ever you like. The api system handles things at the simplest level, which is by id’s (most API’s follow this same type of approach).
This was not implemented with 1.3.3 no so you would have to script it entirely by yourself as needed.
You can write a php script to do what you need.
Something like:
<?php
require('/var/www/fog/commons/base.inc.php');
/**
* Could be handled more dynamically by making this a get variable.
* This would allow you to call <scriptname>.php?groupname=SomeGroupName
*/
//$groupname = filter_input(INPUT_GET, 'groupname');
$groupname = 'groupnamehere';
/**
* Get the group object.
*/
$group = FOGCore::getClass('Group')
->set('name', $groupname)
->load('name');
/**
* If the group isn't valid or doesn't exist, make it.
*/
if (!$group->isValid()) {
$group->save();
}
/**
* Get the groups ID.
*/
$groupID = $group->get('id');
/**
* The file must be in hostname1,hostname2 format (or csv if you will), You could modify to
* process as csv where new hostnames are on new lines too. Something like:
* hostname1
* hostname2
*
* This would be handled by explode("\n", $hostnames) <Use of double quotes important>
*/
$hostnames = file_get_contents('/path/to/csv/of/justhostnames.csv');
/**
* Just filters and makes the list unique (less work to try to process later on).
*/
$listOfNames = array_filter(array_unique(explode(',', $hostnames)));
/**
* Just get's the hostID's that are already present. The hosts must already exist.
* This will only return hostID's for valid hosts.
*/
$hostIDs = FOGCore::getSubObjectIDs(
'Host',
array('name' => $listOfNames)
);
/**
* The group association fields to be inserted.
* GroupID is the group you're associating with gotten earlier.
* HostID is the list of host IDs from earlier.
*/
$fieldstogroupinsert = array(
'groupID',
'hostID'
);
/**
* Starts our array of items to be inserted.
*/
$items = array();
/**
* Loops each of the host ID's to build our query appropriately.
*/
foreach ($hostIDs as &$hostID) {
$items[] = array(
$groupID,
$hostID
);
}
/**
* Inserts all of the new associations.
*/
FOGCore::getClass('GroupAssociationManager')
->insertBatch(
$fieldstogroupinsert,
$items
);
/**
* Just let's you know it's finished.
* NOTE: You could add error checking here if you want.
*/
echo _('All done');
?>
Hopefully all of that makes some sort of sense, if it were me I’d say do the PHP option and stick the file in /var/www/html/.
Then you just call the script with: http://<fogip>/groupinsert.php (if groupinsert.php was the filename you called it.)
Update the csv file when you need it and rerun the url call. You can script the call to the url with a curl request on a cron schedule if you needed to.
Mind you I give no guarantees and I have not tested what I wrote (I just wrote it right now to give an example of how you could approach this). I suggest putting the file in /var/www/html so if you do update you don’t lose your script during upgrades, and the functionality should remain regardless of if you update or not.
Take account you may need to add your own error handling to this script. I’m just giving a base suggestion, how you approach it is totally up to you.
The csv file would just be a csv of hostnames, no headers or other information. e.g.:
hostname1,hostname2,hostname3
The script I wrote does not contain any space handling/trimming, nor does it take into account special characters. So you need to make sure your CSV is safe. Notice I’m not allowing any options, so you don’t have to worry about another person “exploiting” but security is up to you.
If you want to semi automate it, the groupname line (line number three of the script) you could change it to:
$groupname = filter_input(INPUT_GET, 'groupname');
This would allow you to call the script appended with: ?groupname=SomeGroupNameHere. (Not a huge threat).
You can also change the “file-get-contents” to be automated in a similar fashion though i’d highly recommend against it. But if you “must” you would specify the file name similarly. (Could be a huge threat).