#!/usr/bin/perl -w

#############################################################################################
# FindDatacenterByHostname.pl
#
# Copyright (c) 2009, "Reuben M Stump"
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * The name of "Reuben M Stump" may not be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY "Reuben M Stump" ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL "Reuben M Stump" BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#############################################################################################

#############################################################################################
# FindDatacenterByHostname.pl
#
# Script will print out the name of the datacenter to which a specific ESX host is assigned.
#
#   Parameters:
#     --server=<name|ip>
#     --username=<login>
#     --password=<password>
#     --hostname=<FQDN of ESX Host>
#############################################################################################

use strict;
use warnings;

use VMware::VIRuntime;

my %opts = (
	hostname => {
	type => "=s",
	variable => "HOSTNAME",
	help => "FQDN of ESX host server",
	required => 1,
	}
);

Opts::add_options(%opts);
Opts::parse();
Opts::validate();

Util::connect();

my ($host_views, $datacenter, $datacenter_name, $hostname, $entity_view);

$hostname = Opts::get_option('hostname');

# Retrieve a reference to the requested HostSystem using the FQDN provided by the
# hostname parameter to the script. 
my $host = Vim::find_entity_view(
	view_type => 'HostSystem',
	filter => { name => $hostname },
	properties => ['name', 'parent'],
);

# No host for requested $hostname value was found, cleanup and exit.
unless (defined $host)
{
	Util::disconnect();
	die "No host matching '$hostname' found.\n";	
}

FindDatacenter($host->parent, \$datacenter_name);

if (defined $datacenter_name)
{
	print "Datacenter for '$hostname' = $datacenter_name\n";
}

Util::disconnect();


#############################################################################################
## SUBS
#############################################################################################

## Recursive function to walk up the inventory tree using the parent property of each
## ManagedObject.  
sub FindDatacenter
{
	my ($entity, $n_ref) = @_;
	
	# This condition should not happen, even on a standalone ESX host.
	unless ( defined $entity )
	{
		print "Root folder reached!!  Datacenter for $hostname was not found!!\n";
		return;
	}
	
	# If the object type is a Datacenter, set the passed value of the pass by reference
	# of $datacenter_name
	if($entity->type eq "Datacenter")
	{
		my $datacenter_view = Vim::get_view(mo_ref => $entity, properties =>  ['name']);
		
		$$n_ref = $datacenter_view->name;
		return;
	}
	
	$entity_view = Vim::get_view(mo_ref => $entity, properties => ['parent']);
	FindDatacenter($entity_view->parent, $n_ref);
}