diff --git a/ocaml/xapi-cli-server/cli_operations.ml b/ocaml/xapi-cli-server/cli_operations.ml index 2f0f172eb5..d69dd44f94 100644 --- a/ocaml/xapi-cli-server/cli_operations.ml +++ b/ocaml/xapi-cli-server/cli_operations.ml @@ -4314,27 +4314,40 @@ let vm_migrate printer rpc session_id params = finally (fun () -> let host = - let host_matches x self = - let hostname () = remote (Client.Host.get_hostname ~self) in - let uuid () = remote (Client.Host.get_uuid ~self) in - let name_label () = remote (Client.Host.get_name_label ~self) in - hostname () = x || uuid () = x || name_label () = x + let get_host_by_hostname x = + let host_matches x self = + let hostname = remote (Client.Host.get_hostname ~self) in + hostname = x + in + let all_hosts = remote Client.Host.get_all in + List.find_opt (host_matches x) all_hosts + in + let get_host_by_uuid_or_name x = + let ref = + if Uuidm.of_string x |> Option.is_some then + remote (Client.Host.get_by_uuid ~uuid:x) + else + let hosts = remote (Client.Host.get_by_name_label ~label:x) in + Option.fold ~none:Ref.null ~some:Fun.id (List.nth_opt hosts 0) + in + if ref <> Ref.null then + Some ref + else + get_host_by_hostname x in - let matches, fail_msg = + + let search, fail_msg = match List.assoc_opt "host" params with | Some x -> - (host_matches x, Printf.sprintf "Failed to find host: %s" x) + ( get_host_by_uuid_or_name x + , Printf.sprintf "Failed to find host: %s" x + ) | None -> - ( (fun _ -> true) + ( List.nth_opt (remote Client.Host.get_all) 0 , Printf.sprintf "Failed to find a suitable host" ) in - let all_hosts = remote Client.Host.get_all in - match List.find_opt matches all_hosts with - | Some host -> - host - | None -> - failwith fail_msg + match search with Some ref -> ref | None -> failwith fail_msg in let network = let network_matches x self =