From: Martyn Taylor <mtaylor(a)redhat.com>
---
.../controllers/resources/instances_controller.rb | 34 ++++++++++++++------
src/app/models/instance.rb | 9 +++++
src/app/models/instance_hwp.rb | 2 +-
src/features/instance.feature | 34 ++++++++++++++++++++
src/features/step_definitions/instance.rb | 11 ++++++
src/test/fixtures/base_permission_objects.yml | 2 +-
6 files changed, 80 insertions(+), 12 deletions(-)
diff --git a/src/app/controllers/resources/instances_controller.rb
b/src/app/controllers/resources/instances_controller.rb
index 3bd0c45..1bbe878 100644
--- a/src/app/controllers/resources/instances_controller.rb
+++ b/src/app/controllers/resources/instances_controller.rb
@@ -1,7 +1,21 @@
class Resources::InstancesController < ApplicationController
before_filter :require_user
before_filter :load_instance, :only => [:show, :remove_failed, :key, :stop]
- before_filter :load_instances, :only => [:show, :index]
+ before_filter :set_view_vars, :only => [:show, :index]
+
+ def index
+ @params = params
+ @search_term = params[:q]
+ if @search_term.blank?
+ load_instances
+ return
+ end
+
+ search = Instance.search do
+ keywords(params[:q])
+ end
+ @instances = search.results
+ end
def new
@instance = Instance.new(params[:instance])
@@ -64,6 +78,7 @@ class Resources::InstancesController < ApplicationController
end
def show
+ load_instances
@url_params = params.clone
@tab_captions = ['Properties', 'History', 'Permissions']
@details_tab = params[:details_tab].blank? ? 'properties' :
params[:details_tab]
@@ -78,9 +93,6 @@ class Resources::InstancesController < ApplicationController
end
end
- def index
- end
-
def key
unless @instance.instance_key.nil?
send_data @instance.instance_key.pem,
@@ -135,7 +147,7 @@ class Resources::InstancesController < ApplicationController
)
end
- def load_instances
+ def set_view_vars
@header = [
{:name => 'VM NAME', :sort_attr => 'name'},
{:name => 'STATUS', :sortable => false},
@@ -145,11 +157,13 @@ class Resources::InstancesController < ApplicationController
{:name => 'CREATED BY', :sort_attr => 'users.last_name'},
]
- pools = Pool.list_for_user(@current_user, Privilege::INSTANCE_MODIFY)
- @instances = Instance.all(
- :include => [:template, :owner],
- :conditions => {:pool_id => pools},
- :order => (params[:order_field] || 'name') +' '+
(params[:order_dir] || 'asc')
+ @pools = Pool.list_for_user(@current_user, Privilege::INSTANCE_MODIFY)
+ end
+
+ def load_instances
+ @instances = Instance.all(:include => [:template, :owner],
+ :conditions => {:pool_id => @pools},
+ :order => (params[:order_field] || 'name')
+' '+ (params[:order_dir] || 'asc')
)
end
end
diff --git a/src/app/models/instance.rb b/src/app/models/instance.rb
index e7757e3..8fa00d1 100644
--- a/src/app/models/instance.rb
+++ b/src/app/models/instance.rb
@@ -19,10 +19,19 @@
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
+require 'sunspot_rails'
class Instance < ActiveRecord::Base
include SearchFilter
include PermissionedObject
+ searchable do
+ text :name, :as => :code_substring
+ text :external_key, :as => :code_substring
+ text :public_addresses, :as => :code_substring
+ text :private_addresses, :as => :code_substring
+ text :state, :as => :code_substring
+ end
+
cattr_reader :per_page
@@per_page = 15
diff --git a/src/app/models/instance_hwp.rb b/src/app/models/instance_hwp.rb
index 8b52df9..5b751f4 100644
--- a/src/app/models/instance_hwp.rb
+++ b/src/app/models/instance_hwp.rb
@@ -23,4 +23,4 @@ class InstanceHwp < ActiveRecord::Base
has_one :instance
-end
\ No newline at end of file
+end
diff --git a/src/features/instance.feature b/src/features/instance.feature
index 22e38b3..ad22025 100644
--- a/src/features/instance.feature
+++ b/src/features/instance.feature
@@ -76,3 +76,37 @@ Feature: Mange Instances
And I press "Stop"
Then I should be on the instances page
And I should see "mock1: stop action was successfully queued"
+
+ @tag
+ Scenario: Search for instances
+ Given there are the following instances:
+ | name | external_key | state | public_addresses | private_addresses |
+ | mock | ext_mock | running | mock.public.address | mock.private.address |
+ | test | ext_test | pending | test.public.address | test.private.address |
+ | other | ext_other | stopped | other.public.address | other.private.address |
+ And I am on the the instances page
+ When I fill in "q" with "mock"
+ And I press "Search"
+ Then I should see "mock"
+ And I should not see "test"
+ And I should not see "other"
+ When I fill in "q" with "ext_other"
+ And I press "Search"
+ Then I should not see "mock"
+ And I should not see "test"
+ And I should see "other"
+ When I fill in "q" with "pending"
+ And I press "Search"
+ Then I should not see "mock"
+ And I should see "test"
+ And I should not see "other"
+ When I fill in "q" with "mock.public.address"
+ And I press "Search"
+ Then I should see "mock"
+ And I should not see "test"
+ And I should not see "other"
+ When I fill in "q" with "test.private.address"
+ And I press "Search"
+ Then I should not see "mock"
+ And I should see "test"
+ And I should not see "other"
\ No newline at end of file
diff --git a/src/features/step_definitions/instance.rb
b/src/features/step_definitions/instance.rb
index 412cd57..4324891 100644
--- a/src/features/step_definitions/instance.rb
+++ b/src/features/step_definitions/instance.rb
@@ -63,7 +63,18 @@ Given /^there is "([^"]*)" pool$/ do |arg1|
Factory :pool, :name => arg1
end
+
When /^I check "([^"]*)" instance$/ do |name|
inst = Instance.find_by_name(name)
check("inst_ids_#{inst.id}")
end
+
+Given /^there are the following instances:$/ do |table|
+ table.hashes.each do |hash|
+ Factory(:instance, :name => hash['name'],
+ :external_key => hash['external_key'],
+ :state => hash['state'],
+ :public_addresses => hash['public_addresses'],
+ :private_addresses => hash['private_addresses'])
+ end
+end
diff --git a/src/test/fixtures/base_permission_objects.yml
b/src/test/fixtures/base_permission_objects.yml
index 4cf58b8..10585b0 100644
--- a/src/test/fixtures/base_permission_objects.yml
+++ b/src/test/fixtures/base_permission_objects.yml
@@ -6,4 +6,4 @@
# two:
# column: value
base_permission_object:
- name: general_permission_scope
\ No newline at end of file
+ name: general_permission_scope
--
1.7.2.3