Skip to content
Permalink
Browse files

Check connectivity for http

  • Loading branch information...
terry-xiaoyu committed Jun 4, 2019
1 parent d162246 commit 3feffcd5a3f0da78725f1208594cea1b3273ec0b
Showing with 56 additions and 2 deletions.
  1. +16 −0 docs/api_examples.md
  2. +8 −2 src/emqx_rule_engine_api.erl
  3. +32 −0 src/emqx_rule_utils.erl
@@ -124,6 +124,14 @@ $ curl -v --basic -u $APPSECRET -k 'http://localhost:8080/api/v3/resources' -d \
{"code":0,"data":{"attrs":"undefined","config":{"a":1},"description":"test-resource","id":"resource:71df3086","type":"built_in"}}
```

### start

```shell
$ curl -XPOST -v --basic -u $APPSECRET -k 'http://localhost:8080/api/v3/resources/resource:71df3086'
{"code":0}
```

### list

```shell
@@ -140,6 +148,14 @@ $ curl -v --basic -u $APPSECRET -k 'http://localhost:8080/api/v3/resources/resou
{"code":0,"data":{"attrs":"undefined","config":{"a":1},"description":"test-resource","id":"resource:71df3086","type":"built_in"}}
```

### get resource status

```shell
curl -v --basic -u $APPSECRET -k 'http://localhost:8080/api/v3/resource_status/resource:71df3086'
{"code":0,"data":{"is_alive":true}}
```

### delete

```shell
@@ -256,8 +256,14 @@ do_create_resource(Create, Params) ->
throw:{init_resource_failure, Reason} ->
%% Note that we will return OK in case of resource creation failure,
%% users can always re-start the resource later.
?LOG(error, "[RuleEngineAPI] init_resource_failure: ~p", [Reason]),
return(ok);
case Create of
create_resource ->
?LOG(error, "[RuleEngineAPI] init_resource_failure: ~p", [Reason]),
return(ok);
test_resource ->
?LOG(error, "[RuleEngineAPI] test_resource_failure: ~p", [Reason]),
return({error, 500, <<"Test Creating Resource Failed">>})
end;
throw:Reason ->
return({error, 400, ?ERR_BADARGS(Reason)});
_Error:Reason:StackT ->
@@ -29,8 +29,15 @@
, atom_key/1
]).

%% connectivity check
-export([ http_connectivity/1,
tcp_connectivity/2
]).

-define(EX_PLACE_HOLDER, "(\\$\\{[a-zA-Z0-9\\._]+\\})").

-type(uri_string() :: iodata()).

-type(tmpl_token() :: list({var, fun()} | {str, binary()})).

-type(prepare_statement() :: binary()).
@@ -106,6 +113,31 @@ atom_key(Key) when is_binary(Key) ->
catch error:badarg -> error({invalid_key, Key})
end.

-spec(http_connectivity(uri_string()) -> ok | {error, Reason :: term()}).
http_connectivity(Url) ->
case uri_string:parse(uri_string:normalize(Url)) of
{error, Reason, _} ->
{error, Reason};
#{host := Host, port := Port} ->
tcp_connectivity(str(Host), Port);
#{host := Host, scheme := Scheme} ->
tcp_connectivity(str(Host), default_port(Scheme));
_ ->
{error, {invalid_url, Url}}
end.

-spec(tcp_connectivity(Host :: inet:socket_address() | inet:hostname(),
Port :: inet:port_number())
-> ok | {error, Reason :: term()}).
tcp_connectivity(Host, Port) ->
case gen_tcp:connect(Host, Port, [], 3000) of
{ok, Sock} -> gen_tcp:close(Sock), ok;
{error, Reason} -> {error, Reason}
end.

default_port(http) -> 80;
default_port(https) -> 443.

var(<<"${", Val/binary>>) ->
binary:part(Val, {0, byte_size(Val)-1}).

0 comments on commit 3feffcd

Please sign in to comment.
You can’t perform that action at this time.