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
... | ... | @@ -17,7 +17,7 @@ class ApplicationController < ActionController::Base |
17 | 17 | # or we remove the current cert completely. |
18 | 18 | if (@cert.cert.not_after - 1.day + 300) < Time.now |
19 | 19 | @new_cert = @cert.update |
20 | - LxdHost.all.each { |host| | |
20 | + Host.all.each { |host| | |
21 | 21 | host.cert = @cert |
22 | 22 | # add new certificate |
23 | 23 | cert = Lxd::Certificate.new( | ... | ... |
1 | 1 | class DashboardController < ApplicationController |
2 | 2 | def index |
3 | 3 | check_cert |
4 | - @lxd_hosts = LxdHost.all | |
4 | + @hosts = Host.all | |
5 | 5 | |
6 | - @lxd_hosts.map { |host| | |
6 | + @hosts.map { |host| | |
7 | 7 | host.cert = @cert |
8 | 8 | if host.config.auth == 'untrusted' |
9 | 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 | 11 | return |
12 | 12 | end |
13 | 13 | } |
14 | 14 | |
15 | - @certificates = Lxd::Certificate.all @lxd_hosts.first.api | |
15 | + @certificates = Lxd::Certificate.all @hosts.first.api | |
16 | 16 | end |
17 | 17 | end |
18 | 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 | 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 | 7 | def index |
8 | - @lxd_hosts = LxdHost.all | |
8 | + @hosts = Host.all | |
9 | 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 | 13 | def show |
14 | 14 | end |
15 | 15 | |
16 | - # GET /lxd_hosts/new | |
16 | + # GET /hosts/new | |
17 | 17 | def new |
18 | - @lxd_host = LxdHost.new | |
18 | + @host = Host.new | |
19 | 19 | end |
20 | 20 | |
21 | - # GET /lxd_hosts/1/edit | |
21 | + # GET /hosts/1/edit | |
22 | 22 | def edit |
23 | 23 | end |
24 | 24 | |
25 | - # GET /lxd_hosts/1/auth | |
25 | + # GET /hosts/1/auth | |
26 | 26 | def auth |
27 | 27 | end |
28 | 28 | |
29 | - # PATCH/PUT /lxd_hosts/1/add_key | |
29 | + # PATCH/PUT /hosts/1/add_key | |
30 | 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 | 33 | redirect_to session.delete(:return_to) |
34 | 34 | end |
35 | 35 | |
36 | - # POST /lxd_hosts | |
37 | - # POST /lxd_hosts.json | |
36 | + # POST /hosts | |
37 | + # POST /hosts.json | |
38 | 38 | def create |
39 | - @lxd_host = LxdHost.new(lxd_host_params) | |
39 | + @host = Host.new(host_params) | |
40 | 40 | |
41 | 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 | 45 | else |
46 | 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 | 48 | end |
49 | 49 | end |
50 | 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 | 54 | def update |
55 | 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 | 59 | else |
60 | 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 | 62 | end |
63 | 63 | end |
64 | 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 | 68 | def destroy |
69 | - @lxd_host.destroy | |
69 | + @host.destroy | |
70 | 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 | 72 | format.json { head :no_content } |
73 | 73 | end |
74 | 74 | end |
75 | 75 | |
76 | 76 | private |
77 | 77 | # Use callbacks to share common setup or constraints between actions. |
78 | - def set_lxd_host | |
78 | + def set_host | |
79 | 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 | 82 | end |
83 | 83 | |
84 | 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 | 87 | end |
88 | 88 | end |
89 | 89 | # vim: set et ts=2 sw=2: | ... | ... |
1 | 1 | module Lxd::API |
2 | 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 | 8 | con.use_ssl = true |
6 | 9 | con.cert = OpenSSL::X509::Certificate.new certificate.cert |
7 | 10 | con.key = OpenSSL::PKey::RSA.new certificate.key |
8 | 11 | con.verify_mode = OpenSSL::SSL::VERIFY_NONE |
9 | 12 | |
10 | 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 | 16 | end |
14 | 17 | |
15 | 18 | def self.call con, req | ... | ... |
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 | 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 | 6 | <ul> |
7 | - <% @lxd_host.errors.full_messages.each do |message| %> | |
7 | + <% @host.errors.full_messages.each do |message| %> | |
8 | 8 | <li><%= message %></li> |
9 | 9 | <% end %> |
10 | 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 | 4 | <div class="field"> |
7 | 5 | <%= f.label :password %><br> |
8 | 6 | <%= f.password_field :password %> | ... | ... |
app/views/hosts/edit.html.erb
0 → 100644
1 | 1 | <p id="notice"><%= notice %></p> |
2 | 2 | |
3 | -<h1>Listing Lxd Hosts</h1> | |
3 | +<h1>Listing Hosts</h1> | |
4 | 4 | |
5 | 5 | <table> |
6 | 6 | <thead> |
... | ... | @@ -12,13 +12,13 @@ |
12 | 12 | </thead> |
13 | 13 | |
14 | 14 | <tbody> |
15 | - <% @lxd_hosts.each do |lxd_host| %> | |
15 | + <% @hosts.each do |host| %> | |
16 | 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 | 22 | </tr> |
23 | 23 | <% end %> |
24 | 24 | </tbody> |
... | ... | @@ -26,4 +26,4 @@ |
26 | 26 | |
27 | 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 | 2 | |
3 | 3 | <p> |
4 | 4 | <strong>Name:</strong> |
5 | - <%= @lxd_host.name %> | |
5 | + <%= @host.name %> | |
6 | 6 | </p> |
7 | 7 | |
8 | 8 | <p> |
9 | 9 | <strong>Uri:</strong> |
10 | - <%= @lxd_host.uri %> | |
10 | + <%= @host.uri %> | |
11 | 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 | 1 | Rails.application.routes.draw do |
2 | - resources :lxd_hosts | |
2 | + resources :hosts | |
3 | 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 | 7 | get 'dashboard/index' |
8 | 8 | |
9 | 9 | # The priority is based upon order of creation: first created -> highest priority. | ... | ... |
... | ... | @@ -11,7 +11,7 @@ |
11 | 11 | # |
12 | 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 | 16 | create_table "certificates", force: :cascade do |t| |
17 | 17 | t.text "key" |
... | ... | @@ -21,7 +21,7 @@ ActiveRecord::Schema.define(version: 20160425195446) do |
21 | 21 | t.datetime "updated_at", null: false |
22 | 22 | end |
23 | 23 | |
24 | - create_table "lxd_hosts", force: :cascade do |t| | |
24 | + create_table "hosts", force: :cascade do |t| | |
25 | 25 | t.string "name" |
26 | 26 | t.string "uri" |
27 | 27 | t.datetime "created_at", null: false | ... | ... |
1 | 1 | require 'test_helper' |
2 | 2 | |
3 | -class LxdHostsControllerTest < ActionController::TestCase | |
3 | +class HostsControllerTest < ActionController::TestCase | |
4 | 4 | setup do |
5 | - @lxd_host = lxd_hosts(:one) | |
5 | + @host = hosts(:one) | |
6 | 6 | end |
7 | 7 | |
8 | 8 | test "should get index" do |
9 | 9 | get :index |
10 | 10 | assert_response :success |
11 | - assert_not_nil assigns(:lxd_hosts) | |
11 | + assert_not_nil assigns(:hosts) | |
12 | 12 | end |
13 | 13 | |
14 | 14 | test "should get new" do |
... | ... | @@ -16,34 +16,34 @@ class LxdHostsControllerTest < ActionController::TestCase |
16 | 16 | assert_response :success |
17 | 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 | 22 | end |
23 | 23 | |
24 | - assert_redirected_to lxd_host_path(assigns(:lxd_host)) | |
24 | + assert_redirected_to host_path(assigns(:host)) | |
25 | 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 | 29 | assert_response :success |
30 | 30 | end |
31 | 31 | |
32 | 32 | test "should get edit" do |
33 | - get :edit, id: @lxd_host | |
33 | + get :edit, id: @host | |
34 | 34 | assert_response :success |
35 | 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 | 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 | 45 | end |
46 | 46 | |
47 | - assert_redirected_to lxd_hosts_path | |
47 | + assert_redirected_to hosts_path | |
48 | 48 | end |
49 | 49 | end | ... | ... |
Please
register
or
login
to post a comment