diff --git a/app/controllers/api/subcollections/policies.rb b/app/controllers/api/subcollections/policies.rb index 2d3e6a10415..96ee2c027fc 100644 --- a/app/controllers/api/subcollections/policies.rb +++ b/app/controllers/api/subcollections/policies.rb @@ -8,19 +8,11 @@ def policies_query_resource(object) end def policies_assign_resource(object, _type, id = nil, data = nil) - 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 + policy_assign_action(object, :policies, id, data) end def policies_unassign_resource(object, _type, id = nil, data = nil) - 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 + policy_unassign_action(object, :policies, id, data) end def policies_resolve_resource(object, _type, id = nil, data = nil) diff --git a/app/models/miq_policy_set.rb b/app/models/miq_policy_set.rb index 2790411104e..ac53c3247dd 100644 --- a/app/models/miq_policy_set.rb +++ b/app/models/miq_policy_set.rb @@ -25,6 +25,18 @@ def destroy_policy_tags Tag.find_by(:name => "/miq_policy/assignment/#{self.class.to_s.underscore}/#{id}").try!(:destroy) end + def add_policy(policy) + add_member(policy) + end + + def remove_policy(policy) + remove_member(policy) + end + + def get_policies + miq_policies + end + def add_to(ids, db) model = db.respond_to?(:constantize) ? db.constantize : db ids.each do|id| diff --git a/config/api.yml b/config/api.yml index 111d76a87d3..187b13cbbb0 100644 --- a/config/api.yml +++ b/config/api.yml @@ -1160,15 +1160,19 @@ - :name: delete :identifier: profile_delete :disabled: true - :subcollection_actions: + :policies_subcollection_actions: :post: - :name: assign + :identifier: policy_profile_assign - :name: unassign + :identifier: policy_profile_assign - :name: resolve - :subresource_actions: + :policies_subresource_actions: :post: - :name: assign + :identifier: policy_profile_assign - :name: unassign + :identifier: policy_profile_assign - :name: resolve :providers: :description: Providers diff --git a/db/fixtures/miq_product_features.yml b/db/fixtures/miq_product_features.yml index c9722228716..f0f184f06d4 100644 --- a/db/fixtures/miq_product_features.yml +++ b/db/fixtures/miq_product_features.yml @@ -1750,6 +1750,11 @@ :feature_type: admin :hidden: true :identifier: profile_edit + - :name: Edit Policies Assignment + :description: Edit Policy Profile's policies assignments + :feature_type: admin + :hidden: true + :identifier: policy_profile_assign - :name: Policies :description: Everything under Policies Accordion :protected: true diff --git a/spec/requests/api/policies_assignment_spec.rb b/spec/requests/api/policies_assignment_spec.rb index 8d54710715a..fcf46149269 100644 --- a/spec/requests/api/policies_assignment_spec.rb +++ b/spec/requests/api/policies_assignment_spec.rb @@ -118,9 +118,9 @@ def test_unassign_multiple_policies(object_policies_url, collection, subcollecti api_basic_authorize subcollection_action_identifier(collection, subcollection, :unassign) object = options[:object] - [p1, p2, p3].each { |p| object.add_policy(p) } run_post(object_policies_url, gen_request(:unassign, [{:guid => p2.guid}, {:guid => p3.guid}])) + object.reload expect_multiple_action_result(2) expect(object.get_policies.size).to eq(1) @@ -140,25 +140,20 @@ def test_unassign_multiple_policy_profiles(object_policies_url, collection, subc end context "Policy profile policies assignment" do - it "adds Policies to a Policy Profile" do - api_basic_authorize + let(:policy_profile_url) { policy_profiles_url(ps2.id) } + let(:policy_profile_policies_url) { "#{policy_profile_url}/policies" } - 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) + it "adds Policies to a Policy Profile" do + test_assign_multiple_policies(policy_profile_url, + policy_profile_policies_url, + :policy_profiles, + :policies, + :object => ps2, + :policies => [p1, p2, p3]) 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) + it "removes Policies from a Policy Profile" do + test_unassign_multiple_policies(policy_profile_policies_url, :policy_profiles, :policies, :object => ps2) end end