|
@@ -87,7 +87,7 @@ parse_bridge_id(BridgeId) ->
|
|
|
[Type, Name] ->
|
|
[Type, Name] ->
|
|
|
{to_type_atom(Type), validate_name(Name)};
|
|
{to_type_atom(Type), validate_name(Name)};
|
|
|
_ ->
|
|
_ ->
|
|
|
- invalid_bridge_id(
|
|
|
|
|
|
|
+ invalid_data(
|
|
|
<<"should be of pattern {type}:{name}, but got ", BridgeId/binary>>
|
|
<<"should be of pattern {type}:{name}, but got ", BridgeId/binary>>
|
|
|
)
|
|
)
|
|
|
end.
|
|
end.
|
|
@@ -108,14 +108,14 @@ validate_name(Name0) ->
|
|
|
true ->
|
|
true ->
|
|
|
Name0;
|
|
Name0;
|
|
|
false ->
|
|
false ->
|
|
|
- invalid_bridge_id(<<"bad name: ", Name0/binary>>)
|
|
|
|
|
|
|
+ invalid_data(<<"bad name: ", Name0/binary>>)
|
|
|
end;
|
|
end;
|
|
|
false ->
|
|
false ->
|
|
|
- invalid_bridge_id(<<"only 0-9a-zA-Z_-. is allowed in name: ", Name0/binary>>)
|
|
|
|
|
|
|
+ invalid_data(<<"only 0-9a-zA-Z_-. is allowed in name: ", Name0/binary>>)
|
|
|
end.
|
|
end.
|
|
|
|
|
|
|
|
--spec invalid_bridge_id(binary()) -> no_return().
|
|
|
|
|
-invalid_bridge_id(Reason) -> throw({?FUNCTION_NAME, Reason}).
|
|
|
|
|
|
|
+-spec invalid_data(binary()) -> no_return().
|
|
|
|
|
+invalid_data(Reason) -> throw(#{kind => validation_error, reason => Reason}).
|
|
|
|
|
|
|
|
is_id_char(C) when C >= $0 andalso C =< $9 -> true;
|
|
is_id_char(C) when C >= $0 andalso C =< $9 -> true;
|
|
|
is_id_char(C) when C >= $a andalso C =< $z -> true;
|
|
is_id_char(C) when C >= $a andalso C =< $z -> true;
|
|
@@ -130,7 +130,7 @@ to_type_atom(Type) ->
|
|
|
erlang:binary_to_existing_atom(Type, utf8)
|
|
erlang:binary_to_existing_atom(Type, utf8)
|
|
|
catch
|
|
catch
|
|
|
_:_ ->
|
|
_:_ ->
|
|
|
- invalid_bridge_id(<<"unknown type: ", Type/binary>>)
|
|
|
|
|
|
|
+ invalid_data(<<"unknown bridge type: ", Type/binary>>)
|
|
|
end.
|
|
end.
|
|
|
|
|
|
|
|
reset_metrics(ResourceId) ->
|
|
reset_metrics(ResourceId) ->
|
|
@@ -243,12 +243,19 @@ create_dry_run(Type, Conf0) ->
|
|
|
{error, Reason} ->
|
|
{error, Reason} ->
|
|
|
{error, Reason};
|
|
{error, Reason};
|
|
|
{ok, ConfNew} ->
|
|
{ok, ConfNew} ->
|
|
|
- ParseConf = parse_confs(bin(Type), TmpPath, ConfNew),
|
|
|
|
|
- Res = emqx_resource:create_dry_run_local(
|
|
|
|
|
- bridge_to_resource_type(Type), ParseConf
|
|
|
|
|
- ),
|
|
|
|
|
- _ = maybe_clear_certs(TmpPath, ConfNew),
|
|
|
|
|
- Res
|
|
|
|
|
|
|
+ try
|
|
|
|
|
+ ParseConf = parse_confs(bin(Type), TmpPath, ConfNew),
|
|
|
|
|
+ Res = emqx_resource:create_dry_run_local(
|
|
|
|
|
+ bridge_to_resource_type(Type), ParseConf
|
|
|
|
|
+ ),
|
|
|
|
|
+ Res
|
|
|
|
|
+ catch
|
|
|
|
|
+ %% validation errors
|
|
|
|
|
+ throw:Reason ->
|
|
|
|
|
+ {error, Reason}
|
|
|
|
|
+ after
|
|
|
|
|
+ _ = maybe_clear_certs(TmpPath, ConfNew)
|
|
|
|
|
+ end
|
|
|
end.
|
|
end.
|
|
|
|
|
|
|
|
remove(BridgeId) ->
|
|
remove(BridgeId) ->
|
|
@@ -300,10 +307,18 @@ parse_confs(
|
|
|
max_retries := Retry
|
|
max_retries := Retry
|
|
|
} = Conf
|
|
} = Conf
|
|
|
) ->
|
|
) ->
|
|
|
- {BaseUrl, Path} = parse_url(Url),
|
|
|
|
|
- {ok, BaseUrl2} = emqx_http_lib:uri_parse(BaseUrl),
|
|
|
|
|
|
|
+ Url1 = bin(Url),
|
|
|
|
|
+ {BaseUrl, Path} = parse_url(Url1),
|
|
|
|
|
+ BaseUrl1 =
|
|
|
|
|
+ case emqx_http_lib:uri_parse(BaseUrl) of
|
|
|
|
|
+ {ok, BUrl} ->
|
|
|
|
|
+ BUrl;
|
|
|
|
|
+ {error, Reason} ->
|
|
|
|
|
+ Reason1 = emqx_utils:readable_error_msg(Reason),
|
|
|
|
|
+ invalid_data(<<"Invalid URL: ", Url1/binary, ", details: ", Reason1/binary>>)
|
|
|
|
|
+ end,
|
|
|
Conf#{
|
|
Conf#{
|
|
|
- base_url => BaseUrl2,
|
|
|
|
|
|
|
+ base_url => BaseUrl1,
|
|
|
request =>
|
|
request =>
|
|
|
#{
|
|
#{
|
|
|
path => Path,
|
|
path => Path,
|
|
@@ -338,7 +353,7 @@ parse_url(Url) ->
|
|
|
{iolist_to_binary([Scheme, "//", HostPort]), <<>>}
|
|
{iolist_to_binary([Scheme, "//", HostPort]), <<>>}
|
|
|
end;
|
|
end;
|
|
|
[Url] ->
|
|
[Url] ->
|
|
|
- error({invalid_url, Url})
|
|
|
|
|
|
|
+ invalid_data(<<"Missing scheme in URL: ", Url/binary>>)
|
|
|
end.
|
|
end.
|
|
|
|
|
|
|
|
str(Bin) when is_binary(Bin) -> binary_to_list(Bin);
|
|
str(Bin) when is_binary(Bin) -> binary_to_list(Bin);
|