作業控え

ええと、日中の現実トウヒなナニの続きを。
とりあえず手を動かしつつポイントのみ列挙な方向にて。

conns から vm_operations へのつなぎ

current_user.conns を vm_operations でも使うのはあまりに微妙なのでインデクスを渡して、という形に変更。経路の制御を無理矢理弄って

  match 'vm_operations/:index', :to => 'vm_operations#index', :as => 'vm_operations', :via => :get

  resources :vm_operations, only: [ :new, :create ] do
#  resources :vm_operations, only: [ :new, :create, :index ] do
    member do
      get :start, :stop
    end
  end

app/views/conns/index.html.erb が以下なカンジ。まだ当たりかどうかは不明。

<% @conns.each_with_index do |conn, i| %>
  <tr>
    <td><%= conn.provider %></td>
    <td><%= conn.access_key %></td>
    <td><%= conn.secret_access_key %></td>
    <td><%= conn.end_point %></td>
    <td><%= link_to 'VMs', {:controller => 'vm_operations', :action => 'index', :index => i} %>

あとは飛び先の Controller の index アクションが以下なカンジになり

  def index
    @virtual_machines = VirtualMachine.find_by_conn(params[:index])

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @virtual_machines }
    end
  end

VirtualMachine#find_by_conn が以下に。

  def self.find_by_conn(conn)
    virtual_machines = Hash::new
    uri = URI.parse(conn.end_point)
    compute = Fog::Compute.new(:provider => conn.provider,
                               :cloudstack_api_key => conn.access_key,
                               :cloudstack_secret_access_key => conn.secret_access_key,
                               :cloudstack_host => uri.host,
                               :cloudstack_port => uri.port,
                               :cloudstack_path => uri.path,
                               :cloudstack_scheme => uri.scheme,
                               )
    virtual_machines[:conn] = conn
    virtual_machines[:virtual_machines] = compute.list_virtual_machines
    virtual_machines
  end

これ、試験はパスするんかな。駄目か。routing なナニも変更してますね。

試験してみた

いくつか駄目。routing の記述は以下の順番でないと駄目でした。

  resources :vm_operations, only: [ :new, :create ] do
#  resources :vm_operations, only: [ :new, :create, :index ] do
    member do
      get :start, :stop
    end
  end

  match 'vm_operations/:index', :to => 'vm_operations#index', :as => 'vm_operations', :via => :get

マッチの順が、というヤツなのか。あと、spec/request なソレはちょいスルーで。
あるいは、spec/controllers/vm_operations_controller_spec.rb の get 'index' で試験失敗。get な記述を以下なカンジにしておいて

        get 'index', { :index => "0" }

コントローラな記述に微妙な bug があったのを修正。

  def index
    @virtual_machines = VirtualMachine.find_by_conn(current_user.conns[params[:index].to_i])

とりあえず試験パスなんですが、スルーしてるナニをどうするか。

もうすこしがんばる

試験が以下なカンジ。

describe "VmOperations" do
  describe "GET /vm_operations" do
    it "works! (now write some real specs)" do
      # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
      get vm_operations_path
      response.status.should be(200)
    end
  end
end

以下な出力。

Failures:

  1) VmOperations GET /vm_operations works! (now write some real specs)
     Failure/Error: get vm_operations_path
     ActionController::RoutingError:
       No route matches {:controller=>"vm_operations"}

経路はあるんですがね。

           vm_operations GET    /vm_operations/:index(.:format)    vm_operations#index

つうか無いのか。パラメータをどうやって渡すのかがアレ。ちょっと今日は降参orz