Skip to content

Commit

Permalink
API support for adding/removing Policies to/from Policy Profiles
Browse files Browse the repository at this point in the history
Currently, it is only possible to add/remove MiqPolicy from
MiqPolicySet using the UI.
This change adds to API support for these operations, using the
assign/unassign actions.

Example:
POST /api/policy_profiles/:id/policies

Body:
{
    "action": "assign",
    "resources": [
    	{ "href": "/api/policies/1" },
    	{ "href": "/api/policies/2" }
    ]
}
  • Loading branch information
dkorn committed Apr 4, 2017
1 parent 9aa0cfe commit 7aac779
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/controllers/api/subcollections/policies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ def policies_query_resource(object)
end

def policies_assign_resource(object, _type, id = nil, data = nil)
policy_assign_action(object, :policies, id, data)
if object.kind_of?(collection_class(:policy_profiles))
object.add_member(policy_specified(id, data, :policies, MiqPolicy))
else
policy_assign_action(object, :policies, id, data)
end
end

def policies_unassign_resource(object, _type, id = nil, data = nil)
policy_unassign_action(object, :policies, id, data)
if object.kind_of?(collection_class(:policy_profiles))
object.remove_member(policy_specified(id, data, :policies, MiqPolicy))
else
policy_unassign_action(object, :policies, id, data)
end
end

def policies_resolve_resource(object, _type, id = nil, data = nil)
Expand Down
23 changes: 23 additions & 0 deletions spec/requests/api/policies_assignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,29 @@ def test_unassign_multiple_policy_profiles(object_policies_url, collection, subc
expect(object.get_policies.first.guid).to eq(ps1.guid)
end

context "Policy profile policies assignment" do
it "adds Policies to a Policy Profile" do
api_basic_authorize

run_post("#{policy_profiles_url(ps2.id)}/policies", gen_request(:assign, [{"id" => p1.id}, {"id" => p2.id}]))

expect(response).to have_http_status(:ok)
expect(response.parsed_body["results"].count).to eq(2)
expect(ps2.reload.miq_policies.count).to eq(3)
expect(response.parsed_body["results"].first["guid"]).to eq(p1.guid)
expect(response.parsed_body["results"].second["guid"]).to eq(p2.guid)
end

it "removes a Policy from a Policy Profile" do
api_basic_authorize

run_post("#{policy_profiles_url(ps2.id)}/policies", gen_request(:unassign, [{"id" => p3.id}]))

expect(response).to have_http_status(:ok)
expect(ps2.reload.miq_policies.count).to eq(0)
end
end

context "Provider policies subcollection assignment" do
let(:provider_url) { providers_url(provider.id) }
let(:provider_policies_url) { "#{provider_url}/policies" }
Expand Down

0 comments on commit 7aac779

Please sign in to comment.