Browse Source

rename lxd_host to host as I plan a facade that will combine host and the lxd models for a host

master
Georg Hopp 10 years ago
parent
commit
074f574ec4
  1. 0
      app/assets/javascripts/hosts.coffee
  2. 2
      app/assets/stylesheets/hosts.scss
  3. 2
      app/controllers/application_controller.rb
  4. 8
      app/controllers/dashboard_controller.rb
  5. 89
      app/controllers/hosts_controller.rb
  6. 89
      app/controllers/lxd_hosts_controller.rb
  7. 2
      app/helpers/hosts_helper.rb
  8. 2
      app/helpers/lxd_hosts_helper.rb
  9. 2
      app/models/host.rb
  10. 9
      app/models/lxd/api.rb
  11. 2
      app/views/dashboard/index.html.erb
  12. 8
      app/views/hosts/_form.html.erb
  13. 6
      app/views/hosts/auth.html.erb
  14. 6
      app/views/hosts/edit.html.erb
  15. 29
      app/views/hosts/index.html.erb
  16. 4
      app/views/hosts/index.json.jbuilder
  17. 5
      app/views/hosts/new.html.erb
  18. 14
      app/views/hosts/show.html.erb
  19. 1
      app/views/hosts/show.json.jbuilder
  20. 6
      app/views/lxd_hosts/edit.html.erb
  21. 29
      app/views/lxd_hosts/index.html.erb
  22. 4
      app/views/lxd_hosts/index.json.jbuilder
  23. 5
      app/views/lxd_hosts/new.html.erb
  24. 14
      app/views/lxd_hosts/show.html.erb
  25. 1
      app/views/lxd_hosts/show.json.jbuilder
  26. 6
      config/routes.rb
  27. 6
      db/migrate/20160428191808_rename_lxd_host_to_host.rb
  28. 4
      db/schema.rb
  29. 49
      test/controllers/hosts_controller_test.rb
  30. 49
      test/controllers/lxd_hosts_controller_test.rb
  31. 0
      test/fixtures/hosts.yml
  32. 2
      test/models/host_test.rb

0
app/assets/javascripts/lxd_hosts.coffee → app/assets/javascripts/hosts.coffee

2
app/assets/stylesheets/lxd_hosts.scss → app/assets/stylesheets/hosts.scss

@ -1,3 +1,3 @@
// Place all the styles related to the lxd_hosts controller here.
// Place all the styles related to the hosts controller here.
// They will automatically be included in application.css. // They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/ // You can use Sass (SCSS) here: http://sass-lang.com/

2
app/controllers/application_controller.rb

@ -17,7 +17,7 @@ class ApplicationController < ActionController::Base
# or we remove the current cert completely. # or we remove the current cert completely.
if (@cert.cert.not_after - 1.day + 300) < Time.now if (@cert.cert.not_after - 1.day + 300) < Time.now
@new_cert = @cert.update @new_cert = @cert.update
LxdHost.all.each { |host|
Host.all.each { |host|
host.cert = @cert host.cert = @cert
# add new certificate # add new certificate
cert = Lxd::Certificate.new( cert = Lxd::Certificate.new(

8
app/controllers/dashboard_controller.rb

@ -1,18 +1,18 @@
class DashboardController < ApplicationController class DashboardController < ApplicationController
def index def index
check_cert check_cert
@lxd_hosts = LxdHost.all
@hosts = Host.all
@lxd_hosts.map { |host|
@hosts.map { |host|
host.cert = @cert host.cert = @cert
if host.config.auth == 'untrusted' if host.config.auth == 'untrusted'
session[:return_to] = request.env["REQUEST_URI"] session[:return_to] = request.env["REQUEST_URI"]
redirect_to controller: 'lxd_hosts', action: 'auth', id: host.id
redirect_to controller: 'hosts', action: 'auth', id: host.id
return return
end end
} }
@certificates = Lxd::Certificate.all @lxd_hosts.first.api
@certificates = Lxd::Certificate.all @hosts.first.api
end end
end end
# vim: set et ts=2 sw=2: # vim: set et ts=2 sw=2:

89
app/controllers/hosts_controller.rb

@ -0,0 +1,89 @@
class HostsController < ApplicationController
before_action :set_host,
only: [:auth, :add_key, :show, :edit, :update, :destroy]
# GET /hosts
# GET /hosts.json
def index
@hosts = Host.all
end
# GET /hosts/1
# GET /hosts/1.json
def show
end
# GET /hosts/new
def new
@host = Host.new
end
# GET /hosts/1/edit
def edit
end
# GET /hosts/1/auth
def auth
end
# PATCH/PUT /hosts/1/add_key
def add_key
cert = Lxd::Certificate.new api: @host.api
cert.add params[:hosts][:password]
redirect_to session.delete(:return_to)
end
# POST /hosts
# POST /hosts.json
def create
@host = Host.new(host_params)
respond_to do |format|
if @host.save
format.html { redirect_to @host, notice: 'Host was successfully created.' }
format.json { render :show, status: :created, location: @host }
else
format.html { render :new }
format.json { render json: @host.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /hosts/1
# PATCH/PUT /hosts/1.json
def update
respond_to do |format|
if @host.update(host_params)
format.html { redirect_to @host, notice: 'Host was successfully updated.' }
format.json { render :show, status: :ok, location: @host }
else
format.html { render :edit }
format.json { render json: @host.errors, status: :unprocessable_entity }
end
end
end
# DELETE /hosts/1
# DELETE /hosts/1.json
def destroy
@host.destroy
respond_to do |format|
format.html { redirect_to hosts_url, notice: 'Host was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_host
check_cert
@host = Host.find(params[:id])
@host.cert = @cert
end
# Never trust parameters from the scary internet, only allow the white list through.
def host_params
params.require(:host).permit(:name, :uri, :password, :password_confirmation)
end
end
# vim: set et ts=2 sw=2:

89
app/controllers/lxd_hosts_controller.rb

@ -1,89 +0,0 @@
class LxdHostsController < ApplicationController
before_action :set_lxd_host,
only: [:auth, :add_key, :show, :edit, :update, :destroy]
# GET /lxd_hosts
# GET /lxd_hosts.json
def index
@lxd_hosts = LxdHost.all
end
# GET /lxd_hosts/1
# GET /lxd_hosts/1.json
def show
end
# GET /lxd_hosts/new
def new
@lxd_host = LxdHost.new
end
# GET /lxd_hosts/1/edit
def edit
end
# GET /lxd_hosts/1/auth
def auth
end
# PATCH/PUT /lxd_hosts/1/add_key
def add_key
cert = Lxd::Certificate.new api: @lxd_host.api
cert.add params[:lxd_hosts][:password]
redirect_to session.delete(:return_to)
end
# POST /lxd_hosts
# POST /lxd_hosts.json
def create
@lxd_host = LxdHost.new(lxd_host_params)
respond_to do |format|
if @lxd_host.save
format.html { redirect_to @lxd_host, notice: 'Lxd host was successfully created.' }
format.json { render :show, status: :created, location: @lxd_host }
else
format.html { render :new }
format.json { render json: @lxd_host.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /lxd_hosts/1
# PATCH/PUT /lxd_hosts/1.json
def update
respond_to do |format|
if @lxd_host.update(lxd_host_params)
format.html { redirect_to @lxd_host, notice: 'Lxd host was successfully updated.' }
format.json { render :show, status: :ok, location: @lxd_host }
else
format.html { render :edit }
format.json { render json: @lxd_host.errors, status: :unprocessable_entity }
end
end
end
# DELETE /lxd_hosts/1
# DELETE /lxd_hosts/1.json
def destroy
@lxd_host.destroy
respond_to do |format|
format.html { redirect_to lxd_hosts_url, notice: 'Lxd host was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_lxd_host
check_cert
@lxd_host = LxdHost.find(params[:id])
@lxd_host.cert = @cert
end
# Never trust parameters from the scary internet, only allow the white list through.
def lxd_host_params
params.require(:lxd_host).permit(:name, :uri, :password, :password_confirmation)
end
end
# vim: set et ts=2 sw=2:

2
app/helpers/hosts_helper.rb

@ -0,0 +1,2 @@
module HostsHelper
end

2
app/helpers/lxd_hosts_helper.rb

@ -1,2 +0,0 @@
module LxdHostsHelper
end

2
app/models/lxd_host.rb → app/models/host.rb

@ -1,4 +1,4 @@
class LxdHost < ActiveRecord::Base
class Host < ActiveRecord::Base
def cert=(cert) def cert=(cert)
@cert = cert @cert = cert
end end

9
app/models/lxd/api.rb

@ -1,15 +1,18 @@
module Lxd::API module Lxd::API
def self.get host, certificate def self.get host, certificate
begin
uri = URI.parse host.uri uri = URI.parse host.uri
con = Net::HTTP.new uri.host, uri.port
rescue
end
con = Net::HTTP.new uri.host, uri.port ? uri.port : 8443
con.use_ssl = true con.use_ssl = true
con.cert = OpenSSL::X509::Certificate.new certificate.cert con.cert = OpenSSL::X509::Certificate.new certificate.cert
con.key = OpenSSL::PKey::RSA.new certificate.key con.key = OpenSSL::PKey::RSA.new certificate.key
con.verify_mode = OpenSSL::SSL::VERIFY_NONE con.verify_mode = OpenSSL::SSL::VERIFY_NONE
resp = self.call con, Net::HTTP::Get.new('/') resp = self.call con, Net::HTTP::Get.new('/')
raise "unsupported api version" unless resp['metadata'].include? '/1.0'
Lxd::API::V1_0.new con
return Lxd::API::V1_0.new con if resp['metadata'].include? '/1.0'
raise "unsupported api version"
end end
def self.call con, req def self.call con, req

2
app/views/dashboard/index.html.erb

@ -1,7 +1,7 @@
<h1>Dashboard#index</h1> <h1>Dashboard#index</h1>
<p><%= @cert.cert_fpr %></p> <p><%= @cert.cert_fpr %></p>
<p>Serial: <%= @cert.cert.serial %></p> <p>Serial: <%= @cert.cert.serial %></p>
<% @lxd_hosts.each do |host| -%>
<% @hosts.each do |host| -%>
<p><%= host.config.inspect %></p> <p><%= host.config.inspect %></p>
<% end -%> <% end -%>
<% @certificates.each do |cert| -%> <% @certificates.each do |cert| -%>

8
app/views/lxd_hosts/_form.html.erb → app/views/hosts/_form.html.erb

@ -1,10 +1,10 @@
<%= form_for(@lxd_host) do |f| %>
<% if @lxd_host.errors.any? %>
<%= form_for(@host) do |f| %>
<% if @host.errors.any? %>
<div id="error_explanation"> <div id="error_explanation">
<h2><%= pluralize(@lxd_host.errors.count, "error") %> prohibited this lxd_host from being saved:</h2>
<h2><%= pluralize(@host.errors.count, "error") %> prohibited this host from being saved:</h2>
<ul> <ul>
<% @lxd_host.errors.full_messages.each do |message| %>
<% @host.errors.full_messages.each do |message| %>
<li><%= message %></li> <li><%= message %></li>
<% end %> <% end %>
</ul> </ul>

6
app/views/lxd_hosts/auth.html.erb → app/views/hosts/auth.html.erb

@ -1,8 +1,6 @@
<h1>Authenticate Lxd Host: <%= @lxd_host.name %></h1>
<h1>Authenticate Host: <%= @host.name %></h1>
<p>...<%= @data.inspect %></p>
<%= form_for :lxd_hosts, url: { action: "add_key" }, method: 'put' do |f| %>
<%= form_for :hosts, url: { action: "add_key" }, method: 'put' do |f| %>
<div class="field"> <div class="field">
<%= f.label :password %><br> <%= f.label :password %><br>
<%= f.password_field :password %> <%= f.password_field :password %>

6
app/views/hosts/edit.html.erb

@ -0,0 +1,6 @@
<h1>Editing Host</h1>
<%= render 'form' %>
<%= link_to 'Show', @host %> |
<%= link_to 'Back', hosts_path %>

29
app/views/hosts/index.html.erb

@ -0,0 +1,29 @@
<p id="notice"><%= notice %></p>
<h1>Listing Hosts</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Uri</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @hosts.each do |host| %>
<tr>
<td><%= host.name %></td>
<td><%= host.uri %></td>
<td><%= link_to 'Show', host %></td>
<td><%= link_to 'Edit', edit_host_path(host) %></td>
<td><%= link_to 'Destroy', host, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New host', new_host_path %>

4
app/views/hosts/index.json.jbuilder

@ -0,0 +1,4 @@
json.array!(@hosts) do |host|
json.extract! host, :id, :name, :uri
json.url host_url(host, format: :json)
end

5
app/views/hosts/new.html.erb

@ -0,0 +1,5 @@
<h1>New Host</h1>
<%= render 'form' %>
<%= link_to 'Back', hosts_path %>

14
app/views/hosts/show.html.erb

@ -0,0 +1,14 @@
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @host.name %>
</p>
<p>
<strong>Uri:</strong>
<%= @host.uri %>
</p>
<%= link_to 'Edit', edit_host_path(@host) %> |
<%= link_to 'Back', hosts_path %>

1
app/views/hosts/show.json.jbuilder

@ -0,0 +1 @@
json.extract! @host, :id, :name, :uri, :created_at, :updated_at

6
app/views/lxd_hosts/edit.html.erb

@ -1,6 +0,0 @@
<h1>Editing Lxd Host</h1>
<%= render 'form' %>
<%= link_to 'Show', @lxd_host %> |
<%= link_to 'Back', lxd_hosts_path %>

29
app/views/lxd_hosts/index.html.erb

@ -1,29 +0,0 @@
<p id="notice"><%= notice %></p>
<h1>Listing Lxd Hosts</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Uri</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @lxd_hosts.each do |lxd_host| %>
<tr>
<td><%= lxd_host.name %></td>
<td><%= lxd_host.uri %></td>
<td><%= link_to 'Show', lxd_host %></td>
<td><%= link_to 'Edit', edit_lxd_host_path(lxd_host) %></td>
<td><%= link_to 'Destroy', lxd_host, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Lxd host', new_lxd_host_path %>

4
app/views/lxd_hosts/index.json.jbuilder

@ -1,4 +0,0 @@
json.array!(@lxd_hosts) do |lxd_host|
json.extract! lxd_host, :id, :name, :uri
json.url lxd_host_url(lxd_host, format: :json)
end

5
app/views/lxd_hosts/new.html.erb

@ -1,5 +0,0 @@
<h1>New Lxd Host</h1>
<%= render 'form' %>
<%= link_to 'Back', lxd_hosts_path %>

14
app/views/lxd_hosts/show.html.erb

@ -1,14 +0,0 @@
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @lxd_host.name %>
</p>
<p>
<strong>Uri:</strong>
<%= @lxd_host.uri %>
</p>
<%= link_to 'Edit', edit_lxd_host_path(@lxd_host) %> |
<%= link_to 'Back', lxd_hosts_path %>

1
app/views/lxd_hosts/show.json.jbuilder

@ -1 +0,0 @@
json.extract! @lxd_host, :id, :name, :uri, :created_at, :updated_at

6
config/routes.rb

@ -1,9 +1,9 @@
Rails.application.routes.draw do Rails.application.routes.draw do
resources :lxd_hosts
resources :hosts
resources :certificates resources :certificates
get 'lxd_hosts/:id/auth' => 'lxd_hosts#auth'
put 'lxd_hosts/:id/add_key' => 'lxd_hosts#add_key'
get 'hosts/:id/auth' => 'hosts#auth'
put 'hosts/:id/add_key' => 'hosts#add_key'
get 'dashboard/index' get 'dashboard/index'
# The priority is based upon order of creation: first created -> highest priority. # The priority is based upon order of creation: first created -> highest priority.

6
db/migrate/20160428191808_rename_lxd_host_to_host.rb

@ -0,0 +1,6 @@
class RenameLxdHostToHost < ActiveRecord::Migration
def change
rename_table :lxd_hosts, :hosts
end
end
# vim: set ts=2 sw=2:

4
db/schema.rb

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160425195446) do
ActiveRecord::Schema.define(version: 20160428191808) do
create_table "certificates", force: :cascade do |t| create_table "certificates", force: :cascade do |t|
t.text "key" t.text "key"
@ -21,7 +21,7 @@ ActiveRecord::Schema.define(version: 20160425195446) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "lxd_hosts", force: :cascade do |t|
create_table "hosts", force: :cascade do |t|
t.string "name" t.string "name"
t.string "uri" t.string "uri"
t.datetime "created_at", null: false t.datetime "created_at", null: false

49
test/controllers/hosts_controller_test.rb

@ -0,0 +1,49 @@
require 'test_helper'
class HostsControllerTest < ActionController::TestCase
setup do
@host = hosts(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:hosts)
end
test "should get new" do
get :new
assert_response :success
end
test "should create host" do
assert_difference('Host.count') do
post :create, host: { name: @host.name, password: 'secret', password_confirmation: 'secret', uri: @host.uri }
end
assert_redirected_to host_path(assigns(:host))
end
test "should show host" do
get :show, id: @host
assert_response :success
end
test "should get edit" do
get :edit, id: @host
assert_response :success
end
test "should update host" do
patch :update, id: @host, host: { name: @host.name, password: 'secret', password_confirmation: 'secret', uri: @host.uri }
assert_redirected_to host_path(assigns(:host))
end
test "should destroy host" do
assert_difference('Host.count', -1) do
delete :destroy, id: @host
end
assert_redirected_to hosts_path
end
end

49
test/controllers/lxd_hosts_controller_test.rb

@ -1,49 +0,0 @@
require 'test_helper'
class LxdHostsControllerTest < ActionController::TestCase
setup do
@lxd_host = lxd_hosts(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:lxd_hosts)
end
test "should get new" do
get :new
assert_response :success
end
test "should create lxd_host" do
assert_difference('LxdHost.count') do
post :create, lxd_host: { name: @lxd_host.name, password: 'secret', password_confirmation: 'secret', uri: @lxd_host.uri }
end
assert_redirected_to lxd_host_path(assigns(:lxd_host))
end
test "should show lxd_host" do
get :show, id: @lxd_host
assert_response :success
end
test "should get edit" do
get :edit, id: @lxd_host
assert_response :success
end
test "should update lxd_host" do
patch :update, id: @lxd_host, lxd_host: { name: @lxd_host.name, password: 'secret', password_confirmation: 'secret', uri: @lxd_host.uri }
assert_redirected_to lxd_host_path(assigns(:lxd_host))
end
test "should destroy lxd_host" do
assert_difference('LxdHost.count', -1) do
delete :destroy, id: @lxd_host
end
assert_redirected_to lxd_hosts_path
end
end

0
test/fixtures/lxd_hosts.yml → test/fixtures/hosts.yml

2
test/models/lxd_host_test.rb → test/models/host_test.rb

@ -1,6 +1,6 @@
require 'test_helper' require 'test_helper'
class LxdHostTest < ActiveSupport::TestCase
class HostTest < ActiveSupport::TestCase
# test "the truth" do # test "the truth" do
# assert true # assert true
# end # end
Loading…
Cancel
Save