hosts_controller.rb 2.05 KB
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: