#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long qw(:config no_ignore_case);
use Perun::Agent;
use Perun::Common qw(printMessage);

sub help {
	return qq{
	Adds facility hosts. Facility and hosts are required fields.
	--------------------------------------
	Available options:
	--facilityId   | -f facility id
	--facilityName | -F facility name
	--hosts        | -H list Of Hosts
	--batch        | -b batch
	--help         | -h prints this help

	};
}

my ($facilityId, $facilityName, @hosts, $batch);
GetOptions ("help|h" => sub { print help(); exit 0;} ,
	"batch|b" => \$batch,
"facilityId|f=i" => \$facilityId,
"facilityName|F=s" => \$facilityName,
'hosts|H=s@{1,}' => \@hosts) or die help();

# Check options
unless (defined($facilityId) || (defined($facilityName))) { die "ERROR: facilityId or facilityName are required \n";}
unless (@hosts) { die "ERROR: hosts are required \n";}

my $agent = Perun::Agent->new();
my $facilitiesAgent = $agent->getFacilitiesAgent;

unless($facilityId) {
	my $facility = $facilitiesAgent->getFacilityByName(name => $facilityName);
	$facilityId=$facility->getId;
}



$facilitiesAgent->addHosts(facility => $facilityId,hostnames => \@hosts );

printMessage("Hosts '@hosts' successfully added on the facility Id:$facilityId", $batch);
