aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Baines <mail@cbaines.net>2018-06-19 21:24:26 +0100
committerChristopher Baines <mail@cbaines.net>2018-06-19 21:47:20 +0100
commit4adfe630b29a85d02cc575533f20623edc935a24 (patch)
treebd91a510025170d1ec13f25c97028245ff540ee9
parent9d24246300fc0390b0c9719251550d124388c1c3 (diff)
downloadgovuk-mini-environment-admin-4adfe630b29a85d02cc575533f20623edc935a24.tar
govuk-mini-environment-admin-4adfe630b29a85d02cc575533f20623edc935a24.tar.gz
Display Terraform states
Add an index route, and a route to access a historical Terraform state by index. This also makes the ordering explicit, and fixes an issue with wrapped parameters.
-rw-r--r--app/controllers/terraform_http_backend_controller.rb33
-rw-r--r--app/views/terraform_http_backend/index.html.erb53
-rw-r--r--app/views/terraform_http_backend/show.html.erb148
-rw-r--r--config/routes.rb11
4 files changed, 240 insertions, 5 deletions
diff --git a/app/controllers/terraform_http_backend_controller.rb b/app/controllers/terraform_http_backend_controller.rb
index e4d8c54..4d11961 100644
--- a/app/controllers/terraform_http_backend_controller.rb
+++ b/app/controllers/terraform_http_backend_controller.rb
@@ -21,24 +21,47 @@
class TerraformHttpBackendController < ApplicationController
skip_before_action :verify_authenticity_token
+ def index
+ @all_states = TerraformState.all.group_by(&:state_id)
+ end
+
def create
TerraformState.create(
state_id: state_id,
- data: params
+ data: params[:terraform_http_backend]
)
render json: '{ "success": true }', status: 200
end
def show
- state = TerraformState.where(
+ @state = TerraformState.where(
state_id: state_id
- ).last
+ ).order(:id).last
- if state.nil?
+ if @state.nil?
render json: '{}', status: 404
else
- render json: state.data, status: 200
+ render json: @state.data, status: 200
+ end
+ end
+
+ def show_by_index
+ @state = TerraformState.where(
+ state_id: state_id
+ ).order(:id).to_a[params[:index].to_i]
+
+ respond_to do |format|
+ format.html do
+ render :show
+ end
+ format.json do
+ if @state.nil?
+ render json: '{}', status: 404
+ else
+ render json: @state.data, status: 200
+ end
+ end
end
end
diff --git a/app/views/terraform_http_backend/index.html.erb b/app/views/terraform_http_backend/index.html.erb
new file mode 100644
index 0000000..b2e2f92
--- /dev/null
+++ b/app/views/terraform_http_backend/index.html.erb
@@ -0,0 +1,53 @@
+<%#
+
+GOV.UK Mini Environment Admin
+Copyright © 2018 Christopher Baines <mail@cbaines.net>
+
+This file is part of the GOV.UK Mini Environment Admin.
+
+The GOV.UK Mini Environment Admin is free software: you can
+redistribute it and/or modify it under the terms of the GNU Affero
+General Public License as published by the Free Software Foundation,
+either version 3 of the License, or (at your option) any later
+version.
+
+The GOV.UK Mini Environment Admin is distributed in the hope that it
+will be useful, but WITHOUT ANY WARRANTY; without even the implied
+warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+the GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public
+License along with the GOV.UK Mini Environment Admin. If not, see
+<http://www.gnu.org/licenses/>.
+
+%>
+
+<h1>Terraform States</h1>
+
+<table class="table">
+ <thead>
+ <tr>
+ <th>State ID</th>
+ <th>Count</th>
+ </tr>
+ </thead>
+ <tbody>
+ <% @all_states.each do |(state_id, states)| %>
+ <tr>
+ <td style="font-family: monospace;">
+ <a href="<%= terraform_http_backend_show_by_index_path(
+ state_id,
+ states.count - 1
+ )
+ %>">
+
+ <%= state_id %>
+ </a>
+ </td>
+ <td>
+ <%= states.count %>
+ </td>
+ </tr>
+ <% end %>
+ </tbody>
+</table>
diff --git a/app/views/terraform_http_backend/show.html.erb b/app/views/terraform_http_backend/show.html.erb
new file mode 100644
index 0000000..e167541
--- /dev/null
+++ b/app/views/terraform_http_backend/show.html.erb
@@ -0,0 +1,148 @@
+<%#
+
+GOV.UK Mini Environment Admin
+Copyright © 2018 Christopher Baines <mail@cbaines.net>
+
+This file is part of the GOV.UK Mini Environment Admin.
+
+The GOV.UK Mini Environment Admin is free software: you can
+redistribute it and/or modify it under the terms of the GNU Affero
+General Public License as published by the Free Software Foundation,
+either version 3 of the License, or (at your option) any later
+version.
+
+The GOV.UK Mini Environment Admin is distributed in the hope that it
+will be useful, but WITHOUT ANY WARRANTY; without even the implied
+warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+the GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public
+License along with the GOV.UK Mini Environment Admin. If not, see
+<http://www.gnu.org/licenses/>.
+
+%>
+
+<a class="pull-right" href="<%= terraform_states_path %>">
+ View all terraform states
+</a>
+
+<div class="row">
+ <div class="col-md-8">
+ <h1>
+ Terraform state:
+ <span style="font-family: monospace;">
+ <%= @state.state_id %>
+ </span>
+ </h1>
+
+ <br>
+ <table class="table">
+ <tbody>
+ <tr>
+ <td>Created at</td>
+ <td><%= @state.created_at %></td>
+ </tr>
+ <tr>
+ <td>Terraform version</td>
+ <td><%= @state.data['terraform_version'] %></td>
+ </tr>
+ </tbody>
+ </table>
+
+ </div>
+
+ <div class="col-md-4" style="max-height: 400px; overflow-y: auto;">
+ <% historical_states = TerraformState
+ .where(state_id: @state.state_id)
+ .order(:id)
+ .to_a
+ %>
+
+ <h3>History (<%= pluralize(historical_states.count, 'states') %>)</h3>
+
+ <table class="table">
+ <thead>
+ <tr>
+ <th>#</th>
+ <th>Created at</th>
+ <th></th>
+ </tr>
+ </thead>
+ <tbody>
+
+ <% historical_states.each_with_index do |historical_state, index| %>
+ <% current = (@state.id == historical_state.id) %>
+
+ <tr>
+ <td>
+ <%= index %>
+ <% if current %>
+ <strong>(current)</strong>
+ <% end %>
+ </td>
+ <td>
+ <%= historical_state.created_at %>
+ </td>
+ <td>
+ <% unless current %>
+ <a href="<%= terraform_http_backend_show_by_index_path(
+ historical_state.state_id,
+ index
+ )
+ %>">
+ View
+ </a>
+ <% end %>
+ </td>
+ </tr>
+ <% end %>
+ </tbody>
+ </table>
+ </div>
+</div>
+
+<h2>Modules</h2>
+
+<% @state.data.fetch('modules', []).each do |module_data| %>
+
+ <h3>Path: <%= module_data['path'] %></h3>
+
+ <h3>Outputs</h3>
+ <table class="table">
+ <% module_data['outputs'].each do |(key, output)| %>
+ <tr>
+ <td><%= key %></td>
+ <td><%= output['value'] %></td>
+ </tr>
+ <% end %>
+ </table>
+
+ <h3>Resources</h3>
+ <table class="table">
+ <% module_data['resources'].each do |(name, resource)| %>
+ <tr>
+ <td><%= name.gsub('.', '.<wbr/>').html_safe %></td>
+ <td>
+ <table class="table" style="table-layout: fixed;">
+ <thead>
+ <tr>
+ <th>Key</th>
+ <th>Value</th>
+ </tr>
+ </thead>
+ <tbody>
+ <% resource['primary']['attributes'].each do |(name, value)| %>
+ <tr>
+ <td class="col-md-6"><strong><%= name %></strong></td>
+ <td class="col-md-6"
+ style="white-space: pre-wrap; overflow-wrap: break-word; word-break: break-all;"
+ ><%= value %></td>
+ </tr>
+ <% end %>
+ </tbody>
+ </table>
+ </td>
+ </tr>
+ <% end %>
+ </table>
+<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index f6ea63b..3c303bc 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -32,8 +32,19 @@ Rails.application.routes.draw do
end
end
+ get(
+ 'terraform_states',
+ to: 'terraform_http_backend#index',
+ as: 'terraform_states'
+ )
scope :terraform_http_backend do
get(
+ '*state_id/history/*index',
+ to: 'terraform_http_backend#show_by_index',
+ as: 'terraform_http_backend_show_by_index'
+ )
+
+ get(
'*state_id',
to: 'terraform_http_backend#show',
format: false,