From 4adfe630b29a85d02cc575533f20623edc935a24 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Tue, 19 Jun 2018 21:24:26 +0100 Subject: 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. --- .../terraform_http_backend_controller.rb | 33 ++++- app/views/terraform_http_backend/index.html.erb | 53 ++++++++ app/views/terraform_http_backend/show.html.erb | 148 +++++++++++++++++++++ config/routes.rb | 11 ++ 4 files changed, 240 insertions(+), 5 deletions(-) create mode 100644 app/views/terraform_http_backend/index.html.erb create mode 100644 app/views/terraform_http_backend/show.html.erb 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 + +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 +. + +%> + +

Terraform States

+ + + + + + + + + + <% @all_states.each do |(state_id, states)| %> + + + + + <% end %> + +
State IDCount
+ + + <%= state_id %> + + + <%= states.count %> +
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 + +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 +. + +%> + + + View all terraform states + + +
+
+

+ Terraform state: + + <%= @state.state_id %> + +

+ +
+ + + + + + + + + + + +
Created at<%= @state.created_at %>
Terraform version<%= @state.data['terraform_version'] %>
+ +
+ +
+ <% historical_states = TerraformState + .where(state_id: @state.state_id) + .order(:id) + .to_a + %> + +

History (<%= pluralize(historical_states.count, 'states') %>)

+ + + + + + + + + + + + <% historical_states.each_with_index do |historical_state, index| %> + <% current = (@state.id == historical_state.id) %> + + + + + + + <% end %> + +
#Created at
+ <%= index %> + <% if current %> + (current) + <% end %> + + <%= historical_state.created_at %> + + <% unless current %> + + View + + <% end %> +
+
+
+ +

Modules

+ +<% @state.data.fetch('modules', []).each do |module_data| %> + +

Path: <%= module_data['path'] %>

+ +

Outputs

+ + <% module_data['outputs'].each do |(key, output)| %> + + + + + <% end %> +
<%= key %><%= output['value'] %>
+ +

Resources

+ + <% module_data['resources'].each do |(name, resource)| %> + + + + + <% end %> +
<%= name.gsub('.', '.').html_safe %> + + + + + + + + + <% resource['primary']['attributes'].each do |(name, value)| %> + + + + + <% end %> + +
KeyValue
<%= name %><%= value %>
+
+<% end %> diff --git a/config/routes.rb b/config/routes.rb index f6ea63b..3c303bc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -32,7 +32,18 @@ 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', -- cgit v1.2.3