diff --git a/app/assets/javascripts/lxd_hosts.coffee b/app/assets/javascripts/hosts.coffee similarity index 100% rename from app/assets/javascripts/lxd_hosts.coffee rename to app/assets/javascripts/hosts.coffee diff --git a/app/assets/stylesheets/lxd_hosts.scss b/app/assets/stylesheets/hosts.scss similarity index 63% rename from app/assets/stylesheets/lxd_hosts.scss rename to app/assets/stylesheets/hosts.scss index 4752d3d..90133d4 100644 --- a/app/assets/stylesheets/lxd_hosts.scss +++ b/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. // You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f877cc2..934a316 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -17,7 +17,7 @@ class ApplicationController < ActionController::Base # or we remove the current cert completely. if (@cert.cert.not_after - 1.day + 300) < Time.now @new_cert = @cert.update - LxdHost.all.each { |host| + Host.all.each { |host| host.cert = @cert # add new certificate cert = Lxd::Certificate.new( diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index b636b19..cab303c 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -1,18 +1,18 @@ class DashboardController < ApplicationController def index check_cert - @lxd_hosts = LxdHost.all + @hosts = Host.all - @lxd_hosts.map { |host| + @hosts.map { |host| host.cert = @cert if host.config.auth == 'untrusted' 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 end } - @certificates = Lxd::Certificate.all @lxd_hosts.first.api + @certificates = Lxd::Certificate.all @hosts.first.api end end # vim: set et ts=2 sw=2: diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb new file mode 100644 index 0000000..00c3df8 --- /dev/null +++ b/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: diff --git a/app/controllers/lxd_hosts_controller.rb b/app/controllers/lxd_hosts_controller.rb deleted file mode 100644 index 8973692..0000000 --- a/app/controllers/lxd_hosts_controller.rb +++ /dev/null @@ -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: diff --git a/app/helpers/hosts_helper.rb b/app/helpers/hosts_helper.rb new file mode 100644 index 0000000..998691e --- /dev/null +++ b/app/helpers/hosts_helper.rb @@ -0,0 +1,2 @@ +module HostsHelper +end diff --git a/app/helpers/lxd_hosts_helper.rb b/app/helpers/lxd_hosts_helper.rb deleted file mode 100644 index b77befd..0000000 --- a/app/helpers/lxd_hosts_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module LxdHostsHelper -end diff --git a/app/models/lxd_host.rb b/app/models/host.rb similarity index 80% rename from app/models/lxd_host.rb rename to app/models/host.rb index b1458fd..1f1920e 100644 --- a/app/models/lxd_host.rb +++ b/app/models/host.rb @@ -1,4 +1,4 @@ -class LxdHost < ActiveRecord::Base +class Host < ActiveRecord::Base def cert=(cert) @cert = cert end diff --git a/app/models/lxd/api.rb b/app/models/lxd/api.rb index a6c77b5..0f06f8d 100644 --- a/app/models/lxd/api.rb +++ b/app/models/lxd/api.rb @@ -1,15 +1,18 @@ module Lxd::API def self.get host, certificate - uri = URI.parse host.uri - con = Net::HTTP.new uri.host, uri.port + begin + uri = URI.parse host.uri + rescue + end + con = Net::HTTP.new uri.host, uri.port ? uri.port : 8443 con.use_ssl = true con.cert = OpenSSL::X509::Certificate.new certificate.cert con.key = OpenSSL::PKey::RSA.new certificate.key con.verify_mode = OpenSSL::SSL::VERIFY_NONE 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 def self.call con, req diff --git a/app/views/dashboard/index.html.erb b/app/views/dashboard/index.html.erb index e212918..955a357 100644 --- a/app/views/dashboard/index.html.erb +++ b/app/views/dashboard/index.html.erb @@ -1,7 +1,7 @@

Dashboard#index

<%= @cert.cert_fpr %>

Serial: <%= @cert.cert.serial %>

-<% @lxd_hosts.each do |host| -%> +<% @hosts.each do |host| -%>

<%= host.config.inspect %>

<% end -%> <% @certificates.each do |cert| -%> diff --git a/app/views/lxd_hosts/_form.html.erb b/app/views/hosts/_form.html.erb similarity index 71% rename from app/views/lxd_hosts/_form.html.erb rename to app/views/hosts/_form.html.erb index f2a5eeb..8a8fcb7 100644 --- a/app/views/lxd_hosts/_form.html.erb +++ b/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? %>
-

<%= pluralize(@lxd_host.errors.count, "error") %> prohibited this lxd_host from being saved:

+

<%= pluralize(@host.errors.count, "error") %> prohibited this host from being saved:

diff --git a/app/views/lxd_hosts/auth.html.erb b/app/views/hosts/auth.html.erb similarity index 50% rename from app/views/lxd_hosts/auth.html.erb rename to app/views/hosts/auth.html.erb index b486098..1d7a3f7 100644 --- a/app/views/lxd_hosts/auth.html.erb +++ b/app/views/hosts/auth.html.erb @@ -1,8 +1,6 @@ -

Authenticate Lxd Host: <%= @lxd_host.name %>

+

Authenticate Host: <%= @host.name %>

-

...<%= @data.inspect %>

- -<%= form_for :lxd_hosts, url: { action: "add_key" }, method: 'put' do |f| %> +<%= form_for :hosts, url: { action: "add_key" }, method: 'put' do |f| %>
<%= f.label :password %>
<%= f.password_field :password %> diff --git a/app/views/hosts/edit.html.erb b/app/views/hosts/edit.html.erb new file mode 100644 index 0000000..3f37306 --- /dev/null +++ b/app/views/hosts/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Host

+ +<%= render 'form' %> + +<%= link_to 'Show', @host %> | +<%= link_to 'Back', hosts_path %> diff --git a/app/views/hosts/index.html.erb b/app/views/hosts/index.html.erb new file mode 100644 index 0000000..ec4c5fb --- /dev/null +++ b/app/views/hosts/index.html.erb @@ -0,0 +1,29 @@ +

<%= notice %>

+ +

Listing Hosts

+ + + + + + + + + + + + <% @hosts.each do |host| %> + + + + + + + + <% end %> + +
NameUri
<%= host.name %><%= host.uri %><%= link_to 'Show', host %><%= link_to 'Edit', edit_host_path(host) %><%= link_to 'Destroy', host, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New host', new_host_path %> diff --git a/app/views/hosts/index.json.jbuilder b/app/views/hosts/index.json.jbuilder new file mode 100644 index 0000000..a838364 --- /dev/null +++ b/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 diff --git a/app/views/hosts/new.html.erb b/app/views/hosts/new.html.erb new file mode 100644 index 0000000..ef19a22 --- /dev/null +++ b/app/views/hosts/new.html.erb @@ -0,0 +1,5 @@ +

New Host

+ +<%= render 'form' %> + +<%= link_to 'Back', hosts_path %> diff --git a/app/views/hosts/show.html.erb b/app/views/hosts/show.html.erb new file mode 100644 index 0000000..6a4ee42 --- /dev/null +++ b/app/views/hosts/show.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

+ Name: + <%= @host.name %> +

+ +

+ Uri: + <%= @host.uri %> +

+ +<%= link_to 'Edit', edit_host_path(@host) %> | +<%= link_to 'Back', hosts_path %> diff --git a/app/views/hosts/show.json.jbuilder b/app/views/hosts/show.json.jbuilder new file mode 100644 index 0000000..af715c5 --- /dev/null +++ b/app/views/hosts/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @host, :id, :name, :uri, :created_at, :updated_at diff --git a/app/views/lxd_hosts/edit.html.erb b/app/views/lxd_hosts/edit.html.erb deleted file mode 100644 index 83c0643..0000000 --- a/app/views/lxd_hosts/edit.html.erb +++ /dev/null @@ -1,6 +0,0 @@ -

Editing Lxd Host

- -<%= render 'form' %> - -<%= link_to 'Show', @lxd_host %> | -<%= link_to 'Back', lxd_hosts_path %> diff --git a/app/views/lxd_hosts/index.html.erb b/app/views/lxd_hosts/index.html.erb deleted file mode 100644 index e281d81..0000000 --- a/app/views/lxd_hosts/index.html.erb +++ /dev/null @@ -1,29 +0,0 @@ -

<%= notice %>

- -

Listing Lxd Hosts

- - - - - - - - - - - - <% @lxd_hosts.each do |lxd_host| %> - - - - - - - - <% end %> - -
NameUri
<%= lxd_host.name %><%= lxd_host.uri %><%= link_to 'Show', lxd_host %><%= link_to 'Edit', edit_lxd_host_path(lxd_host) %><%= link_to 'Destroy', lxd_host, method: :delete, data: { confirm: 'Are you sure?' } %>
- -
- -<%= link_to 'New Lxd host', new_lxd_host_path %> diff --git a/app/views/lxd_hosts/index.json.jbuilder b/app/views/lxd_hosts/index.json.jbuilder deleted file mode 100644 index c00060f..0000000 --- a/app/views/lxd_hosts/index.json.jbuilder +++ /dev/null @@ -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 diff --git a/app/views/lxd_hosts/new.html.erb b/app/views/lxd_hosts/new.html.erb deleted file mode 100644 index 56a4896..0000000 --- a/app/views/lxd_hosts/new.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -

New Lxd Host

- -<%= render 'form' %> - -<%= link_to 'Back', lxd_hosts_path %> diff --git a/app/views/lxd_hosts/show.html.erb b/app/views/lxd_hosts/show.html.erb deleted file mode 100644 index 98dbe18..0000000 --- a/app/views/lxd_hosts/show.html.erb +++ /dev/null @@ -1,14 +0,0 @@ -

<%= notice %>

- -

- Name: - <%= @lxd_host.name %> -

- -

- Uri: - <%= @lxd_host.uri %> -

- -<%= link_to 'Edit', edit_lxd_host_path(@lxd_host) %> | -<%= link_to 'Back', lxd_hosts_path %> diff --git a/app/views/lxd_hosts/show.json.jbuilder b/app/views/lxd_hosts/show.json.jbuilder deleted file mode 100644 index 7f7df55..0000000 --- a/app/views/lxd_hosts/show.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.extract! @lxd_host, :id, :name, :uri, :created_at, :updated_at diff --git a/config/routes.rb b/config/routes.rb index dee9a0f..17d70e2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,9 @@ Rails.application.routes.draw do - resources :lxd_hosts + resources :hosts 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' # The priority is based upon order of creation: first created -> highest priority. diff --git a/db/migrate/20160428191808_rename_lxd_host_to_host.rb b/db/migrate/20160428191808_rename_lxd_host_to_host.rb new file mode 100644 index 0000000..3e04d8c --- /dev/null +++ b/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: diff --git a/db/schema.rb b/db/schema.rb index 36fd2a7..bba22cb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # 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| t.text "key" @@ -21,7 +21,7 @@ ActiveRecord::Schema.define(version: 20160425195446) do t.datetime "updated_at", null: false end - create_table "lxd_hosts", force: :cascade do |t| + create_table "hosts", force: :cascade do |t| t.string "name" t.string "uri" t.datetime "created_at", null: false diff --git a/test/controllers/hosts_controller_test.rb b/test/controllers/hosts_controller_test.rb new file mode 100644 index 0000000..f752e70 --- /dev/null +++ b/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 diff --git a/test/controllers/lxd_hosts_controller_test.rb b/test/controllers/lxd_hosts_controller_test.rb deleted file mode 100644 index 2055c29..0000000 --- a/test/controllers/lxd_hosts_controller_test.rb +++ /dev/null @@ -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 diff --git a/test/fixtures/lxd_hosts.yml b/test/fixtures/hosts.yml similarity index 100% rename from test/fixtures/lxd_hosts.yml rename to test/fixtures/hosts.yml diff --git a/test/models/lxd_host_test.rb b/test/models/host_test.rb similarity index 63% rename from test/models/lxd_host_test.rb rename to test/models/host_test.rb index fd6fc25..e4bb6df 100644 --- a/test/models/lxd_host_test.rb +++ b/test/models/host_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class LxdHostTest < ActiveSupport::TestCase +class HostTest < ActiveSupport::TestCase # test "the truth" do # assert true # end