diff --git a/src/api_handler.erl b/src/api_handler.erl index 2d650db..4f3663a 100644 --- a/src/api_handler.erl +++ b/src/api_handler.erl @@ -20,8 +20,8 @@ init(Req, Opts) -> extract_module_from_path(Path, Start, Length) -> Path@0 = erlang:binary_to_list(Path), Path@1 = lists:flatten( - lists:join("/", lists:sublist(string:split(Path@0, "/", all), Start, Length)) - ), + lists:join("/", lists:sublist(string:split(Path@0, "/", all), Start, Length)) + ), Path@2 = erlang:list_to_binary(Path@1), Path@2. @@ -44,12 +44,12 @@ handler_lookup(<<"fact">>) -> dog_fact. content_types_provided(Req, State) -> { - [ - {<<"application/json">>, to_json}, - {<<"text/plain">>, to_text} - ], - Req, - State + [ + {<<"application/json">>, to_json}, + {<<"text/plain">>, to_text} + ], + Req, + State }. content_types_accepted(Req, State) -> @@ -75,14 +75,14 @@ resource_exists(Req@0, State@0) -> ObjectName = maps:get(<<"name">>, Map), Object = Handler:get_by_name(ObjectName), Result = - case Object of - {error, notfound} -> - false; - {validation_error, _Error} -> - false; - {ok, _} -> - true - end, + case Object of + {error, notfound} -> + false; + {validation_error, _Error} -> + false; + {ok, _} -> + true + end, State@1 = maps:put(<<"object">>, Object, State@0), State@2 = maps:put(<<"content">>, NewContent, State@1), {Result, Req@1, State@2}; @@ -96,14 +96,14 @@ resource_exists(Req@0, State@0) -> ObjectName = maps:get(<<"name">>, Map), Object = Handler:get_by_name(ObjectName), Result = - case Object of - {error, notfound} -> - false; - {validation_error, _Error} -> - false; - {ok, _} -> - true - end, + case Object of + {error, notfound} -> + false; + {validation_error, _Error} -> + false; + {ok, _} -> + true + end, State@1 = maps:put(<<"object">>, Object, State@0), State@2 = maps:put(<<"content">>, NewContent, State@1), {Result, Req@1, State@2}; @@ -172,11 +172,11 @@ from_post_json(Req@0, State) -> case Handler:create(Map) of {validation_error, Error} -> cowboy_req:reply( - 400, - #{<<"content-type">> => <<"application/json">>}, - Error, - Req@0 - ); + 400, + #{<<"content-type">> => <<"application/json">>}, + Error, + Req@0 + ); {ok, Id} -> SuccessMap = #{<<"id">> => Id, <<"result">> => <<"created">>}, Req@1 = cowboy_req:set_resp_body([jsx:encode(SuccessMap)], Req@0), @@ -193,11 +193,11 @@ from_post_json(Req@0, State) -> {{true, list_to_binary(Uri)}, Req@1, State}; {validation_error, Error} -> cowboy_req:reply( - 400, - #{<<"content-type">> => <<"application/json">>}, - Error, - Req@0 - ) + 400, + #{<<"content-type">> => <<"application/json">>}, + Error, + Req@0 + ) end. from_put_json(Req@0, State) -> @@ -212,12 +212,12 @@ from_put_json(Req@0, State) -> InPlace = cowboy_req:match_qs([{inplace, [], plain}], Req@1), ?LOG_INFO("InPlace: ~p", [InPlace]), Response = - case InPlace of - #{inplace := <<"True">>} -> - Handler:update(Id, UpdateMap, true); - _ -> - Handler:update(Id, UpdateMap) - end, + case InPlace of + #{inplace := <<"True">>} -> + Handler:update(Id, UpdateMap, true); + _ -> + Handler:update(Id, UpdateMap) + end, case Response of {false, Error} when is_atom(Error) -> ?LOG_INFO("{false, ~p}", [Error]), @@ -229,14 +229,14 @@ from_put_json(Req@0, State) -> Uri = io_lib:format("~s/~s/~s", [?ROOT, HandlerPath, binary_to_list(ObjectId)]), ?LOG_INFO("ObjectId: ~p", [ObjectId]), Req@2 = cowboy_req:reply( - 303, - #{ - <<"content-type">> => <<"application/json">>, - <<"location">> => Uri - }, - binary_to_list(jsx:encode(SuccessMap2)), - Req@1 - ), + 303, + #{ + <<"content-type">> => <<"application/json">>, + <<"location">> => Uri + }, + binary_to_list(jsx:encode(SuccessMap2)), + Req@1 + ), {stop, Req@2, State}; {true, ObjectId} -> ?LOG_INFO("{true, ~p}", [ObjectId]), @@ -244,22 +244,22 @@ from_put_json(Req@0, State) -> Uri = io_lib:format("~s/~s/~s", [?ROOT, HandlerPath, binary_to_list(ObjectId)]), ?LOG_INFO("ObjectId: ~p", [ObjectId]), Req@2 = cowboy_req:reply( - 303, - #{ - <<"content-type">> => <<"application/json">>, - <<"location">> => Uri - }, - binary_to_list(jsx:encode(SuccessMap2)), - Req@1 - ), + 303, + #{ + <<"content-type">> => <<"application/json">>, + <<"location">> => Uri + }, + binary_to_list(jsx:encode(SuccessMap2)), + Req@1 + ), {stop, Req@2, State}; {validation_error, Error} -> cowboy_req:reply( - 400, - #{<<"content-type">> => <<"application/json">>}, - Error, - Req@1 - ) + 400, + #{<<"content-type">> => <<"application/json">>}, + Error, + Req@1 + ) end. to_json(Req, State) -> @@ -270,65 +270,65 @@ to_json(Req, State) -> HandlerPath = get_handler_path(Path), Handler = get_handler_module(Path), Json = - case HandlerPath of - "group" -> - case Id@0 of - undefined -> - jsx:encode(Object); - Id -> - case Sub of - undefined -> - jsx:encode(Object); - <<"ips">> -> - {ok, ObjectIps} = dog_group:get_all_ips_by_id(Id), - jsx:encode(ObjectIps); - <<"ipv4s">> -> - {ok, ObjectIps} = dog_group:get_all_ipv4s_by_id(Id), - jsx:encode(ObjectIps); - <<"ipv6s">> -> - {ok, ObjectIps} = dog_group:get_all_ipv6s_by_id(Id), - jsx:encode(ObjectIps); - <<"internal_ips">> -> - {ok, ObjectIps} = dog_group:get_internal_ips_by_id(Id), - jsx:encode(ObjectIps); - <<"internal_ipv4s">> -> - {ok, ObjectIps} = dog_group:get_internal_ipv4s_by_id(Id), - jsx:encode(ObjectIps); - <<"internal_ipv6s">> -> - {ok, ObjectIps} = dog_group:get_internal_ipv6s_by_id(Id), - jsx:encode(ObjectIps); - <<"external_ips">> -> - {ok, ObjectIps} = dog_group:get_external_ips_by_id(Id), - jsx:encode(ObjectIps); - <<"external_ipv4s">> -> - {ok, ObjectIps} = dog_group:get_external_ipv4s_by_id(Id), - jsx:encode(ObjectIps); - <<"external_ipv6s">> -> - {ok, ObjectIps} = dog_group:get_external_ipv6s_by_id(Id), - jsx:encode(ObjectIps); - <<"hosts">> -> - {ok, ObjectHosts} = dog_group:get_hosts_by_id(Id), - jsx:encode(ObjectHosts); - <<"ec2_security_group_ids">> -> - ObjectHosts = dog_group:get_internal_ec2_security_group_ids_by_id(Id), - jsx:encode(ObjectHosts); - <<"hcl">> -> - Handler:to_hcl_by_id(Id) - end - end; - _ -> - case Id@0 of - undefined -> - jsx:encode(Object); - Id -> - case Sub of - undefined -> - jsx:encode(Object); - <<"hcl">> -> - Handler:to_hcl_by_id(Id) - end - end - end, + case HandlerPath of + "group" -> + case Id@0 of + undefined -> + jsx:encode(Object); + Id -> + case Sub of + undefined -> + jsx:encode(Object); + <<"ips">> -> + {ok, ObjectIps} = dog_group:get_all_ips_by_id(Id), + jsx:encode(ObjectIps); + <<"ipv4s">> -> + {ok, ObjectIps} = dog_group:get_all_ipv4s_by_id(Id), + jsx:encode(ObjectIps); + <<"ipv6s">> -> + {ok, ObjectIps} = dog_group:get_all_ipv6s_by_id(Id), + jsx:encode(ObjectIps); + <<"internal_ips">> -> + {ok, ObjectIps} = dog_group:get_internal_ips_by_id(Id), + jsx:encode(ObjectIps); + <<"internal_ipv4s">> -> + {ok, ObjectIps} = dog_group:get_internal_ipv4s_by_id(Id), + jsx:encode(ObjectIps); + <<"internal_ipv6s">> -> + {ok, ObjectIps} = dog_group:get_internal_ipv6s_by_id(Id), + jsx:encode(ObjectIps); + <<"external_ips">> -> + {ok, ObjectIps} = dog_group:get_external_ips_by_id(Id), + jsx:encode(ObjectIps); + <<"external_ipv4s">> -> + {ok, ObjectIps} = dog_group:get_external_ipv4s_by_id(Id), + jsx:encode(ObjectIps); + <<"external_ipv6s">> -> + {ok, ObjectIps} = dog_group:get_external_ipv6s_by_id(Id), + jsx:encode(ObjectIps); + <<"hosts">> -> + {ok, ObjectHosts} = dog_group:get_hosts_by_id(Id), + jsx:encode(ObjectHosts); + <<"ec2_security_group_ids">> -> + ObjectHosts = dog_group:get_internal_ec2_security_group_ids_by_id(Id), + jsx:encode(ObjectHosts); + <<"hcl">> -> + Handler:to_hcl_by_id(Id) + end + end; + _ -> + case Id@0 of + undefined -> + jsx:encode(Object); + Id -> + case Sub of + undefined -> + jsx:encode(Object); + <<"hcl">> -> + Handler:to_hcl_by_id(Id) + end + end + end, {Json, Req, State}. to_text(Req, State) -> @@ -337,124 +337,127 @@ to_text(Req, State) -> Path = cowboy_req:path(Req), HandlerPath = get_handler_path(Path), Text = - case Id of - undefined -> - error; - Id -> - case HandlerPath of - "profile" -> - case Sub of - undefined -> - case cowboy_req:match_qs([{diff, [], plain}], Req) of - #{diff := plain} -> - case dog_profile:get_by_id(Id) of - {ok, Profile} -> - {ok, T} = dog_profile:to_text(Profile), - T; - {error, Error} -> - Error - end; - #{diff := DiffId} -> - ProfileResult1 = dog_profile:get_by_id(Id), - ProfileResult2 = dog_profile:get_by_id(DiffId), - _ = - case {ProfileResult1, ProfileResult2} of - {{ok, Profile1}, {ok, Profile2}} -> - {ok, T1} = dog_profile:to_text(Profile1), - {ok, T2} = dog_profile:to_text(Profile2), - {ok, Diff} = diff:diff_text(T1, T2), - Diff; - {{error, Error1}, {error, Error2}} -> - dog_common:to_list(Error1) ++ - dog_common:to_list(Error2); - {{error, Error1}, {ok, _Profile2}} -> - Error1; - {{ok, _Profile1}, {error, Error2}} -> - Error2 - end - end, - case cowboy_req:match_qs([{git_diff, [], plain}], Req) of - #{git_diff := plain} -> - ?LOG_DEBUG("here ~n", []), - case dog_profile:get_by_id(Id) of - {ok, Profile_} -> - {ok, T_} = dog_profile:to_text(Profile_), - T_; - {error, Error_} -> - Error_ - end; - #{git_diff := DiffId_} -> - ?LOG_DEBUG("here WHAT~n", []), - ProfileResult1_ = dog_profile:get_by_id(Id), - ProfileResult2_ = dog_profile:get_by_id(DiffId_), - case {ProfileResult1_, ProfileResult2_} of - {{ok, Profile1_}, {ok, Profile2_}} -> - {ok, T1_} = dog_profile:to_text(Profile1_), - {ok, T2_} = dog_profile:to_text(Profile2_), - {ok, Diff_} = diff:diff_git(T1_, T2_), - Diff_; - {{error, Error1_}, {error, Error2_}} -> - dog_common:to_list(Error1_) ++ - dog_common:to_list(Error2_); - {{error, Error1_}, {ok, _Profile2_}} -> - Error1_; - {{ok, _Profile1_}, {error, Error2_}} -> - Error2_ - end - end - end; - "group" -> - case Sub of - undefined -> - error; - _ -> - case cowboy_req:match_qs([{git_diff, [], plain}], Req) of - #{git_diff := plain} -> - case Sub of - <<"iptablesv4">> -> - {_, Iptables} = dog_profile:generate_ipv4_iptables_ruleset_by_group_id( - Id - ), - Iptables; - <<"iptablesv6">> -> - {_, Iptables} = dog_profile:generate_ipv4_iptables_ruleset_by_group_id( - Id - ), - Iptables; - <<"ipsetsv4">> -> - {Ipsets, _} = dog_profile:generate_ipv4_iptables_ruleset_by_group_id( - Id - ), - Ipsets; - <<"ipsetsv6">> -> - {Ipsets, _} = dog_profile:generate_ipv6_iptables_ruleset_by_group_id( - Id - ), - Ipsets - end; - #{git_diff := DiffId_} -> - ProfileResult1_ = dog_profile:get_by_id(Id), - ProfileResult2_ = dog_profile:get_by_id(DiffId_), - case {ProfileResult1_, ProfileResult2_} of - {{ok, Profile1_}, {ok, Profile2_}} -> - {ok, T1_} = dog_profile:to_text(Profile1_), - {ok, T2_} = dog_profile:to_text(Profile2_), - {ok, Diff_} = diff:diff_git(T1_, T2_), - Diff_; - {{error, Error1_}, {error, Error2_}} -> - dog_common:to_list(Error1_) ++ - dog_common:to_list(Error2_); - {{error, Error1_}, {ok, _Profile2_}} -> - Error1_; - {{ok, _Profile1_}, {error, Error2_}} -> - Error2_ - end - end - end; - _ -> - error - end - end, + case Id of + undefined -> + error; + Id -> + case HandlerPath of + "profile" -> + case Sub of + <<"hcl">> -> + RulesetId = dog_ruleset:get_id_by_profile_id(Id), + dog_ruleset_api_v2:to_hcl_by_id(RulesetId); + undefined -> + case cowboy_req:match_qs([{diff, [], plain}], Req) of + #{diff := plain} -> + case dog_profile:get_by_id(Id) of + {ok, Profile} -> + {ok, T} = dog_profile:to_text(Profile), + T; + {error, Error} -> + Error + end; + #{diff := DiffId} -> + ProfileResult1 = dog_profile:get_by_id(Id), + ProfileResult2 = dog_profile:get_by_id(DiffId), + _ = + case {ProfileResult1, ProfileResult2} of + {{ok, Profile1}, {ok, Profile2}} -> + {ok, T1} = dog_profile:to_text(Profile1), + {ok, T2} = dog_profile:to_text(Profile2), + {ok, Diff} = diff:diff_text(T1, T2), + Diff; + {{error, Error1}, {error, Error2}} -> + dog_common:to_list(Error1) ++ + dog_common:to_list(Error2); + {{error, Error1}, {ok, _Profile2}} -> + Error1; + {{ok, _Profile1}, {error, Error2}} -> + Error2 + end + end, + case cowboy_req:match_qs([{git_diff, [], plain}], Req) of + #{git_diff := plain} -> + ?LOG_DEBUG("here ~n", []), + case dog_profile:get_by_id(Id) of + {ok, Profile_} -> + {ok, T_} = dog_profile:to_text(Profile_), + T_; + {error, Error_} -> + Error_ + end; + #{git_diff := DiffId_} -> + ?LOG_DEBUG("here WHAT~n", []), + ProfileResult1_ = dog_profile:get_by_id(Id), + ProfileResult2_ = dog_profile:get_by_id(DiffId_), + case {ProfileResult1_, ProfileResult2_} of + {{ok, Profile1_}, {ok, Profile2_}} -> + {ok, T1_} = dog_profile:to_text(Profile1_), + {ok, T2_} = dog_profile:to_text(Profile2_), + {ok, Diff_} = diff:diff_git(T1_, T2_), + Diff_; + {{error, Error1_}, {error, Error2_}} -> + dog_common:to_list(Error1_) ++ + dog_common:to_list(Error2_); + {{error, Error1_}, {ok, _Profile2_}} -> + Error1_; + {{ok, _Profile1_}, {error, Error2_}} -> + Error2_ + end + end + end; + "group" -> + case Sub of + undefined -> + error; + _ -> + case cowboy_req:match_qs([{git_diff, [], plain}], Req) of + #{git_diff := plain} -> + case Sub of + <<"iptablesv4">> -> + {_, Iptables} = dog_profile:generate_ipv4_iptables_ruleset_by_group_id( + Id + ), + Iptables; + <<"iptablesv6">> -> + {_, Iptables} = dog_profile:generate_ipv4_iptables_ruleset_by_group_id( + Id + ), + Iptables; + <<"ipsetsv4">> -> + {Ipsets, _} = dog_profile:generate_ipv4_iptables_ruleset_by_group_id( + Id + ), + Ipsets; + <<"ipsetsv6">> -> + {Ipsets, _} = dog_profile:generate_ipv6_iptables_ruleset_by_group_id( + Id + ), + Ipsets + end; + #{git_diff := DiffId_} -> + ProfileResult1_ = dog_profile:get_by_id(Id), + ProfileResult2_ = dog_profile:get_by_id(DiffId_), + case {ProfileResult1_, ProfileResult2_} of + {{ok, Profile1_}, {ok, Profile2_}} -> + {ok, T1_} = dog_profile:to_text(Profile1_), + {ok, T2_} = dog_profile:to_text(Profile2_), + {ok, Diff_} = diff:diff_git(T1_, T2_), + Diff_; + {{error, Error1_}, {error, Error2_}} -> + dog_common:to_list(Error1_) ++ + dog_common:to_list(Error2_); + {{error, Error1_}, {ok, _Profile2_}} -> + Error1_; + {{ok, _Profile1_}, {error, Error2_}} -> + Error2_ + end + end + end; + _ -> + error + end + end, {Text, Req, State}. delete_resource(Req@0, State) -> @@ -462,12 +465,12 @@ delete_resource(Req@0, State) -> Handler = get_handler_module(Path), Id = cowboy_req:binding(id, Req@0), {Result, Req@1} = - case Handler:delete(Id) of - ok -> - {true, Req@0}; - {error, Error} -> - ErrorReq = cowboy_req:set_resp_body(jsx:encode(Error), Req@0), + case Handler:delete(Id) of + ok -> + {true, Req@0}; + {error, Error} -> + ErrorReq = cowboy_req:set_resp_body(jsx:encode(Error), Req@0), - {false, ErrorReq} - end, + {false, ErrorReq} + end, {Result, Req@1, State}. diff --git a/src/api_handler_v2.erl b/src/api_handler_v2.erl index 9fde5ec..cd3ee2c 100644 --- a/src/api_handler_v2.erl +++ b/src/api_handler_v2.erl @@ -163,6 +163,7 @@ from_post_json(Req@0, State) -> % {error, notfound} -> case Handler:create(Map) of {validation_error, Error} -> + cowboy_req:reply( 409, #{<<"content-type">> => <<"application/json">>}, diff --git a/src/dog_api_router.erl b/src/dog_api_router.erl index 3919db4..57d928e 100644 --- a/src/dog_api_router.erl +++ b/src/dog_api_router.erl @@ -7,117 +7,111 @@ start_link() -> Dispatch = cowboy_router:compile([ {'_', [ - {"/api/external", api_handler, #{}}, - {"/api/external/:id", api_handler, #{}}, - {"/api/external/:id/:sub", api_handler, #{}}, - {"/api/externals", plural_api_handler, #{}}, - {"/api/externals/:sub", plural_api_handler, #{}}, {"/api/V2/external", api_handler_v2, #{}}, {"/api/V2/external/:id", api_handler_v2, #{}}, + {"/api/V2/external/:id", plural_api_handler_v2, #{}}, {"/api/V2/external/:id/:sub", api_handler_v2, #{}}, {"/api/V2/externals", plural_api_handler_v2, #{}}, {"/api/V2/externals/:sub", plural_api_handler_v2, #{}}, + {"/api/V2/fact", api_handler_v2, #{}}, + {"/api/V2/fact/:id", api_handler_v2, #{}}, + {"/api/V2/fact/:id/:sub", api_handler_v2, #{}}, + {"/api/V2/facts", plural_api_handler_v2, #{}}, + {"/api/V2/facts/:sub", plural_api_handler_v2, #{}}, {"/api/V2/file_transfer/:id", dog_file_transfer_api_v2, #{}}, - {"/api/V2/external/:id", plural_api_handler_v2, #{}}, - {"/api/group", api_handler, #{}}, - {"/api/group/:id", api_handler, #{}}, - {"/api/group/:id/:sub", api_handler, #{}}, - {"/api/groups", plural_api_handler, #{}}, - {"/api/groups/:sub", plural_api_handler, #{}}, - {"/api/group", api_handler, #{}}, - {"/api/group/:id", api_handler, #{}}, - {"/api/group/:id/:sub", api_handler, #{}}, - {"/api/groups", plural_api_handler, #{}}, - {"/api/groups/:sub", plural_api_handler, #{}}, - {"/api/V2/ipsets", plural_api_handler_v2, #{}}, {"/api/V2/group", api_handler_v2, #{}}, - {"/api/V2/group/:id", api_handler_v2, #{}}, - {"/api/V2/group/:id/:sub", api_handler_v2, #{}}, - {"/api/V2/groups", plural_api_handler_v2, #{}}, - {"/api/V2/groups/:sub", plural_api_handler_v2, #{}}, {"/api/V2/group", api_handler_v2, #{}}, {"/api/V2/group/:id", api_handler_v2, #{}}, + {"/api/V2/group/:id", api_handler_v2, #{}}, + {"/api/V2/group/:id/:sub", api_handler_v2, #{}}, {"/api/V2/group/:id/:sub", api_handler_v2, #{}}, {"/api/V2/groups", plural_api_handler_v2, #{}}, + {"/api/V2/groups", plural_api_handler_v2, #{}}, + {"/api/V2/groups/:sub", plural_api_handler_v2, #{}}, {"/api/V2/groups/:sub", plural_api_handler_v2, #{}}, - {"/api/healthcheck", healthcheck_api_handler, #{}}, {"/api/V2/host", api_handler_v2, #{}}, {"/api/V2/host/:id", api_handler_v2, #{}}, {"/api/V2/host/:id/:sub", api_handler_v2, #{}}, {"/api/V2/hosts", plural_api_handler_v2, #{}}, {"/api/V2/hosts/:sub", plural_api_handler_v2, #{}}, {"/api/V2/hosts/ips", plural_api_handler_v2, #{}}, - {"/api/host", api_handler, #{}}, - {"/api/host/:id", api_handler, #{}}, - {"/api/host/:id/:sub", api_handler, #{}}, - {"/api/hosts", plural_api_handler, #{}}, - {"/api/hosts/:sub", plural_api_handler, #{}}, - {"/api/hosts/ips", plural_api_handler, #{}}, - {"/api/link", api_handler, #{}}, - {"/api/link/:id", api_handler, #{}}, - {"/api/link/:id/:sub", api_handler, #{}}, - {"/api/links", plural_api_handler, #{}}, - {"/api/links/:sub", plural_api_handler, #{}}, + {"/api/V2/ipsets", plural_api_handler_v2, #{}}, {"/api/V2/link", api_handler_v2, #{}}, {"/api/V2/link/:id", api_handler_v2, #{}}, {"/api/V2/link/:id/:sub", api_handler_v2, #{}}, {"/api/V2/links", plural_api_handler_v2, #{}}, {"/api/V2/links/:sub", plural_api_handler_v2, #{}}, - {"/api/profile", api_handler, #{}}, - {"/api/profile/:id", api_handler, #{}}, - {"/api/profile/:id/:sub", api_handler, #{}}, - {"/api/profiles", plural_api_handler, #{}}, - {"/api/profiles/:sub", plural_api_handler, #{}}, {"/api/V2/profile", api_handler_v2, #{}}, {"/api/V2/profile/:id", api_handler_v2, #{}}, {"/api/V2/profile/:id/:sub", api_handler_v2, #{}}, {"/api/V2/profiles", plural_api_handler_v2, #{}}, {"/api/V2/profiles/:sub", plural_api_handler_v2, #{}}, - {"/api/ruleset", api_handler, #{}}, - {"/api/ruleset/:id", api_handler, #{}}, - {"/api/ruleset/:id/:sub", api_handler, #{}}, - {"/api/rulesets", plural_api_handler, #{}}, - {"/api/rulesets/:sub", plural_api_handler, #{}}, {"/api/V2/ruleset", api_handler_v2, #{}}, {"/api/V2/ruleset/:id", api_handler_v2, #{}}, {"/api/V2/ruleset/:id/:sub", api_handler_v2, #{}}, {"/api/V2/rulesets", plural_api_handler_v2, #{}}, {"/api/V2/rulesets/:sub", plural_api_handler_v2, #{}}, - {"/api/publish", publish_api_handler, #{}}, - {"/api/service", api_handler, #{}}, - {"/api/service/:id", api_handler, #{}}, - {"/api/service/:id/:sub", api_handler, #{}}, - {"/api/services", plural_api_handler, #{}}, - {"/api/services/:sub", plural_api_handler, #{}}, {"/api/V2/service", api_handler_v2, #{}}, {"/api/V2/service/:id", api_handler_v2, #{}}, {"/api/V2/service/:id/:sub", api_handler_v2, #{}}, {"/api/V2/services", plural_api_handler_v2, #{}}, {"/api/V2/services/:sub", plural_api_handler_v2, #{}}, - {"/api/zone/", api_handler, #{}}, - {"/api/zone/:id", api_handler, #{}}, - {"/api/zone/:id/:sub", api_handler, #{}}, - {"/api/zones", plural_api_handler, #{}}, - {"/api/zones/:sub", plural_api_handler, #{}}, - {"/api/zones/ips", plural_api_handler, #{}}, {"/api/V2/zone/", api_handler_v2, #{}}, {"/api/V2/zone/:id", api_handler_v2, #{}}, {"/api/V2/zone/:id/:sub", api_handler_v2, #{}}, {"/api/V2/zones", plural_api_handler_v2, #{}}, {"/api/V2/zones/:sub", plural_api_handler_v2, #{}}, {"/api/V2/zones/ips", plural_api_handler_v2, #{}}, + {"/api/external", api_handler, #{}}, + {"/api/external/:id", api_handler, #{}}, + {"/api/external/:id/:sub", api_handler, #{}}, + {"/api/externals", plural_api_handler, #{}}, + {"/api/externals/:sub", plural_api_handler, #{}}, {"/api/fact", api_handler, #{}}, {"/api/fact/:id", api_handler, #{}}, {"/api/fact/:id/:sub", api_handler, #{}}, {"/api/facts", plural_api_handler, #{}}, {"/api/facts/:sub", plural_api_handler, #{}}, - {"/api/V2/fact", api_handler_v2, #{}}, - {"/api/V2/fact/:id", api_handler_v2, #{}}, - {"/api/V2/fact/:id/:sub", api_handler_v2, #{}}, - {"/api/V2/facts", plural_api_handler_v2, #{}}, - {"/api/V2/facts/:sub", plural_api_handler_v2, #{}}, + {"/api/group", api_handler, #{}}, + {"/api/group/:id", api_handler, #{}}, + {"/api/group/:id/:sub", api_handler, #{}}, + {"/api/groups", plural_api_handler, #{}}, + {"/api/groups/:sub", plural_api_handler, #{}}, {"/api/hcl/:sub", hcl_api_handler, #{}}, - {"/api/V2/hcl/:sub", hcl_api_handler_v2, #{}} + {"/api/healthcheck", healthcheck_api_handler, #{}}, + {"/api/host", api_handler, #{}}, + {"/api/host/:id", api_handler, #{}}, + {"/api/host/:id/:sub", api_handler, #{}}, + {"/api/hosts", plural_api_handler, #{}}, + {"/api/hosts/:sub", plural_api_handler, #{}}, + {"/api/hosts/ips", plural_api_handler, #{}}, + {"/api/link", api_handler, #{}}, + {"/api/link/:id", api_handler, #{}}, + {"/api/link/:id/:sub", api_handler, #{}}, + {"/api/links", plural_api_handler, #{}}, + {"/api/links/:sub", plural_api_handler, #{}}, + {"/api/profile", api_handler, #{}}, + {"/api/profile/:id", api_handler, #{}}, + {"/api/profile/:id/:sub", api_handler, #{}}, + {"/api/profiles", plural_api_handler, #{}}, + {"/api/profiles/:sub", plural_api_handler, #{}}, + {"/api/publish", publish_api_handler, #{}}, + {"/api/ruleset", api_handler, #{}}, + {"/api/ruleset/:id", api_handler, #{}}, + {"/api/ruleset/:id/:sub", api_handler, #{}}, + {"/api/rulesets", plural_api_handler, #{}}, + {"/api/rulesets/:sub", plural_api_handler, #{}}, + {"/api/service", api_handler, #{}}, + {"/api/service/:id", api_handler, #{}}, + {"/api/service/:id/:sub", api_handler, #{}}, + {"/api/services", plural_api_handler, #{}}, + {"/api/services/:sub", plural_api_handler, #{}}, + {"/api/zone/", api_handler, #{}}, + {"/api/zone/:id", api_handler, #{}}, + {"/api/zone/:id/:sub", api_handler, #{}}, + {"/api/zones", plural_api_handler, #{}}, + {"/api/zones/:sub", plural_api_handler, #{}}, + {"/api/zones/ips", plural_api_handler, #{}} ]} ]), {ok, _} = cowboy:start_clear( diff --git a/src/dog_profile.erl b/src/dog_profile.erl index 9527221..5ce7c32 100644 --- a/src/dog_profile.erl +++ b/src/dog_profile.erl @@ -36,6 +36,8 @@ get_role_groups_in_profile/1, get_zone_groups_in_profile/1, init/0, + to_hcl/1, + to_hcl_by_id/1, to_text/1, where_used/1 ]). @@ -1291,3 +1293,12 @@ split(String, Delimiter, all) -> split(String, Delimiter). split(String, Delimiter) -> re:split(String, Delimiter, [{return, list}]). + +-spec to_hcl_by_id(ProfileId :: iolist()) -> iolist(). +to_hcl_by_id(ProfileId) -> + RulesetId = dog_ruleset:get_id_by_profile_id(ProfileId), + dog_ruleset_api_v2:to_hcl_by_id(RulesetId). + +-spec to_hcl(Profile :: map()) -> binary(). +to_hcl(Profile) -> + dog_profile_api_v2:to_hcl(Profile). diff --git a/src/dog_ruleset.erl b/src/dog_ruleset.erl index 65f72e6..80c009b 100644 --- a/src/dog_ruleset.erl +++ b/src/dog_ruleset.erl @@ -23,6 +23,8 @@ all/0, all_active/0, init/0, + to_hcl/1, + to_hcl_by_id/1, to_text/1, where_used/1 ]). @@ -317,3 +319,11 @@ get_id_by_profile_id(ProfileId) -> -spec get_schema() -> binary(). get_schema() -> dog_json_schema:get_file(?VALIDATION_TYPE). + +-spec to_hcl_by_id(RulesetId :: iolist()) -> iolist(). +to_hcl_by_id(RulesetId) -> + dog_ruleset_api_v2:to_hcl_by_id(RulesetId). + +-spec to_hcl(Ruleset :: map()) -> binary(). +to_hcl(Ruleset) -> + dog_ruleset_api_v2:to_hcl(Ruleset). diff --git a/test/group.json b/test/group.json index d1dd764..59748fb 100644 --- a/test/group.json +++ b/test/group.json @@ -1,5 +1,6 @@ { "name":"drewtest", "profile_name":"test", -"profile_version":"latest" +"profile_version":"latest", +"ec2_security_group_ids":[] }