Skip to content

Commit

Permalink
Add compliances subcollection
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Feb 19, 2020
1 parent 086814a commit fe35a59
Show file tree
Hide file tree
Showing 4 changed files with 110 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
17 changes: 17 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,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 @@ -3946,6 +3952,7 @@
- :policies
- :policy_profiles
- :accounts
- :compliances
- :custom_attributes
- :security_groups
- :software
Expand Down Expand Up @@ -4107,6 +4114,11 @@
- :name: read
:identifier:
- vm_show
:compliances_subcollection_actions:
:get:
- :name: read
:identifier:
- vm_show
:disks_subresource_actions:
:get:
- :name: read
Expand All @@ -4117,6 +4129,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(subcollection_action_identifier(:vms, :compliances, :read, :get))

_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 compliance details of compliances" do
api_basic_authorize(subcollection_action_identifier(:vms, :compliances, :read, :get))

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(subcollection_action_identifier(:vms, :compliances, :read, :get))

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(subcollection_action_identifier(:vms, :compliances, :read, :get))

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 fe35a59

Please sign in to comment.