Skip to content

Commit

Permalink
Allow partial POST edits on miq policy REST
Browse files Browse the repository at this point in the history
Fix https://bugzilla.redhat.com/show_bug.cgi?id=1435777

policies API (MiqPolicy) should allow partial POST edits
e.g

POST localhost:3000/api/policies/1000000000007
{
  "action": "edit",
  "description": "Desc bar"
}
In addition the current error message is wrong as it is about creation not edit:

{
  "error": {
    "kind": "bad_request",
    "message": "Resource conditions_ids, policy_contents needs be specified for creating a new policies",
    "klass": "Api::BadRequestError"
  }
}
  • Loading branch information
Mooli Tayer committed Mar 28, 2017
1 parent fbd5ebf commit 8cb8017
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 2 additions & 3 deletions app/controllers/api/policies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ def create_resource(type, _id, data = {})

def edit_resource(type, id = nil, data = {})
raise BadRequestError, "Must specify an id for editing a #{type} resource" unless id
assert_all_required_fields_exists(data, type, %w(conditions_ids policy_contents))
policy = resource_search(id, type, collection_class(:policies))
begin
add_policies_content(data, policy)
add_policies_content(data, policy) if data["policy_contents"]
policy.conditions = Condition.where(:id => data.delete("conditions_ids")) if data["conditions_ids"]
policy.update_attributes(data)
rescue => err
Expand Down Expand Up @@ -46,7 +45,7 @@ def add_policies_content(data, policy)
policy.miq_policy_contents.destroy_all
data.delete("policy_contents").each do |policy_content|
add_policy_content(policy_content, policy)
end if data["policy_contents"]
end
end

def add_policy_content(policy_content, policy)
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/api/policies_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -412,5 +412,14 @@ def test_policy_profile_query(object, object_policy_profiles_url)
expect(policy.events.count).to eq(1)
expect(miq_policy.conditions.count).to eq(0)
end

it "edits just the description" do
api_basic_authorize collection_action_identifier(:policies, :edit)
expect(miq_policy.description).to_not eq("BAR")
run_post(policies_url(miq_policy.id), gen_request(:edit, :description => "BAR"))
policy = MiqPolicy.find(response.parsed_body["id"])
expect(response).to have_http_status(:ok)
expect(policy.description).to eq("BAR")
end
end
end

0 comments on commit 8cb8017

Please sign in to comment.