Skip to content

Commit

Permalink
Merge pull request #742 from d-m-u/adding_subcollection_for_complianc…
Browse files Browse the repository at this point in the history
…e_details

Add compliances subcollection

(cherry picked from commit 309e538)

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1806660
  • Loading branch information
gtanzillo authored and simaishi committed Feb 24, 2020
1 parent 32158db commit b5cecae
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/controllers/api/subcollections/compliances.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Api
module Subcollections
module Compliances
def compliances_query_resource(object)
object.compliances
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
@@ -1,6 +1,7 @@
module Api
class VmsController < BaseController
include Subcollections::Disks
include Subcollections::Compliances
include Subcollections::Tags
include Subcollections::Policies
include Subcollections::PolicyProfiles
Expand Down
19 changes: 19 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,12 @@
:identifier: ems_cluster_protect
- :name: unassign
:identifier: ems_cluster_protect
:compliances:
:description: Compliances
:options:
- :subcollection
:verbs: *g
:klass: Compliance
:conditions:
:description: Conditions
:identifier: condition
Expand Down Expand Up @@ -3881,6 +3887,7 @@
- :policies
- :policy_profiles
- :accounts
- :compliances
- :custom_attributes
- :security_groups
- :software
Expand Down Expand Up @@ -4042,6 +4049,13 @@
- :name: read
:identifier:
- vm_show
:compliances_subcollection_actions:
:get:
- :name: get
:identifier:
- vm_show
- vm_check_compliance
- instance_check_compliance
:disks_subresource_actions:
:get:
- :name: read
Expand All @@ -4052,6 +4066,11 @@
- :name: read
:identifier:
- vm_show
:compliances_subresource_actions:
:get:
- :name: read
:identifier:
- vm_show
:tags_subcollection_actions:
:post:
- :name: assign
Expand Down
83 changes: 83 additions & 0 deletions spec/requests/compliances_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
RSpec.describe "Compliances API" do
let(:vm) { FactoryBot.create(:vm_vmware) }
let!(:compliance) { FactoryBot.create(:compliance, :resource => vm) }
let!(:compliance_detail) { FactoryBot.create(:compliance_detail, :compliance_id => compliance.id) }

describe "as a subcollection of VMs" do
describe "GET /api/vms/:c_id/compliances" do
it "can list the compliances of a VM" do
api_basic_authorize("vm_show")

_other_compliance = FactoryBot.create(:compliance)

get(api_vm_compliances_url(nil, vm))

expected = {
"count" => 2,
"name" => "compliances",
"subcount" => 1,
"resources" => [
{"href" => api_vm_compliance_url(nil, vm, compliance)}
]
}

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

it "can list the details of compliances" do
api_basic_authorize("vm_show")

get(api_vm_compliances_url(nil, vm), :params => {:expand => 'resources', :attributes => 'compliance_details'})

expected = {
"count" => 1,
"name" => "compliances",
"subcount" => 1,
"resources" => [a_hash_including("compliance_details" => [a_hash_including("compliance_id"=>compliance.id.to_s)])]
}

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

describe "GET /api/vms/:c_id/compliances/:s_id" do
it "can show a VM's compliances" do
api_basic_authorize("vm_show")

get(api_vm_compliance_url(nil, vm, compliance))

expected = {
"href" => api_vm_compliance_url(nil, vm, compliance),
"id" => compliance.id.to_s,
}

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

it "can show a VM's compliance details" do
api_basic_authorize("vm_show")

get(api_vm_compliance_url(nil, vm, compliance), :params => {:expand => 'resources', :attributes => 'compliance_details'})

expected = {
"href" => api_vm_compliance_url(nil, vm, compliance),
"id" => compliance.id.to_s,
"compliance_details" => [a_hash_including("compliance_id" => compliance.id.to_s)]
}

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

it "will not show a compliance unless authorized" do
api_basic_authorize

get(api_vm_compliance_url(nil, vm, compliance))
expect(response).to have_http_status(:forbidden)
end
end
end
end

0 comments on commit b5cecae

Please sign in to comment.