44
44
-export ([import_raw /1 , import_raw /2 , import_parsed /1 , import_parsed /2 ,
45
45
import_parsed_with_hashing /1 , import_parsed_with_hashing /2 ,
46
46
apply_defs /2 , apply_defs /3 ,
47
- should_skip_if_unchanged /0 ]).
47
+ should_skip_if_unchanged /0
48
+ ]).
48
49
49
50
-export ([all_definitions /0 ]).
50
51
-export ([
53
54
list_exchanges /0 , list_queues /0 , list_bindings /0 ,
54
55
is_internal_parameter /1
55
56
]).
56
- -export ([decode /1 , decode /2 , args /1 ]).
57
+ -export ([decode /1 , decode /2 , args /1 , validate_definitions /1 ]).
58
+
59
+ % % for tests
60
+ -export ([
61
+ maybe_load_definitions_from_local_filesystem_if_unchanged /3 ,
62
+ maybe_load_definitions_from_pluggable_source_if_unchanged /2
63
+ ]).
57
64
58
65
-import (rabbit_misc , [pget /2 , pget /3 ]).
59
66
-import (rabbit_data_coercion , [to_binary /1 ]).
@@ -97,6 +104,21 @@ maybe_load_definitions() ->
97
104
{error , E } -> {error , E }
98
105
end .
99
106
107
+ validate_definitions (Defs ) when is_list (Defs ) ->
108
+ lists :foldl (fun (_Body , false ) ->
109
+ false ;
110
+ (Body , true ) ->
111
+ case decode (Body ) of
112
+ {ok , _Map } -> true ;
113
+ {error , _Err } -> false
114
+ end
115
+ end , true , Defs );
116
+ validate_definitions (Body ) when is_binary (Body ) ->
117
+ case decode (Body ) of
118
+ {ok , _Map } -> true ;
119
+ {error , _Err } -> false
120
+ end .
121
+
100
122
-spec import_raw (Body :: binary () | iolist ()) -> ok | {error , term ()}.
101
123
import_raw (Body ) ->
102
124
rabbit_log :info (" Asked to import definitions. Acting user: ~ts " , [? INTERNAL_USER ]),
@@ -271,20 +293,25 @@ maybe_load_definitions_from_local_filesystem(App, Key) ->
271
293
rabbit_log :debug (" Will re-import definitions even if they have not changed" ),
272
294
Mod :load (IsDir , Path );
273
295
true ->
274
- Algo = rabbit_definitions_hashing :hashing_algorithm (),
275
- rabbit_log :debug (" Will import definitions only if definition file/directory has changed, hashing algo: ~ts " , [Algo ]),
276
- CurrentHash = rabbit_definitions_hashing :stored_global_hash (),
277
- rabbit_log :debug (" Previously stored hash value of imported definitions: ~ts ..." , [binary :part (rabbit_misc :hexify (CurrentHash ), 0 , 12 )]),
278
- case Mod :load_with_hashing (IsDir , Path , CurrentHash , Algo ) of
279
- CurrentHash ->
280
- rabbit_log :info (" Hash value of imported definitions matches current contents" );
281
- UpdatedHash ->
282
- rabbit_log :debug (" Hash value of imported definitions has changed to ~ts " , [binary :part (rabbit_misc :hexify (UpdatedHash ), 0 , 12 )]),
283
- rabbit_definitions_hashing :store_global_hash (UpdatedHash )
284
- end
296
+ maybe_load_definitions_from_local_filesystem_if_unchanged (Mod , IsDir , Path )
285
297
end
286
298
end .
287
299
300
+ maybe_load_definitions_from_local_filesystem_if_unchanged (Mod , IsDir , Path ) ->
301
+ Algo = rabbit_definitions_hashing :hashing_algorithm (),
302
+ rabbit_log :debug (" Will import definitions only if definition file/directory has changed, hashing algo: ~ts " , [Algo ]),
303
+ CurrentHash = rabbit_definitions_hashing :stored_global_hash (),
304
+ rabbit_log :debug (" Previously stored hash value of imported definitions: ~ts ..." , [binary :part (rabbit_misc :hexify (CurrentHash ), 0 , 12 )]),
305
+ case Mod :load_with_hashing (IsDir , Path , CurrentHash , Algo ) of
306
+ {error , Err } ->
307
+ {error , Err };
308
+ CurrentHash ->
309
+ rabbit_log :info (" Hash value of imported definitions matches current contents" );
310
+ UpdatedHash ->
311
+ rabbit_log :debug (" Hash value of imported definitions has changed to ~ts " , [binary :part (rabbit_misc :hexify (UpdatedHash ), 0 , 12 )]),
312
+ rabbit_definitions_hashing :store_global_hash (UpdatedHash )
313
+ end .
314
+
288
315
maybe_load_definitions_from_pluggable_source (App , Key ) ->
289
316
case application :get_env (App , Key ) of
290
317
undefined -> ok ;
@@ -296,26 +323,33 @@ maybe_load_definitions_from_pluggable_source(App, Key) ->
296
323
{error , " definition import source is configured but definitions.import_backend is not set" };
297
324
ModOrAlias ->
298
325
Mod = normalize_backend_module (ModOrAlias ),
299
- case should_skip_if_unchanged () of
300
- false ->
301
- rabbit_log :debug (" Will use module ~ts to import definitions" , [Mod ]),
302
- Mod :load (Proplist );
303
- true ->
304
- rabbit_log :debug (" Will use module ~ts to import definitions (if definition file/directory/source has changed)" , [Mod ]),
305
- CurrentHash = rabbit_definitions_hashing :stored_global_hash (),
306
- rabbit_log :debug (" Previously stored hash value of imported definitions: ~ts ..." , [binary :part (rabbit_misc :hexify (CurrentHash ), 0 , 12 )]),
307
- Algo = rabbit_definitions_hashing :hashing_algorithm (),
308
- case Mod :load_with_hashing (Proplist , CurrentHash , Algo ) of
309
- CurrentHash ->
310
- rabbit_log :info (" Hash value of imported definitions matches current contents" );
311
- UpdatedHash ->
312
- rabbit_log :debug (" Hash value of imported definitions has changed to ~ts ..." , [binary :part (rabbit_misc :hexify (CurrentHash ), 0 , 12 )]),
313
- rabbit_definitions_hashing :store_global_hash (UpdatedHash )
314
- end
315
- end
326
+ maybe_load_definitions_from_pluggable_source_if_unchanged (Mod , Proplist )
316
327
end
317
328
end .
318
329
330
+ maybe_load_definitions_from_pluggable_source_if_unchanged (Mod , Proplist ) ->
331
+ case should_skip_if_unchanged () of
332
+ false ->
333
+ rabbit_log :debug (" Will use module ~ts to import definitions" , [Mod ]),
334
+ Mod :load (Proplist );
335
+ true ->
336
+ rabbit_log :debug (" Will use module ~ts to import definitions (if definition file/directory/source has changed)" , [Mod ]),
337
+ CurrentHash = rabbit_definitions_hashing :stored_global_hash (),
338
+ rabbit_log :debug (" Previously stored hash value of imported definitions: ~ts ..." , [binary :part (rabbit_misc :hexify (CurrentHash ), 0 , 12 )]),
339
+ Algo = rabbit_definitions_hashing :hashing_algorithm (),
340
+ case Mod :load_with_hashing (Proplist , CurrentHash , Algo ) of
341
+ {error , Err } ->
342
+ {error , Err };
343
+ CurrentHash ->
344
+ rabbit_log :info (" Hash value of imported definitions matches current contents" );
345
+ UpdatedHash ->
346
+ rabbit_log :debug (" Hash value of imported definitions has changed to ~ts ..." , [binary :part (rabbit_misc :hexify (CurrentHash ), 0 , 12 )]),
347
+ rabbit_definitions_hashing :store_global_hash (UpdatedHash )
348
+ end
349
+ end .
350
+
351
+
352
+
319
353
normalize_backend_module (local_filesystem ) ->
320
354
rabbit_definitions_import_local_filesystem ;
321
355
normalize_backend_module (local ) ->
0 commit comments