Commit 074f574ec4640be723659965c3ca39ebf131cc85
1 parent
680d8a99
rename lxd_host to host as I plan a facade that will combine host and the lxd models for a host
Showing
27 changed files
with
115 additions
and
108 deletions
1 | -// Place all the styles related to the lxd_hosts controller here. | 1 | +// Place all the styles related to the hosts controller here. |
2 | // They will automatically be included in application.css. | 2 | // They will automatically be included in application.css. |
3 | // You can use Sass (SCSS) here: http://sass-lang.com/ | 3 | // You can use Sass (SCSS) here: http://sass-lang.com/ |
@@ -17,7 +17,7 @@ class ApplicationController < ActionController::Base | @@ -17,7 +17,7 @@ class ApplicationController < ActionController::Base | ||
17 | # or we remove the current cert completely. | 17 | # or we remove the current cert completely. |
18 | if (@cert.cert.not_after - 1.day + 300) < Time.now | 18 | if (@cert.cert.not_after - 1.day + 300) < Time.now |
19 | @new_cert = @cert.update | 19 | @new_cert = @cert.update |
20 | - LxdHost.all.each { |host| | 20 | + Host.all.each { |host| |
21 | host.cert = @cert | 21 | host.cert = @cert |
22 | # add new certificate | 22 | # add new certificate |
23 | cert = Lxd::Certificate.new( | 23 | cert = Lxd::Certificate.new( |
1 | class DashboardController < ApplicationController | 1 | class DashboardController < ApplicationController |
2 | def index | 2 | def index |
3 | check_cert | 3 | check_cert |
4 | - @lxd_hosts = LxdHost.all | 4 | + @hosts = Host.all |
5 | 5 | ||
6 | - @lxd_hosts.map { |host| | 6 | + @hosts.map { |host| |
7 | host.cert = @cert | 7 | host.cert = @cert |
8 | if host.config.auth == 'untrusted' | 8 | if host.config.auth == 'untrusted' |
9 | session[:return_to] = request.env["REQUEST_URI"] | 9 | session[:return_to] = request.env["REQUEST_URI"] |
10 | - redirect_to controller: 'lxd_hosts', action: 'auth', id: host.id | 10 | + redirect_to controller: 'hosts', action: 'auth', id: host.id |
11 | return | 11 | return |
12 | end | 12 | end |
13 | } | 13 | } |
14 | 14 | ||
15 | - @certificates = Lxd::Certificate.all @lxd_hosts.first.api | 15 | + @certificates = Lxd::Certificate.all @hosts.first.api |
16 | end | 16 | end |
17 | end | 17 | end |
18 | # vim: set et ts=2 sw=2: | 18 | # vim: set et ts=2 sw=2: |
1 | -class LxdHostsController < ApplicationController | ||
2 | - before_action :set_lxd_host, | 1 | +class HostsController < ApplicationController |
2 | + before_action :set_host, | ||
3 | only: [:auth, :add_key, :show, :edit, :update, :destroy] | 3 | only: [:auth, :add_key, :show, :edit, :update, :destroy] |
4 | 4 | ||
5 | - # GET /lxd_hosts | ||
6 | - # GET /lxd_hosts.json | 5 | + # GET /hosts |
6 | + # GET /hosts.json | ||
7 | def index | 7 | def index |
8 | - @lxd_hosts = LxdHost.all | 8 | + @hosts = Host.all |
9 | end | 9 | end |
10 | 10 | ||
11 | - # GET /lxd_hosts/1 | ||
12 | - # GET /lxd_hosts/1.json | 11 | + # GET /hosts/1 |
12 | + # GET /hosts/1.json | ||
13 | def show | 13 | def show |
14 | end | 14 | end |
15 | 15 | ||
16 | - # GET /lxd_hosts/new | 16 | + # GET /hosts/new |
17 | def new | 17 | def new |
18 | - @lxd_host = LxdHost.new | 18 | + @host = Host.new |
19 | end | 19 | end |
20 | 20 | ||
21 | - # GET /lxd_hosts/1/edit | 21 | + # GET /hosts/1/edit |
22 | def edit | 22 | def edit |
23 | end | 23 | end |
24 | 24 | ||
25 | - # GET /lxd_hosts/1/auth | 25 | + # GET /hosts/1/auth |
26 | def auth | 26 | def auth |
27 | end | 27 | end |
28 | 28 | ||
29 | - # PATCH/PUT /lxd_hosts/1/add_key | 29 | + # PATCH/PUT /hosts/1/add_key |
30 | def add_key | 30 | def add_key |
31 | - cert = Lxd::Certificate.new api: @lxd_host.api | ||
32 | - cert.add params[:lxd_hosts][:password] | 31 | + cert = Lxd::Certificate.new api: @host.api |
32 | + cert.add params[:hosts][:password] | ||
33 | redirect_to session.delete(:return_to) | 33 | redirect_to session.delete(:return_to) |
34 | end | 34 | end |
35 | 35 | ||
36 | - # POST /lxd_hosts | ||
37 | - # POST /lxd_hosts.json | 36 | + # POST /hosts |
37 | + # POST /hosts.json | ||
38 | def create | 38 | def create |
39 | - @lxd_host = LxdHost.new(lxd_host_params) | 39 | + @host = Host.new(host_params) |
40 | 40 | ||
41 | respond_to do |format| | 41 | respond_to do |format| |
42 | - if @lxd_host.save | ||
43 | - format.html { redirect_to @lxd_host, notice: 'Lxd host was successfully created.' } | ||
44 | - format.json { render :show, status: :created, location: @lxd_host } | 42 | + if @host.save |
43 | + format.html { redirect_to @host, notice: 'Host was successfully created.' } | ||
44 | + format.json { render :show, status: :created, location: @host } | ||
45 | else | 45 | else |
46 | format.html { render :new } | 46 | format.html { render :new } |
47 | - format.json { render json: @lxd_host.errors, status: :unprocessable_entity } | 47 | + format.json { render json: @host.errors, status: :unprocessable_entity } |
48 | end | 48 | end |
49 | end | 49 | end |
50 | end | 50 | end |
51 | 51 | ||
52 | - # PATCH/PUT /lxd_hosts/1 | ||
53 | - # PATCH/PUT /lxd_hosts/1.json | 52 | + # PATCH/PUT /hosts/1 |
53 | + # PATCH/PUT /hosts/1.json | ||
54 | def update | 54 | def update |
55 | respond_to do |format| | 55 | respond_to do |format| |
56 | - if @lxd_host.update(lxd_host_params) | ||
57 | - format.html { redirect_to @lxd_host, notice: 'Lxd host was successfully updated.' } | ||
58 | - format.json { render :show, status: :ok, location: @lxd_host } | 56 | + if @host.update(host_params) |
57 | + format.html { redirect_to @host, notice: 'Host was successfully updated.' } | ||
58 | + format.json { render :show, status: :ok, location: @host } | ||
59 | else | 59 | else |
60 | format.html { render :edit } | 60 | format.html { render :edit } |
61 | - format.json { render json: @lxd_host.errors, status: :unprocessable_entity } | 61 | + format.json { render json: @host.errors, status: :unprocessable_entity } |
62 | end | 62 | end |
63 | end | 63 | end |
64 | end | 64 | end |
65 | 65 | ||
66 | - # DELETE /lxd_hosts/1 | ||
67 | - # DELETE /lxd_hosts/1.json | 66 | + # DELETE /hosts/1 |
67 | + # DELETE /hosts/1.json | ||
68 | def destroy | 68 | def destroy |
69 | - @lxd_host.destroy | 69 | + @host.destroy |
70 | respond_to do |format| | 70 | respond_to do |format| |
71 | - format.html { redirect_to lxd_hosts_url, notice: 'Lxd host was successfully destroyed.' } | 71 | + format.html { redirect_to hosts_url, notice: 'Host was successfully destroyed.' } |
72 | format.json { head :no_content } | 72 | format.json { head :no_content } |
73 | end | 73 | end |
74 | end | 74 | end |
75 | 75 | ||
76 | private | 76 | private |
77 | # Use callbacks to share common setup or constraints between actions. | 77 | # Use callbacks to share common setup or constraints between actions. |
78 | - def set_lxd_host | 78 | + def set_host |
79 | check_cert | 79 | check_cert |
80 | - @lxd_host = LxdHost.find(params[:id]) | ||
81 | - @lxd_host.cert = @cert | 80 | + @host = Host.find(params[:id]) |
81 | + @host.cert = @cert | ||
82 | end | 82 | end |
83 | 83 | ||
84 | # Never trust parameters from the scary internet, only allow the white list through. | 84 | # Never trust parameters from the scary internet, only allow the white list through. |
85 | - def lxd_host_params | ||
86 | - params.require(:lxd_host).permit(:name, :uri, :password, :password_confirmation) | 85 | + def host_params |
86 | + params.require(:host).permit(:name, :uri, :password, :password_confirmation) | ||
87 | end | 87 | end |
88 | end | 88 | end |
89 | # vim: set et ts=2 sw=2: | 89 | # vim: set et ts=2 sw=2: |
1 | module Lxd::API | 1 | module Lxd::API |
2 | def self.get host, certificate | 2 | def self.get host, certificate |
3 | - uri = URI.parse host.uri | ||
4 | - con = Net::HTTP.new uri.host, uri.port | 3 | + begin |
4 | + uri = URI.parse host.uri | ||
5 | + rescue | ||
6 | + end | ||
7 | + con = Net::HTTP.new uri.host, uri.port ? uri.port : 8443 | ||
5 | con.use_ssl = true | 8 | con.use_ssl = true |
6 | con.cert = OpenSSL::X509::Certificate.new certificate.cert | 9 | con.cert = OpenSSL::X509::Certificate.new certificate.cert |
7 | con.key = OpenSSL::PKey::RSA.new certificate.key | 10 | con.key = OpenSSL::PKey::RSA.new certificate.key |
8 | con.verify_mode = OpenSSL::SSL::VERIFY_NONE | 11 | con.verify_mode = OpenSSL::SSL::VERIFY_NONE |
9 | 12 | ||
10 | resp = self.call con, Net::HTTP::Get.new('/') | 13 | resp = self.call con, Net::HTTP::Get.new('/') |
11 | - raise "unsupported api version" unless resp['metadata'].include? '/1.0' | ||
12 | - Lxd::API::V1_0.new con | 14 | + return Lxd::API::V1_0.new con if resp['metadata'].include? '/1.0' |
15 | + raise "unsupported api version" | ||
13 | end | 16 | end |
14 | 17 | ||
15 | def self.call con, req | 18 | def self.call con, req |
1 | <h1>Dashboard#index</h1> | 1 | <h1>Dashboard#index</h1> |
2 | <p><%= @cert.cert_fpr %></p> | 2 | <p><%= @cert.cert_fpr %></p> |
3 | <p>Serial: <%= @cert.cert.serial %></p> | 3 | <p>Serial: <%= @cert.cert.serial %></p> |
4 | -<% @lxd_hosts.each do |host| -%> | 4 | +<% @hosts.each do |host| -%> |
5 | <p><%= host.config.inspect %></p> | 5 | <p><%= host.config.inspect %></p> |
6 | <% end -%> | 6 | <% end -%> |
7 | <% @certificates.each do |cert| -%> | 7 | <% @certificates.each do |cert| -%> |
1 | -<%= form_for(@lxd_host) do |f| %> | ||
2 | - <% if @lxd_host.errors.any? %> | 1 | +<%= form_for(@host) do |f| %> |
2 | + <% if @host.errors.any? %> | ||
3 | <div id="error_explanation"> | 3 | <div id="error_explanation"> |
4 | - <h2><%= pluralize(@lxd_host.errors.count, "error") %> prohibited this lxd_host from being saved:</h2> | 4 | + <h2><%= pluralize(@host.errors.count, "error") %> prohibited this host from being saved:</h2> |
5 | 5 | ||
6 | <ul> | 6 | <ul> |
7 | - <% @lxd_host.errors.full_messages.each do |message| %> | 7 | + <% @host.errors.full_messages.each do |message| %> |
8 | <li><%= message %></li> | 8 | <li><%= message %></li> |
9 | <% end %> | 9 | <% end %> |
10 | </ul> | 10 | </ul> |
1 | -<h1>Authenticate Lxd Host: <%= @lxd_host.name %></h1> | 1 | +<h1>Authenticate Host: <%= @host.name %></h1> |
2 | 2 | ||
3 | -<p>...<%= @data.inspect %></p> | ||
4 | - | ||
5 | -<%= form_for :lxd_hosts, url: { action: "add_key" }, method: 'put' do |f| %> | 3 | +<%= form_for :hosts, url: { action: "add_key" }, method: 'put' do |f| %> |
6 | <div class="field"> | 4 | <div class="field"> |
7 | <%= f.label :password %><br> | 5 | <%= f.label :password %><br> |
8 | <%= f.password_field :password %> | 6 | <%= f.password_field :password %> |
app/views/hosts/edit.html.erb
0 → 100644
1 | <p id="notice"><%= notice %></p> | 1 | <p id="notice"><%= notice %></p> |
2 | 2 | ||
3 | -<h1>Listing Lxd Hosts</h1> | 3 | +<h1>Listing Hosts</h1> |
4 | 4 | ||
5 | <table> | 5 | <table> |
6 | <thead> | 6 | <thead> |
@@ -12,13 +12,13 @@ | @@ -12,13 +12,13 @@ | ||
12 | </thead> | 12 | </thead> |
13 | 13 | ||
14 | <tbody> | 14 | <tbody> |
15 | - <% @lxd_hosts.each do |lxd_host| %> | 15 | + <% @hosts.each do |host| %> |
16 | <tr> | 16 | <tr> |
17 | - <td><%= lxd_host.name %></td> | ||
18 | - <td><%= lxd_host.uri %></td> | ||
19 | - <td><%= link_to 'Show', lxd_host %></td> | ||
20 | - <td><%= link_to 'Edit', edit_lxd_host_path(lxd_host) %></td> | ||
21 | - <td><%= link_to 'Destroy', lxd_host, method: :delete, data: { confirm: 'Are you sure?' } %></td> | 17 | + <td><%= host.name %></td> |
18 | + <td><%= host.uri %></td> | ||
19 | + <td><%= link_to 'Show', host %></td> | ||
20 | + <td><%= link_to 'Edit', edit_host_path(host) %></td> | ||
21 | + <td><%= link_to 'Destroy', host, method: :delete, data: { confirm: 'Are you sure?' } %></td> | ||
22 | </tr> | 22 | </tr> |
23 | <% end %> | 23 | <% end %> |
24 | </tbody> | 24 | </tbody> |
@@ -26,4 +26,4 @@ | @@ -26,4 +26,4 @@ | ||
26 | 26 | ||
27 | <br> | 27 | <br> |
28 | 28 | ||
29 | -<%= link_to 'New Lxd host', new_lxd_host_path %> | 29 | +<%= link_to 'New host', new_host_path %> |
app/views/hosts/index.json.jbuilder
0 → 100644
app/views/hosts/new.html.erb
0 → 100644
@@ -2,13 +2,13 @@ | @@ -2,13 +2,13 @@ | ||
2 | 2 | ||
3 | <p> | 3 | <p> |
4 | <strong>Name:</strong> | 4 | <strong>Name:</strong> |
5 | - <%= @lxd_host.name %> | 5 | + <%= @host.name %> |
6 | </p> | 6 | </p> |
7 | 7 | ||
8 | <p> | 8 | <p> |
9 | <strong>Uri:</strong> | 9 | <strong>Uri:</strong> |
10 | - <%= @lxd_host.uri %> | 10 | + <%= @host.uri %> |
11 | </p> | 11 | </p> |
12 | 12 | ||
13 | -<%= link_to 'Edit', edit_lxd_host_path(@lxd_host) %> | | ||
14 | -<%= link_to 'Back', lxd_hosts_path %> | 13 | +<%= link_to 'Edit', edit_host_path(@host) %> | |
14 | +<%= link_to 'Back', hosts_path %> |
app/views/hosts/show.json.jbuilder
0 → 100644
1 | +json.extract! @host, :id, :name, :uri, :created_at, :updated_at |
app/views/lxd_hosts/edit.html.erb
deleted
100644 → 0
app/views/lxd_hosts/new.html.erb
deleted
100644 → 0
1 | -json.extract! @lxd_host, :id, :name, :uri, :created_at, :updated_at |
1 | Rails.application.routes.draw do | 1 | Rails.application.routes.draw do |
2 | - resources :lxd_hosts | 2 | + resources :hosts |
3 | resources :certificates | 3 | resources :certificates |
4 | 4 | ||
5 | - get 'lxd_hosts/:id/auth' => 'lxd_hosts#auth' | ||
6 | - put 'lxd_hosts/:id/add_key' => 'lxd_hosts#add_key' | 5 | + get 'hosts/:id/auth' => 'hosts#auth' |
6 | + put 'hosts/:id/add_key' => 'hosts#add_key' | ||
7 | get 'dashboard/index' | 7 | get 'dashboard/index' |
8 | 8 | ||
9 | # The priority is based upon order of creation: first created -> highest priority. | 9 | # The priority is based upon order of creation: first created -> highest priority. |
@@ -11,7 +11,7 @@ | @@ -11,7 +11,7 @@ | ||
11 | # | 11 | # |
12 | # It's strongly recommended that you check this file into your version control system. | 12 | # It's strongly recommended that you check this file into your version control system. |
13 | 13 | ||
14 | -ActiveRecord::Schema.define(version: 20160425195446) do | 14 | +ActiveRecord::Schema.define(version: 20160428191808) do |
15 | 15 | ||
16 | create_table "certificates", force: :cascade do |t| | 16 | create_table "certificates", force: :cascade do |t| |
17 | t.text "key" | 17 | t.text "key" |
@@ -21,7 +21,7 @@ ActiveRecord::Schema.define(version: 20160425195446) do | @@ -21,7 +21,7 @@ ActiveRecord::Schema.define(version: 20160425195446) do | ||
21 | t.datetime "updated_at", null: false | 21 | t.datetime "updated_at", null: false |
22 | end | 22 | end |
23 | 23 | ||
24 | - create_table "lxd_hosts", force: :cascade do |t| | 24 | + create_table "hosts", force: :cascade do |t| |
25 | t.string "name" | 25 | t.string "name" |
26 | t.string "uri" | 26 | t.string "uri" |
27 | t.datetime "created_at", null: false | 27 | t.datetime "created_at", null: false |
1 | require 'test_helper' | 1 | require 'test_helper' |
2 | 2 | ||
3 | -class LxdHostsControllerTest < ActionController::TestCase | 3 | +class HostsControllerTest < ActionController::TestCase |
4 | setup do | 4 | setup do |
5 | - @lxd_host = lxd_hosts(:one) | 5 | + @host = hosts(:one) |
6 | end | 6 | end |
7 | 7 | ||
8 | test "should get index" do | 8 | test "should get index" do |
9 | get :index | 9 | get :index |
10 | assert_response :success | 10 | assert_response :success |
11 | - assert_not_nil assigns(:lxd_hosts) | 11 | + assert_not_nil assigns(:hosts) |
12 | end | 12 | end |
13 | 13 | ||
14 | test "should get new" do | 14 | test "should get new" do |
@@ -16,34 +16,34 @@ class LxdHostsControllerTest < ActionController::TestCase | @@ -16,34 +16,34 @@ class LxdHostsControllerTest < ActionController::TestCase | ||
16 | assert_response :success | 16 | assert_response :success |
17 | end | 17 | end |
18 | 18 | ||
19 | - test "should create lxd_host" do | ||
20 | - assert_difference('LxdHost.count') do | ||
21 | - post :create, lxd_host: { name: @lxd_host.name, password: 'secret', password_confirmation: 'secret', uri: @lxd_host.uri } | 19 | + test "should create host" do |
20 | + assert_difference('Host.count') do | ||
21 | + post :create, host: { name: @host.name, password: 'secret', password_confirmation: 'secret', uri: @host.uri } | ||
22 | end | 22 | end |
23 | 23 | ||
24 | - assert_redirected_to lxd_host_path(assigns(:lxd_host)) | 24 | + assert_redirected_to host_path(assigns(:host)) |
25 | end | 25 | end |
26 | 26 | ||
27 | - test "should show lxd_host" do | ||
28 | - get :show, id: @lxd_host | 27 | + test "should show host" do |
28 | + get :show, id: @host | ||
29 | assert_response :success | 29 | assert_response :success |
30 | end | 30 | end |
31 | 31 | ||
32 | test "should get edit" do | 32 | test "should get edit" do |
33 | - get :edit, id: @lxd_host | 33 | + get :edit, id: @host |
34 | assert_response :success | 34 | assert_response :success |
35 | end | 35 | end |
36 | 36 | ||
37 | - test "should update lxd_host" do | ||
38 | - patch :update, id: @lxd_host, lxd_host: { name: @lxd_host.name, password: 'secret', password_confirmation: 'secret', uri: @lxd_host.uri } | ||
39 | - assert_redirected_to lxd_host_path(assigns(:lxd_host)) | 37 | + test "should update host" do |
38 | + patch :update, id: @host, host: { name: @host.name, password: 'secret', password_confirmation: 'secret', uri: @host.uri } | ||
39 | + assert_redirected_to host_path(assigns(:host)) | ||
40 | end | 40 | end |
41 | 41 | ||
42 | - test "should destroy lxd_host" do | ||
43 | - assert_difference('LxdHost.count', -1) do | ||
44 | - delete :destroy, id: @lxd_host | 42 | + test "should destroy host" do |
43 | + assert_difference('Host.count', -1) do | ||
44 | + delete :destroy, id: @host | ||
45 | end | 45 | end |
46 | 46 | ||
47 | - assert_redirected_to lxd_hosts_path | 47 | + assert_redirected_to hosts_path |
48 | end | 48 | end |
49 | end | 49 | end |
Please
register
or
login
to post a comment