Skip to content

Commit

Permalink
fixes #10720 Adds API to get host vm attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
hufman committed Jun 5, 2015
1 parent a6b8542 commit 81f1fa7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions Contributors
Expand Up @@ -180,6 +180,7 @@ Trey Dockendorf <treydock@tamu.edu>
Unknown <flo@WKS-W7-LDN-0034.tradition.int>
Vanya Jauhal <vanyajauhal1995@gmail.com>
Walden Raines <walden@redhat.com>
Walter Huf <hufman@google.com>
William Hutson <wilrnh@msn.com>
abenari <abenari@redhat.com>
cyberkov <admin@cyberkov.at>
Expand Down
26 changes: 26 additions & 0 deletions app/controllers/api/v2/hosts_controller.rb
Expand Up @@ -126,6 +126,30 @@ def status
render :json => { :status => @host.host_status }.to_json if @host
end

api :GET, "/hosts/:id/vm_compute_attributes", N_("Get vm attributes of host")
param :id, :identifier_dottable, :required => true
description <<-eos
Return the host's compute attributes that can be used to create a clone of this VM
eos

def vm_compute_attributes
if @host
attrs = @host.vm_compute_attributes
safe_attrs = {}
attrs.each_pair do |k,v|
# clean up the compute attributes to be suitable for output
if v.is_a?(Proc)
safe_attrs[k] = v.call()
elsif v.respond_to?('parent')
# don't add folders, causes recursive json issues
else
safe_attrs[k] = v
end
end
end
render :json => { :compute_attributes => safe_attrs }.to_json if @host
end

api :PUT, "/hosts/:id/puppetrun", N_("Force a Puppet agent run on the host")
param :id, :identifier_dottable, :required => true

Expand Down Expand Up @@ -221,6 +245,8 @@ def action_permission
:console
when 'disassociate'
:edit
when 'vm_compute_attributes'
:view
else
super
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/host/managed.rb
Expand Up @@ -660,6 +660,10 @@ def sp_name
bmc_nic.try(:name)
end

def vm_compute_attributes
compute_resource ? compute_resource.vm_compute_attributes_for(uuid) : nil
end

def host_status
if build
N_("Pending Installation")
Expand Down
1 change: 1 addition & 0 deletions config/routes/api/v2.rb
Expand Up @@ -268,6 +268,7 @@
end
resources :hosts, :except => [:new, :edit] do
get :status, :on => :member
get :vm_compute_attributes, :on => :member
put :puppetrun, :on => :member
put :disassociate, :on => :member
put :boot, :on => :member
Expand Down
10 changes: 10 additions & 0 deletions test/functional/api/v2/hosts_controller_test.rb
Expand Up @@ -252,6 +252,16 @@ def last_host
assert_response :not_found
end

test "should show hosts vm attributes" do
host = FactoryGirl.create(:host, :compute_resource => compute_resources(:one))
ComputeResource.any_instance.stubs(:vm_compute_attributes_for).returns( :cpus => 4 )
get :vm_compute_attributes, { :id => host.to_param }
assert_response :success
data = JSON.parse(@response.body)
assert_equal data, "compute_attributes" => { "cpus" => 4 }
ComputeResource.any_instance.unstub(:vm_compute_attributes_for)
end

def set_remote_user_to(user)
@request.env['REMOTE_USER'] = user.login
end
Expand Down
11 changes: 11 additions & 0 deletions test/unit/host_test.rb
Expand Up @@ -147,6 +147,17 @@ class HostTest < ActiveSupport::TestCase
host.update_attributes!(:mac => "52:54:00:dd:ee:ff")
end

test "can fetch vm compute attributes" do
host = FactoryGirl.create(:host, :compute_resource => compute_resources(:one))
ComputeResource.any_instance.stubs(:vm_compute_attributes_for).returns({:cpus => 4})
assert_equal host.vm_compute_attributes, :cpus => 4
end

test "fetches nil vm compute attributes for bare metal" do
host = FactoryGirl.create(:host)
assert_equal host.vm_compute_attributes, nil
end

context "when unattended is false" do
def setup
SETTINGS[:unattended] = false
Expand Down

0 comments on commit 81f1fa7

Please sign in to comment.