Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Enhancement to support filtering on id attributes by compressed id's #13645

Merged
merged 2 commits into from
Jan 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/controllers/api/base_controller/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def parse_filter(filter, operators)
end
end

filter_value = from_cid(filter_value) if filter_attr =~ /[_]?id$/ && cid?(filter_value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the [ and ] needed in this regex?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they are optional, I tend to use them regardless, one or more character being matched for consistency.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about pulling this out into a named constant?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh, it's only used here for now, up to you. it will eventually be needed by https://www.pivotaltracker.com/story/show/121850107


if filter_value =~ /%|\*/
filter_value = "/\\A#{Regexp.escape(filter_value)}\\z/"
filter_value.gsub!(/%|\\\*/, ".*")
Expand Down
42 changes: 42 additions & 0 deletions spec/requests/api/querying_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,48 @@ def create_vms_by_name(names)
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

it "supports filtering by compressed id" do
vm1, _vm2 = create_vms_by_name(%w(aa bb))

run_get vms_url, :expand => "resources",
:filter => ["id = #{ApplicationRecord.compress_id(vm1.id)}"]

expect_query_result(:vms, 1, 2)
expect_result_resources_to_match_hash([{"name" => vm1.name, "guid" => vm1.guid}])
end

it "supports filtering by compressed id as string" do
_vm1, vm2 = create_vms_by_name(%w(aa bb))

run_get vms_url, :expand => "resources",
:filter => ["id = '#{ApplicationRecord.compress_id(vm2.id)}'"]

expect_query_result(:vms, 1, 2)
expect_result_resources_to_match_hash([{"name" => vm2.name, "guid" => vm2.guid}])
end

it "supports filtering by compressed id on *_id named attributes" do
zone = FactoryGirl.create(:zone, :name => "api_zone")
ems1 = FactoryGirl.create(:ems_vmware, :zone => zone)
ems2 = FactoryGirl.create(:ems_vmware, :zone => zone)
host = FactoryGirl.create(:host)

_vm = FactoryGirl.create(:vm_vmware,
:host => host,
:ext_management_system => ems1,
:raw_power_state => "poweredOn")
vm2 = FactoryGirl.create(:vm_vmware,
:host => host,
:ext_management_system => ems2,
:raw_power_state => "poweredOff")

run_get vms_url, :expand => "resources",
:filter => ["ems_id = #{ApplicationRecord.compress_id(ems2.id)}"]

expect_query_result(:vms, 1, 2)
expect_result_resources_to_match_hash([{"name" => vm2.name, "guid" => vm2.guid}])
end
end

describe "Querying vm attributes" do
Expand Down