Skip to content

Commit

Permalink
Add a subcollection under VMs for displaying CD-ROMs
Browse files Browse the repository at this point in the history
  • Loading branch information
skateman committed Jun 17, 2019
1 parent f541baa commit f1766be
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/controllers/api/subcollections/cdroms.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Api
module Subcollections
module Cdroms
def cdroms_query_resource(object)
object.hardware.cdroms
end
end
end
end
1 change: 1 addition & 0 deletions app/controllers/api/vms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class VmsController < BaseController
include Subcollections::Software
include Subcollections::Snapshots
include Subcollections::MetricRollups
include Subcollections::Cdroms
extend Api::Mixins::CentralAdmin

VALID_EDIT_ATTRS = %w(description child_resources parent_resource).freeze
Expand Down
17 changes: 17 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,12 @@
- :subcollection
:verbs: *g
:klass: Disk
:cdroms:
:description: CD-ROMs
:options:
- :subcollection
:verbs: *g
:klass: Disk
:enterprises:
:description: Enterprises
:options:
Expand Down Expand Up @@ -3752,6 +3758,7 @@
:klass: Vm
:subcollections:
- :disks
- :cdroms
- :tags
- :policies
- :policy_profiles
Expand Down Expand Up @@ -3908,11 +3915,21 @@
- :name: read
:identifier:
- vm_show
:cdroms_subcollection_actions:
:get:
- :name: read
:identifier:
- vm_show
:disks_subresource_actions:
:get:
- :name: read
:identifier:
- vm_show
:cdroms_subresource_actions:
:get:
- :name: read
:identifier:
- vm_show
:tags_subcollection_actions:
:post:
- :name: assign
Expand Down
50 changes: 50 additions & 0 deletions spec/requests/cdroms_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
RSpec.describe "CD-ROMs API" do
let(:hw) { FactoryBot.create(:hardware) }
let(:vm) { FactoryBot.create(:vm_vmware, :hardware => hw) }
let!(:cdrom) { FactoryBot.create(:disk, :hardware => hw, :device_type => 'cdrom') }
describe "as a subcollection of VMs" do
describe "GET /api/vms/:c_id/cdroms" do
it "can list the CD-ROMs of a VM" do
api_basic_authorize(subcollection_action_identifier(:vms, :cdroms, :read, :get))

_other_disk = FactoryBot.create(:disk, :device_type => 'cdrom')

get(api_vm_cdroms_url(nil, vm))

expected = {
"count" => 2,
"name" => "cdroms",
"subcount" => 1,
"resources" => [
{"href" => api_vm_cdrom_url(nil, vm, cdrom)}
]
}

expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end
end

describe "GET /api/vms/:c_id/cdroms/:s_id" do
it "can show a VM's CD-ROM" do
api_basic_authorize(subcollection_action_identifier(:vms, :cdroms, :read, :get))

get(api_vm_cdrom_url(nil, vm, cdrom))

expected = {
"href" => api_vm_cdrom_url(nil, vm, cdrom),
"id" => cdrom.id.to_s,
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

it "will not show a CD-ROM unless authorized" do
api_basic_authorize

get(api_vm_cdrom_url(nil, vm, cdrom))
expect(response).to have_http_status(:forbidden)
end
end
end
end

0 comments on commit f1766be

Please sign in to comment.