#!/usr/bin/env python
"""
Python program for shutdown the VMs on an ESX / vCenter.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from __future__ import print_function
import atexit
from pyVim.connect import SmartConnectNoSSL, Disconnect
from pyVmomi import vim
from tools import cli

MAX_DEPTH = 10


def setup_args():

    """
    Get standard connection arguments
    """
    parser = cli.build_arg_parser()
    parser.add_argument(
        '-f', '--folder',
        default='',
        help='Folder name of the VirtualMachine you want to shutdown.')
    my_args = parser.parse_args()

    return cli.prompt_for_password(my_args)


def get_obj(content, vimtype, name):
    """
     Get the vsphere object associated with a given text name
    """
    obj = None
    container = content.viewManager.CreateContainerView(
        content.rootFolder, vimtype, True)
    for c in container.view:
        if c.name == name:
            obj = c
            break
    return obj


def shutdown(vm, depth=1):
    """
    Print information for a particular virtual machine or recurse into a folder
    with depth protection
    """

    # if this is a group it will have children. if it does, recurse into them
    # and then return
    if hasattr(vm, 'childEntity'):
        if depth > MAX_DEPTH:
            return
        vmlist = vm.childEntity
        for child in vmlist:
            shutdown(child, depth+1)
        return

    summary = vm.summary
    if vm.runtime.powerState == "poweredOn":
        vm.ShutdownGuest()
        print(
            vm.parent.name + "\\" + vm.name + " is shutdown. (" +
            summary.config.uuid + ")")


def main():
    """
    Simple command-line program for listing the virtual machines on a host.
    """

    args = setup_args()
    si = None
    try:
        si = SmartConnectNoSSL(host=args.host,
                               user=args.user,
                               pwd=args.password,
                               port=int(args.port))
        atexit.register(Disconnect, si)
    except vim.fault.InvalidLogin:
        raise SystemExit("Unable to connect to host "
                         "with supplied credentials.")

    content = si.RetrieveContent()

    if args.folder == "":
        for child in content.rootFolder.childEntity:
            if hasattr(child, 'vmFolder'):
                datacenter = child
                vmfolder = datacenter.vmFolder
                vmlist = vmfolder.childEntity
                for vm in vmlist:
                    shutdown(vm)
    else:
        vmfolder = get_obj(content, [vim.Folder], args.folder)
        vmlist = vmfolder.childEntity
        for vm in vmlist:
            shutdown(vm)


# Start program
if __name__ == "__main__":
    main()
