@@ -48,6 +48,14 @@ create_path(ReqData, Context) ->
4848 {" dummy" , ReqData , Context }.
4949
5050to_json (ReqData , Context ) ->
51+ case rabbit_mgmt_util :vhost (ReqData ) of
52+ none ->
53+ all_definitions (ReqData , Context );
54+ _VHost ->
55+ vhost_definitions (ReqData , Context )
56+ end .
57+
58+ all_definitions (ReqData , Context ) ->
5159 Xs = [X || X <- rabbit_mgmt_wm_exchanges :basic (ReqData ),
5260 export_exchange (X )],
5361 Qs = [Q || Q <- rabbit_mgmt_wm_queues :basic (ReqData ),
@@ -76,6 +84,31 @@ to_json(ReqData, Context) ->
7684 end ,
7785 Context ).
7886
87+ vhost_definitions (ReqData , Context ) ->
88+ Xs = [strip_vhost (X ) || X <- rabbit_mgmt_wm_exchanges :basic (ReqData ),
89+ export_exchange (X )],
90+ Qs = [strip_vhost (Q ) || Q <- rabbit_mgmt_wm_queues :basic (ReqData ),
91+ export_queue (Q )],
92+ QNames = [{pget (name , Q ), pget (vhost , Q )} || Q <- Qs ],
93+ Bs = [strip_vhost (B ) || B <- rabbit_mgmt_wm_bindings :basic (ReqData ),
94+ export_binding (B , QNames )],
95+ {ok , Vsn } = application :get_key (rabbit , vsn ),
96+ rabbit_mgmt_util :reply (
97+ [{rabbit_version , list_to_binary (Vsn )}] ++
98+ filter (
99+ [{policies , rabbit_mgmt_wm_policies :basic (ReqData )},
100+ {queues , Qs },
101+ {exchanges , Xs },
102+ {bindings , Bs }]),
103+ case wrq :get_qs_value (" download" , ReqData ) of
104+ undefined -> ReqData ;
105+ Filename -> rabbit_mgmt_util :set_resp_header (
106+ " Content-Disposition" ,
107+ " attachment; filename=" ++
108+ mochiweb_util :unquote (Filename ), ReqData )
109+ end ,
110+ Context ).
111+
79112accept_json (ReqData , Context ) ->
80113 accept (wrq :req_body (ReqData ), ReqData , Context ).
81114
@@ -116,8 +149,15 @@ is_authorized_qs(ReqData, Context, Auth) ->
116149% %--------------------------------------------------------------------
117150
118151accept (Body , ReqData , Context ) ->
119- apply_defs (Body , fun () -> {true , ReqData , Context } end ,
120- fun (E ) -> rabbit_mgmt_util :bad_request (E , ReqData , Context ) end ).
152+ case rabbit_mgmt_util :vhost (ReqData ) of
153+ none ->
154+ apply_defs (Body , fun () -> {true , ReqData , Context } end ,
155+ fun (E ) -> rabbit_mgmt_util :bad_request (E , ReqData , Context ) end );
156+ VHost ->
157+ apply_defs (Body , fun () -> {true , ReqData , Context } end ,
158+ fun (E ) -> rabbit_mgmt_util :bad_request (E , ReqData , Context ) end ,
159+ VHost )
160+ end .
121161
122162apply_defs (Body , SuccessFun , ErrorFun ) ->
123163 case rabbit_mgmt_util :decode ([], Body ) of
@@ -139,6 +179,22 @@ apply_defs(Body, SuccessFun, ErrorFun) ->
139179 end
140180 end .
141181
182+ apply_defs (Body , SuccessFun , ErrorFun , VHost ) ->
183+ case rabbit_mgmt_util :decode ([], Body ) of
184+ {error , E } ->
185+ ErrorFun (E );
186+ {ok , _ , All } ->
187+ try
188+ for_all (policies , All , VHost , fun add_policy /2 ),
189+ for_all (queues , All , VHost , fun add_queue /2 ),
190+ for_all (exchanges , All , VHost , fun add_exchange /2 ),
191+ for_all (bindings , All , VHost , fun add_binding /2 ),
192+ SuccessFun ()
193+ catch {error , E } -> ErrorFun (format (E ));
194+ exit :E -> ErrorFun (format (E ))
195+ end
196+ end .
197+
142198format (# amqp_error {name = Name , explanation = Explanation }) ->
143199 list_to_binary (rabbit_misc :format (" ~s : ~s " , [Name , Explanation ]));
144200format (E ) ->
@@ -195,6 +251,9 @@ filter_items(Name, List, Allowed) ->
195251filter_item (Item , Allowed ) ->
196252 [{K , Fact } || {K , Fact } <- Item , lists :member (K , Allowed )].
197253
254+ strip_vhost (Item ) ->
255+ lists :keydelete (vhost , 1 , Item ).
256+
198257% %--------------------------------------------------------------------
199258
200259for_all (Name , All , Fun ) ->
@@ -204,6 +263,13 @@ for_all(Name, All, Fun) ->
204263 {struct , I } <- List ]
205264 end .
206265
266+ for_all (Name , All , VHost , Fun ) ->
267+ case pget (Name , All ) of
268+ undefined -> ok ;
269+ List -> [Fun (VHost , [{atomise_name (K ), V } || {K , V } <- I ]) ||
270+ {struct , I } <- List ]
271+ end .
272+
207273atomise_name (N ) -> list_to_atom (binary_to_list (N )).
208274
209275% %--------------------------------------------------------------------
@@ -222,6 +288,9 @@ add_parameter(Param) ->
222288
223289add_policy (Param ) ->
224290 VHost = pget (vhost , Param ),
291+ add_policy (VHost , Param ).
292+
293+ add_policy (VHost , Param ) ->
225294 Key = pget (name , Param ),
226295 case rabbit_policy :set (
227296 VHost , Key , pget (pattern , Param ),
@@ -249,33 +318,62 @@ add_permission(Permission) ->
249318 pget (read , Permission )).
250319
251320add_queue (Queue ) ->
252- rabbit_amqqueue :declare (r (queue , Queue ),
321+ add_queue_int (Queue , r (queue , Queue )).
322+
323+ add_queue (VHost , Queue ) ->
324+ add_queue_int (Queue , rv (VHost , queue , Queue )).
325+
326+ add_queue_int (Queue , Name ) ->
327+ rabbit_amqqueue :declare (Name ,
253328 pget (durable , Queue ),
254329 pget (auto_delete , Queue ),
255330 rabbit_mgmt_util :args (pget (arguments , Queue )),
256331 none ).
257332
258333add_exchange (Exchange ) ->
334+ add_exchange_int (Exchange , r (exchange , Exchange )).
335+
336+ add_exchange (VHost , Exchange ) ->
337+ add_exchange_int (Exchange , rv (VHost , exchange , Exchange )).
338+
339+ add_exchange_int (Exchange , Name ) ->
259340 Internal = case pget (internal , Exchange ) of
260341 undefined -> false ; % % =< 2.2.0
261342 I -> I
262343 end ,
263- rabbit_exchange :declare (r ( exchange , Exchange ) ,
344+ rabbit_exchange :declare (Name ,
264345 rabbit_exchange :check_type (pget (type , Exchange )),
265346 pget (durable , Exchange ),
266347 pget (auto_delete , Exchange ),
267348 Internal ,
268349 rabbit_mgmt_util :args (pget (arguments , Exchange ))).
269350
270351add_binding (Binding ) ->
271- DestType = list_to_atom (binary_to_list (pget (destination_type , Binding ))),
352+ DestType = dest_type (Binding ),
353+ add_binding_int (Binding , r (exchange , source , Binding ),
354+ r (DestType , destination , Binding )).
355+
356+ add_binding (VHost , Binding ) ->
357+ DestType = dest_type (Binding ),
358+ add_binding_int (Binding , rv (VHost , exchange , source , Binding ),
359+ rv (VHost , DestType , destination , Binding )).
360+
361+ add_binding_int (Binding , Source , Destination ) ->
272362 rabbit_binding :add (
273- # binding {source = r ( exchange , source , Binding ) ,
274- destination = r ( DestType , destination , Binding ) ,
275- key = pget (routing_key , Binding ),
363+ # binding {source = Source ,
364+ destination = Destination ,
365+ key = pget (routing_key , Binding ),
276366 args = rabbit_mgmt_util :args (pget (arguments , Binding ))}).
277367
368+ dest_type (Binding ) ->
369+ list_to_atom (binary_to_list (pget (destination_type , Binding ))).
370+
278371r (Type , Props ) -> r (Type , name , Props ).
279372
280373r (Type , Name , Props ) ->
281374 rabbit_misc :r (pget (vhost , Props ), Type , pget (Name , Props )).
375+
376+ rv (VHost , Type , Props ) -> rv (VHost , Type , name , Props ).
377+
378+ rv (VHost , Type , Name , Props ) ->
379+ rabbit_misc :r (VHost , Type , pget (Name , Props )).
0 commit comments