@ -180,7 +180,15 @@ def main():
datacenter = dict ( type = ' str ' , required = True ) ,
datacenter = dict ( type = ' str ' , required = True ) ,
)
)
module = AnsibleModule (
module = AnsibleModule (
argument_spec = argument_spec , required_one_of = [ [ ' name ' , ' uuid ' ] ] )
argument_spec = argument_spec ,
required_one_of = [
[ ' name ' , ' uuid ' ]
] ,
mutually_exclusive = [
[ ' name ' , ' uuid ' ]
] ,
supports_check_mode = True
)
# FindByInventoryPath() does not require an absolute path
# FindByInventoryPath() does not require an absolute path
# so we should leave the input folder path unmodified
# so we should leave the input folder path unmodified
@ -195,12 +203,18 @@ def main():
if vm :
if vm :
try :
try :
vm_path = pyv . get_vm_path ( pyv . content , vm ) . lstrip ( ' / ' )
vm_path = pyv . get_vm_path ( pyv . content , vm ) . lstrip ( ' / ' )
vm_full = vm_path + ' / ' + module . params [ ' name ' ]
if module . params [ ' name ' ] :
folder = search_index . FindByInventoryPath (
vm_name = module . params [ ' name ' ]
module . params [ ' dest_folder ' ] )
else :
vm_name = vm . name
vm_full = vm_path + ' / ' + vm_name
folder = search_index . FindByInventoryPath ( module . params [ ' dest_folder ' ] )
if folder is None :
if folder is None :
module . fail_json ( msg = " Folder name and/or path does not exist " )
module . fail_json ( msg = " Folder name and/or path does not exist " )
vm_to_move = search_index . FindByInventoryPath ( vm_full )
vm_to_move = search_index . FindByInventoryPath ( vm_full )
if module . check_mode :
module . exit_json ( changed = True , instance = pyv . gather_facts ( vm ) )
if vm_path != module . params [ ' dest_folder ' ] . lstrip ( ' / ' ) :
if vm_path != module . params [ ' dest_folder ' ] . lstrip ( ' / ' ) :
move_task = folder . MoveInto ( [ vm_to_move ] )
move_task = folder . MoveInto ( [ vm_to_move ] )
changed , err = wait_for_task ( move_task )
changed , err = wait_for_task ( move_task )
@ -213,6 +227,8 @@ def main():
module . fail_json ( msg = " Failed to move VM with exception %s " %
module . fail_json ( msg = " Failed to move VM with exception %s " %
to_native ( exc ) )
to_native ( exc ) )
else :
else :
if module . check_mode :
module . exit_json ( changed = False )
module . fail_json ( msg = " Unable to find VM %s to move to %s " % (
module . fail_json ( msg = " Unable to find VM %s to move to %s " % (
( module . params . get ( ' uuid ' ) or module . params . get ( ' name ' ) ) ,
( module . params . get ( ' uuid ' ) or module . params . get ( ' name ' ) ) ,
module . params . get ( ' dest_folder ' ) ) )
module . params . get ( ' dest_folder ' ) ) )