Skip to content

Commit

Permalink
Allow looking up keys in the web ui #264
Browse files Browse the repository at this point in the history
  • Loading branch information
oneiros committed Mar 1, 2024
1 parent 85c89ee commit fdab68b
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 10 deletions.
7 changes: 7 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ def load_environments
authorize! :show, @environment
end

def load_nodes
@nodes = Node.all
@nodes.select! { |n| current_user.may_access?(n) }
@node = Node.find(params[:node_id])
authorize! :show, @node
end

def display_error_page(error)
@error = error
render template: "page/error", status: :internal_server_error
Expand Down
9 changes: 0 additions & 9 deletions app/controllers/keys_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,4 @@ def destroy
redirect_to environment_node_key_path(@environment, @node, @key),
notice: "Value was removed successfully"
end

private

def load_nodes
@nodes = Node.all
@nodes.select! { |n| current_user.may_access?(n) }
@node = Node.find(params[:node_id])
authorize! :show, @node
end
end
18 changes: 18 additions & 0 deletions app/controllers/lookups_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class LookupsController < ApplicationController
before_action :load_environments
before_action :load_nodes
before_action :load_keys

def show
@result = @key.lookup(@node)
end

private

def load_keys
@keys = Key.all_for(@node, environment: @environment)
@keys.select! { |k| current_user.may_access?(k) }
@key = Key.new(environment: @environment, name: params[:key_id])
authorize! :show, @key
end
end
4 changes: 4 additions & 0 deletions app/models/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def lookup_options(node)
@lookup_options ||= load_lookup_options(node)
end

def lookup(node)
hiera_data.lookup(name, facts: node.facts)
end

def to_param
name
end
Expand Down
23 changes: 23 additions & 0 deletions app/views/keys/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<div class="text-end pb-2">
<b>Lookup options:</b>
<span class="badge bg-primary"><%= @key.lookup_options(@node) %></span>
<button type="button" class="btn btn-secondary btn-sm" data-bs-toggle="modal" data-bs-target="#lookup-result">
<%= icon "search" %>
Show lookup result
</button>
</div>
<% index = 0 %>
<% @environment.hierarchies.each do |hierarchy| %>
Expand Down Expand Up @@ -64,3 +68,22 @@
<% end %>
</div>
</div>

<div class="modal fade" id="lookup-result" tabindex="-1" aria-labelledby="lookup-result-label" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="lookup-result-label">Lookup result for <code><%= @key.name %></code></h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<%= turbo_frame_tag "lookup-result-frame", src: environment_node_key_lookup_path(@environment, @node, @key), loading: "lazy" do %>
Loading...
<% end %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@
</a>
</div>
</footer>
<%= yield :modals %>
</body>
</html>
5 changes: 5 additions & 0 deletions app/views/lookups/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= turbo_frame_tag "lookup-result-frame" do %>
<code><pre>
<%= @result.to_yaml.sub(/^---$/, "") %>
</pre></code>
<% end %>
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@

resources :nodes, only: :index, constraints: { id: /.+/ } do
if Rails.configuration.hdm['read_only']
resources :keys, only: [:index, :show]
resources :keys, only: [:index, :show] do
resource :lookup, only: [:show]
end
else
resources :keys, only: [:index, :show, :update, :destroy] do
resources :hierarchies, only: [] do
resources :data_files, only: [] do
resource :value, only: [:update, :destroy]
end
end

resource :lookup, only: [:show]
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions test/controllers/lookups_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class LookupsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

0 comments on commit fdab68b

Please sign in to comment.