Skip to content

Commit cbd156c

Browse files
authored
Merge pull request #5773 from psafont/getallwhere
2 parents a0464a7 + ba47a87 commit cbd156c

File tree

7 files changed

+122
-108
lines changed

7 files changed

+122
-108
lines changed

ocaml/idl/datamodel_types.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ and obj_op =
586586
| GetByLabel
587587
| GetRecord
588588
| GetAll
589+
| GetAllWhere
589590
| GetAllRecordsWhere
590591
| GetAllRecords
591592
| Private of private_op

ocaml/idl/datamodel_types.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ and obj_op =
210210
| GetByLabel
211211
| GetRecord
212212
| GetAll
213+
| GetAllWhere
213214
| GetAllRecordsWhere
214215
| GetAllRecords
215216
| Private of private_op

ocaml/idl/datamodel_utils.ml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,36 @@ let messages_of_obj (x : obj) document_order : message list =
683683
}
684684
in
685685

686+
let get_all_where =
687+
{
688+
get_all_public with
689+
msg_name= "get_all_where"
690+
; msg_tag= FromObject GetAllWhere
691+
; msg_params=
692+
[
693+
{
694+
param_type= String
695+
; param_name= "expr"
696+
; param_doc= "expression matching records"
697+
; param_release= x.obj_release
698+
; param_default= None
699+
}
700+
]
701+
; msg_result= Some (Set (Ref x.name), "references to all matching objects")
702+
; msg_release=
703+
{
704+
opensource= []
705+
; internal=
706+
x.obj_release.internal
707+
(* This should be the release of getallwhere, or the class'
708+
introduction, whichever is last. *)
709+
; internal_deprecated_since= None
710+
}
711+
; msg_allowed_roles= x.obj_implicit_msg_allowed_roles
712+
; msg_hide_from_docs= true
713+
}
714+
in
715+
686716
(* And the 'get_all_records_where' semi-public function *)
687717
let get_all_records_where =
688718
{
@@ -738,7 +768,7 @@ let messages_of_obj (x : obj) document_order : message list =
738768
in
739769
let get_all_public =
740770
if List.mem x.name expose_get_all_messages_for then
741-
[get_all_public; get_all_records_where; get_all_records]
771+
[get_all_public; get_all_where; get_all_records_where; get_all_records]
742772
else
743773
[]
744774
in

ocaml/idl/ocaml_backend/gen_db_actions.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,12 @@ let db_action api : O.Module.t =
536536
"let expr' = Xapi_database.Db_filter.expr_of_string expr in"
537537
; "get_records_where ~" ^ Gen_common.context ^ " ~expr:expr'"
538538
]
539+
| FromObject GetAllWhere ->
540+
String.concat "\n"
541+
[
542+
"let expr' = Xapi_database.Db_filter.expr_of_string expr in"
543+
; "get_refs_where ~" ^ Gen_common.context ^ " ~expr:expr'"
544+
]
539545
| _ ->
540546
assert false
541547
in

ocaml/idl/ocaml_backend/gen_empty_custom.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,21 @@ let operation_requires_side_effect ({msg_tag= tag; _} as msg) =
6262
match tag with
6363
| FromField (Setter, fld) ->
6464
fld.DT.field_has_effect
65+
| FromField ((Getter | Add | Remove), _) ->
66+
false
6567
| FromObject
6668
( GetRecord
6769
| GetByUuid
6870
| GetByLabel
6971
| GetAll
72+
| GetAllWhere
7073
| GetAllRecordsWhere
7174
| GetAllRecords ) ->
7275
false
73-
| FromObject _ ->
76+
| FromObject (Make | Delete | Private _) ->
7477
true
7578
| Custom ->
7679
msg.DT.msg_has_effect && msg.DT.msg_forward_to = None
77-
| _ ->
78-
false
7980

8081
let make_custom_api api =
8182
Dm_api.filter

ocaml/xapi-cli-server/cli_operations.ml

Lines changed: 67 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -4580,68 +4580,58 @@ let vm_migrate printer rpc session_id params =
45804580
Client.Session.login_with_password ~rpc:remote_rpc ~uname ~pwd
45814581
~version:"1.3" ~originator:Constants.xapi_user_agent
45824582
in
4583+
let remote f = f ~rpc:remote_rpc ~session_id:remote_session in
45834584
finally
45844585
(fun () ->
4585-
let host, host_record =
4586-
let all =
4587-
Client.Host.get_all_records ~rpc:remote_rpc
4588-
~session_id:remote_session
4586+
let host =
4587+
let expr_match x =
4588+
Printf.sprintf
4589+
{|(field "hostname"="%s") or (field "name__label"="%s") or (field "uuid"="%s")|}
4590+
x x x
45894591
in
4590-
if List.mem_assoc "host" params then
4591-
let x = List.assoc "host" params in
4592-
try
4593-
List.find
4594-
(fun (_, h) ->
4595-
h.API.host_hostname = x
4596-
|| h.API.host_name_label = x
4597-
|| h.API.host_uuid = x
4598-
)
4599-
all
4600-
with Not_found ->
4601-
failwith (Printf.sprintf "Failed to find host: %s" x)
4602-
else
4603-
List.hd all
4592+
let expr, fail_msg =
4593+
match List.assoc_opt "host" params with
4594+
| Some x ->
4595+
(expr_match x, Printf.sprintf "Failed to find host: %s" x)
4596+
| None ->
4597+
("true", Printf.sprintf "Failed to find a suitable host")
4598+
in
4599+
match remote Client.Host.get_all_where ~expr with
4600+
| host :: _ ->
4601+
host
4602+
| [] ->
4603+
failwith fail_msg
46044604
in
4605-
let network, network_record =
4606-
let all =
4607-
Client.Network.get_all_records ~rpc:remote_rpc
4608-
~session_id:remote_session
4605+
let network =
4606+
let expr x =
4607+
Printf.sprintf
4608+
{|(field "bridge"="%s") or (field "name__label"="%s") or (field "uuid"="%s")|}
4609+
x x x
46094610
in
4610-
if List.mem_assoc "remote-network" params then
4611-
let x = List.assoc "remote-network" params in
4612-
try
4613-
List.find
4614-
(fun (_, net) ->
4615-
net.API.network_bridge = x
4616-
|| net.API.network_name_label = x
4617-
|| net.API.network_uuid = x
4618-
)
4619-
all
4620-
with Not_found ->
4621-
failwith (Printf.sprintf "Failed to find network: %s" x)
4622-
else
4623-
let pifs = host_record.API.host_PIFs in
4624-
let management_pifs =
4625-
List.filter
4626-
(fun self ->
4627-
Client.PIF.get_management ~rpc:remote_rpc
4628-
~session_id:remote_session ~self
4629-
)
4630-
pifs
4631-
in
4632-
if management_pifs = [] then
4633-
failwith
4634-
(Printf.sprintf "Could not find management PIF on host %s"
4635-
host_record.API.host_uuid
4636-
) ;
4637-
let pif = List.hd management_pifs in
4638-
let net =
4639-
Client.PIF.get_network ~rpc:remote_rpc ~session_id:remote_session
4640-
~self:pif
4641-
in
4642-
( net
4643-
, Client.Network.get_record ~rpc:remote_rpc
4644-
~session_id:remote_session ~self:net
4611+
match List.assoc_opt "remote-network" params with
4612+
| Some x -> (
4613+
match remote Client.Network.get_all_where ~expr:(expr x) with
4614+
| network :: _ ->
4615+
network
4616+
| [] ->
4617+
failwith (Printf.sprintf "Failed to find network: %s" x)
4618+
)
4619+
| None -> (
4620+
let expr =
4621+
Printf.sprintf
4622+
{|(field "host"="%s") and (field "management"="true")|}
4623+
Ref.(string_of host)
4624+
in
4625+
let management_pifs = remote Client.PIF.get_all_where ~expr in
4626+
match management_pifs with
4627+
| [] ->
4628+
let host_uuid = remote Client.Host.get_uuid ~self:host in
4629+
failwith
4630+
(Printf.sprintf "Could not find management PIF on host %s"
4631+
host_uuid
4632+
)
4633+
| pif :: _ ->
4634+
remote Client.PIF.get_network ~self:pif
46454635
)
46464636
in
46474637
let vif_map =
@@ -4650,10 +4640,7 @@ let vm_migrate printer rpc session_id params =
46504640
let vif =
46514641
Client.VIF.get_by_uuid ~rpc ~session_id ~uuid:vif_uuid
46524642
in
4653-
let net =
4654-
Client.Network.get_by_uuid ~rpc:remote_rpc
4655-
~session_id:remote_session ~uuid:net_uuid
4656-
in
4643+
let net = remote Client.Network.get_by_uuid ~uuid:net_uuid in
46574644
(vif, net)
46584645
)
46594646
(read_map_params "vif" params)
@@ -4664,10 +4651,7 @@ let vm_migrate printer rpc session_id params =
46644651
let vdi =
46654652
Client.VDI.get_by_uuid ~rpc ~session_id ~uuid:vdi_uuid
46664653
in
4667-
let sr =
4668-
Client.SR.get_by_uuid ~rpc:remote_rpc ~session_id:remote_session
4669-
~uuid:sr_uuid
4670-
in
4654+
let sr = remote Client.SR.get_by_uuid ~uuid:sr_uuid in
46714655
(vdi, sr)
46724656
)
46734657
(read_map_params "vdi" params)
@@ -4679,8 +4663,7 @@ let vm_migrate printer rpc session_id params =
46794663
Client.VGPU.get_by_uuid ~rpc ~session_id ~uuid:vgpu_uuid
46804664
in
46814665
let gpu_group =
4682-
Client.GPU_group.get_by_uuid ~rpc:remote_rpc
4683-
~session_id:remote_session ~uuid:gpu_group_uuid
4666+
remote Client.GPU_group.get_by_uuid ~uuid:gpu_group_uuid
46844667
in
46854668
(vgpu, gpu_group)
46864669
)
@@ -4696,19 +4679,12 @@ let vm_migrate printer rpc session_id params =
46964679
{|(field "host"="%s") and (field "currently_attached"="true")|}
46974680
(Ref.string_of host)
46984681
in
4699-
let host_pbds =
4700-
Client.PBD.get_all_records_where ~rpc:remote_rpc
4701-
~session_id:remote_session ~expr
4702-
in
47034682
let srs =
4704-
List.map
4705-
(fun (_, pbd_rec) ->
4706-
( pbd_rec.API.pBD_SR
4707-
, Client.SR.get_record ~rpc:remote_rpc
4708-
~session_id:remote_session ~self:pbd_rec.API.pBD_SR
4709-
)
4710-
)
4711-
host_pbds
4683+
remote Client.PBD.get_all_where ~expr
4684+
|> List.map (fun pbd ->
4685+
let sr = remote Client.PBD.get_SR ~self:pbd in
4686+
(sr, remote Client.SR.get_record ~self:sr)
4687+
)
47124688
in
47134689
(* In the following loop, the current SR:sr' will be compared with previous checked ones,
47144690
first if it is an ISO type, then pass this one for selection, then the only shared one from this and
@@ -4807,13 +4783,20 @@ let vm_migrate printer rpc session_id params =
48074783
)
48084784
params
48094785
in
4786+
let host_name_label =
4787+
Client.Host.get_name_label ~rpc:remote_rpc ~session_id:remote_session
4788+
~self:host
4789+
in
4790+
let network_name_label =
4791+
Client.Network.get_name_label ~rpc:remote_rpc
4792+
~session_id:remote_session ~self:network
4793+
in
48104794
printer
48114795
(Cli_printer.PMsg
48124796
(Printf.sprintf
48134797
"Will migrate to remote host: %s, using remote network: %s. \
48144798
Here is the VDI mapping:"
4815-
host_record.API.host_name_label
4816-
network_record.API.network_name_label
4799+
host_name_label network_name_label
48174800
)
48184801
) ;
48194802
List.iter
@@ -4822,16 +4805,13 @@ let vm_migrate printer rpc session_id params =
48224805
(Cli_printer.PMsg
48234806
(Printf.sprintf "VDI %s -> SR %s"
48244807
(Client.VDI.get_uuid ~rpc ~session_id ~self:vdi)
4825-
(Client.SR.get_uuid ~rpc:remote_rpc
4826-
~session_id:remote_session ~self:sr
4827-
)
4808+
(remote Client.SR.get_uuid ~self:sr)
48284809
)
48294810
)
48304811
)
48314812
vdi_map ;
48324813
let token =
4833-
Client.Host.migrate_receive ~rpc:remote_rpc ~session_id:remote_session
4834-
~host ~network ~options
4814+
remote Client.Host.migrate_receive ~host ~network ~options
48354815
in
48364816
let new_vm =
48374817
do_vm_op ~include_control_vms:false ~include_template_vms:true printer
@@ -4847,13 +4827,7 @@ let vm_migrate printer rpc session_id params =
48474827
|> List.hd
48484828
in
48494829
if get_bool_param params "copy" then
4850-
printer
4851-
(Cli_printer.PList
4852-
[
4853-
Client.VM.get_uuid ~rpc:remote_rpc ~session_id:remote_session
4854-
~self:new_vm
4855-
]
4856-
)
4830+
printer (Cli_printer.PList [remote Client.VM.get_uuid ~self:new_vm])
48574831
)
48584832
(fun () ->
48594833
Client.Session.logout ~rpc:remote_rpc ~session_id:remote_session

ocaml/xapi/xapi_role.ml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,26 @@ let get_record ~__context ~self =
9292
~static_fn:(fun static_record -> get_api_record ~static_record)
9393
~db_fn:(fun ~__context ~self -> Db.Role.get_record ~__context ~self)
9494

95-
(* val get_all_records_where : __context:Context.t -> expr:string -> ref_role_to_role_t_map*)
9695
let expr_no_permissions = "subroles<>[]"
9796

9897
let expr_only_permissions = "subroles=[]"
9998

100-
let get_all_records_where ~__context ~expr =
99+
let get_common_where ~__context ~expr ~f =
101100
if expr = expr_no_permissions then (* composite role, ie. not a permission *)
102-
List.map
103-
(fun r -> (ref_of_role ~role:r, get_api_record ~static_record:r))
104-
Rbac_static.all_static_roles
101+
List.map f Rbac_static.all_static_roles
105102
else if expr = expr_only_permissions then
106103
(* composite role, ie. a permission *)
107-
List.map
108-
(fun r -> (ref_of_role ~role:r, get_api_record ~static_record:r))
109-
Rbac_static.all_static_permissions
104+
List.map f Rbac_static.all_static_permissions
110105
else (* anything in this table, ie. roles+permissions *)
111-
List.map
112-
(fun r -> (ref_of_role ~role:r, get_api_record ~static_record:r))
113-
get_all_static_roles
106+
List.map f get_all_static_roles
107+
108+
let get_all_where ~__context ~expr =
109+
let f r = ref_of_role ~role:r in
110+
get_common_where ~__context ~expr ~f
111+
112+
let get_all_records_where ~__context ~expr =
113+
let f r = (ref_of_role ~role:r, get_api_record ~static_record:r) in
114+
get_common_where ~__context ~expr ~f
114115

115116
(*@ (* concatenate with Db table *)
116117
(* TODO: this line is crashing for some unknown reason, but not needed in RBAC 1 *)

0 commit comments

Comments
 (0)