Browse Source

chore(license): change api responses after review

Thales Macedo Garitezi 3 years ago
parent
commit
1a66e53c49

+ 42 - 20
lib-ee/emqx_license/src/emqx_license_http_api.erl

@@ -47,17 +47,7 @@ schema("/license") ->
                     map(),
                     map(),
                     #{
                     #{
                         sample_license_info => #{
                         sample_license_info => #{
-                            value => #{
-                                customer => "Foo",
-                                customer_type => 10,
-                                deployment => "bar-deployment",
-                                email => "contact@foo.com",
-                                expiry => false,
-                                expiry_at => "2295-10-27",
-                                max_connections => 10,
-                                start_at => "2022-01-11",
-                                type => "trial"
-                            }
+                            value => sample_license_info_response()
                         }
                         }
                     }
                     }
                 )
                 )
@@ -93,13 +83,36 @@ schema("/license/upload") ->
                 }
                 }
             ),
             ),
             responses => #{
             responses => #{
-                200 => <<"ok">>,
-                400 => emqx_dashboard_swagger:error_codes([?BAD_REQUEST], <<"bad request">>),
-                404 => emqx_dashboard_swagger:error_codes([?NOT_FOUND], <<"file not found">>)
+                200 => emqx_dashboard_swagger:schema_with_examples(
+                    map(),
+                    #{
+                        sample_license_info => #{
+                            value => sample_license_info_response()
+                        }
+                    }
+                ),
+                400 => emqx_dashboard_swagger:error_codes([?BAD_REQUEST], <<"Bad license key">>),
+                404 => emqx_dashboard_swagger:error_codes([?NOT_FOUND], <<"File not found">>)
             }
             }
         }
         }
     }.
     }.
 
 
+sample_license_info_response() ->
+    #{
+        customer => "Foo",
+        customer_type => 10,
+        deployment => "bar-deployment",
+        email => "contact@foo.com",
+        expiry => false,
+        expiry_at => "2295-10-27",
+        max_connections => 10,
+        start_at => "2022-01-11",
+        type => "trial"
+    }.
+
+error_msg(Code, Msg) ->
+    #{code => Code, message => emqx_misc:readable_error_msg(Msg)}.
+
 '/license'(get, _Params) ->
 '/license'(get, _Params) ->
     License = maps:from_list(emqx_license_checker:dump()),
     License = maps:from_list(emqx_license_checker:dump()),
     {200, License}.
     {200, License}.
@@ -111,20 +124,28 @@ schema("/license/upload") ->
                 msg => "license_file_not_found",
                 msg => "license_file_not_found",
                 path => Filepath
                 path => Filepath
             }),
             }),
-            {404, <<"file not found">>};
+            {404, error_msg(?NOT_FOUND, <<"File not found">>)};
+        {error, Error} when is_atom(Error) ->
+            ?SLOG(error, #{
+                msg => "bad_license_file",
+                reason => Error,
+                path => Filepath
+            }),
+            {400, error_msg(?BAD_REQUEST, emqx_misc:explain_posix(Error))};
         {error, Error} ->
         {error, Error} ->
             ?SLOG(error, #{
             ?SLOG(error, #{
                 msg => "bad_license_file",
                 msg => "bad_license_file",
                 reason => Error,
                 reason => Error,
                 path => Filepath
                 path => Filepath
             }),
             }),
-            {400, <<"bad request">>};
+            {400, error_msg(?BAD_REQUEST, <<"Bad license file">>)};
         {ok, _} ->
         {ok, _} ->
             ?SLOG(info, #{
             ?SLOG(info, #{
                 msg => "updated_license_file",
                 msg => "updated_license_file",
                 path => Filepath
                 path => Filepath
             }),
             }),
-            {200, <<"ok">>}
+            License = maps:from_list(emqx_license_checker:dump()),
+            {200, License}
     end;
     end;
 '/license/upload'(post, #{body := #{<<"key">> := Key}}) ->
 '/license/upload'(post, #{body := #{<<"key">> := Key}}) ->
     case emqx_license:update_key(Key) of
     case emqx_license:update_key(Key) of
@@ -133,10 +154,11 @@ schema("/license/upload") ->
                 msg => "bad_license_key",
                 msg => "bad_license_key",
                 reason => Error
                 reason => Error
             }),
             }),
-            {400, <<"bad request">>};
+            {400, error_msg(?BAD_REQUEST, <<"Bad license key">>)};
         {ok, _} ->
         {ok, _} ->
             ?SLOG(info, #{msg => "updated_license_key"}),
             ?SLOG(info, #{msg => "updated_license_key"}),
-            {200, <<"ok">>}
+            License = maps:from_list(emqx_license_checker:dump()),
+            {200, License}
     end;
     end;
 '/license/upload'(post, _Params) ->
 '/license/upload'(post, _Params) ->
-    {400, <<"bad request">>}.
+    {400, error_msg(?BAD_REQUEST, <<"Invalid request params">>)}.

+ 87 - 36
lib-ee/emqx_license/test/emqx_license_http_api_SUITE.erl

@@ -117,13 +117,26 @@ t_license_upload_file_success(_Config) ->
     Path = "/tmp/new.lic",
     Path = "/tmp/new.lic",
     ok = file:write_file(Path, NewKey),
     ok = file:write_file(Path, NewKey),
     try
     try
+        Res = request(
+            post,
+            uri(["license", "upload"]),
+            #{file => Path}
+        ),
+        ?assertMatch({ok, 200, _}, Res),
+        {ok, 200, Payload} = Res,
         ?assertEqual(
         ?assertEqual(
-            {ok, 200, <<"ok">>},
-            request(
-                post,
-                uri(["license", "upload"]),
-                #{file => Path}
-            )
+            #{
+                <<"customer">> => <<"Foo">>,
+                <<"customer_type">> => 10,
+                <<"deployment">> => <<"bar-deployment">>,
+                <<"email">> => <<"contact@foo.com">>,
+                <<"expiry">> => false,
+                <<"expiry_at">> => <<"2295-10-27">>,
+                <<"max_connections">> => 999,
+                <<"start_at">> => <<"2022-01-11">>,
+                <<"type">> => <<"trial">>
+            },
+            emqx_json:decode(Payload, [return_maps])
         ),
         ),
         ?assertMatch(
         ?assertMatch(
             #{max_connections := 999},
             #{max_connections := 999},
@@ -136,13 +149,20 @@ t_license_upload_file_success(_Config) ->
     end.
     end.
 
 
 t_license_upload_file_not_found(_Config) ->
 t_license_upload_file_not_found(_Config) ->
+    Res = request(
+        post,
+        uri(["license", "upload"]),
+        #{file => "/tmp/inexistent.lic"}
+    ),
+
+    ?assertMatch({ok, 404, _}, Res),
+    {ok, 404, Payload} = Res,
     ?assertEqual(
     ?assertEqual(
-        {ok, 404, <<"file not found">>},
-        request(
-            post,
-            uri(["license", "upload"]),
-            #{file => "/tmp/inexistent.lic"}
-        )
+        #{
+            <<"code">> => <<"NOT_FOUND">>,
+            <<"message">> => <<"File not found">>
+        },
+        emqx_json:decode(Payload, [return_maps])
     ),
     ),
     assert_untouched_license(),
     assert_untouched_license(),
     ok.
     ok.
@@ -150,13 +170,19 @@ t_license_upload_file_not_found(_Config) ->
 t_license_upload_file_reading_error(_Config) ->
 t_license_upload_file_reading_error(_Config) ->
     %% eisdir
     %% eisdir
     Path = "/tmp/",
     Path = "/tmp/",
+    Res = request(
+        post,
+        uri(["license", "upload"]),
+        #{file => Path}
+    ),
+    ?assertMatch({ok, 400, _}, Res),
+    {ok, 400, Payload} = Res,
     ?assertEqual(
     ?assertEqual(
-        {ok, 400, <<"bad request">>},
-        request(
-            post,
-            uri(["license", "upload"]),
-            #{file => Path}
-        )
+        #{
+            <<"code">> => <<"BAD_REQUEST">>,
+            <<"message">> => <<"Illegal operation on a directory">>
+        },
+        emqx_json:decode(Payload, [return_maps])
     ),
     ),
     assert_untouched_license(),
     assert_untouched_license(),
     ok.
     ok.
@@ -165,13 +191,19 @@ t_license_upload_file_bad_license(_Config) ->
     Path = "/tmp/bad.lic",
     Path = "/tmp/bad.lic",
     ok = file:write_file(Path, <<"bad key">>),
     ok = file:write_file(Path, <<"bad key">>),
     try
     try
+        Res = request(
+            post,
+            uri(["license", "upload"]),
+            #{file => Path}
+        ),
+        ?assertMatch({ok, 400, _}, Res),
+        {ok, 400, Payload} = Res,
         ?assertEqual(
         ?assertEqual(
-            {ok, 400, <<"bad request">>},
-            request(
-                post,
-                uri(["license", "upload"]),
-                #{file => Path}
-            )
+            #{
+                <<"code">> => <<"BAD_REQUEST">>,
+                <<"message">> => <<"Bad license file">>
+            },
+            emqx_json:decode(Payload, [return_maps])
         ),
         ),
         assert_untouched_license(),
         assert_untouched_license(),
         ok
         ok
@@ -182,13 +214,26 @@ t_license_upload_file_bad_license(_Config) ->
 
 
 t_license_upload_key_success(_Config) ->
 t_license_upload_key_success(_Config) ->
     NewKey = emqx_license_test_lib:make_license(#{max_connections => "999"}),
     NewKey = emqx_license_test_lib:make_license(#{max_connections => "999"}),
+    Res = request(
+        post,
+        uri(["license", "upload"]),
+        #{key => NewKey}
+    ),
+    ?assertMatch({ok, 200, _}, Res),
+    {ok, 200, Payload} = Res,
     ?assertEqual(
     ?assertEqual(
-        {ok, 200, <<"ok">>},
-        request(
-            post,
-            uri(["license", "upload"]),
-            #{key => NewKey}
-        )
+        #{
+            <<"customer">> => <<"Foo">>,
+            <<"customer_type">> => 10,
+            <<"deployment">> => <<"bar-deployment">>,
+            <<"email">> => <<"contact@foo.com">>,
+            <<"expiry">> => false,
+            <<"expiry_at">> => <<"2295-10-27">>,
+            <<"max_connections">> => 999,
+            <<"start_at">> => <<"2022-01-11">>,
+            <<"type">> => <<"trial">>
+        },
+        emqx_json:decode(Payload, [return_maps])
     ),
     ),
     ?assertMatch(
     ?assertMatch(
         #{max_connections := 999},
         #{max_connections := 999},
@@ -198,13 +243,19 @@ t_license_upload_key_success(_Config) ->
 
 
 t_license_upload_key_bad_key(_Config) ->
 t_license_upload_key_bad_key(_Config) ->
     BadKey = <<"bad key">>,
     BadKey = <<"bad key">>,
+    Res = request(
+        post,
+        uri(["license", "upload"]),
+        #{key => BadKey}
+    ),
+    ?assertMatch({ok, 400, _}, Res),
+    {ok, 400, Payload} = Res,
     ?assertEqual(
     ?assertEqual(
-        {ok, 400, <<"bad request">>},
-        request(
-            post,
-            uri(["license", "upload"]),
-            #{key => BadKey}
-        )
+        #{
+            <<"code">> => <<"BAD_REQUEST">>,
+            <<"message">> => <<"Bad license key">>
+        },
+        emqx_json:decode(Payload, [return_maps])
     ),
     ),
     assert_untouched_license(),
     assert_untouched_license(),
     ok.
     ok.