增加参数校验
This commit is contained in:
parent
cea9fff718
commit
616cad6daf
@ -63,21 +63,27 @@ handle_request("POST", "/container/push_config", _,
|
|||||||
handle_request("POST", "/container/deploy", _, #{<<"uuid">> := UUID, <<"task_id">> := TaskId, <<"config">> := Config})
|
handle_request("POST", "/container/deploy", _, #{<<"uuid">> := UUID, <<"task_id">> := TaskId, <<"config">> := Config})
|
||||||
when is_binary(UUID), is_integer(TaskId), is_map(Config) ->
|
when is_binary(UUID), is_integer(TaskId), is_map(Config) ->
|
||||||
|
|
||||||
case iot_host:get_pid(UUID) of
|
case validate_config(Config) of
|
||||||
undefined ->
|
ok ->
|
||||||
{ok, 200, iot_util:json_error(404, <<"host not found">>)};
|
case iot_host:get_pid(UUID) of
|
||||||
Pid when is_pid(Pid) ->
|
undefined ->
|
||||||
case iot_host:deploy_container(Pid, TaskId, Config) of
|
{ok, 200, iot_util:json_error(404, <<"host not found">>)};
|
||||||
{ok, Ref} ->
|
Pid when is_pid(Pid) ->
|
||||||
case iot_host:await_reply(Ref, ?REQ_TIMEOUT) of
|
case iot_host:deploy_container(Pid, TaskId, Config) of
|
||||||
{ok, Result} ->
|
{ok, Ref} ->
|
||||||
{ok, 200, iot_util:json_data(Result)};
|
case iot_host:await_reply(Ref, ?REQ_TIMEOUT) of
|
||||||
{error, Reason} ->
|
{ok, Result} ->
|
||||||
|
{ok, 200, iot_util:json_data(Result)};
|
||||||
|
{error, Reason} ->
|
||||||
|
{ok, 200, iot_util:json_error(400, Reason)}
|
||||||
|
end;
|
||||||
|
{error, Reason} when is_binary(Reason) ->
|
||||||
{ok, 200, iot_util:json_error(400, Reason)}
|
{ok, 200, iot_util:json_error(400, Reason)}
|
||||||
end;
|
end
|
||||||
{error, Reason} when is_binary(Reason) ->
|
end;
|
||||||
{ok, 200, iot_util:json_error(400, Reason)}
|
{error, Errors} ->
|
||||||
end
|
Reason = iolist_to_binary(lists:join(<<"|||">>, Errors)),
|
||||||
|
{ok, 200, iot_util:json_error(400, Reason)}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
%% 启动服务
|
%% 启动服务
|
||||||
@ -186,34 +192,34 @@ validate_config(Config) when is_map(Config) ->
|
|||||||
Required = [
|
Required = [
|
||||||
{image, binary},
|
{image, binary},
|
||||||
{container_name, binary},
|
{container_name, binary},
|
||||||
{command, list},
|
{command, {list, binary}},
|
||||||
{restart, binary},
|
{restart, binary},
|
||||||
{privileged, boolean}
|
{privileged, boolean}
|
||||||
],
|
],
|
||||||
|
|
||||||
%% 可选参数(附带默认值)
|
%% 可选参数(附带默认值)
|
||||||
Optional = [
|
Optional = [
|
||||||
{envs, list},
|
{envs, {list, binary}},
|
||||||
{ports, list},
|
{ports, {list, binary}},
|
||||||
{expose, list},
|
{expose, {list, binary}},
|
||||||
{volumes, list},
|
{volumes, {list, binary}},
|
||||||
{networks, list},
|
{networks, {list, binary}},
|
||||||
{labels, map},
|
{labels, {map, {binary, binary}}},
|
||||||
{user, binary},
|
{user, binary},
|
||||||
{working_dir, binary},
|
{working_dir, binary},
|
||||||
{hostname, binary},
|
{hostname, binary},
|
||||||
{cap_add, list},
|
{cap_add, {list, binary}},
|
||||||
{cap_drop, list},
|
{cap_drop, {list, binary}},
|
||||||
{devices, list},
|
{devices, {list, binary}},
|
||||||
{mem_limit, binary},
|
{mem_limit, binary},
|
||||||
{mem_reservation, binary},
|
{mem_reservation, binary},
|
||||||
{cpu_shares, integer},
|
{cpu_shares, integer},
|
||||||
{cpus, number},
|
{cpus, number},
|
||||||
{ulimits, map},
|
{ulimits, {map, {binary, binary}}},
|
||||||
{sysctls, map},
|
{sysctls, {map, {binary, binary}}},
|
||||||
{tmpfs, list},
|
{tmpfs, {list, binary}},
|
||||||
{extra_hosts, list},
|
{extra_hosts, {list, binary}},
|
||||||
{healthcheck, map}
|
{healthcheck, {map, {binary, any}}}
|
||||||
],
|
],
|
||||||
|
|
||||||
Errors1 = check_required(Config, Required),
|
Errors1 = check_required(Config, Required),
|
||||||
@ -273,10 +279,27 @@ check_optional(Config, Fields) ->
|
|||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% 类型检查辅助函数(binary版)
|
%% 类型检查辅助函数(binary版)
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
check_type(Value, binary) -> is_binary(Value);
|
check_type(Value, binary) ->
|
||||||
check_type(Value, integer) -> is_integer(Value);
|
is_binary(Value);
|
||||||
check_type(Value, number) -> is_number(Value);
|
check_type(Value, integer) ->
|
||||||
check_type(Value, list) -> is_list(Value);
|
is_integer(Value);
|
||||||
check_type(Value, map) -> is_map(Value);
|
check_type(Value, number) ->
|
||||||
check_type(Value, boolean) -> is_boolean(Value);
|
is_number(Value);
|
||||||
check_type(_, _) -> false.
|
check_type(Value, list) when is_list(Value) ->
|
||||||
|
true;
|
||||||
|
check_type(Value, {list, binary}) when is_list(Value) ->
|
||||||
|
lists:all(fun(E) -> is_binary(E) end, Value);
|
||||||
|
check_type(Value, {list, number}) when is_list(Value) ->
|
||||||
|
lists:all(fun(E) -> is_number(E) end, Value);
|
||||||
|
check_type(Value, {list, integer}) when is_list(Value) ->
|
||||||
|
lists:all(fun(E) -> is_integer(E) end, Value);
|
||||||
|
check_type(Value, map) when is_map(Value) ->
|
||||||
|
true;
|
||||||
|
check_type(Value, {map, {binary, binary}}) when is_map(Value) ->
|
||||||
|
lists:all(fun({K, V}) -> is_binary(K) andalso is_binary(V) end, maps:to_list(Value));
|
||||||
|
check_type(Value, {map, {binary, any}}) when is_map(Value) ->
|
||||||
|
lists:all(fun({K, _}) -> is_binary(K) end, maps:to_list(Value));
|
||||||
|
check_type(Value, boolean) ->
|
||||||
|
is_boolean(Value);
|
||||||
|
check_type(_, _) ->
|
||||||
|
false.
|
||||||
@ -23,8 +23,9 @@ init(Req0, Opts) ->
|
|||||||
|
|
||||||
lager:debug("method: ~p, path: ~p, get: ~p", [Method, Path, GetParams]),
|
lager:debug("method: ~p, path: ~p, get: ~p", [Method, Path, GetParams]),
|
||||||
Req1 = cowboy_req:stream_reply(200, #{
|
Req1 = cowboy_req:stream_reply(200, #{
|
||||||
<<"content-type">> => <<"text/event-stream">>,
|
<<"Content-Type">> => <<"text/event-stream">>,
|
||||||
<<"cache-control">> => <<"no-cache">>
|
<<"Cache-Control">> => <<"no-cache">>,
|
||||||
|
<<"Connection">> => <<"keep-alive">>
|
||||||
}, Req0),
|
}, Req0),
|
||||||
|
|
||||||
ok = iot_event_stream_observer:add_listener(self(), TaskId),
|
ok = iot_event_stream_observer:add_listener(self(), TaskId),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user