Skip to content

Commit

Permalink
more hcl endpoints: /api/V2/*/ID/hcl
Browse files Browse the repository at this point in the history
  • Loading branch information
dgulinobw committed Mar 2, 2024
1 parent bcf097c commit 96a7116
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/dog_api_router.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ start_link() ->
{"/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/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, #{}},
Expand Down Expand Up @@ -92,11 +94,13 @@ start_link() ->
{"/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, #{}},
Expand Down
10 changes: 7 additions & 3 deletions src/dog_common.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
inverse_map_of_lists/1,
create_hash/1,
eel_test/0,
format_value/1,
format_var/1,
format_vars/1,
list_of_maps_to_map/2,
lmm/2,
Expand Down Expand Up @@ -212,13 +214,15 @@ format_vars(Vars) ->
maps:from_list(VarsList).

format_var({Key,Value}) ->
Value2 =case is_list(Value) of
{Key, format_value(Value)}.

format_value(Value) ->
case is_list(Value) of
false ->
string:replace(string:replace(io_lib:format("~p",[Value]),"<<",""),">>","");
true ->
ListValues = lists:map(fun(L) ->
string:replace(string:replace(io_lib:format("~p",[L]),"<<",""),">>","")
end, Value),
"[" ++ lists:join(",",ListValues) ++ "]"
end,
{Key, Value2}.
end.
2 changes: 0 additions & 2 deletions src/dog_group_api_v2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,6 @@ to_hcl(Group) ->
"<% _ -> %>"
" vars = jsonencode({\n"
"<%= lists:map(fun({Key,Value}) -> %>"
%"<% Value2 = dog_group_api_v2:format_var(Value) %>"
%"<% Value2 = string:replace(string:replace(io_lib:format(\"~p\",[Value]),\"<<\",\"\"),\">>\",\"\"), %>"
" <%= Key .%> = <%= Value .%> \n"
"<% end, maps:to_list(Vars)) .%>"
" })\n"
Expand Down
15 changes: 2 additions & 13 deletions src/dog_host_api_v2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ to_hcl(Host) ->
'Group' => maps:get(<<"group">>, Host),
'HostKey' => maps:get(<<"hostkey">>, Host),
'Location' => maps:get(<<"location">>, Host),
'Vars' => maps:get(<<"vars">>, Host,[]),
'Vars' => dog_common:format_vars(maps:get(<<"vars">>, Host,[])),
'Provider' => <<"dog">>
},
{ok, Snapshot} = eel:compile(<<
Expand All @@ -221,8 +221,7 @@ to_hcl(Host) ->
"<% _ -> %>"
" vars = jsonencode({\n"
"<%= lists:map(fun({Key,Value}) -> %>"
"<% Value2 = format_var(Value) %>"
" <%= Key .%> = <%= Value2 .%> \n"
" <%= Key .%> = <%= Value .%> \n"
"<% end, maps:to_list(Vars)) .%>"
" })\n"
"<% end .%>"
Expand All @@ -231,13 +230,3 @@ to_hcl(Host) ->
{ok, RenderSnapshot} = eel_renderer:render(Bindings, Snapshot),
{IoData, _} = {eel_evaluator:eval(RenderSnapshot), RenderSnapshot},
erlang:iolist_to_binary(IoData).

format_var(Var) ->
case is_list(Var) of
false ->
string:replace(string:replace(io_lib:format("~p",[Var]),"<<",""),">>","");
true ->
lists:map(fun(L) ->
string:replace(string:replace(io_lib:format("~p",[L]),"<<",""),">>","")
end, Var)
end.
63 changes: 63 additions & 0 deletions src/dog_link_api_v2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
get_all/0,
get_by_id/1,
get_by_name/1,
to_hcl/1,
to_hcl_by_id/1,
update/2
]).

Expand Down Expand Up @@ -176,3 +178,64 @@ update(Id, UpdateMap) ->
{error, Error} ->
{false, Error}
end.

-spec to_hcl_by_id(LinkId :: iolist()) -> iolist().
to_hcl_by_id(LinkId) ->
{ok, Link} = get_by_id(LinkId),
to_hcl(Link).

-spec to_hcl(Link :: map()) -> binary().
to_hcl(Link) ->
DogConnection = maps:get(<<"connection">>,Link),
SSLOptions = maps:get(<<"ssl_options">>,DogConnection),
Bindings = #{
'TerraformName' => dog_common:to_terraform_name(maps:get(<<"name">>, Link)),
'Environment' => <<"qa">>,
'AddressHandling' => maps:get(<<"address_handling">>,Link),
'ConnectionApiPort' => maps:get(<<"api_port">>,DogConnection),
'ConnectionHost' => maps:get(<<"host">>,DogConnection),
'ConnectionPassword' => maps:get(<<"password">>,DogConnection),
'ConnectionPort' => maps:get(<<"port">>,DogConnection),
'ConnectionSSLOptionsCaCertFile' => maps:get(<<"cacertfile">>,SSLOptions),
'ConnectionSSLOptionsCertFile' => maps:get(<<"certfile">>,SSLOptions),
'ConnectionSSLOptionsFailIfNoPeerCert' => maps:get(<<"fail_if_no_peer_cert">>,SSLOptions),
'ConnectionSSLOptionsKeyFile' => maps:get(<<"keyfile">>,SSLOptions),
'ConnectionSSLOptionsServerNameIndication' => maps:get(<<"server_name_indication">>,SSLOptions),
'ConnectionSSLOptionsVerify' => maps:get(<<"verify">>,SSLOptions),
'ConnectionUser' => maps:get(<<"user">>,DogConnection),
'ConnectionVirtualHost' => maps:get(<<"virtual_host">>,DogConnection),
'ConnectionType' => maps:get(<<"connection_type">>,Link),
'Direction' => maps:get(<<"direction">>,Link),
'Enabled' => maps:get(<<"enabled">>,Link),
'Name' => maps:get(<<"name">>,Link)
},
{ok, Snapshot} = eel:compile(<<
"resource \"dog_link\" \"<%= TerraformName .%>\" {\n"
" address_handling = \"<%= AddressHandling .%>\"\n"
" dog_connection = {\n"
" api_port = <%= ConnectionApiPort .%>\n"
" host = \"<%= ConnectionHost .%>\"\n"
" password = \"<%= ConnectionPassword .%>\"\n"
" port = <%= ConnectionPort .%>\n"
" ssl_options = {\n"
" cacertfile = \"<%= ConnectionSSLOptionsCaCertFile .%>\"\n"
" certfile = \"<%= ConnectionSSLOptionsCertFile .%>\"\n"
" fail_if_no_peer_cert = <%= ConnectionSSLOptionsFailIfNoPeerCert .%>\n"
" keyfile = \"<%= ConnectionSSLOptionsKeyFile .%>\"\n"
" server_name_indication = \"<%= ConnectionSSLOptionsServerNameIndication .%>\"\n"
" verify = \"<%= ConnectionSSLOptionsVerify .%>\"\n"
" }\n"
" user = \"<%= ConnectionUser .%>\"\n"
" virtual_host = \"<%= ConnectionVirtualHost .%>\"\n"
" }\n"
" connection_type = \"<%= ConnectionType .%>\"\n"
" direction = \"<%= Direction .%>\"\n"
" enabled = <%= Enabled .%>\n"
" name = \"<%= Name .%>\"\n"
" provider = dog.<%= Environment .%>\n"
"}\n"
"\n"
>>),
{ok, RenderSnapshot} = eel_renderer:render(Bindings, Snapshot),
{IoData, _} = {eel_evaluator:eval(RenderSnapshot), RenderSnapshot},
erlang:iolist_to_binary(IoData).
25 changes: 25 additions & 0 deletions src/dog_profile_api_v2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
get_by_name/1,
update/2,
update/3,
to_hcl/1,
to_hcl_by_id/1,
where_used/1
]).

Expand Down Expand Up @@ -209,3 +211,26 @@ update_in_place(Id, UpdateMap) ->
{error, Error} ->
{false, Error}
end.

-spec to_hcl_by_id(ProfileId :: iolist()) -> iolist().
to_hcl_by_id(ProfileId) ->
{ok, Profile} = get_by_id(ProfileId),
to_hcl(Profile).

-spec to_hcl(Profile :: map()) -> binary().
to_hcl(Profile) ->
Bindings = #{
'TerraformName' => dog_common:to_terraform_name(maps:get(<<"name">>, Profile)),
'Name' => maps:get(<<"name">>, Profile),
'Environment' => <<"qa">>
},
{ok, Snapshot} = eel:compile(<<
"resource \"dog_profile\" \"<%= TerraformName .%>\" {\n"
" name = \"<%= Name .%>\"\n"
" provider = dog.<%= Environment .%>\n"
"}\n"
"\n"
>>),
{ok, RenderSnapshot} = eel_renderer:render(Bindings, Snapshot),
{IoData, _} = {eel_evaluator:eval(RenderSnapshot), RenderSnapshot},
erlang:iolist_to_binary(IoData).
31 changes: 31 additions & 0 deletions src/dog_zone_api_v2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
get_by_id/1,
get_by_name/1,
get_schema/0,
to_hcl/1,
to_hcl_by_id/1,
update/2
]).

Expand Down Expand Up @@ -125,3 +127,32 @@ update(Id, UpdateMap@0) ->
-spec get_schema() -> binary().
get_schema() ->
dog_json_schema:get_file(?VALIDATION_TYPE).

-spec to_hcl_by_id(ZoneId :: iolist()) -> iolist().
to_hcl_by_id(ZoneId) ->
{ok, Zone} = get_by_id(ZoneId),
to_hcl(Zone).

-spec to_hcl(Zone :: map()) -> binary().
to_hcl(Zone) ->
Bindings = #{
'TerraformName' => dog_common:to_terraform_name(maps:get(<<"name">>, Zone)),
'Name' => maps:get(<<"name">>, Zone),
'Environment' => <<"qa">>,
'IPv4Addresses' =>
dog_common:format_value(maps:get(<<"ipv4_addresses">>,Zone)),
'IPv6Addresses' =>
dog_common:format_value(maps:get(<<"ipv6_addresses">>,Zone))
},
{ok, Snapshot} = eel:compile(<<
"resource \"dog_zone\" \"<%= TerraformName .%>\" {\n"
" name = \"<%= Name .%>\"\n"
" ipv4_addresses = <%= IPv4Addresses .%>\n"
" ipv6_addresses = <%= IPv6Addresses .%>\n"
" provider = dog.<%= Environment .%>\n"
"}\n"
"\n"
>>),
{ok, RenderSnapshot} = eel_renderer:render(Bindings, Snapshot),
{IoData, _} = {eel_evaluator:eval(RenderSnapshot), RenderSnapshot},
erlang:iolist_to_binary(IoData).

0 comments on commit 96a7116

Please sign in to comment.