     migrationNetwork.ref
     {} # Other parameters
   )
-
-  vdiMap = {}
-  for vbdId in VM.$VBDs
-    VBD = @getObject vbdId, 'VBD'
-    continue if VBD.is_cd_drive
-    VDI = @getObject VBD.VDI, 'VDI'
-    vdiMap[VDI.ref] = SR.ref
-
-  vifMap = {}
-  for vifId in VM.VIFs
-    VIF = @getObject vifId, 'VIF'
-    vifMap[VIF.ref] = network.ref
 
   $wait (@getXAPI VM).call(
     'VM.migrate_send'

{
  isArray: $isArray
} = require 'underscore'


}) ->
  try
    # TODO: map multiple VDI and VIF
    VM = @getObject id, 'VM'
    host = @getObject target_host_id, 'host'

    # Optional parameters
    # if no target_network_id given, try to use the management network
    network = if target_network_id
      @getObject target_network_id, 'network'
    else
      PIF = $findWhere (@getObjects host.$PIFs), management: true
      @getObject PIF.$network, 'network'

    # if no migration_network_id given, use the target_network_id
    migrationNetwork = if migration_network_id
      @getObject migration_network_id, 'network'
    else
      network

    # if no target_sr_id given, try to find the default Pool SR
    SR = if target_sr_id
      @getObject target_sr_id, 'SR'
    else
      pool = @getObject host.poolRef, 'pool'
      target_sr_id = pool.default_SR
      @getObject target_sr_id, 'SR'

  catch
    @throw 'NO_SUCH_OBJECT'

  unless $isVMRunning VM
    @throw 'INVALID_PARAMS', 'The VM can only be migrated when running'

  vdiMap = {}
  for vbdId in VM.$VBDs
    VBD = @getObject vbdId, 'VBD'
    continue if VBD.is_cd_drive
    VDI = @getObject VBD.VDI, 'VDI'
    vdiMap[VDI.ref] = SR.ref

  vifMap = {}
  for vifId in VM.VIFs
    VIF = @getObject vifId, 'VIF'
    vifMap[VIF.ref] = network.ref

  token = $wait (@getXAPI host).call(
    'host.migrate_receive'
